aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/QUtil.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2020-01-14 17:38:48 +0100
committerJay Berkenbilt <ejb@ql.org>2020-01-14 17:40:51 +0100
commita44b5a34a07b9f2905d419d5571fd53832c1f6c0 (patch)
treeb263fc2ca894f5f08fc8f71e14e1be74b2af42ad /libqpdf/QUtil.cc
parentab4061f1ee4e71a586f60bd65b9be4c96bf0bed8 (diff)
downloadqpdf-a44b5a34a07b9f2905d419d5571fd53832c1f6c0.tar.zst
Pull wmain -> main code from qpdf.cc into QUtil.cc
Diffstat (limited to 'libqpdf/QUtil.cc')
-rw-r--r--libqpdf/QUtil.cc36
1 files changed, 36 insertions, 0 deletions
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 <stdlib.h>
#include <string.h>
#include <fcntl.h>
+#include <memory>
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
@@ -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<int(int, char*[])> 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<std::shared_ptr<char>> 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<unsigned short>(argv[i][j]);
+ utf16.append(1, static_cast<char>(
+ QIntC::to_uchar(codepoint >> 8)));
+ utf16.append(1, static_cast<char>(
+ QIntC::to_uchar(codepoint & 0xff)));
+ }
+ std::string utf8 = QUtil::utf16_to_utf8(utf16);
+ utf8_argv.push_back(std::shared_ptr<char>(QUtil::copy_string(utf8.c_str()), std::default_delete<char[]>()));
+ }
+ auto utf8_argv_sp =
+ std::shared_ptr<char*>(new char*[1+utf8_argv.size()], std::default_delete<char*[]>());
+ 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);
+}