Convert args to UTF-8 under Windows
This commit is contained in:
parent
89cb027786
commit
59a8dfe833
|
@ -112,8 +112,8 @@ set(META_APP_AUTHOR "Martchus")
|
||||||
set(META_APP_URL "https://github.com/${META_APP_AUTHOR}/${META_PROJECT_NAME}")
|
set(META_APP_URL "https://github.com/${META_APP_AUTHOR}/${META_PROJECT_NAME}")
|
||||||
set(META_APP_DESCRIPTION "Common C++ classes and routines used by my applications such as argument parser, IO and conversion utilities")
|
set(META_APP_DESCRIPTION "Common C++ classes and routines used by my applications such as argument parser, IO and conversion utilities")
|
||||||
set(META_VERSION_MAJOR 4)
|
set(META_VERSION_MAJOR 4)
|
||||||
set(META_VERSION_MINOR 3)
|
set(META_VERSION_MINOR 4)
|
||||||
set(META_VERSION_PATCH 1)
|
set(META_VERSION_PATCH 0)
|
||||||
|
|
||||||
# find required 3rd party libraries
|
# find required 3rd party libraries
|
||||||
include(3rdParty)
|
include(3rdParty)
|
||||||
|
|
|
@ -73,6 +73,42 @@ void startConsole()
|
||||||
// sync
|
// sync
|
||||||
ios::sync_with_stdio(true);
|
ios::sync_with_stdio(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Convert command line arguments to UTF-8.
|
||||||
|
* \remarks Only available on Windows (on other platforms we can assume passed arguments are already UTF-8 encoded).
|
||||||
|
*/
|
||||||
|
pair<vector<unique_ptr<char[]> >, vector<char *> > convertArgsToUtf8()
|
||||||
|
{
|
||||||
|
pair<vector<unique_ptr<char[]> >, vector<char *> > res;
|
||||||
|
int argc;
|
||||||
|
|
||||||
|
LPWSTR *argv_w = CommandLineToArgvW(GetCommandLineW(), &argc);
|
||||||
|
if(!argv_w || argc <= 0) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.first.reserve(static_cast<size_t>(argc));
|
||||||
|
res.second.reserve(static_cast<size_t>(argc));
|
||||||
|
for(; argv_w; ++argv_w) {
|
||||||
|
int requiredSize = WideCharToMultiByte(CP_UTF8, 0, *argv_w, -1, nullptr, 0, 0, 0);
|
||||||
|
if(requiredSize <= 0) {
|
||||||
|
break; // just stop on error
|
||||||
|
}
|
||||||
|
|
||||||
|
auto argv = make_unique<char[]>(static_cast<size_t>(requiredSize));
|
||||||
|
requiredSize = WideCharToMultiByte(CP_UTF8, 0, *argv_w, -1, argv.get(), requiredSize, 0, 0);
|
||||||
|
if(requiredSize <= 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.second.emplace_back(argv.get());
|
||||||
|
res.first.emplace_back(move(argv));
|
||||||
|
}
|
||||||
|
|
||||||
|
LocalFree(argv_w);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
} // namespace ApplicationUtilities
|
} // namespace ApplicationUtilities
|
||||||
|
|
|
@ -5,6 +5,11 @@
|
||||||
|
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|
||||||
|
#ifdef PLATFORM_WINDOWS
|
||||||
|
# include "../misc/memory.h"
|
||||||
|
# include <vector>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace ApplicationUtilities {
|
namespace ApplicationUtilities {
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -21,13 +26,20 @@ bool CPP_UTILITIES_EXPORT confirmPrompt(const char *message, Response defaultRes
|
||||||
|
|
||||||
#ifdef PLATFORM_WINDOWS
|
#ifdef PLATFORM_WINDOWS
|
||||||
void CPP_UTILITIES_EXPORT startConsole();
|
void CPP_UTILITIES_EXPORT startConsole();
|
||||||
# define CMD_UTILS_START_CONSOLE ::ApplicationUtilities::startConsole();
|
std::pair<std::vector<std::unique_ptr<char[]> >, std::vector<char *> > CPP_UTILITIES_EXPORT convertArgsToUtf8();
|
||||||
|
# define CMD_UTILS_START_CONSOLE \
|
||||||
|
::ApplicationUtilities::startConsole();
|
||||||
|
# define CMD_UTILS_CONVERT_ARGS_TO_UTF8 \
|
||||||
|
auto utf8Args = ::ApplicationUtilities::convertArgsToUtf8(); \
|
||||||
|
argv = utf8Args.second.data(); \
|
||||||
|
argc = static_cast<int>(utf8Args.second.size());
|
||||||
#else
|
#else
|
||||||
# define CMD_UTILS_START_CONSOLE
|
# define CMD_UTILS_START_CONSOLE
|
||||||
|
# define CMD_UTILS_CONVERT_ARGS_TO_UTF8
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief The Indent class allows printing indentation conveniently, eg. cout << Ident(4) << ...
|
* \brief The Indentation class allows printing indentation conveniently, eg. cout << Indentation(4) << ...
|
||||||
*/
|
*/
|
||||||
class CPP_UTILITIES_EXPORT Indentation
|
class CPP_UTILITIES_EXPORT Indentation
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue