SJIS encoder, Support class added.

This commit is contained in:
cata%netscape.com 1999-03-10 22:08:00 +00:00
Родитель c1bbe9efbd
Коммит f47e93cb75
9 изменённых файлов: 621 добавлений и 38 удалений

Просмотреть файл

@ -30,6 +30,8 @@ CSRCS = \
CPPSRCS = \
nsSJIS2Unicode.cpp \
nsUnicodeToSJIS.cpp \
nsUCvJaSupport.cpp \
nsUCVJADll.cpp \
$(NULL)

Просмотреть файл

@ -27,21 +27,19 @@ DLLNAME = ucvja
DLL=.\$(OBJDIR)\$(DLLNAME).dll
CPPSRCS = \
nsSJIS2Unicode.cpp \
nsUnicodeToSJIS.cpp \
nsUCvJaSupport.cpp \
nsUCVJADll.cpp \
nsSJIS2Unicode.cpp \
$(NULL)
CPP_OBJS= \
.\$(OBJDIR)\nsUCVJADll.obj \
.\$(OBJDIR)\nsSJIS2Unicode.obj \
.\$(OBJDIR)\nsUnicodeToSJIS.obj \
.\$(OBJDIR)\nsUCvJaSupport.obj \
.\$(OBJDIR)\nsUCVJADll.obj \
$(NULL)
CSRCS = \
$(NULL)
OBJS = \
$(NULL)
EXPORTS= nsUCVJACID.h \
$(NULL)

Просмотреть файл

@ -25,6 +25,11 @@
// Class ID for our SJIS2Unicode charset converter
// {0E6892C1-A9AD-11d2-B3AE-00805F8A6670}
NS_DECLARE_ID(kSJIS2UnicodeCID,
0xe6892c1, 0xa9ad, 0x11d2, 0xb3, 0xae, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70);
0xe6892c1, 0xa9ad, 0x11d2, 0xb3, 0xae, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70);
// Class ID for our UnicodeToSJIS charset converter
// {E28AB250-D66D-11d2-8AAC-00600811A836}
NS_DECLARE_ID(kUnicodeToSJISCID,
0xe28ab250, 0xd66d, 0x11d2, 0x8a, 0xac, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36);
#endif /* nsUCVJACID_h___ */

Просмотреть файл

@ -21,25 +21,31 @@
#include "pratom.h"
#include "nsIComponentManager.h"
#include "nsIServiceManager.h"
#include "nsIFactory.h"
#include "nsCOMPtr.h"
#include "nsICharsetConverterInfo.h"
#include "nsUCVJACID.h"
#include "nsUCVJADll.h"
#include "nsSJIS2Unicode.h"
#include "nsIServiceManager.h"
#include "nsCOMPtr.h"
static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID);
#include "nsUnicodeToSJIS.h"
// just for NS_IMPL_IDS; this is a good, central place to implement GUIDs
#include "nsIUnicodeDecoder.h"
#include "nsIUnicodeDecodeUtil.h"
#include "nsIUnicodeEncoder.h"
#include "nsIUnicodeEncodeHelper.h"
#include "nsICharsetConverterManager.h"
//----------------------------------------------------------------------
// Global functions and data [declaration]
extern "C" PRInt32 g_InstanceCount = 0;
extern "C" PRInt32 g_LockCount = 0;
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
static NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID);
static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID);
PRInt32 g_InstanceCount = 0;
PRInt32 g_LockCount = 0;
typedef nsresult (* fpCreateInstance) (nsISupports **);
@ -59,6 +65,12 @@ FactoryData g_FactoryData[] =
"Shift_JIS",
"Unicode"
},
{
&kUnicodeToSJISCID,
nsUnicodeToSJIS::CreateInstance,
"Unicode",
"Shift_JIS"
}
};
#define ARRAY_SIZE(_array) \
@ -112,8 +124,6 @@ public:
//----------------------------------------------------------------------
// Global functions and data [implementation]
NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID);
extern "C" NS_EXPORT PRBool NSCanUnload(nsISupports* aServMgr)
{
return PRBool(g_InstanceCount == 0 && g_LockCount == 0);
@ -225,28 +235,19 @@ nsresult nsConverterFactory::QueryInterface(REFNSIID aIID,
}
*aInstancePtr = NULL;
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
static NS_DEFINE_IID(kClassIID, kICharsetConverterInfoIID);
static NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID);
if (aIID.Equals(kClassIID)) {
*aInstancePtr = (void*) ((nsICharsetConverterInfo*)this);
NS_ADDREF_THIS();
return NS_OK;
}
if (aIID.Equals(kIFactoryIID)) {
*aInstancePtr = (void*) ((nsIFactory*)this);
NS_ADDREF_THIS();
return NS_OK;
}
if (aIID.Equals(kISupportsIID)) {
if (aIID.Equals(kICharsetConverterInfoIID)) {
*aInstancePtr = (void*) ((nsICharsetConverterInfo*)this);
} else if (aIID.Equals(kIFactoryIID)) {
*aInstancePtr = (void*) ((nsIFactory*)this);
} else if (aIID.Equals(kISupportsIID)) {
*aInstancePtr = (void*) ((nsISupports*)(nsIFactory*)this);
NS_ADDREF_THIS();
return NS_OK;
}
} else {
return NS_NOINTERFACE;
}
return NS_NOINTERFACE;
NS_ADDREF_THIS();
return NS_OK;
}
//----------------------------------------------------------------------

Просмотреть файл

@ -22,7 +22,7 @@
#include "prtypes.h"
extern "C" PRInt32 g_InstanceCount;
extern "C" PRInt32 g_LockCount;
extern PRInt32 g_InstanceCount;
extern PRInt32 g_LockCount;
#endif /* nsUCVJADll_h___ */

Просмотреть файл

@ -0,0 +1,308 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (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/NPL/
*
* 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 Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1998
* Netscape Communications Corporation. All Rights Reserved.
*/
#include "pratom.h"
#include "nsIComponentManager.h"
#include "nsUCvJaSupport.h"
#include "nsUCVJADll.h"
//----------------------------------------------------------------------
// Class nsEncoderSupport [implementation]
nsEncoderSupport::nsEncoderSupport()
{
mBufferCapacity = 16;
mBuffer = new char[mBufferCapacity];
mErrBehavior = kOnError_Signal;
mErrChar = 0;
mErrEncoder = NULL;
Reset();
NS_INIT_REFCNT();
PR_AtomicIncrement(&g_InstanceCount);
}
nsEncoderSupport::~nsEncoderSupport()
{
delete [] mBuffer;
NS_IF_RELEASE(mErrEncoder);
PR_AtomicDecrement(&g_InstanceCount);
}
NS_IMETHODIMP nsEncoderSupport::ConvertNoBuff(const PRUnichar * aSrc,
PRInt32 * aSrcLength,
char * aDest,
PRInt32 * aDestLength)
{
// we do all operations using pointers internally
const PRUnichar * src = aSrc;
const PRUnichar * srcEnd = aSrc + *aSrcLength;
char * dest = aDest;
char * destEnd = aDest + *aDestLength;
PRInt32 bcr, bcw; // byte counts for read & write;
nsresult res;
for (;;) {
bcr = srcEnd - src;
bcw = destEnd - dest;
res = ConvertNoBuffNoErr(src, &bcr, dest, &bcw);
src += bcr;
dest += bcw;
if (res == NS_ERROR_UENC_NOMAPPING) {
if (mErrBehavior == kOnError_Replace) {
const PRUnichar buff[] = {mErrChar};
bcr = 1;
bcw = destEnd - dest;
src--; // back the input: maybe the guy won't consume consume anything.
res = ConvertNoBuffNoErr(buff, &bcr, dest, &bcw);
src += bcr;
dest += bcw;
if (res != NS_OK) break;
} else if (mErrBehavior == kOnError_CallBack) {
bcw = destEnd - dest;
src--;
res = mErrEncoder->Convert(*src, dest, &bcw);
dest += bcw;
// if enought output space then the last char was used
if (res != NS_OK_UENC_MOREOUTPUT) src++;
if (res != NS_OK) break;
} else break;
}
else break;
}
*aSrcLength -= srcEnd - src;
*aDestLength -= destEnd - dest;
return res;
}
NS_IMETHODIMP nsEncoderSupport::FinishNoBuff(char * aDest,
PRInt32 * aDestLength)
{
*aDestLength = 0;
return NS_OK;
}
NS_IMETHODIMP nsEncoderSupport::FlushBuffer(char ** aDest,
const char * aDestEnd)
{
PRInt32 bcr, bcw; // byte counts for read & write;
nsresult res = NS_OK;
char * dest = *aDest;
if (mBufferStart < mBufferEnd) {
bcr = mBufferEnd - mBufferStart;
bcw = aDestEnd - dest;
if (bcw < bcr) bcr = bcw;
memcpy(dest, mBufferStart, bcr);
dest += bcr;
mBufferStart += bcr;
if (mBufferStart < mBufferEnd) res = NS_OK_UENC_MOREOUTPUT;
}
*aDest = dest;
return res;
}
//----------------------------------------------------------------------
// Interface nsISupports [implementation]
NS_IMPL_ADDREF(nsEncoderSupport);
NS_IMPL_RELEASE(nsEncoderSupport);
nsresult nsEncoderSupport::QueryInterface(REFNSIID aIID,
void** aInstancePtr)
{
if (NULL == aInstancePtr) {
return NS_ERROR_NULL_POINTER;
}
*aInstancePtr = NULL;
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
if (aIID.Equals(kIUnicodeEncoderIID)) {
*aInstancePtr = (void*) ((nsIUnicodeEncoder*)this);
NS_ADDREF_THIS();
return NS_OK;
}
if (aIID.Equals(kISupportsIID)) {
*aInstancePtr = (void*) ((nsISupports*)this);
NS_ADDREF_THIS();
return NS_OK;
}
return NS_NOINTERFACE;
}
//----------------------------------------------------------------------
// Interface nsIUnicodeEncoder [implementation]
NS_IMETHODIMP nsEncoderSupport::Convert(const PRUnichar * aSrc,
PRInt32 * aSrcLength,
char * aDest,
PRInt32 * aDestLength)
{
// we do all operations using pointers internally
const PRUnichar * src = aSrc;
const PRUnichar * srcEnd = aSrc + *aSrcLength;
char * dest = aDest;
char * destEnd = aDest + *aDestLength;
PRInt32 bcr, bcw; // byte counts for read & write;
nsresult res;
res = FlushBuffer(&dest, destEnd);
if (res == NS_OK_UENC_MOREOUTPUT) goto final;
bcr = srcEnd - src;
bcw = destEnd - dest;
res = ConvertNoBuff(src, &bcr, dest, &bcw);
src += bcr;
dest += bcw;
if ((res == NS_OK_UENC_MOREOUTPUT) && (dest < destEnd)) {
// convert exactly one character into the internal buffer
// at this point, there should be at least a char in the input
for (;;) {
bcr = 1;
bcw = mBufferCapacity;
res = ConvertNoBuff(src, &bcr, mBuffer, &bcw);
if (res == NS_OK_UENC_MOREOUTPUT) {
delete [] mBuffer;
mBufferCapacity *= 2;
mBuffer = new char [mBufferCapacity];
} else {
src += bcr;
mBufferStart = mBufferEnd = mBuffer;
mBufferEnd += bcw;
break;
}
}
res = FlushBuffer(&dest, destEnd);
}
final:
*aSrcLength -= srcEnd - src;
*aDestLength -= destEnd - dest;
return res;
}
NS_IMETHODIMP nsEncoderSupport::Finish(char * aDest, PRInt32 * aDestLength)
{
// we do all operations using pointers internally
char * dest = aDest;
char * destEnd = aDest + *aDestLength;
PRInt32 bcw; // byte count for write;
nsresult res;
res = FlushBuffer(&dest, destEnd);
if (res == NS_OK_UENC_MOREOUTPUT) goto final;
// do the finish into the internal buffer.
for (;;) {
bcw = mBufferCapacity;
res = FinishNoBuff(mBuffer, &bcw);
if (res == NS_OK_UENC_MOREOUTPUT) {
delete [] mBuffer;
mBufferCapacity *= 2;
mBuffer = new char [mBufferCapacity];
} else {
mBufferStart = mBufferEnd = mBuffer;
mBufferEnd += bcw;
break;
}
}
res = FlushBuffer(&dest, destEnd);
final:
*aDestLength -= destEnd - dest;
return res;
}
NS_IMETHODIMP nsEncoderSupport::Reset()
{
mBufferStart = mBufferEnd;
return NS_OK;
}
NS_IMETHODIMP nsEncoderSupport::SetOutputErrorBehavior(
PRInt32 aBehavior,
nsIUnicharEncoder * aEncoder,
PRUnichar aChar)
{
if ((aBehavior == kOnError_CallBack) && (aEncoder == NULL))
return NS_ERROR_NULL_POINTER;
NS_IF_RELEASE(aEncoder);
mErrEncoder = aEncoder;
NS_IF_ADDREF(aEncoder);
mErrBehavior = aBehavior;
mErrChar = aChar;
return NS_OK;
}
//----------------------------------------------------------------------
// Class nsTableEncoderSupport [implementation]
nsTableEncoderSupport::nsTableEncoderSupport(uShiftTable * aShiftTable,
uMappingTable * aMappingTable)
: nsEncoderSupport()
{
mHelper = NULL;
mShiftTable = aShiftTable;
mMappingTable = aMappingTable;
}
nsTableEncoderSupport::~nsTableEncoderSupport()
{
NS_IF_RELEASE(mHelper);
}
//----------------------------------------------------------------------
// Subclassing of nsEncoderSupport class [implementation]
NS_IMETHODIMP nsTableEncoderSupport::ConvertNoBuffNoErr(
const PRUnichar * aSrc,
PRInt32 * aSrcLength,
char * aDest,
PRInt32 * aDestLength)
{
nsresult res;
if (mHelper == nsnull) {
res = nsComponentManager::CreateInstance(kUnicodeEncodeHelperCID, NULL,
kIUnicodeEncodeHelperIID, (void**) & mHelper);
if (NS_FAILED(res)) return NS_ERROR_UENC_NOHELPER;
}
res = mHelper->ConvertByTable(aSrc, aSrcLength, aDest, aDestLength,
mShiftTable, mMappingTable);
return res;
}

Просмотреть файл

@ -0,0 +1,150 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (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/NPL/
*
* 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 Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1998
* Netscape Communications Corporation. All Rights Reserved.
*/
#ifndef nsUCvJaSupport_h___
#define nsUCvJaSupport_h___
#include "nsIUnicodeEncoder.h"
#include "nsIUnicodeEncodeHelper.h"
//----------------------------------------------------------------------
// Class nsEncoderSupport [declaration]
/**
* Support class for the Unicode encoders.
*
* The class source files for this class are in /ucvlatin/nsUCvLatinSupport.
* However, because these objects requires non-xpcom subclassing, local copies
* will be made into the other directories using them. Just don't forget to
* keep in sync with the master copy!
*
* This class implements:
* - nsISupports
* - the buffer management
* - error handling procedure(s)
*
* @created 17/Feb/1999
* @author Catalin Rotaru [CATA]
*/
class nsEncoderSupport : public nsIUnicodeEncoder
{
NS_DECL_ISUPPORTS
protected:
/**
* Internal buffer for partial conversions.
*/
char * mBuffer;
PRInt32 mBufferCapacity;
char * mBufferStart;
char * mBufferEnd;
/**
* Error handling stuff
*/
PRInt32 mErrBehavior;
nsIUnicharEncoder * mErrEncoder;
PRUnichar mErrChar;
/**
* Convert method but *without* the buffer management stuff and *with*
* error handling stuff.
*/
NS_IMETHOD ConvertNoBuff(const PRUnichar * aSrc, PRInt32 * aSrcLength,
char * aDest, PRInt32 * aDestLength);
/**
* Convert method but *without* the buffer management stuff and *without*
* error handling stuff.
*/
NS_IMETHOD ConvertNoBuffNoErr(const PRUnichar * aSrc, PRInt32 * aSrcLength,
char * aDest, PRInt32 * aDestLength) = 0;
/**
* Finish method but *without* the buffer management stuff.
*/
NS_IMETHOD FinishNoBuff(char * aDest, PRInt32 * aDestLength);
/**
* Copy as much as possible from the internal buffer to the destination.
*/
NS_IMETHOD FlushBuffer(char ** aDest, const char * aDestEnd);
public:
/**
* Class constructor.
*/
nsEncoderSupport();
/**
* Class destructor.
*/
virtual ~nsEncoderSupport();
//--------------------------------------------------------------------
// Interface nsIUnicodeEncoder [declaration]
NS_IMETHOD Convert(const PRUnichar * aSrc, PRInt32 * aSrcLength,
char * aDest, PRInt32 * aDestLength);
NS_IMETHOD Finish(char * aDest, PRInt32 * aDestLength);
NS_IMETHOD Reset();
NS_IMETHOD SetOutputErrorBehavior(PRInt32 aBehavior,
nsIUnicharEncoder * aEncoder, PRUnichar aChar);
};
//----------------------------------------------------------------------
// Class nsTableEncoderSupport [declaration]
/**
* Support class for a single-table-driven Unicode encoder.
*
* @created 17/Feb/1999
* @author Catalin Rotaru [CATA]
*/
class nsTableEncoderSupport : public nsEncoderSupport
{
public:
/**
* Class constructor.
*/
nsTableEncoderSupport(uShiftTable * aShiftTable,
uMappingTable * aMappingTable);
/**
* Class destructor.
*/
virtual ~nsTableEncoderSupport();
protected:
nsIUnicodeEncodeHelper * mHelper; // encoder helper object
uShiftTable * mShiftTable;
uMappingTable * mMappingTable;
//--------------------------------------------------------------------
// Subclassing of nsEncoderSupport class [declaration]
NS_IMETHOD ConvertNoBuffNoErr(const PRUnichar * aSrc, PRInt32 * aSrcLength,
char * aDest, PRInt32 * aDestLength);
};
#endif /* nsUCvJaSupport_h___ */

Просмотреть файл

@ -0,0 +1,62 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (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/NPL/
*
* 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 Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1998
* Netscape Communications Corporation. All Rights Reserved.
*/
#include "nsUnicodeToSJIS.h"
//----------------------------------------------------------------------
// Global functions and data [declaration]
static PRUint16 g_SJISMappingTable[] = {
#include "sjis.uf"
};
static PRInt16 g_SJISShiftTable[] = {
4, uMultibytesCharset,
ShiftCell(u1ByteChar, 1, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x7F),
ShiftCell(u1ByteChar, 1, 0xA1, 0xDF, 0x00, 0xA1, 0x00, 0xDF),
ShiftCell(u2BytesChar, 2, 0x81, 0x9F, 0x81, 0x40, 0x9F, 0xFC),
ShiftCell(u2BytesChar, 2, 0xE0, 0xFC, 0xE0, 0x40, 0xFC, 0xFC)
};
//----------------------------------------------------------------------
// Class nsUnicodeToSJIS [implementation]
nsUnicodeToSJIS::nsUnicodeToSJIS()
: nsTableEncoderSupport((uShiftTable*) &g_SJISShiftTable,
(uMappingTable*) &g_SJISMappingTable)
{
}
nsresult nsUnicodeToSJIS::CreateInstance(nsISupports ** aResult)
{
*aResult = new nsUnicodeToSJIS();
return (*aResult == NULL)? NS_ERROR_OUT_OF_MEMORY : NS_OK;
}
//----------------------------------------------------------------------
// Subclassing of nsTableEncoderSupport class [implementation]
NS_IMETHODIMP nsUnicodeToSJIS::GetMaxLength(const PRUnichar * aSrc,
PRInt32 aSrcLength,
PRInt32 * aDestLength)
{
// XXX write me
*aDestLength = 0;
return NS_OK_UENC_EXACTLENGTH;
}

Просмотреть файл

@ -0,0 +1,57 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (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/NPL/
*
* 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 Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1998
* Netscape Communications Corporation. All Rights Reserved.
*/
#ifndef nsUnicodeToSJIS_h___
#define nsUnicodeToSJIS_h___
#include "nsUCvJaSupport.h"
//----------------------------------------------------------------------
// Class nsUnicodeToSJIS [declaration]
/**
* A character set converter from Unicode to SJIS.
*
* @created 17/Feb/1999
* @author Catalin Rotaru [CATA]
*/
class nsUnicodeToSJIS : public nsTableEncoderSupport
{
public:
/**
* Class constructor.
*/
nsUnicodeToSJIS();
/**
* Static class constructor.
*/
static nsresult CreateInstance(nsISupports **aResult);
protected:
//--------------------------------------------------------------------
// Subclassing of nsEncoderSupport class [declaration]
NS_IMETHOD GetMaxLength(const PRUnichar * aSrc, PRInt32 aSrcLength,
PRInt32 * aDestLength);
};
#endif /* nsUnicodeToSJIS_h___ */