Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / toolkit / xre / nsWindowsWMain.cpp
blob21944141e733f2b01a6a907f6247fe019b0a445d
1 // This file is a .cpp file meant to be included in nsBrowserApp.cpp and other
2 // similar bootstrap code. It converts wide-character windows wmain into UTF-8
3 // narrow-character strings.
5 #ifndef XP_WIN
6 #error This file only makes sense on Windows.
7 #endif
9 #include "nsUTF8Utils.h"
11 #ifdef __MINGW32__
13 /* MingW currently does not implement a wide version of the
14 startup routines. Workaround is to implement something like
15 it ourselves. See bug 411826 */
17 #include <shellapi.h>
19 int wmain(int argc, WCHAR **argv);
21 int main(int argc, char **argv)
23 LPWSTR commandLine = GetCommandLineW();
24 int argcw = 0;
25 LPWSTR *argvw = CommandLineToArgvW(commandLine, &argcw);
26 if (!argvw)
27 return 127;
29 int result = wmain(argcw, argvw);
30 LocalFree(argvw);
31 return result;
33 #endif /* __MINGW32__ */
35 #define main NS_internal_main
37 int main(int argc, char **argv);
39 static char*
40 AllocConvertUTF16toUTF8(const WCHAR *arg)
42 // be generous... UTF16 units can expand up to 3 UTF8 units
43 int len = wcslen(arg);
44 char *s = new char[len * 3 + 1];
45 if (!s)
46 return NULL;
48 ConvertUTF16toUTF8 convert(s);
49 convert.write(arg, len);
50 convert.write_terminator();
51 return s;
54 static void
55 FreeAllocStrings(int argc, char **argv)
57 while (argc) {
58 --argc;
59 delete [] argv[argc];
62 delete [] argv;
65 int wmain(int argc, WCHAR **argv)
67 char **argvConverted = new char*[argc + 1];
68 if (!argvConverted)
69 return 127;
71 for (int i = 0; i < argc; ++i) {
72 argvConverted[i] = AllocConvertUTF16toUTF8(argv[i]);
73 if (!argvConverted[i]) {
74 return 127;
77 argvConverted[argc] = NULL;
79 // need to save argvConverted copy for later deletion.
80 char **deleteUs = new char*[argc+1];
81 if (!deleteUs) {
82 FreeAllocStrings(argc, argvConverted);
83 return 127;
85 for (int i=0; i<argc; i++)
86 deleteUs[i] = argvConverted[i];
87 int result = main(argc, argvConverted);
89 delete[] argvConverted;
90 FreeAllocStrings(argc, deleteUs);
92 return result;