Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add winmain.h to fix build #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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