diff -ur galeon-0.12.orig/src/mozilla.cpp galeon-0.12/src/mozilla.cpp --- galeon-0.12.orig/src/mozilla.cpp Wed Aug 15 16:43:13 2001 +++ galeon-0.12/src/mozilla.cpp Tue Aug 21 07:39:58 2001 @@ -33,6 +33,7 @@ #include "nsICharsetConverterManager.h" #include "nsICharsetConverterManager2.h" #include "nsIUnicodeEncoder.h" +#include "nsIUnicodeDecoder.h" #include "nsISupportsPrimitives.h" #include "nsIPrintOptions.h" #include "nsGfxCIID.h" @@ -52,9 +53,11 @@ #include "nsIDOMWindowInternal.h" #include "nsIJVMManager.h" #include "nsIJSConsoleService.h" +#include "nsIPlatformCharset.h" static NS_DEFINE_CID(kPrintOptionsCID, NS_PRINTOPTIONS_CID); static NS_DEFINE_CID(kJVMManagerCID, NS_JVMMANAGER_CID); +static NS_DEFINE_CID(kPlatformCharsetCID, NS_PLATFORMCHARSET_CID); /* local function prototypes */ static char *convert_ns_string_to_c_string (const nsString & ns_string); @@ -1037,10 +1040,10 @@ extern "C" gchar * mozilla_unicode_to_locale (const PRUnichar *uniStr) { - PRInt32 sSize; - wchar_t *wide; + PRInt32 sSize,dSize; gchar *output; - gint i, count; + nsAutoString platformCharset; + nsresult rv; /* sanity */ if (uniStr == NULL) @@ -1048,39 +1051,41 @@ return NULL; } - const nsString str (uniStr); - sSize = str.Length (); - - /* allocate a wide string big enough to hold the unicode string, - * this is necessary since wchar_t is 32-bits with glibc */ - wide = g_new0 (wchar_t, sSize + 1); - for (i = 0; i < sSize + 1; i++) + nsCOMPtr platformCharsetService + = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv); + if (! NS_SUCCEEDED(rv)) { - wide[i] = uniStr[i]; + return NULL; } - /* use glibc function to determine the size of this string once - * encoded to a locale specfic multibyte string */ - count = wcstombs (NULL, wide, 0); + rv = platformCharsetService->GetCharset(kPlatformCharsetSel_Menu, + platformCharset); + nsCOMPtr ccm = + do_GetService (NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv); + if (! NS_SUCCEEDED(rv)) + { + return NULL; + } - /* check for success */ - if (count == -1) + nsCOMPtr encoder; + rv = ccm->GetUnicodeEncoder(&platformCharset, getter_AddRefs(encoder)); + if (! NS_SUCCEEDED(rv)) { - /* let Mozilla do a (lossy) conversion then */ - nsCString str; - str.AssignWithConversion(uniStr); - g_free (wide); - return g_strdup (str.get()); /* FIXME strdup needed? */ - /* Phil: Oh Yes! indeed. */ + return NULL; } - /* allocate a string big enough and do the actual conversion */ - output = g_new0 (gchar, count + 1); - count = wcstombs (output, wide, count + 1); - g_assert (count != -1); + const nsString str (uniStr); + sSize = str.Length (); + encoder->GetMaxLength (str.get(), sSize, &dSize); + if (! dSize) + { + return NULL; + } - /* free wide version and return */ - g_free (wide); + output = g_new0 (gchar, dSize + 1); + encoder->Convert (str.get(), &sSize, output, &dSize); + encoder->Finish (output, &dSize); + encoder->Reset (); return output; } @@ -1120,9 +1125,10 @@ extern "C" PRUnichar * mozilla_locale_to_unicode (const gchar *locStr) { + PRInt32 sSize,dSize; PRUnichar *uniStr; - wchar_t *wide; - gint i, count; + nsAutoString platformCharset; + nsresult rv; /* sanity */ if (locStr == NULL) @@ -1130,28 +1136,41 @@ return NULL; } - /* count the number of wide characters which will be produced */ - count = mbstowcs (NULL, locStr, 0); - if (count == -1) + nsCOMPtr platformCharsetService + = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv); + if (! NS_SUCCEEDED(rv)) { - /* hmm, shouldnt happen but fallback to utf8 */ - g_warning ("unusual locale string: [%s]\n", locStr); - return mozilla_utf8_to_unicode (locStr); + return NULL; } - /* allocate and decode */ - wide = g_new0 (wchar_t, count + 1); - mbstowcs (wide, locStr, count + 1); + rv = platformCharsetService->GetCharset(kPlatformCharsetSel_Menu, + platformCharset); + nsCOMPtr ccm = + do_GetService (NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv); + if (! NS_SUCCEEDED(rv)) + { + return NULL; + } - /* make a unicode string and copy into it */ - uniStr = g_new0 (PRUnichar, count + 1); - for (i = 0; i < count + 1; i++) + nsCOMPtr decoder; + rv = ccm->GetUnicodeDecoder(&platformCharset, getter_AddRefs(decoder)); + if (! NS_SUCCEEDED(rv)) { - uniStr[i] = wide[i]; + return NULL; } - /* free wide string and return the unicode one */ - g_free (wide); + /* FIXME if locStr in UTF-16, this will not work */ + sSize = strlen(locStr); + decoder->GetMaxLength (locStr, sSize, &dSize); + if (! dSize) + { + return NULL; + } + + uniStr = g_new0 (PRUnichar, dSize + 1); + decoder->Convert (locStr, &sSize, uniStr, &dSize); + uniStr[dSize] = '\0'; + decoder->Reset (); return uniStr; }