2000-04-17 16:27:12 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is the Mozilla browser.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications, Inc. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1999, Mozilla. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2000-12-15 16:39:23 +03:00
|
|
|
* Conrad Carlen <ccarlen@netscape.com>
|
2000-04-17 16:27:12 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "UMacUnicode.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsIUnicodeEncoder.h"
|
|
|
|
#include "nsIUnicodeDecoder.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsICharsetConverterManager.h"
|
|
|
|
|
2000-12-15 16:39:23 +03:00
|
|
|
CPlatformUCSConversion *CPlatformUCSConversion::mgInstance = nsnull;
|
2000-04-17 16:27:12 +04:00
|
|
|
|
2000-12-15 16:39:23 +03:00
|
|
|
CPlatformUCSConversion::CPlatformUCSConversion() :
|
|
|
|
mCharsetSel(kPlatformCharsetSel_FileName)
|
|
|
|
{
|
|
|
|
mEncoder = nsnull;
|
|
|
|
mDecoder = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CPlatformUCSConversion*
|
|
|
|
CPlatformUCSConversion::GetInstance()
|
|
|
|
{
|
|
|
|
if (!mgInstance)
|
|
|
|
mgInstance = new CPlatformUCSConversion;
|
|
|
|
|
|
|
|
return mgInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CPlatformUCSConversion::SetCharsetSelector(nsPlatformCharsetSel aSel)
|
|
|
|
{
|
|
|
|
if (mCharsetSel != aSel) {
|
|
|
|
mCharsetSel = aSel;
|
|
|
|
mPlatformCharset.Truncate(0);
|
|
|
|
mEncoder = nsnull;
|
|
|
|
mDecoder = nsnull;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-04-17 16:27:12 +04:00
|
|
|
|
2000-12-15 16:39:23 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CPlatformUCSConversion::PreparePlatformCharset()
|
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
if (mPlatformCharset.Length() == 0)
|
|
|
|
{
|
2001-07-25 11:54:28 +04:00
|
|
|
nsCOMPtr<nsIPlatformCharset> pcharset =
|
|
|
|
do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &res);
|
2000-12-15 16:39:23 +03:00
|
|
|
if (!(NS_SUCCEEDED(res) && pcharset)) {
|
|
|
|
NS_WARNING("cannot get platform charset");
|
|
|
|
}
|
|
|
|
if(NS_SUCCEEDED(res) && pcharset) {
|
|
|
|
res = pcharset->GetCharset(mCharsetSel, mPlatformCharset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CPlatformUCSConversion::PrepareEncoder()
|
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
if(! mEncoder)
|
|
|
|
{
|
|
|
|
res = PreparePlatformCharset();
|
|
|
|
if(NS_SUCCEEDED(res)) {
|
2001-07-25 11:54:28 +04:00
|
|
|
nsCOMPtr<nsICharsetConverterManager> ucmgr =
|
|
|
|
do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res);
|
2000-12-15 16:39:23 +03:00
|
|
|
NS_ASSERTION((NS_SUCCEEDED(res) && ucmgr),
|
|
|
|
"cannot get charset converter manager ");
|
|
|
|
if(NS_SUCCEEDED(res) && ucmgr)
|
|
|
|
res = ucmgr->GetUnicodeEncoder( &mPlatformCharset, getter_AddRefs(mEncoder));
|
|
|
|
NS_ASSERTION((NS_SUCCEEDED(res) && mEncoder),
|
|
|
|
"cannot find the unicode encoder");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2000-04-17 16:27:12 +04:00
|
|
|
|
|
|
|
|
2000-12-15 16:39:23 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
CPlatformUCSConversion::PrepareDecoder()
|
2000-04-17 16:27:12 +04:00
|
|
|
{
|
2000-12-15 16:39:23 +03:00
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
if(! mDecoder)
|
|
|
|
{
|
|
|
|
res = PreparePlatformCharset();
|
|
|
|
if(NS_SUCCEEDED(res)) {
|
2001-07-25 11:54:28 +04:00
|
|
|
nsCOMPtr<nsICharsetConverterManager> ucmgr =
|
|
|
|
do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res);
|
2000-12-15 16:39:23 +03:00
|
|
|
NS_ASSERTION((NS_SUCCEEDED(res) && ucmgr),
|
|
|
|
"cannot get charset converter manager ");
|
|
|
|
if(NS_SUCCEEDED(res) && ucmgr)
|
|
|
|
res = ucmgr->GetUnicodeDecoder( &mPlatformCharset, getter_AddRefs(mDecoder));
|
|
|
|
NS_ASSERTION((NS_SUCCEEDED(res) && mDecoder),
|
|
|
|
"cannot find the unicode decoder");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
2000-04-17 16:27:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-15 16:39:23 +03:00
|
|
|
NS_IMETHODIMP
|
2002-03-24 00:35:01 +03:00
|
|
|
CPlatformUCSConversion::UCSToPlatform(const nsAString& aIn, nsACString& aOut)
|
2000-04-17 16:27:12 +04:00
|
|
|
{
|
2000-12-15 16:39:23 +03:00
|
|
|
nsresult res;
|
|
|
|
|
|
|
|
aOut.Truncate(0);
|
|
|
|
res = PrepareEncoder();
|
|
|
|
if(NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
nsReadingIterator<PRUnichar> done_reading;
|
|
|
|
aIn.EndReading(done_reading);
|
|
|
|
|
|
|
|
// for each chunk of |aIn|...
|
|
|
|
PRUint32 fragmentLength = 0;
|
|
|
|
nsReadingIterator<PRUnichar> iter;
|
|
|
|
for (aIn.BeginReading(iter); iter != done_reading && NS_SUCCEEDED(res); iter.advance(PRInt32(fragmentLength)))
|
|
|
|
{
|
|
|
|
fragmentLength = PRUint32(iter.size_forward());
|
|
|
|
PRInt32 inLength = fragmentLength;
|
|
|
|
PRInt32 outLength;
|
|
|
|
|
|
|
|
res = mEncoder->GetMaxLength(iter.get(), fragmentLength, &outLength);
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
char *outBuf = (char*)nsMemory::Alloc(outLength);
|
|
|
|
if (outBuf)
|
|
|
|
{
|
|
|
|
res = mEncoder->Convert(iter.get(), &inLength, outBuf, &outLength);
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
aOut.Append(outBuf, outLength);
|
|
|
|
nsMemory::Free(outBuf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
2000-04-17 16:27:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-15 16:39:23 +03:00
|
|
|
NS_IMETHODIMP
|
2002-03-24 00:35:01 +03:00
|
|
|
CPlatformUCSConversion::UCSToPlatform(const nsAString& aIn, Str255& aOut)
|
2000-04-17 16:27:12 +04:00
|
|
|
{
|
2000-12-15 16:39:23 +03:00
|
|
|
nsresult res;
|
|
|
|
nsCAutoString cStr;
|
|
|
|
|
|
|
|
res = UCSToPlatform(aIn, cStr);
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
PRUint32 outLength = cStr.Length();
|
|
|
|
if (outLength > 255)
|
|
|
|
outLength = 255;
|
2001-03-02 12:26:57 +03:00
|
|
|
memcpy(&aOut[1], cStr.get(), outLength);
|
2000-12-15 16:39:23 +03:00
|
|
|
aOut[0] = outLength;
|
|
|
|
}
|
|
|
|
return res;
|
2000-04-17 16:27:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-15 16:39:23 +03:00
|
|
|
NS_IMETHODIMP
|
2002-03-24 00:35:01 +03:00
|
|
|
CPlatformUCSConversion::PlatformToUCS(const nsACString& aIn, nsAString& aOut)
|
2000-12-15 16:39:23 +03:00
|
|
|
{
|
|
|
|
nsresult res;
|
|
|
|
|
|
|
|
aOut.Truncate(0);
|
|
|
|
res = PrepareDecoder();
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
nsReadingIterator<char> done_reading;
|
|
|
|
aIn.EndReading(done_reading);
|
|
|
|
|
|
|
|
// for each chunk of |aIn|...
|
|
|
|
PRUint32 fragmentLength = 0;
|
|
|
|
nsReadingIterator<char> iter;
|
|
|
|
for (aIn.BeginReading(iter); iter != done_reading && NS_SUCCEEDED(res); iter.advance(PRInt32(fragmentLength)))
|
|
|
|
{
|
|
|
|
fragmentLength = PRUint32(iter.size_forward());
|
|
|
|
PRInt32 inLength = fragmentLength;
|
|
|
|
PRInt32 outLength;
|
|
|
|
|
|
|
|
res = mDecoder->GetMaxLength(iter.get(), inLength, &outLength);
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
PRUnichar *outBuf = (PRUnichar*)nsMemory::Alloc(outLength * sizeof(PRUnichar));
|
|
|
|
if (outBuf)
|
|
|
|
{
|
|
|
|
res = mDecoder->Convert(iter.get(), &inLength, outBuf, &outLength);
|
|
|
|
if(NS_SUCCEEDED(res))
|
|
|
|
aOut.Append(outBuf, outLength);
|
|
|
|
nsMemory::Free(outBuf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-24 00:35:01 +03:00
|
|
|
CPlatformUCSConversion::PlatformToUCS(const Str255& aIn, nsAString& aOut)
|
2000-04-17 16:27:12 +04:00
|
|
|
{
|
2000-12-15 16:39:23 +03:00
|
|
|
char charBuf[256];
|
|
|
|
|
|
|
|
memcpy(charBuf, &aIn[1], aIn[0]);
|
|
|
|
charBuf[aIn[0]] = '\0';
|
2001-05-19 15:27:30 +04:00
|
|
|
return PlatformToUCS(nsDependentCString(charBuf, aIn[0]), aOut);
|
2000-04-17 16:27:12 +04:00
|
|
|
}
|