зеркало из https://github.com/mozilla/pjs.git
NOT PART OF THE BUILD
This commit is contained in:
Родитель
21d37dd6e5
Коммит
2be799cac1
|
@ -17,7 +17,7 @@
|
|||
* Copyright (C) 1999, Mozilla. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Conrad Carlen <conrad@ingress.com>
|
||||
* Conrad Carlen <ccarlen@netscape.com>
|
||||
*/
|
||||
|
||||
#include "UMacUnicode.h"
|
||||
|
@ -25,107 +25,212 @@
|
|||
#include "nsIUnicodeEncoder.h"
|
||||
#include "nsIUnicodeDecoder.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#define NS_IMPL_IDS
|
||||
#include "nsIPlatformCharset.h"
|
||||
#undef NS_IMPL_IDS
|
||||
#include "nsICharsetConverterManager.h"
|
||||
|
||||
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
|
||||
CPlatformUCSConversion *CPlatformUCSConversion::mgInstance = nsnull;
|
||||
|
||||
static nsIUnicodeEncoder *gUnicodeEncoder;
|
||||
static nsIUnicodeDecoder *gUnicodeDecoder;
|
||||
|
||||
static void GetFileSystemCharset(nsString & fileSystemCharset);
|
||||
|
||||
|
||||
void UMacUnicode::ReleaseUnit()
|
||||
CPlatformUCSConversion::CPlatformUCSConversion() :
|
||||
mCharsetSel(kPlatformCharsetSel_FileName)
|
||||
{
|
||||
NS_IF_RELEASE(gUnicodeEncoder);
|
||||
NS_IF_RELEASE(gUnicodeDecoder);
|
||||
mEncoder = nsnull;
|
||||
mDecoder = nsnull;
|
||||
}
|
||||
|
||||
|
||||
void UMacUnicode::StringToStr255(const nsString& aText, Str255& aStr255)
|
||||
CPlatformUCSConversion*
|
||||
CPlatformUCSConversion::GetInstance()
|
||||
{
|
||||
char buffer[256];
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
// get file system charset and create a unicode encoder
|
||||
if (nsnull == gUnicodeEncoder) {
|
||||
nsAutoString fileSystemCharset;
|
||||
GetFileSystemCharset(fileSystemCharset);
|
||||
|
||||
NS_WITH_SERVICE(nsICharsetConverterManager, ccm, kCharsetConverterManagerCID, &rv);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = ccm->GetUnicodeEncoder(&fileSystemCharset, &gUnicodeEncoder);
|
||||
}
|
||||
}
|
||||
|
||||
// converts from unicode to the file system charset
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
PRInt32 inLength = aText.Length();
|
||||
PRInt32 outLength = 255;
|
||||
rv = gUnicodeEncoder->Convert(aText.GetUnicode(), &inLength, (char *) &aStr255[1], &outLength);
|
||||
if (NS_SUCCEEDED(rv))
|
||||
aStr255[0] = outLength;
|
||||
}
|
||||
|
||||
if (NS_FAILED(rv)) {
|
||||
// NS_ASSERTION(0, "error: charset covnersion");
|
||||
aText.ToCString(buffer, 255);
|
||||
PRInt32 len = nsCRT::strlen(buffer);
|
||||
memcpy(&aStr255[1], buffer, len);
|
||||
aStr255[0] = len;
|
||||
}
|
||||
if (!mgInstance)
|
||||
mgInstance = new CPlatformUCSConversion;
|
||||
|
||||
return mgInstance;
|
||||
}
|
||||
|
||||
|
||||
void UMacUnicode::Str255ToString(const Str255& aStr255, nsString& aText)
|
||||
NS_IMETHODIMP
|
||||
CPlatformUCSConversion::SetCharsetSelector(nsPlatformCharsetSel aSel)
|
||||
{
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
// get file system charset and create a unicode encoder
|
||||
if (nsnull == gUnicodeDecoder) {
|
||||
nsAutoString fileSystemCharset;
|
||||
GetFileSystemCharset(fileSystemCharset);
|
||||
|
||||
NS_WITH_SERVICE(nsICharsetConverterManager, ccm, kCharsetConverterManagerCID, &rv);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = ccm->GetUnicodeDecoder(&fileSystemCharset, &gUnicodeDecoder);
|
||||
}
|
||||
}
|
||||
|
||||
// converts from the file system charset to unicode
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
PRUnichar buffer[512];
|
||||
PRInt32 inLength = aStr255[0];
|
||||
PRInt32 outLength = 512;
|
||||
rv = gUnicodeDecoder->Convert((char *) &aStr255[1], &inLength, buffer, &outLength);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
aText.Assign(buffer, outLength);
|
||||
}
|
||||
}
|
||||
|
||||
if (NS_FAILED(rv)) {
|
||||
// NS_ASSERTION(0, "error: charset covnersion");
|
||||
aText.AssignWithConversion((char *) &aStr255[1], aStr255[0]);
|
||||
}
|
||||
if (mCharsetSel != aSel) {
|
||||
mCharsetSel = aSel;
|
||||
mPlatformCharset.Truncate(0);
|
||||
mEncoder = nsnull;
|
||||
mDecoder = nsnull;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
static void GetFileSystemCharset(nsString & fileSystemCharset)
|
||||
NS_IMETHODIMP
|
||||
CPlatformUCSConversion::PreparePlatformCharset()
|
||||
{
|
||||
static nsAutoString aCharset;
|
||||
nsresult rv;
|
||||
nsresult res = NS_OK;
|
||||
|
||||
if (aCharset.Length() < 1) {
|
||||
nsCOMPtr <nsIPlatformCharset> platformCharset = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv);
|
||||
if (NS_SUCCEEDED(rv))
|
||||
rv = platformCharset->GetCharset(kPlatformCharsetSel_FileName, aCharset);
|
||||
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "error getting platform charset");
|
||||
if (NS_FAILED(rv))
|
||||
aCharset.AssignWithConversion("x-mac-roman");
|
||||
}
|
||||
fileSystemCharset = aCharset;
|
||||
if (mPlatformCharset.Length() == 0)
|
||||
{
|
||||
NS_WITH_SERVICE(nsIPlatformCharset, pcharset, NS_PLATFORMCHARSET_CONTRACTID, &res);
|
||||
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)) {
|
||||
NS_WITH_SERVICE(nsICharsetConverterManager,
|
||||
ucmgr, NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
NS_IMETHODIMP
|
||||
CPlatformUCSConversion::PrepareDecoder()
|
||||
{
|
||||
nsresult res = NS_OK;
|
||||
|
||||
if(! mDecoder)
|
||||
{
|
||||
res = PreparePlatformCharset();
|
||||
if(NS_SUCCEEDED(res)) {
|
||||
NS_WITH_SERVICE(nsICharsetConverterManager,
|
||||
ucmgr, NS_CHARSETCONVERTERMANAGER_CONTRACTID, &res);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
NS_IMETHODIMP
|
||||
CPlatformUCSConversion::UCSToPlatform(const nsAReadableString& aIn, nsAWritableCString& aOut)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
NS_IMETHODIMP
|
||||
CPlatformUCSConversion::UCSToPlatform(const nsAReadableString& aIn, Str255& aOut)
|
||||
{
|
||||
nsresult res;
|
||||
nsCAutoString cStr;
|
||||
|
||||
res = UCSToPlatform(aIn, cStr);
|
||||
if (NS_SUCCEEDED(res))
|
||||
{
|
||||
PRUint32 outLength = cStr.Length();
|
||||
if (outLength > 255)
|
||||
outLength = 255;
|
||||
memcpy(&aOut[1], cStr.GetBuffer(), outLength);
|
||||
aOut[0] = outLength;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
NS_IMETHODIMP
|
||||
CPlatformUCSConversion::PlatformToUCS(const nsAReadableCString& aIn, nsAWritableString& aOut)
|
||||
{
|
||||
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
|
||||
CPlatformUCSConversion::PlatformToUCS(const Str255& aIn, nsAWritableString& aOut)
|
||||
{
|
||||
char charBuf[256];
|
||||
|
||||
memcpy(charBuf, &aIn[1], aIn[0]);
|
||||
charBuf[aIn[0]] = '\0';
|
||||
return PlatformToUCS(nsLiteralCString(charBuf), aOut);
|
||||
}
|
||||
|
|
|
@ -17,22 +17,45 @@
|
|||
* Copyright (C) 1999, Mozilla. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Conrad Carlen <conrad@ingress.com>
|
||||
* Conrad Carlen <ccarlen@netscape.com>
|
||||
*/
|
||||
|
||||
#ifndef __UMacUnicode__
|
||||
#define __UMacUnicode__
|
||||
|
||||
#include "nsIPlatformCharset.h"
|
||||
#include "nsIUnicodeEncoder.h"
|
||||
#include "nsIUnicodeDecoder.h"
|
||||
#include "nsString.h"
|
||||
#include "nsCOMPtr.h"
|
||||
|
||||
class nsString;
|
||||
|
||||
namespace UMacUnicode
|
||||
{
|
||||
class CPlatformUCSConversion {
|
||||
public:
|
||||
CPlatformUCSConversion();
|
||||
virtual ~CPlatformUCSConversion(){};
|
||||
|
||||
static CPlatformUCSConversion* GetInstance();
|
||||
|
||||
NS_IMETHOD SetCharsetSelector(nsPlatformCharsetSel aSel);
|
||||
|
||||
NS_IMETHOD UCSToPlatform(const nsAReadableString& aIn, nsAWritableCString& aOut);
|
||||
NS_IMETHOD UCSToPlatform(const nsAReadableString& aIn, Str255& aOut);
|
||||
|
||||
NS_IMETHOD PlatformToUCS(const nsAReadableCString& ain, nsAWritableString& aOut);
|
||||
NS_IMETHOD PlatformToUCS(const Str255& aIn, nsAWritableString& aOut);
|
||||
|
||||
void ReleaseUnit();
|
||||
|
||||
void StringToStr255(const nsString& aText, Str255& aStr255);
|
||||
void Str255ToString(const Str255& aStr255, nsString& aText);
|
||||
}
|
||||
private:
|
||||
static CPlatformUCSConversion *mgInstance;
|
||||
|
||||
NS_IMETHOD PreparePlatformCharset();
|
||||
NS_IMETHOD PrepareEncoder();
|
||||
NS_IMETHOD PrepareDecoder();
|
||||
|
||||
nsPlatformCharsetSel mCharsetSel;
|
||||
nsAutoString mPlatformCharset;
|
||||
nsCOMPtr<nsIUnicodeEncoder> mEncoder;
|
||||
nsCOMPtr<nsIUnicodeDecoder> mDecoder;
|
||||
};
|
||||
|
||||
#endif // __UMacUnicode__
|
||||
|
|
Загрузка…
Ссылка в новой задаче