Bug 488157. Use UTF8 instead of native codepages during startup. r=bsmedberg
authorHiroyuki Ikezoe <ikezoe@clear-code.com>
Wed, 13 May 2009 22:16:45 +1200
changeset 28297 8836a87bcdb9a51613c1f2631a9e8c07009bcf92
parent 28296 80b21b3fdb63f3ff595956b14a59f3da50ab7ba5
child 28298 ed499003e5771bcfbac8f8698e1d8f2f0ea1794f
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs488157
milestone1.9.2a1pre
Bug 488157. Use UTF8 instead of native codepages during startup. r=bsmedberg
xpcom/glue/nsINIParser.cpp
xpcom/glue/standalone/nsGlueLinkingWin.cpp
xpcom/glue/standalone/nsXPCOMGlue.cpp
xulrunner/stub/nsXULStub.cpp
--- a/xpcom/glue/nsINIParser.cpp
+++ b/xpcom/glue/nsINIParser.cpp
@@ -40,21 +40,40 @@
 
 #include "nsINIParser.h"
 #include "nsError.h"
 #include "nsILocalFile.h"
 #include "nsCRTGlue.h"
 
 #include <stdlib.h>
 #include <stdio.h>
+#ifdef XP_WIN
+#include <windows.h>
+#endif
 
-#if defined(XP_WIN) || defined(XP_OS2)
-#define BINARY_MODE "b"
+#if defined(XP_WIN)
+#define READ_BINARYMODE L"rb"
+#elif defined(XP_OS2)
+#define READ_BINARYMODE "rb"
 #else
-#define BINARY_MODE
+#define READ_BINARYMODE "r"
+#endif
+
+#ifdef XP_WIN
+inline FILE *TS_tfopen (const char *path, const wchar_t *mode)
+{
+    wchar_t wPath[MAX_PATH];
+    MultiByteToWideChar(CP_UTF8, 0, path, -1, wPath, MAX_PATH);
+    return _wfopen(wPath, mode);
+}
+#else
+inline FILE *TS_tfopen (const char *path, const char *mode)
+{
+    return fopen(path, mode);
+}
 #endif
 
 // Stack based FILE wrapper to ensure that fclose is called, copied from
 // toolkit/mozapps/update/src/updater/readstrings.cpp
 
 class AutoFILE {
 public:
   AutoFILE(FILE *fp = nsnull) : fp_(fp) {}
@@ -77,35 +96,36 @@ nsINIParser::Init(nsILocalFile* aFile)
 
     AutoFILE fd;
 
 #ifdef XP_WIN
     nsAutoString path;
     rv = aFile->GetPath(path);
     NS_ENSURE_SUCCESS(rv, rv);
 
-    fd = _wfopen(path.get(), L"rb");
+    fd = _wfopen(path.get(), READ_BINARYMODE);
 #else
     nsCAutoString path;
     rv = aFile->GetNativePath(path);
-    NS_ENSURE_SUCCESS(rv, rv);
 
-    fd = fopen(path.get(), "r" BINARY_MODE);
+    fd = fopen(path.get(), READ_BINARYMODE);
 #endif
+
     if (!fd)
       return NS_ERROR_FAILURE;
 
     return InitFromFILE(fd);
 }
 
 nsresult
 nsINIParser::Init(const char *aPath)
 {
     /* open the file */
-    AutoFILE fd = fopen(aPath, "r" BINARY_MODE);
+    AutoFILE fd = TS_tfopen(aPath, READ_BINARYMODE);
+
     if (!fd)
         return NS_ERROR_FAILURE;
 
     return InitFromFILE(fd);
 }
 
 static const char kNL[] = "\r\n";
 static const char kEquals[] = "=";
--- a/xpcom/glue/standalone/nsGlueLinkingWin.cpp
+++ b/xpcom/glue/standalone/nsGlueLinkingWin.cpp
@@ -72,17 +72,17 @@ AppendDependentLib(HINSTANCE libHandle)
 
     sTop = d;
 }
 
 static void
 ReadDependentCB(const char *aDependentLib)
 {
     wchar_t wideDependentLib[MAX_PATH];
-    MultiByteToWideChar(CP_ACP, 0, aDependentLib, -1, wideDependentLib, MAX_PATH);
+    MultiByteToWideChar(CP_UTF8, 0, aDependentLib, -1, wideDependentLib, MAX_PATH);
 
     HINSTANCE h =
         LoadLibraryExW(wideDependentLib, NULL, MOZ_LOADLIBRARY_FLAGS);
 
     if (!h) {
         wprintf(L"Error loading %s\n", wideDependentLib);
         return;
     }
@@ -136,17 +136,17 @@ bool ns_isRelPath(wchar_t* path)
     return true;
     
 }
 
 GetFrozenFunctionsFunc
 XPCOMGlueLoad(const char *aXpcomFile)
 {
     wchar_t xpcomFile[MAXPATHLEN];
-    MultiByteToWideChar(CP_ACP, 0, aXpcomFile,-1,
+    MultiByteToWideChar(CP_UTF8, 0, aXpcomFile,-1,
                         xpcomFile, MAXPATHLEN);
    
     
     if (xpcomFile[0] == '.' && xpcomFile[1] == '\0') {
         wcscpy(xpcomFile, LXPCOM_DLL);
     }
     else {
         wchar_t xpcomDir[MAXPATHLEN];
@@ -158,17 +158,17 @@ XPCOMGlueLoad(const char *aXpcomFile)
         else 
         {
             wcscpy(xpcomDir, xpcomFile);
         }
         wchar_t *lastSlash = ns_wcspbrk(xpcomDir, L"/\\");
         if (lastSlash) {
             *lastSlash = '\0';
             char xpcomDir_narrow[MAXPATHLEN];
-            WideCharToMultiByte(CP_ACP, 0, xpcomDir,-1,
+            WideCharToMultiByte(CP_UTF8, 0, xpcomDir,-1,
                                 xpcomDir_narrow, MAX_PATH, NULL, NULL);
 
             XPCOMGlueLoadDependentLibs(xpcomDir_narrow, ReadDependentCB);
             
             _snwprintf(lastSlash, MAXPATHLEN - wcslen(xpcomDir), L"\\" LXUL_DLL);
             sXULLibrary =
                 LoadLibraryExW(xpcomDir, NULL, MOZ_LOADLIBRARY_FLAGS);
 
--- a/xpcom/glue/standalone/nsXPCOMGlue.cpp
+++ b/xpcom/glue/standalone/nsXPCOMGlue.cpp
@@ -77,30 +77,46 @@ nsresult XPCOMGlueStartup(const char* xp
     if (NS_FAILED(rv)) {
         XPCOMGlueUnload();
         return rv;
     }
 
     return NS_OK;
 }
 
-#if defined(XP_WIN) || defined(XP_OS2)
-#define READ_TEXTMODE "t"
+#if defined(XP_WIN)
+#define READ_TEXTMODE L"rt"
+#elif defined(XP_OS2)
+#define READ_TEXTMODE "rt"
 #else
-#define READ_TEXTMODE
+#define READ_TEXTMODE "r"
+#endif
+
+#ifdef XP_WIN
+inline FILE *TS_tfopen (const char *path, const wchar_t *mode)
+{
+    wchar_t wPath[MAX_PATH];
+    MultiByteToWideChar(CP_UTF8, 0, path, -1, wPath, MAX_PATH);
+    return _wfopen(wPath, mode);
+}
+#else
+inline FILE *TS_tfopen (const char *path, const char *mode)
+{
+    return fopen(path, mode);
+}
 #endif
 
 void
 XPCOMGlueLoadDependentLibs(const char *xpcomDir, DependentLibsCallback cb)
 {
     char buffer[MAXPATHLEN];
     sprintf(buffer, "%s" XPCOM_FILE_PATH_SEPARATOR XPCOM_DEPENDENT_LIBS_LIST,
             xpcomDir);
 
-    FILE *flist = fopen(buffer, "r" READ_TEXTMODE);
+    FILE *flist = TS_tfopen(buffer, READ_TEXTMODE);
     if (!flist)
         return;
 
     while (fgets(buffer, sizeof(buffer), flist)) {
         int l = strlen(buffer);
 
         // ignore empty lines and comments
         if (l == 0 || *buffer == '#')
--- a/xulrunner/stub/nsXULStub.cpp
+++ b/xulrunner/stub/nsXULStub.cpp
@@ -449,18 +449,25 @@ main(int argc, char **argv)
   }
 
   NS_LogInit();
 
   int retval;
 
   { // Scope COMPtr and AutoAppData
     nsCOMPtr<nsILocalFile> iniFile;
+#ifdef XP_WIN
+    // On Windows and Windows CE, iniPath is UTF-8 encoded,
+    // so we need to convert it.
+    rv = NS_NewLocalFile(NS_ConvertUTF8toUTF16(iniPath), PR_FALSE,
+                         getter_AddRefs(iniFile));
+#else
     rv = NS_NewNativeLocalFile(nsDependentCString(iniPath), PR_FALSE,
                                getter_AddRefs(iniFile));
+#endif
     if (NS_FAILED(rv)) {
       Output(PR_TRUE, "Couldn't find application.ini file.\n");
       return 1;
     }
 
     AutoAppData appData(iniFile);
     if (!appData) {
       Output(PR_TRUE, "Error: couldn't parse application.ini.\n");
@@ -470,18 +477,24 @@ main(int argc, char **argv)
     NS_ASSERTION(appData->directory, "Failed to get app directory.");
 
     if (!appData->xreDirectory) {
       // chop "libxul.so" off the GRE path
       lastSlash = strrchr(greDir, PATH_SEPARATOR_CHAR);
       if (lastSlash) {
         *lastSlash = '\0';
       }
+#ifdef XP_WIN
+      // same as iniPath.
+      NS_NewLocalFile(NS_ConvertUTF8toUTF16(greDir), PR_FALSE,
+                      &appData->xreDirectory);
+#else
       NS_NewNativeLocalFile(nsDependentCString(greDir), PR_FALSE,
                             &appData->xreDirectory);
+#endif
     }
 
     retval = XRE_main(argc, argv, appData);
   }
 
   NS_LogTerm();
 
   XPCOMGlueShutdown();