]> git.tdb.fi Git - libs/core.git/commitdiff
Convert command line arguments on Windows from UTF-16 to UTF-8
authorMikko Rasa <tdb@tdb.fi>
Tue, 4 Sep 2012 20:06:45 +0000 (23:06 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 4 Sep 2012 20:06:45 +0000 (23:06 +0300)
source/core/main.cpp

index 19791eefddf697c148901094d6e62398ea46a833..dc8122dd3031bb02bcb43d10694f48b2760958e4 100644 (file)
@@ -1,14 +1,40 @@
 #ifdef WIN32
 #include <windows.h>
+#include <msp/stringcodec/utf16.h>
+#include <msp/stringcodec/utf8.h>
 #endif
 #include "application.h"
 
 #ifdef WIN32
+using namespace std;
+using namespace Msp;
+
 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int /*nCmdShow*/)
 { 
        int argc        = 0;
-       LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc);
-       return Msp::Application::run(argc, (char **)argv, hInstance);
+       LPWSTR *argv_w = CommandLineToArgvW(GetCommandLineW(), &argc);
+
+       unsigned max_size = 0;
+       for(int i=0; i<argc; ++i)
+               max_size += wcslen(argv_w[i])*4+1;
+
+       char *argv_buf = new char[max_size+(argc+1)*sizeof(char *)];
+       char **argv = reinterpret_cast<char **>(argv_buf);
+       char *argv_ptr = argv_buf+(argc+1)*sizeof(char *);
+       for(int i=0; i<argc; ++i)
+       {
+               argv[i] = argv_ptr;
+               string arg(reinterpret_cast<char *>(argv_w[i]), wcslen(argv_w[i])*2);
+               arg = StringCodec::transcode<StringCodec::Utf16Le, StringCodec::Utf8>(arg);
+               copy(arg.begin(), arg.end(), argv_ptr);
+               argv_ptr += arg.size();
+               *argv_ptr++ = 0;
+       }
+
+       LocalFree(argv_w);
+       int exit_code = Msp::Application::run(argc, argv, hInstance);
+       delete[] argv_buf;
+       return exit_code;
 }
 #endif