diff --git a/intl/uconv/ucvja/nsSJIS2Unicode.cpp b/intl/uconv/ucvja/nsSJIS2Unicode.cpp index ef3b6fa5e78a..bb8afcc3fbbc 100644 --- a/intl/uconv/ucvja/nsSJIS2Unicode.cpp +++ b/intl/uconv/ucvja/nsSJIS2Unicode.cpp @@ -18,86 +18,9 @@ */ #include "pratom.h" - -#define NS_IMPL_IDS - #include "nsRepository.h" -#include "nsIUnicodeDecoder.h" -#include "nsIUnicodeDecodeUtil.h" #include "nsSJIS2Unicode.h" -#include "nsICharsetConverterManager.h" #include "nsUCVJADll.h" -#include "nsUCVJACID.h" - -//---------------------------------------------------------------------- -// Global functions and data [declaration] - -#define NS_SRC_CHARSET "Shift_JIS" -#define NS_DEST_CHARSET "Unicode" - -//---------------------------------------------------------------------- -// Class nsSJIS2Unicode [declaration] - -/** - * A character set converter from SJIS to Unicode. - * - * This particular converter does not use the general single-byte converter - * helper object. That is because someone may want to optimise this converter - * to the fullest, as it is the most heavily used one. - * - * Multithreading: not an issue, the object has one instance per user thread. - * As a plus, it is also stateless! - * - * @created 23/Nov/1998 - * @author Catalin Rotaru [CATA] - */ -class nsSJIS2Unicode : public nsIUnicodeDecoder -{ - NS_DECL_ISUPPORTS - -public: - - /** - * Class constructor. - */ - nsSJIS2Unicode(); - - /** - * Class destructor. - */ - ~nsSJIS2Unicode(); - - //-------------------------------------------------------------------- - // Interface nsIUnicodeDecoder [declaration] - - NS_IMETHOD Convert(PRUnichar * aDest, PRInt32 aDestOffset, - PRInt32 * aDestLength,const char * aSrc, PRInt32 aSrcOffset, - PRInt32 * aSrcLength); - NS_IMETHOD Finish(PRUnichar * aDest, PRInt32 aDestOffset, - PRInt32 * aDestLength); - NS_IMETHOD Length(const char * aSrc, PRInt32 aSrcOffset, PRInt32 aSrcLength, - PRInt32 * aDestLength); - NS_IMETHOD Reset(); - NS_IMETHOD SetInputErrorBehavior(PRInt32 aBehavior); - -private: - PRInt32 mBehavior; - nsIUnicodeDecodeUtil *mUtil; - -}; - -static PRInt16 gShiftTable[] = { - 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) -}; - -static PRUint16 gMappingTable[] = { -#include "sjis.ut" -}; - //---------------------------------------------------------------------- // Class nsSJIS2Unicode [implementation] @@ -118,10 +41,30 @@ nsSJIS2Unicode::~nsSJIS2Unicode() PR_AtomicDecrement(&g_InstanceCount); } +nsresult nsSJIS2Unicode::CreateInstance(nsISupports ** aResult) +{ + *aResult = new nsSJIS2Unicode(); + return (*aResult == NULL)? NS_ERROR_OUT_OF_MEMORY : NS_OK; +} //---------------------------------------------------------------------- // Interface nsICharsetConverter [implementation] +static PRInt16 gShiftTable[] = { + 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) +}; + +static PRUint16 gMappingTable[] = { +#include "sjis.ut" +}; + +// XXX quick hack so I don't have to include nsICharsetConverterManager +extern "C" const nsID kCharsetConverterManagerCID; + NS_IMETHODIMP nsSJIS2Unicode::Convert(PRUnichar * aDest, PRInt32 aDestOffset, PRInt32 * aDestLength, const char * aSrc, PRInt32 aSrcOffset, @@ -177,97 +120,3 @@ NS_IMETHODIMP nsSJIS2Unicode::SetInputErrorBehavior(PRInt32 aBehavior) mBehavior = aBehavior; return NS_OK; } - -//---------------------------------------------------------------------- -// Class nsSJIS2UnicodeFactory [implementation] - -nsSJIS2UnicodeFactory::nsSJIS2UnicodeFactory() -{ - NS_INIT_REFCNT(); - PR_AtomicIncrement(&g_InstanceCount); -} - -nsSJIS2UnicodeFactory::~nsSJIS2UnicodeFactory() -{ - PR_AtomicDecrement(&g_InstanceCount); -} - -//---------------------------------------------------------------------- -// Interface nsISupports [implementation] - -NS_IMPL_ADDREF(nsSJIS2UnicodeFactory); -NS_IMPL_RELEASE(nsSJIS2UnicodeFactory); - -nsresult nsSJIS2UnicodeFactory::QueryInterface(REFNSIID aIID, - void** aInstancePtr) -{ - if (NULL == aInstancePtr) { - return NS_ERROR_NULL_POINTER; - } - - *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)) { - *aInstancePtr = (void*) ((nsISupports*)(nsIFactory*)this); - NS_ADDREF_THIS(); - return NS_OK; - } - - return NS_NOINTERFACE; -} - -//---------------------------------------------------------------------- -// Interface nsIFactory [implementation] - -NS_IMETHODIMP nsSJIS2UnicodeFactory::CreateInstance(nsISupports *aDelegate, - const nsIID &aIID, - void **aResult) -{ - if (aResult == NULL) return NS_ERROR_NULL_POINTER; - if (aDelegate != NULL) return NS_ERROR_NO_AGGREGATION; - - nsIUnicodeDecoder * t = new nsSJIS2Unicode; - if (t == NULL) return NS_ERROR_OUT_OF_MEMORY; - - nsresult res = t->QueryInterface(aIID, aResult); - if (NS_FAILED(res)) delete t; - - return res; -} - -NS_IMETHODIMP nsSJIS2UnicodeFactory::LockFactory(PRBool aLock) -{ - if (aLock) PR_AtomicIncrement(&g_LockCount); - else PR_AtomicDecrement(&g_LockCount); - - return NS_OK; -} - -//---------------------------------------------------------------------- -// Interface nsICharsetConverterInfo [implementation] - -NS_IMETHODIMP nsSJIS2UnicodeFactory::GetCharsetSrc(char ** aCharset) -{ - (*aCharset) = NS_SRC_CHARSET; - return NS_OK; -} - -NS_IMETHODIMP nsSJIS2UnicodeFactory::GetCharsetDest(char ** aCharset) -{ - (*aCharset) = NS_DEST_CHARSET; - return NS_OK; -} diff --git a/intl/uconv/ucvja/nsSJIS2Unicode.h b/intl/uconv/ucvja/nsSJIS2Unicode.h index 498fc58a30e2..107b60b83e93 100644 --- a/intl/uconv/ucvja/nsSJIS2Unicode.h +++ b/intl/uconv/ucvja/nsSJIS2Unicode.h @@ -20,18 +20,26 @@ #ifndef nsSJIS2Unicode_h___ #define nsSJIS2Unicode_h___ -#include "nsIFactory.h" -#include "nsICharsetConverterInfo.h" +#include "nsIUnicodeDecoder.h" +#include "nsIUnicodeDecodeUtil.h" //---------------------------------------------------------------------- -// Class nsSJIS2UnicodeFactory [declaration] +// Class nsSJIS2Unicode [declaration] /** - * Factory class for the nsSJIS2Unicode objects. + * A character set converter from SJIS to Unicode. + * + * This particular converter does not use the general single-byte converter + * helper object. That is because someone may want to optimise this converter + * to the fullest, as it is the most heavily used one. + * + * Multithreading: not an issue, the object has one instance per user thread. + * As a plus, it is also stateless! * + * @created 23/Nov/1998 + * @author Catalin Rotaru [CATA] */ -class nsSJIS2UnicodeFactory : public nsIFactory, -public nsICharsetConverterInfo +class nsSJIS2Unicode : public nsIUnicodeDecoder { NS_DECL_ISUPPORTS @@ -40,28 +48,35 @@ public: /** * Class constructor. */ - nsSJIS2UnicodeFactory(); + nsSJIS2Unicode(); /** * Class destructor. */ - ~nsSJIS2UnicodeFactory(); + ~nsSJIS2Unicode(); + + /** + * Static class constructor. + */ + static nsresult CreateInstance(nsISupports **aResult); //-------------------------------------------------------------------- - // Interface nsIFactory [declaration] + // Interface nsIUnicodeDecoder [declaration] - NS_IMETHOD CreateInstance(nsISupports *aDelegate, const nsIID &aIID, - void **aResult); + NS_IMETHOD Convert(PRUnichar * aDest, PRInt32 aDestOffset, + PRInt32 * aDestLength,const char * aSrc, PRInt32 aSrcOffset, + PRInt32 * aSrcLength); + NS_IMETHOD Finish(PRUnichar * aDest, PRInt32 aDestOffset, + PRInt32 * aDestLength); + NS_IMETHOD Length(const char * aSrc, PRInt32 aSrcOffset, PRInt32 aSrcLength, + PRInt32 * aDestLength); + NS_IMETHOD Reset(); + NS_IMETHOD SetInputErrorBehavior(PRInt32 aBehavior); - NS_IMETHOD LockFactory(PRBool aLock); +private: + PRInt32 mBehavior; + nsIUnicodeDecodeUtil *mUtil; - //-------------------------------------------------------------------- - // Interface nsICharsetConverterInfo [declaration] - - NS_IMETHOD GetCharsetSrc(char ** aCharset); - NS_IMETHOD GetCharsetDest(char ** aCharset); }; - - #endif /* nsSJIS2Unicode_h___ */ diff --git a/intl/uconv/ucvja/nsUCVJACID.h b/intl/uconv/ucvja/nsUCVJACID.h index 85970b13ad81..10793dc84e20 100644 --- a/intl/uconv/ucvja/nsUCVJACID.h +++ b/intl/uconv/ucvja/nsUCVJACID.h @@ -27,5 +27,4 @@ NS_DECLARE_ID(kSJIS2UnicodeCID, 0xe6892c1, 0xa9ad, 0x11d2, 0xb3, 0xae, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70); - #endif /* nsUCVJACID_h___ */ diff --git a/intl/uconv/ucvja/nsUCVJADll.cpp b/intl/uconv/ucvja/nsUCVJADll.cpp index 6537b62a93bc..6e64f56c09bc 100644 --- a/intl/uconv/ucvja/nsUCVJADll.cpp +++ b/intl/uconv/ucvja/nsUCVJADll.cpp @@ -17,10 +17,19 @@ * Netscape Communications Corporation. All Rights Reserved. */ +#define NS_IMPL_IDS +#include "pratom.h" #include "nsRepository.h" -#include "nsSJIS2Unicode.h" +#include "nsIFactory.h" +#include "nsICharsetConverterInfo.h" #include "nsUCVJACID.h" +#include "nsSJIS2Unicode.h" + +// just for NS_IMPL_IDS; this is a good, central place to implement GUIDs +#include "nsIUnicodeDecoder.h" +#include "nsIUnicodeDecodeUtil.h" +#include "nsICharsetConverterManager.h" //---------------------------------------------------------------------- // Global functions and data [declaration] @@ -28,6 +37,74 @@ extern "C" PRInt32 g_InstanceCount = 0; extern "C" PRInt32 g_LockCount = 0; +typedef nsresult (* fpCreateInstance) (nsISupports **); + +struct FactoryData +{ + const nsCID * mCID; + fpCreateInstance CreateInstance; + char * mCharsetSrc; + char * mCharsetDest; +}; + +FactoryData g_FactoryData[] = +{ + { + &kSJIS2UnicodeCID, + nsSJIS2Unicode::CreateInstance, + "Shift_JIS", + "Unicode" + }, +}; + +#define ARRAY_SIZE(_array) \ + (sizeof(_array) / sizeof(_array[0])) + +//---------------------------------------------------------------------- +// Class nsConverterFactory [declaration] + +/** + * General factory class for converter objects. + * + * @created 24/Feb/1998 + * @author Catalin Rotaru [CATA] + */ +class nsConverterFactory : public nsIFactory, +public nsICharsetConverterInfo +{ + NS_DECL_ISUPPORTS + +private: + + FactoryData * mData; + +public: + + /** + * Class constructor. + */ + nsConverterFactory(FactoryData * aData); + + /** + * Class destructor. + */ + virtual ~nsConverterFactory(); + + //-------------------------------------------------------------------- + // Interface nsIFactory [declaration] + + NS_IMETHOD CreateInstance(nsISupports *aDelegate, const nsIID &aIID, + void **aResult); + + NS_IMETHOD LockFactory(PRBool aLock); + + //-------------------------------------------------------------------- + // Interface nsICharsetConverterInfo [declaration] + + NS_IMETHOD GetCharsetSrc(char ** aCharset); + NS_IMETHOD GetCharsetDest(char ** aCharset); +}; + //---------------------------------------------------------------------- // Global functions and data [implementation] @@ -38,22 +115,28 @@ extern "C" NS_EXPORT PRBool NSCanUnload() return PRBool(g_InstanceCount == 0 && g_LockCount == 0); } -extern "C" NS_EXPORT nsresult NSGetFactory(const nsCID &aCID, nsISupports* serviceMgr, +extern "C" NS_EXPORT nsresult NSGetFactory(const nsCID &aCID, + nsISupports* serviceMgr, nsIFactory **aFactory) { if (aFactory == NULL) return NS_ERROR_NULL_POINTER; - // the SJIS2Unicode converter - if (aCID.Equals(kSJIS2UnicodeCID)) { - nsSJIS2UnicodeFactory *factory = new nsSJIS2UnicodeFactory(); - nsresult res = factory->QueryInterface(kIFactoryIID, (void **) aFactory); + nsresult res; + nsConverterFactory * fac; + FactoryData * data; - if (NS_FAILED(res)) { - *aFactory = NULL; - delete factory; + for (PRInt32 i=0; imCID))) { + fac = new nsConverterFactory(data); + res = fac->QueryInterface(kIFactoryIID, (void **) aFactory); + if (NS_FAILED(res)) { + *aFactory = NULL; + delete fac; + } + + return res; } - - return res; } return NS_NOINTERFACE; @@ -63,8 +146,11 @@ extern "C" NS_EXPORT nsresult NSRegisterSelf(const char * path) { nsresult res; - res = nsRepository::RegisterFactory(kSJIS2UnicodeCID, path, + for (PRInt32 i=0; iCreateInstance(&t); + if (t == NULL) return NS_ERROR_OUT_OF_MEMORY; + + nsresult res = t->QueryInterface(aIID, aResult); + if (NS_FAILED(res)) delete t; + + return res; +} + +NS_IMETHODIMP nsConverterFactory::LockFactory(PRBool aLock) +{ + if (aLock) PR_AtomicIncrement(&g_LockCount); + else PR_AtomicDecrement(&g_LockCount); + + return NS_OK; +} + +//---------------------------------------------------------------------- +// Interface nsICharsetConverterInfo [implementation] + +NS_IMETHODIMP nsConverterFactory::GetCharsetSrc(char ** aCharset) +{ + (*aCharset) = mData->mCharsetSrc; + return NS_OK; +} + +NS_IMETHODIMP nsConverterFactory::GetCharsetDest(char ** aCharset) +{ + (*aCharset) = mData->mCharsetDest; + return NS_OK; +}