Skip to content

Commit

Permalink
config editors can crash if locale 'en_GB' misses #469
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Laurendeau committed Sep 8, 2017
1 parent cad9b27 commit 11a6431
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 30 deletions.
34 changes: 25 additions & 9 deletions config/gimx-config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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"));
}
Expand All @@ -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"));
}
Expand Down
1 change: 1 addition & 0 deletions config/gimx-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ class configFrame: public wxFrame
//*)

wxLocale* locale;
wxString decimalPoint;

event_catcher* evcatch;
ConfigurationFile configFile;
Expand Down
54 changes: 33 additions & 21 deletions fpsconfig/gimx-fpsconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<ControlMapper>* ButtonMappers;
std::list<ControlMapper>* AxisMappers;
double mx, my;
Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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<ControlMapper>* ButtonMappers[2];
std::list<ControlMapper>* AxisMappers[2];
std::list<MouseOptions>* mouseOptions;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -1800,22 +1804,30 @@ 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)
{
str = wxT("invalid");
}

if(!str.ToDouble(&value))
if(!ToDouble(str, &value, decimalPoint))
{
if (text == TextCtrlFilterHipFire || text == TextCtrlFilterADS)
{
Expand Down
1 change: 1 addition & 0 deletions fpsconfig/gimx-fpsconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ class fpsconfigFrame: public wxFrame
wxBackgroundBitmap* ToolBarBackground;

wxLocale* locale;
wxString decimalPoint;

event_catcher* evcatch;
ConfigurationFile configFile;
Expand Down

0 comments on commit 11a6431

Please sign in to comment.