From a44b5a34a07b9f2905d419d5571fd53832c1f6c0 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Tue, 14 Jan 2020 11:38:48 -0500 Subject: Pull wmain -> main code from qpdf.cc into QUtil.cc --- libqpdf/QUtil.cc | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'libqpdf/QUtil.cc') diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index 63adbed2..1d29bccf 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -23,6 +23,7 @@ #include #include #include +#include #ifdef _WIN32 #include #include @@ -2361,3 +2362,38 @@ QUtil::possible_repaired_encodings(std::string supplied) } return t; } + +int +QUtil::call_main_from_wmain(int argc, wchar_t* argv[], std::function realmain) +{ + // argv contains UTF-16-encoded strings with a 16-bit wchar_t. + // Convert this to UTF-8-encoded strings for compatibility with + // other systems. That way the rest of qpdf.cc can just act like + // arguments are UTF-8. + + std::vector> utf8_argv; + for (int i = 0; i < argc; ++i) + { + std::string utf16; + for (size_t j = 0; j < wcslen(argv[i]); ++j) + { + unsigned short codepoint = static_cast(argv[i][j]); + utf16.append(1, static_cast( + QIntC::to_uchar(codepoint >> 8))); + utf16.append(1, static_cast( + QIntC::to_uchar(codepoint & 0xff))); + } + std::string utf8 = QUtil::utf16_to_utf8(utf16); + utf8_argv.push_back(std::shared_ptr(QUtil::copy_string(utf8.c_str()), std::default_delete())); + } + auto utf8_argv_sp = + std::shared_ptr(new char*[1+utf8_argv.size()], std::default_delete()); + char** new_argv = utf8_argv_sp.get(); + for (size_t i = 0; i < utf8_argv.size(); ++i) + { + new_argv[i] = utf8_argv.at(i).get(); + } + argc = QIntC::to_int(utf8_argv.size()); + new_argv[argc] = 0; + return realmain(argc, new_argv); +} -- cgit v1.2.3-54-g00ecf