Skip to content

Commit

Permalink
[win32] JSONVariantWriter::Write(): use wide string version of setloc…
Browse files Browse the repository at this point in the history
…ale() to prevent crash on win32
  • Loading branch information
Karlson2k committed Feb 18, 2015
1 parent cfddde9 commit 7d40084
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions xbmc/utils/JSONVariantWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,23 @@ string CJSONVariantWriter::Write(const CVariant &value, bool compact)
yajl_gen_config(g, yajl_gen_indent_string, "\t");

// Set locale to classic ("C") to ensure valid JSON numbers
#ifndef TARGET_WINDOWS
const char *currentLocale = setlocale(LC_NUMERIC, NULL);
std::string backupLocale;
if (currentLocale != NULL && (currentLocale[0] != 'C' || currentLocale[1] != 0))
{
backupLocale = currentLocale;
setlocale(LC_NUMERIC, "C");
}
#else // TARGET_WINDOWS
const wchar_t* const currentLocale = _wsetlocale(LC_NUMERIC, NULL);
std::wstring backupLocale;
if (currentLocale != NULL && (currentLocale[0] != L'C' || currentLocale[1] != 0))
{
backupLocale = currentLocale;
_wsetlocale(LC_NUMERIC, L"C");
}
#endif // TARGET_WINDOWS

if (InternalWrite(g, value))
{
Expand All @@ -51,8 +61,13 @@ string CJSONVariantWriter::Write(const CVariant &value, bool compact)
}

// Re-set locale to what it was before using yajl
#ifndef TARGET_WINDOWS
if (!backupLocale.empty())
setlocale(LC_NUMERIC, backupLocale.c_str());
#else // TARGET_WINDOWS
if (!backupLocale.empty())
_wsetlocale(LC_NUMERIC, backupLocale.c_str());
#endif // TARGET_WINDOWS

yajl_gen_clear(g);
yajl_gen_free(g);
Expand Down

0 comments on commit 7d40084

Please sign in to comment.