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"
|
2003-02-12 18:35:40 +03:00
|
|
|
|
|
|
|
#include <TextCommon.h>
|
|
|
|
#include <Script.h>
|
|
|
|
|
2000-04-17 16:27:12 +04:00
|
|
|
#include "nsString.h"
|
2003-02-12 18:35:40 +03:00
|
|
|
|
|
|
|
static TextEncoding getSystemEncoding()
|
|
|
|
{
|
|
|
|
OSStatus err;
|
|
|
|
TextEncoding theEncoding;
|
|
|
|
|
|
|
|
err = ::UpgradeScriptInfoToTextEncoding(smSystemScript, kTextLanguageDontCare,
|
|
|
|
kTextRegionDontCare, NULL, &theEncoding);
|
|
|
|
|
|
|
|
if (err != noErr)
|
|
|
|
theEncoding = kTextEncodingMacRoman;
|
|
|
|
|
|
|
|
return theEncoding;
|
|
|
|
}
|
2000-04-17 16:27:12 +04:00
|
|
|
|
2000-12-15 16:39:23 +03:00
|
|
|
CPlatformUCSConversion *CPlatformUCSConversion::mgInstance = nsnull;
|
2003-02-12 18:35:40 +03:00
|
|
|
UnicodeToTextInfo CPlatformUCSConversion::sEncoderInfo = nsnull;
|
|
|
|
TextToUnicodeInfo CPlatformUCSConversion::sDecoderInfo = nsnull;
|
2000-04-17 16:27:12 +04:00
|
|
|
|
2003-02-12 18:35:40 +03:00
|
|
|
CPlatformUCSConversion::CPlatformUCSConversion()
|
2000-12-15 16:39:23 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CPlatformUCSConversion*
|
|
|
|
CPlatformUCSConversion::GetInstance()
|
|
|
|
{
|
|
|
|
if (!mgInstance)
|
|
|
|
mgInstance = new CPlatformUCSConversion;
|
|
|
|
|
|
|
|
return mgInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
CPlatformUCSConversion::PrepareEncoder()
|
|
|
|
{
|
2003-02-12 18:35:40 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (!sEncoderInfo) {
|
|
|
|
OSStatus err;
|
|
|
|
err = ::CreateUnicodeToTextInfoByEncoding(getSystemEncoding(), &sEncoderInfo);
|
|
|
|
if (err)
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return rv;
|
2000-12-15 16:39:23 +03:00
|
|
|
}
|
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
|
|
|
{
|
2003-02-12 18:35:40 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (!sDecoderInfo) {
|
|
|
|
OSStatus err;
|
|
|
|
err = ::CreateTextToUnicodeInfoByEncoding(getSystemEncoding(), &sDecoderInfo);
|
|
|
|
if (err)
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return rv;
|
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
|
|
|
{
|
2003-02-12 18:35:40 +03:00
|
|
|
nsresult rv = PrepareEncoder();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
OSStatus err = noErr;
|
|
|
|
char stackBuffer[512];
|
2000-12-15 16:39:23 +03:00
|
|
|
|
|
|
|
aOut.Truncate(0);
|
2003-02-12 18:35:40 +03:00
|
|
|
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 && err == noErr; iter.advance(PRInt32(fragmentLength)))
|
|
|
|
{
|
|
|
|
fragmentLength = PRUint32(iter.size_forward());
|
|
|
|
UInt32 bytesLeft = fragmentLength * sizeof(UniChar);
|
|
|
|
nsReadingIterator<PRUnichar> sub_iter(iter);
|
|
|
|
|
|
|
|
do {
|
|
|
|
UInt32 bytesRead = 0, bytesWritten = 0;
|
|
|
|
err = ::ConvertFromUnicodeToText(sEncoderInfo,
|
|
|
|
bytesLeft,
|
|
|
|
(const UniChar*)sub_iter.get(),
|
|
|
|
kUnicodeUseFallbacksMask | kUnicodeLooseMappingsMask,
|
|
|
|
0, nsnull, nsnull, nsnull,
|
|
|
|
sizeof(stackBuffer),
|
|
|
|
&bytesRead,
|
|
|
|
&bytesWritten,
|
|
|
|
stackBuffer);
|
|
|
|
if (err == kTECUsedFallbacksStatus)
|
|
|
|
err = noErr;
|
|
|
|
else if (err == kTECOutputBufferFullStatus) {
|
|
|
|
bytesLeft -= bytesRead;
|
|
|
|
sub_iter.advance(bytesRead / sizeof(UniChar));
|
2000-12-15 16:39:23 +03:00
|
|
|
}
|
2003-02-12 18:35:40 +03:00
|
|
|
aOut.Append(stackBuffer, bytesWritten);
|
2000-12-15 16:39:23 +03:00
|
|
|
}
|
2003-02-12 18:35:40 +03:00
|
|
|
while (err == kTECOutputBufferFullStatus);
|
|
|
|
}
|
|
|
|
return (err == noErr) ? NS_OK : NS_ERROR_FAILURE;
|
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
|
|
|
{
|
2003-02-12 18:35:40 +03:00
|
|
|
nsresult rv = PrepareDecoder();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
OSStatus err = noErr;
|
|
|
|
UniChar stackBuffer[512];
|
2000-12-15 16:39:23 +03:00
|
|
|
|
|
|
|
aOut.Truncate(0);
|
2003-02-12 18:35:40 +03:00
|
|
|
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 && err == noErr; iter.advance(PRInt32(fragmentLength)))
|
2000-12-15 16:39:23 +03:00
|
|
|
{
|
2003-02-12 18:35:40 +03:00
|
|
|
fragmentLength = PRUint32(iter.size_forward());
|
|
|
|
UInt32 bytesLeft = fragmentLength;
|
|
|
|
nsReadingIterator<char> sub_iter(iter);
|
|
|
|
|
|
|
|
do {
|
|
|
|
UInt32 bytesRead = 0, bytesWritten = 0;
|
|
|
|
err = ::ConvertFromTextToUnicode(sDecoderInfo,
|
|
|
|
bytesLeft,
|
|
|
|
sub_iter.get(),
|
|
|
|
kUnicodeUseFallbacksMask | kUnicodeLooseMappingsMask,
|
|
|
|
0, nsnull, nsnull, nsnull,
|
|
|
|
sizeof(stackBuffer),
|
|
|
|
&bytesRead,
|
|
|
|
&bytesWritten,
|
|
|
|
stackBuffer);
|
|
|
|
if (err == kTECUsedFallbacksStatus)
|
|
|
|
err = noErr;
|
|
|
|
else if (err == kTECOutputBufferFullStatus) {
|
|
|
|
bytesLeft -= bytesRead;
|
|
|
|
sub_iter.advance(bytesRead);
|
2000-12-15 16:39:23 +03:00
|
|
|
}
|
2003-02-12 18:35:40 +03:00
|
|
|
aOut.Append((PRUnichar *)stackBuffer, bytesWritten / sizeof(PRUnichar));
|
2000-12-15 16:39:23 +03:00
|
|
|
}
|
2003-02-12 18:35:40 +03:00
|
|
|
while (err == kTECOutputBufferFullStatus);
|
|
|
|
}
|
|
|
|
return (err == noErr) ? NS_OK : NS_ERROR_FAILURE;
|
2000-12-15 16:39:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|