forked from mtlynch/crfpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
winmain.h
69 lines (60 loc) · 1.79 KB
/
winmain.h
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
//
// CRF++ -- Yet Another CRF toolkit
//
// $Id: common.h 1588 2007-02-12 09:03:39Z taku $;
//
// Copyright(C) 2005-2007 Taku Kudo <[email protected]>
//
#if defined(_WIN32) || defined(__CYGWIN__)
#include <windows.h>
#include <string>
namespace {
class CommandLine {
public:
CommandLine(int argc, wchar_t **argv) : argc_(argc), argv_(0) {
argv_ = new char * [argc_];
for (int i = 0; i < argc_; ++i) {
const std::string arg = WideToUtf8(argv[i]);
argv_[i] = new char[arg.size() + 1];
::memcpy(argv_[i], arg.data(), arg.size());
argv_[i][arg.size()] = '\0';
}
}
~CommandLine() {
for (int i = 0; i < argc_; ++i) {
delete [] argv_[i];
}
delete [] argv_;
}
int argc() const { return argc_; }
char **argv() const { return argv_; }
private:
static std::string WideToUtf8(const std::wstring &input) {
const int output_length = ::WideCharToMultiByte(CP_UTF8, 0,
input.c_str(), -1, NULL, 0,
NULL, NULL);
if (output_length == 0) {
return "";
}
char *input_encoded = new char[output_length + 1];
const int result = ::WideCharToMultiByte(CP_UTF8, 0, input.c_str(), -1,
input_encoded,
output_length + 1, NULL, NULL);
std::string output;
if (result > 0) {
output.assign(input_encoded);
}
delete [] input_encoded;
return output;
}
int argc_;
char **argv_;
};
} // namespace
#define main(argc, argv) wmain_to_main_wrapper(argc, argv)
int wmain_to_main_wrapper(int argc, char **argv);
int wmain(int argc, wchar_t **argv) {
CommandLine cmd(argc, argv);
return wmain_to_main_wrapper(cmd.argc(), cmd.argv());
}
#endif