Skip to content

Commit 6645ae2

Browse files
committed
Windows: convert program arguments to UTF-8
See #83
1 parent 835e504 commit 6645ae2

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

source/turbo/app.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct TurboApp : public TApplication, EditorWindowParent
2828
turbo::SearchSettings searchSettings;
2929
std::string mostRecentDir;
3030

31-
TurboApp(int argc=0, const char *argv[]=0) noexcept;
31+
TurboApp(int argc, const char **argv) noexcept;
3232
static TMenuBar* initMenuBar(TRect r);
3333
static TStatusLine* initStatusLine(TRect r);
3434

source/turbo/main.cc

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,69 @@
11
#include "app.h"
22

3-
int main(int argc, const char *argv[])
3+
static void runTurbo(int argc, const char **argv)
44
{
55
TurboApp app(argc, argv);
66
app.run();
77
app.shutDown();
88
}
9+
10+
#if !defined(_WIN32)
11+
12+
int main(int argc, const char *argv[])
13+
{
14+
runTurbo(argc, argv);
15+
}
16+
17+
#else
18+
19+
static const char *convertToUtf8(const wchar_t *wstr) {
20+
int bytes = WideCharToMultiByte(
21+
/*CodePage*/ CP_UTF8,
22+
/*dwFlags*/ 0,
23+
/*lpWideCharStr*/ wstr,
24+
/*cchWideChar*/ -1,
25+
/*lpMultiByteStr*/ nullptr,
26+
/*cbMultiByte*/ 0,
27+
/*lpDefaultChar*/ nullptr,
28+
/*lpUsedDefaultChar*/ nullptr
29+
);
30+
31+
char *result;
32+
if (bytes <= 1)
33+
{
34+
result = new char[1];
35+
result[0] = '\0';
36+
}
37+
else
38+
{
39+
result = new char[bytes];
40+
WideCharToMultiByte(
41+
/*CodePage*/ CP_UTF8,
42+
/*dwFlags*/ 0,
43+
/*lpWideCharStr*/ wstr,
44+
/*cchWideChar*/ -1,
45+
/*lpMultiByteStr*/ result,
46+
/*cbMultiByte*/ bytes,
47+
/*lpDefaultChar*/ nullptr,
48+
/*lpUsedDefaultChar*/ nullptr
49+
);
50+
}
51+
52+
return result;
53+
}
54+
55+
int wmain(int argc, const wchar_t *wargv[])
56+
{
57+
// We need to convert arguments to UTF-8 manually.
58+
const char **argv = new const char *[argc];
59+
for (int i = 0; i < argc; ++i)
60+
argv[i] = convertToUtf8(wargv[i]);
61+
62+
runTurbo(argc, argv);
63+
64+
for (int i = 0; i < argc; ++i)
65+
delete[] argv[i];
66+
delete[] argv;
67+
}
68+
69+
#endif // _WIN32

0 commit comments

Comments
 (0)