-
Notifications
You must be signed in to change notification settings - Fork 28.3k
/
utf8_args.c
47 lines (37 loc) · 999 Bytes
/
utf8_args.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "utf8_args.h"
#ifdef _WIN32
#include <malloc.h>
#include <shellapi.h>
#include <windows.h>
void GetUtf8CommandLineArgs(int* argc, char*** argv) {
int wargc;
wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), &wargc);
if (wargv == NULL) {
*argc = 0;
*argv = NULL;
return;
}
*argc = wargc;
*argv = (char**)malloc(wargc * sizeof(char*));
for (int i = 0; i < wargc; ++i) {
int len =
WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL);
(*argv)[i] = malloc(len);
WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, (*argv)[i], len, NULL, NULL);
}
LocalFree(wargv);
}
void FreeUtf8CommandLineArgs(int argc, char** argv) {
for (int i = 0; i < argc; ++i) {
free(argv[i]);
}
free(argv);
}
#else
void GetUtf8CommandLineArgs(int* /*argc*/, char*** /*argv*/) {
// Do nothing on non-Windows platforms.
}
void FreeUtf8CommandLineArgs(int /*argc*/, char** /*argv*/) {
// Do nothing on non-Windows platforms.
}
#endif