From 11a643109ac07fae965c5d81e8b428de8cf29795 Mon Sep 17 00:00:00 2001 From: Mathieu Laurendeau Date: Fri, 8 Sep 2017 23:20:19 +0200 Subject: [PATCH] config editors can crash if locale 'en_GB' misses #469 --- config/gimx-config.cpp | 34 +++++++++++++++++------ config/gimx-config.h | 1 + fpsconfig/gimx-fpsconfig.cpp | 54 ++++++++++++++++++++++-------------- fpsconfig/gimx-fpsconfig.h | 1 + 4 files changed, 60 insertions(+), 30 deletions(-) diff --git a/config/gimx-config.cpp b/config/gimx-config.cpp index 4a579bdf..30a9d4d5 100644 --- a/config/gimx-config.cpp +++ b/config/gimx-config.cpp @@ -576,6 +576,13 @@ void configFrame::readLabels() } } +static bool ToDouble(const wxString & from, double * to, const wxString & decimalPoint) +{ + wxString tmp = from; + tmp.Replace(wxT("."), decimalPoint); + return tmp.ToDouble(to); +} + /* * \brief Constructor. * @@ -591,6 +598,9 @@ configFrame::configFrame(wxString file,wxWindow* parent, wxWindowID id __attribu #endif locale->AddCatalog(wxT("gimx")); + struct lconv * l = localeconv(); + decimalPoint = wxString(l->decimal_point, wxConvUTF8); + //(*Initialize(configFrame) wxFlexGridSizer* FlexGridSizer30; wxFlexGridSizer* FlexGridSizer46; @@ -3478,8 +3488,6 @@ void configFrame::OnMenuSetMouseDPI(wxCommandEvent& event __attribute__((unused) */ void configFrame::OnTextCtrl(wxCommandEvent& event) { - wxLocale eng(wxLANGUAGE_ENGLISH); // make sure to use '.' as decimal separator - wxString str; wxTextCtrl* text; double value; @@ -3489,14 +3497,22 @@ void configFrame::OnTextCtrl(wxCommandEvent& event) long pos = text->GetInsertionPoint(); str = text->GetValue(); - if(str.IsEmpty() || str == wxT("-") || str == wxT(".") || str == wxT("-.")) + if(str.Replace(wxT(","), wxT("."))) { - return; + text->SetValue(str); } - if(str.Replace(wxT(","), wxT("."))) + if (decimalPoint != wxT(".")) // avoid infinite recursion { - text->SetValue(str); + if(str.Replace(decimalPoint, wxT("."))) + { + text->SetValue(str); + } + } + + if(str.IsEmpty() || str == wxT("-") || str == wxT(".") || str == wxT("-.")) + { + return; } if(str.Freq('.') > 1) @@ -3521,14 +3537,14 @@ void configFrame::OnTextCtrl(wxCommandEvent& event) } else if(text == AxisTabSensitivity) { - if(!str.ToDouble(&value)) + if(!ToDouble(str, &value, decimalPoint)) { text->SetValue(wxT("1.00")); } } else if(text == AxisTabAcceleration) { - if(!str.ToDouble(&value)) + if(!ToDouble(str, &value, decimalPoint)) { text->SetValue(wxT("1.00")); } @@ -3554,7 +3570,7 @@ void configFrame::OnTextCtrl(wxCommandEvent& event) } else if(text == MouseOptionsFilter) { - if(!str.ToDouble(&value)) + if(!ToDouble(str, &value, decimalPoint)) { text->SetValue(wxT("0.00")); } diff --git a/config/gimx-config.h b/config/gimx-config.h index 8214aaee..770bede0 100644 --- a/config/gimx-config.h +++ b/config/gimx-config.h @@ -502,6 +502,7 @@ class configFrame: public wxFrame //*) wxLocale* locale; + wxString decimalPoint; event_catcher* evcatch; ConfigurationFile configFile; diff --git a/fpsconfig/gimx-fpsconfig.cpp b/fpsconfig/gimx-fpsconfig.cpp index 7baff3b7..ab5bbf8b 100644 --- a/fpsconfig/gimx-fpsconfig.cpp +++ b/fpsconfig/gimx-fpsconfig.cpp @@ -203,6 +203,13 @@ const char* axis_labels[AI_MAX] = "rel_axis_0-" }; +static bool ToDouble(const wxString & from, double * to, const wxString & decimalPoint) +{ + wxString tmp = from; + tmp.Replace(wxT("."), decimalPoint); + return tmp.ToDouble(to); +} + fpsconfigFrame::fpsconfigFrame(wxString file,wxWindow* parent,wxWindowID id __attribute__((unused))) { unsigned int i; @@ -213,6 +220,9 @@ fpsconfigFrame::fpsconfigFrame(wxString file,wxWindow* parent,wxWindowID id __at #endif locale->AddCatalog(wxT("gimx")); + struct lconv * l = localeconv(); + decimalPoint = wxString(l->decimal_point, wxConvUTF8); + //(*Initialize(fpsconfigFrame) wxMenu* MenuFile; wxMenu* MenuAdvanced; @@ -1071,8 +1081,6 @@ void fpsconfigFrame::OnMenuSaveAs(wxCommandEvent& event) void fpsconfigFrame::OnMenuSave(wxCommandEvent& event __attribute__((unused))) { - wxLocale eng(wxLANGUAGE_ENGLISH); // make sure to use '.' as decimal separator - std::list* ButtonMappers; std::list* AxisMappers; double mx, my; @@ -1198,7 +1206,7 @@ void fpsconfigFrame::OnMenuSave(wxCommandEvent& event __attribute__((unused))) } wsmx = TextCtrlSensitivityHipFire->GetValue(); wsxyratio = TextCtrlXyRatioHipFire->GetValue(); - if(wsmx.ToDouble(&mx) && wsxyratio.ToDouble(&xyratio)) + if(ToDouble(wsmx, &mx, decimalPoint) && ToDouble(wsxyratio, &xyratio, decimalPoint)) { my = mx * values[4]; } @@ -1339,7 +1347,7 @@ void fpsconfigFrame::OnMenuSave(wxCommandEvent& event __attribute__((unused))) } wsmx = TextCtrlSensitivityADS->GetValue(); wsxyratio = TextCtrlXyRatioADS->GetValue(); - if(wsmx.ToDouble(&mx) && wsxyratio.ToDouble(&xyratio)) + if(ToDouble(wsmx, &mx, decimalPoint) && ToDouble(wsxyratio, &xyratio, decimalPoint)) { my = mx * values[5]; } @@ -1385,8 +1393,6 @@ void fpsconfigFrame::OnMenuSave(wxCommandEvent& event __attribute__((unused))) void fpsconfigFrame::LoadConfig() { - wxLocale eng(wxLANGUAGE_ENGLISH); // make sure to use '.' as decimal separator - std::list* ButtonMappers[2]; std::list* AxisMappers[2]; std::list* mouseOptions; @@ -1596,7 +1602,7 @@ void fpsconfigFrame::LoadConfig() wsf = wxString(it2->GetFilter().c_str(), wxConvUTF8); - if(wsf.ToDouble(&f)) + if(ToDouble(wsf, &f, decimalPoint)) { TextCtrlFilterHipFire->SetValue(wsf); SpinCtrlFilterHipFire->SetValue(f*100); @@ -1607,7 +1613,7 @@ void fpsconfigFrame::LoadConfig() wsmx = wxString(it->GetEvent()->GetMultiplier().c_str(), wxConvUTF8); - if(wsmx.ToDouble(&mx)) + if(ToDouble(wsmx, &mx, decimalPoint)) { TextCtrlSensitivityHipFire->SetValue(wsmx); SpinCtrlSensitivityHipFire->SetValue(mx*100); @@ -1623,7 +1629,7 @@ void fpsconfigFrame::LoadConfig() wsexp = wxString(it->GetEvent()->GetExponent().c_str(), wxConvUTF8); - if(wsexp.ToDouble(&exp)) + if(ToDouble(wsexp, &exp, decimalPoint)) { TextCtrlAccelerationHipFire->SetValue(wxString(it->GetEvent()->GetExponent().c_str(), wxConvUTF8)); SpinCtrlAccelerationHipFire->SetValue(exp*100); @@ -1633,7 +1639,7 @@ void fpsconfigFrame::LoadConfig() { wsmy = wxString(it->GetEvent()->GetMultiplier().c_str(), wxConvUTF8); - if(wsmy.ToDouble(&my) && mx && my) + if(ToDouble(wsmy, &my, decimalPoint) && mx && my) { xyratio = my / mx; wsxyratio = wxString::Format(wxT("%.02f"), xyratio); @@ -1715,7 +1721,7 @@ void fpsconfigFrame::LoadConfig() wsf = wxString(it2->GetFilter().c_str(), wxConvUTF8); - if(wsf.ToDouble(&f)) + if(ToDouble(wsf, &f, decimalPoint)) { TextCtrlFilterADS->SetValue(wsf); SpinCtrlFilterADS->SetValue(f*100); @@ -1726,7 +1732,7 @@ void fpsconfigFrame::LoadConfig() wsmx = wxString(it->GetEvent()->GetMultiplier().c_str(), wxConvUTF8); - if(wsmx.ToDouble(&mx)) + if(ToDouble(wsmx, &mx, decimalPoint)) { TextCtrlSensitivityADS->SetValue(wsmx); SpinCtrlSensitivityADS->SetValue(mx*100); @@ -1742,7 +1748,7 @@ void fpsconfigFrame::LoadConfig() wsexp = wxString(it->GetEvent()->GetExponent().c_str(), wxConvUTF8); - if(wsexp.ToDouble(&exp)) + if(ToDouble(wsexp, &exp, decimalPoint)) { TextCtrlAccelerationADS->SetValue(wxString(it->GetEvent()->GetExponent().c_str(), wxConvUTF8)); SpinCtrlAccelerationADS->SetValue(exp*100); @@ -1752,7 +1758,7 @@ void fpsconfigFrame::LoadConfig() { wsmy = wxString(it->GetEvent()->GetMultiplier().c_str(), wxConvUTF8); - if(wsmy.ToDouble(&my) && mx && my) + if(ToDouble(wsmy, &my, decimalPoint) && mx && my) { xyratio = my / mx; wsxyratio = wxString::Format(wxT("%.02f"), xyratio); @@ -1789,8 +1795,6 @@ void fpsconfigFrame::OnMenuOpen(wxCommandEvent& event __attribute__((unused))) void fpsconfigFrame::OnTextCtrlText(wxCommandEvent& event) { - wxLocale eng(wxLANGUAGE_ENGLISH); // make sure to use '.' as decimal separator - wxString str; wxTextCtrl* text; double value; @@ -1800,14 +1804,22 @@ void fpsconfigFrame::OnTextCtrlText(wxCommandEvent& event) long pos = text->GetInsertionPoint(); str = text->GetValue(); - if(str.IsEmpty() || str == wxT("-") || str == wxT(".") || str == wxT("-.")) + if(str.Replace(wxT(","), wxT("."))) { - return; + text->SetValue(str); } - if(str.Replace(wxT(","), wxT("."))) + if (decimalPoint != wxT(".")) // avoid infinite recursion { - text->SetValue(str); + if(str.Replace(decimalPoint, wxT("."))) + { + text->SetValue(str); + } + } + + if(str.IsEmpty() || str == wxT("-") || str == wxT(".") || str == wxT("-.")) + { + return; } if(str.Freq('.') > 1) @@ -1815,7 +1827,7 @@ void fpsconfigFrame::OnTextCtrlText(wxCommandEvent& event) str = wxT("invalid"); } - if(!str.ToDouble(&value)) + if(!ToDouble(str, &value, decimalPoint)) { if (text == TextCtrlFilterHipFire || text == TextCtrlFilterADS) { diff --git a/fpsconfig/gimx-fpsconfig.h b/fpsconfig/gimx-fpsconfig.h index fae79011..737916da 100644 --- a/fpsconfig/gimx-fpsconfig.h +++ b/fpsconfig/gimx-fpsconfig.h @@ -237,6 +237,7 @@ class fpsconfigFrame: public wxFrame wxBackgroundBitmap* ToolBarBackground; wxLocale* locale; + wxString decimalPoint; event_catcher* evcatch; ConfigurationFile configFile;