X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/758f356c53a3f473f642361ec09422405046148d..ec080ef1873995bcd9b20b0fc1a6d208a78540d3:/src/common/cmdline.cpp diff --git a/src/common/cmdline.cpp b/src/common/cmdline.cpp index 1a143d476c..ffa7e5ba36 100644 --- a/src/common/cmdline.cpp +++ b/src/common/cmdline.cpp @@ -37,11 +37,13 @@ #if wxUSE_CMDLINE_PARSER #include +#include // for LC_ALL #include "wx/datetime.h" #include "wx/msgout.h" #include "wx/filename.h" #include "wx/apptrait.h" +#include "wx/scopeguard.h" // ---------------------------------------------------------------------------- // private functions @@ -230,13 +232,45 @@ wxCmdLineParserData::wxCmdLineParserData() #endif } +namespace +{ + +// Small helper function setting locale for all categories. +// +// We define it because wxSetlocale() can't be easily used with wxScopeGuard as +// it has several overloads -- while this one can. +inline char *SetAllLocaleFacets(const char *loc) +{ + return wxSetlocale(LC_ALL, loc); +} + +} // private namespace + void wxCmdLineParserData::SetArguments(int argc, char **argv) { m_arguments.clear(); + // Command-line arguments are supposed to be in the user locale encoding + // (what else?) but wxLocale probably wasn't initialized yet as we're + // called early during the program startup and so our locale might not have + // been set from the environment yet. To work around this problem we + // temporarily change the locale here. The only drawback is that changing + // the locale is thread-unsafe but precisely because we're called so early + // it's hopefully safe to assume that no other threads had been created yet. + char * const locOld = SetAllLocaleFacets(""); + wxON_BLOCK_EXIT1( SetAllLocaleFacets, locOld ); + for ( int n = 0; n < argc; n++ ) { - m_arguments.push_back(wxString::FromAscii(argv[n])); + // try to interpret the string as being in the current locale + wxString arg(argv[n]); + + // but just in case we guessed wrongly and the conversion failed, do + // try to salvage at least something + if ( arg.empty() && argv[n][0] != '\0' ) + arg = wxString(argv[n], wxConvISO8859_1); + + m_arguments.push_back(arg); } }