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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
// license:GPL-2.0+
// copyright-holders:Couriersud
#include "pmain.h"
#ifdef _WIN32
#include <windows.h>
#include <cstring>
#include <tchar.h>
#endif
namespace plib {
#ifdef _WIN32
static pstring toutf8(const wchar_t *w)
{
auto wlen = wcslen(w);
int dst_char_count = WideCharToMultiByte(CP_UTF8, 0, w, wlen, nullptr, 0, nullptr, nullptr);
char *buf = new char[dst_char_count + 1];
WideCharToMultiByte(CP_UTF8, 0, w, wlen, buf, dst_char_count, nullptr, nullptr);
buf[dst_char_count] = 0;
auto ret = pstring(buf);
delete [] buf;
return ret;
}
#endif
app::app()
: options()
, pout(&std::cout)
, perr(&std::cerr)
{
}
int app::main_utfX(int argc, char **argv)
{
auto r = this->parse(argc, argv);
int ret = 0;
if (r != argc)
{
this->perr("Error parsing {}\n", argv[r]);
//FIXME: usage_short
this->perr(this->usage());
ret = 1;
}
else
ret = this->execute();
return ret;
}
#ifdef _WIN32
int app::main_utfX(int argc, wchar_t *argv[])
{
std::vector<pstring> argv_vectors(argc);
std::vector<char *> utf8_argv(argc);
// convert arguments to UTF-8
for (int i = 0; i < argc; i++)
{
argv_vectors[i] = toutf8(argv[i]);
utf8_argv[i] = const_cast<char *>(argv_vectors[i].c_str());
}
// run utf8_main
return main_utfX(argc, utf8_argv.data());
}
#endif
} // namespace plib
|