From: Vadim Zeitlin Date: Mon, 31 May 2010 11:55:53 +0000 (+0000) Subject: Use C locale for numbers in wx(File)Config. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/7e22c2bd02fcd8f6759dfbbf626302dd4a51822a Use C locale for numbers in wx(File)Config. Using the current locale decimal point in config files results in problems when moving the files to another machine or even using a different locale on the same one, so don't do it. Always write the numbers using C locale and try to read them in C locale too first, but also try the current locale if we failed for backwards compatibility and to be tolerant with users who edit their config files by hand. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64450 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index cf099e3225..459fb4f533 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -450,6 +450,7 @@ All: resources. - Added wxMessageQueue::Clear(). - Added wxConfig::Read(float *) overload (Terry Farnham). +- Always use decimal point (and not the current locale separator) in wxConfig. Unix: diff --git a/interface/wx/config.h b/interface/wx/config.h index 3c9feeba3a..9e059fbb3d 100644 --- a/interface/wx/config.h +++ b/interface/wx/config.h @@ -689,6 +689,14 @@ public: /** Writes the double value to the config file and returns @true on success. + + Notice that if floating point numbers are saved as strings (as is the + case with the configuration files used by wxFileConfig), this function + uses the C locale for writing out the number, i.e. it will always use a + period as the decimal separator, irrespectively of the current locale. + This behaviour is new since wxWidgets 2.9.1 as the current locale was + used before, but the change should be transparent because both C and + current locales are tried when reading the numbers back. */ bool Write(const wxString& key, double value); /** diff --git a/src/common/config.cpp b/src/common/config.cpp index 7ecc5d5778..81ff589b68 100644 --- a/src/common/config.cpp +++ b/src/common/config.cpp @@ -233,7 +233,14 @@ bool wxConfigBase::DoReadDouble(const wxString& key, double* val) const wxString str; if ( Read(key, &str) ) { - return str.ToDouble(val); + if ( str.ToCDouble(val) ) + return true; + + // Previous versions of wxFileConfig wrote the numbers out using the + // current locale and not the C one as now, so attempt to parse the + // string as a number in the current locale too, for compatibility. + if ( str.ToDouble(val) ) + return true; } return false; @@ -256,7 +263,10 @@ wxString wxConfigBase::ExpandEnvVars(const wxString& str) const bool wxConfigBase::DoWriteDouble(const wxString& key, double val) { - return DoWriteString(key, wxString::Format(wxT("%g"), val)); + // Notice that we always write out the numbers in C locale and not the + // current one. This makes the config files portable between machines using + // different locales. + return DoWriteString(key, wxString::FromCDouble(val)); } bool wxConfigBase::DoWriteBool(const wxString& key, bool value)