Skip to content

Commit

Permalink
add winmain.h to fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
humem authored and Nathan Glenn committed Feb 16, 2016
1 parent 934a795 commit 4d01f41
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions winmain.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,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

0 comments on commit 4d01f41

Please sign in to comment.