Ticket #495: patch.diff

File patch.diff, 2.2 KB (added by Sergio Fabian Vier, 13 years ago)
  • source/ps/GameSetup/GameSetup.h

     
    3333 **/
    3434extern void EarlyInit();
    3535
     36extern void setDefaultIfLocaleInvalid();
     37
    3638enum InitFlags
    3739{
    3840    // avoid setting a video mode / initializing OpenGL; assume that has
  • source/ps/GameSetup/GameSetup.cpp

     
    721721    debug_filter_add(L"TIMER");
    722722    debug_filter_add(L"HRT");
    723723
     724    setDefaultIfLocaleInvalid();
     725
    724726    cpu_ConfigureFloatingPoint();
    725727
    726728    timer_LatchStartTime();
     
    740742    srand(time(NULL));  // NOTE: this rand should *not* be used for simulation!
    741743}
    742744
     745void setDefaultIfLocaleInvalid()
     746{
     747    // I don't believe $LANGUAGE needs to be included because it isn't checked
     748    // by setlocale() nor std::locale::locale(const chr*).
     749    // LC_ALL is checked first, and LANG last.
     750    const char* const LocaleEnvVars[] = {"LC_ALL", "LC_COLLATE",
     751            "LC_CTYPE", "LC_MONETARY",
     752            "LC_NUMERIC", "LC_TIME",
     753            "LC_MESSAGES", "LANG"};
     754
     755    try
     756    {
     757        // this constructor is similar to setlocale(LC_ALL, ""),
     758        // but instead of returning NULL, it throws runtime_error
     759        // when the first locale env variable found contains an invalid value
     760        std::locale::locale("");
     761    }
     762    catch (std::runtime_error& e)
     763    {
     764        LOGWARNING(L"Warning: Invalid Locale Settings");
     765
     766        for (size_t i = 0; i < ARRAY_SIZE(LocaleEnvVars); i++)
     767        {
     768            if (char* envval = getenv(LocaleEnvVars[i]))
     769            {
     770                LOGWARNING(L"%hs=\"%hs\"", LocaleEnvVars[i], envval);
     771            }
     772            else
     773            {
     774                LOGWARNING(L"%hs=\"(unset)\"", LocaleEnvVars[i]);
     775            }
     776        }
     777
     778        // We should set LC_ALL since it overrides LANG
     779        if (setenv("LC_ALL", std::locale::classic().name().c_str(), 1))
     780        {
     781            LOGERROR(L"Unable to set LC_ALL env variable.");
     782            exit(EXIT_FAILURE);
     783        }
     784
     785        LOGWARNING(L"Setting LC_ALL env variable to: %hs", getenv("LC_ALL"));
     786    }
     787
     788    return;
     789}
     790
    743791static bool Autostart(const CmdLineArgs& args);
    744792
    745793void Init(const CmdLineArgs& args, int UNUSED(flags))