From d9d429cc55bc147a2f12b685d48f795d2d388a69 Mon Sep 17 00:00:00 2001 From: "yokoyama%netscape.com" Date: Wed, 1 Aug 2001 22:36:54 +0000 Subject: [PATCH] Bug:54857 a new ScriptableUnicodeConverter IDL /r=nhotta,/sr=blizzard --- intl/uconv/idl/Makefile.in | 1 + intl/uconv/idl/makefile.win | 1 + intl/uconv/idl/nsIScriptableUConv.idl | 54 +++++++++ intl/uconv/src/Makefile.in | 1 + intl/uconv/src/makefile.win | 2 + intl/uconv/src/nsScriptableUConv.cpp | 156 ++++++++++++++++++++++++++ intl/uconv/src/nsScriptableUConv.h | 49 ++++++++ intl/uconv/src/nsUConvModule.cpp | 8 ++ 8 files changed, 272 insertions(+) create mode 100644 intl/uconv/idl/nsIScriptableUConv.idl create mode 100644 intl/uconv/src/nsScriptableUConv.cpp create mode 100644 intl/uconv/src/nsScriptableUConv.h diff --git a/intl/uconv/idl/Makefile.in b/intl/uconv/idl/Makefile.in index 7b1b7725fa27..21a45672ec3c 100644 --- a/intl/uconv/idl/Makefile.in +++ b/intl/uconv/idl/Makefile.in @@ -32,6 +32,7 @@ XPIDLSRCS = \ nsITextToSubURI.idl \ nsICurrentCharsetListener.idl \ nsICharsetConverterManager2.idl \ + nsIScriptableUConv.idl \ $(NULL) include $(topsrcdir)/config/rules.mk diff --git a/intl/uconv/idl/makefile.win b/intl/uconv/idl/makefile.win index 316540b1f6a7..8c429d896207 100644 --- a/intl/uconv/idl/makefile.win +++ b/intl/uconv/idl/makefile.win @@ -27,6 +27,7 @@ XPIDLSRCS = \ .\nsITextToSubURI.idl \ .\nsICurrentCharsetListener.idl \ .\nsICharsetConverterManager2.idl \ + .\nsIScriptableUConv.idl \ $(NULL) include <$(DEPTH)/config/rules.mak> diff --git a/intl/uconv/idl/nsIScriptableUConv.idl b/intl/uconv/idl/nsIScriptableUConv.idl new file mode 100644 index 000000000000..df46ad24fdae --- /dev/null +++ b/intl/uconv/idl/nsIScriptableUConv.idl @@ -0,0 +1,54 @@ +/* -*- 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.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/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. + * + * Contributor(s): + */ + +#include "nsISupports.idl" + +%{C++ +// {0A698C44-3BFF-11d4-9649-00C0CA135B4E} +#define NS_ISCRIPTABLEUNICODECONVERTER_CID { 0x0A698C44, 0x3BFF, 0x11d4, { 0x96, 0x49, 0x00, 0xC0, 0xCA, 0x13, 0x5B, 0x4E } } +#define NS_ISCRIPTABLEUNICODECONVERTER_CONTRACTID "@mozilla.org/intl/scriptableunicodeconverter" +%} + +/** + * This interface is unicode encoder using from script + * + * @created 8/Jun/2000 + * @author Makoto Kato [m_kato@ga2.so-net.ne.jp] + */ +[scriptable, uuid(0A698C44-3BFF-11d4-9649-00C0CA135B4E)] +interface nsIScriptableUnicodeConverter : nsISupports +{ + /** + * Converts the data from Unicode to one Charset. + */ + string ConvertFromUnicode([const] in wstring aSrc); + + /** + * Converts the data from one Charset to Unicode. + */ + wstring ConvertToUnicode([const] in string aSrc); + + /** + * Current charactor set + */ + attribute wstring charset; +}; diff --git a/intl/uconv/src/Makefile.in b/intl/uconv/src/Makefile.in index a9344fde10b2..f6e759f158bd 100644 --- a/intl/uconv/src/Makefile.in +++ b/intl/uconv/src/Makefile.in @@ -61,6 +61,7 @@ CPPSRCS = \ nsUnicodeToMacRoman.cpp \ nsUnicodeToUTF8.cpp \ nsUCvMinSupport.cpp \ + nsScriptableUConv.cpp \ $(NULL) EXPORT_RESOURCE = \ diff --git a/intl/uconv/src/makefile.win b/intl/uconv/src/makefile.win index 4f1f852c985e..f6b09ada81d1 100644 --- a/intl/uconv/src/makefile.win +++ b/intl/uconv/src/makefile.win @@ -48,6 +48,7 @@ CPPSRCS = \ nsUnicodeToMacRoman.cpp \ nsUnicodeToUTF8.cpp \ nsUCvMinSupport.cpp \ + nsScriptableUConv.cpp \ $(NULL) CPP_OBJS= \ @@ -70,6 +71,7 @@ CPP_OBJS= \ .\$(OBJDIR)\nsUnicodeToMacRoman.obj \ .\$(OBJDIR)\nsUnicodeToUTF8.obj \ .\$(OBJDIR)\nsUCvMinSupport.obj \ + .\$(OBJDIR)\nsScriptableUConv.obj \ $(NULL) CSRCS = \ diff --git a/intl/uconv/src/nsScriptableUConv.cpp b/intl/uconv/src/nsScriptableUConv.cpp new file mode 100644 index 000000000000..9309a1af3618 --- /dev/null +++ b/intl/uconv/src/nsScriptableUConv.cpp @@ -0,0 +1,156 @@ +/* -*- 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.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/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. + * + * Contributor(s): + * Makoto Kato + * Ryoichi Furukawa + */ + +#include "pratom.h" +#include "nsString.h" +#include "nsIServiceManager.h" +#include "nsICharsetConverterManager.h" +#include "nsICharsetConverterManager2.h" +#include "nsIScriptableUConv.h" +#include "nsScriptableUConv.h" + +#define NS_IMPL_IDS +#include "nsIPlatformCharset.h" +#undef NS_IMPL_IDS + +static NS_DEFINE_CID(kIScriptableUnicodeConverterCID, NS_ISCRIPTABLEUNICODECONVERTER_CID); +static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID); + +static PRInt32 gInstanceCount = 0; + +/* Implementation file */ +NS_IMPL_ISUPPORTS1(nsScriptableUnicodeConverter, nsIScriptableUnicodeConverter) + +nsScriptableUnicodeConverter::nsScriptableUnicodeConverter() +{ + NS_INIT_REFCNT(); + PR_AtomicIncrement(&gInstanceCount); +} + +nsScriptableUnicodeConverter::~nsScriptableUnicodeConverter() +{ + PR_AtomicDecrement(&gInstanceCount); +} + +/* string ConvertFromUnicode ([const] in wstring src); */ +NS_IMETHODIMP +nsScriptableUnicodeConverter::ConvertFromUnicode(const PRUnichar *aSrc, char **_retval) +{ + if (!mEncoder) + return NS_ERROR_FAILURE; + + nsresult rv = NS_OK; + PRInt32 inLength = nsCRT::strlen(aSrc); + PRInt32 outLength; + rv = mEncoder->GetMaxLength(aSrc, inLength, &outLength); + if (NS_SUCCEEDED(rv)) { + *_retval = (char*) nsMemory::Alloc(outLength+1); + if (!*_retval) + return NS_ERROR_OUT_OF_MEMORY; + + rv = mEncoder->Convert(aSrc, &inLength, *_retval, &outLength); + if (NS_SUCCEEDED(rv)) + { + (*_retval)[outLength] = '\0'; + return NS_OK; + } + nsMemory::Free(*_retval); + } + *_retval = nsnull; + return NS_ERROR_FAILURE; +} + +/* wstring ConvertToUnicode ([const] in string src); */ +NS_IMETHODIMP +nsScriptableUnicodeConverter::ConvertToUnicode(const char *aSrc, PRUnichar **_retval) +{ + if (!mDecoder) + return NS_ERROR_FAILURE; + + nsresult rv = NS_OK; + PRInt32 inLength = nsCRT::strlen(aSrc); + PRInt32 outLength; + rv = mDecoder->GetMaxLength(aSrc, inLength, &outLength); + if (NS_SUCCEEDED(rv)) + { + *_retval = (PRUnichar*) nsMemory::Alloc((outLength+1)*sizeof(PRUnichar)); + if (!*_retval) + return NS_ERROR_OUT_OF_MEMORY; + + rv = mDecoder->Convert(aSrc, &inLength, *_retval, &outLength); + if (NS_SUCCEEDED(rv)) + { + (*_retval)[outLength] = 0; + return NS_OK; + } + nsMemory::Free(*_retval); + } + *_retval = nsnull; + return NS_ERROR_FAILURE; +} + +/* attribute wstring charset; */ +NS_IMETHODIMP +nsScriptableUnicodeConverter::GetCharset(PRUnichar * *aCharset) +{ + *aCharset = mCharset.ToNewUnicode() ; + if (!*aCharset) + return NS_ERROR_OUT_OF_MEMORY; + + return NS_OK; +} + +NS_IMETHODIMP +nsScriptableUnicodeConverter::SetCharset(const PRUnichar * aCharset) +{ + mCharset.Assign(aCharset); + return InitConverter(); +} + +nsresult +nsScriptableUnicodeConverter::InitConverter() +{ + nsresult rv = NS_OK; + mEncoder = NULL ; + + nsCOMPtr ccm2 = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv); + + if (NS_SUCCEEDED( rv) && (nsnull != ccm2)) { + // get charset atom due to getting unicode converter + nsCOMPtr charsetAtom; + rv = ccm2->GetCharsetAtom(mCharset.get(), getter_AddRefs(charsetAtom)); + if (NS_SUCCEEDED(rv)) { + // get an unicode converter + rv = ccm2->GetUnicodeEncoder(charsetAtom, getter_AddRefs(mEncoder)); + if(NS_SUCCEEDED(rv)) { + rv = mEncoder->SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Replace, nsnull, (PRUnichar)'?'); + if(NS_SUCCEEDED(rv)) { + rv = ccm2->GetUnicodeDecoder(charsetAtom, getter_AddRefs(mDecoder)); + } + } + } + } + + return rv ; +} diff --git a/intl/uconv/src/nsScriptableUConv.h b/intl/uconv/src/nsScriptableUConv.h new file mode 100644 index 000000000000..21576f45b552 --- /dev/null +++ b/intl/uconv/src/nsScriptableUConv.h @@ -0,0 +1,49 @@ +/* -*- 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.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/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. + * + * Contributor(s): + * Makoto Kato + * Ryoichi Furukawa + */ + + +#ifndef __nsScriptableUConv_h_ +#define __nsScriptableUConv_h_ + +#include "nsICharsetConverterManager2.h" +#include "nsIScriptableUConv.h" + +class nsScriptableUnicodeConverter : public nsIScriptableUnicodeConverter +{ +public: + NS_DECL_ISUPPORTS + NS_DECL_NSISCRIPTABLEUNICODECONVERTER + + nsScriptableUnicodeConverter(); + virtual ~nsScriptableUnicodeConverter(); + +protected: + nsAutoString mCharset; + nsCOMPtr mEncoder; + nsCOMPtr mDecoder; + + nsresult InitConverter(); +}; + +#endif diff --git a/intl/uconv/src/nsUConvModule.cpp b/intl/uconv/src/nsUConvModule.cpp index 7361b6dd3b63..ddd33b363088 100644 --- a/intl/uconv/src/nsUConvModule.cpp +++ b/intl/uconv/src/nsUConvModule.cpp @@ -37,6 +37,7 @@ #include "nsUConvDll.h" #include "nsFileSpec.h" #include "nsIFile.h" +#include "nsIScriptableUConv.h" #include "nsUCvMinSupport.h" #include "nsISO88591ToUnicode.h" @@ -47,6 +48,7 @@ #include "nsUnicodeToCP1252.h" #include "nsUnicodeToMacRoman.h" #include "nsUnicodeToUTF8.h" +#include "nsScriptableUConv.h" //---------------------------------------------------------------------- // Global functions and data [declaration] @@ -56,6 +58,7 @@ static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CI static NS_DEFINE_CID(kCharsetMenuCID, NS_CHARSETMENU_CID); static NS_DEFINE_CID(kTextToSubURICID, NS_TEXTTOSUBURI_CID); static NS_DEFINE_CID(kPlatformCharsetCID, NS_PLATFORMCHARSET_CID); +static NS_DEFINE_CID(kIScriptableUnicodeConverterCID, NS_ISCRIPTABLEUNICODECONVERTER_CID); // converters NS_DEFINE_CID(kISO88591ToUnicodeCID, NS_ISO88591TOUNICODE_CID); @@ -66,6 +69,7 @@ NS_DEFINE_CID(kUnicodeToISO88591CID, NS_UNICODETOISO88591_CID); NS_DEFINE_CID(kUnicodeToCP1252CID, NS_UNICODETOCP1252_CID); NS_DEFINE_CID(kUnicodeToMacRomanCID, NS_UNICODETOMACROMAN_CID); NS_DEFINE_CID(kUnicodeToUTF8CID, NS_UNICODETOUTF8_CID); +NS_GENERIC_FACTORY_CONSTRUCTOR(nsScriptableUnicodeConverter); class nsUConvModule : public nsIModule { public: @@ -184,6 +188,10 @@ static Components gComponents[] = { { { "Unicode To UTF-8 Converter", NS_UNICODETOUTF8_CID, NS_UNICODETOUTF8_CONTRACTID, NS_NewUnicodeToUTF8 }, "Unicode", "UTF-8", }, + + { { "Unicode Encoder / Decoder for Script", NS_ISCRIPTABLEUNICODECONVERTER_CID, + NS_ISCRIPTABLEUNICODECONVERTER_CONTRACTID, nsScriptableUnicodeConverterConstructor }, + NULL, NULL, }, }; #define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))