From af120a3eafd1ee722c454ee9a71b9d1384c46dea Mon Sep 17 00:00:00 2001 From: Stephen Anthony Date: Fri, 7 Feb 2020 21:00:06 -0330 Subject: [PATCH 1/4] Wrap std::stoi inside BSPF::stringToInt, and handle its exceptions. --- src/common/bspf.hxx | 7 +++++++ src/emucore/Console.cxx | 12 +++--------- src/emucore/EventHandler.cxx | 6 +++--- src/emucore/ProfilingRunner.cxx | 2 +- src/emucore/Props.cxx | 2 +- src/emucore/TIASurface.cxx | 6 +----- src/gui/GameInfoDialog.cxx | 13 +++---------- 7 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/common/bspf.hxx b/src/common/bspf.hxx index 2de88cfec..e7cc60024 100644 --- a/src/common/bspf.hxx +++ b/src/common/bspf.hxx @@ -145,6 +145,13 @@ namespace BSPF return s; } + // Convert string to integer, using default value on any error + inline int stringToInt(const string& s, const int defaultValue = 0) + { + try { return std::stoi(s); } + catch(...) { return defaultValue; } + } + // Compare two strings, ignoring case inline int compareIgnoreCase(const string& s1, const string& s2) { diff --git a/src/emucore/Console.cxx b/src/emucore/Console.cxx index 5cef206b0..3a7e0841e 100644 --- a/src/emucore/Console.cxx +++ b/src/emucore/Console.cxx @@ -110,7 +110,7 @@ Console::Console(OSystem& osystem, unique_ptr& cart, myCart->setStartBankFromPropsFunc([this]() { const string& startbank = myProperties.get(PropType::Cart_StartBank); return (startbank == EmptyString || BSPF::equalsIgnoreCase(startbank, "AUTO")) - ? -1 : stoi(startbank); + ? -1 : BSPF::stringToInt(startbank); }); // We can only initialize after all the devices/components have been created @@ -583,13 +583,7 @@ void Console::togglePhosphor() // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Console::changePhosphor(int direction) { - int blend = 0; - - try - { - blend = stoi(myProperties.get(PropType::Display_PPBlend)); - } - catch (...) {} + int blend = BSPF::stringToInt(myProperties.get(PropType::Display_PPBlend)); if(direction == +1) // increase blend { @@ -795,7 +789,7 @@ void Console::changeScanlineAdjust(int direction) void Console::setTIAProperties() { Int32 vcenter = BSPF::clamp( - static_cast(stoi(myProperties.get(PropType::Display_VCenter))), TIAConstants::minVcenter, TIAConstants::maxVcenter + static_cast(BSPF::stringToInt(myProperties.get(PropType::Display_VCenter))), TIAConstants::minVcenter, TIAConstants::maxVcenter ); if(myDisplayFormat == "NTSC" || myDisplayFormat == "PAL60" || diff --git a/src/emucore/EventHandler.cxx b/src/emucore/EventHandler.cxx index 70b3c6d0c..4e6b3712d 100644 --- a/src/emucore/EventHandler.cxx +++ b/src/emucore/EventHandler.cxx @@ -1143,7 +1143,7 @@ void EventHandler::setComboMap() { string key; buf >> key; - if(stoi(key) == COMBO_SIZE) + if(BSPF::stringToInt(key) == COMBO_SIZE) { // Fill the combomap table with events for as long as they exist int combocount = 0; @@ -1156,7 +1156,7 @@ void EventHandler::setComboMap() int eventcount = 0; while(buf2 >> key && eventcount < EVENTS_PER_COMBO) { - myComboTable[combocount][eventcount] = Event::Type(stoi(key)); + myComboTable[combocount][eventcount] = Event::Type(BSPF::stringToInt(key)); ++eventcount; } ++combocount; @@ -1445,7 +1445,7 @@ void EventHandler::setComboListForEvent(Event::Type event, const StringList& eve int combo = event - Event::Combo1; for(uInt32 i = 0; i < 8; ++i) { - uInt32 idx = stoi(events[i]); + uInt32 idx = BSPF::stringToInt(events[i]); if(idx < ourEmulActionList.size()) myComboTable[combo][i] = EventHandler::ourEmulActionList[idx].event; else diff --git a/src/emucore/ProfilingRunner.cxx b/src/emucore/ProfilingRunner.cxx index 3fc378907..f561dc7a1 100644 --- a/src/emucore/ProfilingRunner.cxx +++ b/src/emucore/ProfilingRunner.cxx @@ -69,7 +69,7 @@ ProfilingRunner::ProfilingRunner(int argc, char* argv[]) if (splitPoint == string::npos) run.runtime = RUNTIME_DEFAULT; else { - int runtime = stoi(arg.substr(splitPoint+1, string::npos)); + int runtime = BSPF::stringToInt(arg.substr(splitPoint+1, string::npos)); run.runtime = runtime > 0 ? runtime : RUNTIME_DEFAULT; } } diff --git a/src/emucore/Props.cxx b/src/emucore/Props.cxx index 0ff65f975..fad325d85 100644 --- a/src/emucore/Props.cxx +++ b/src/emucore/Props.cxx @@ -63,7 +63,7 @@ void Properties::set(PropType key, const string& value) case PropType::Display_PPBlend: { - int blend = stoi(myProperties[pos]); + int blend = BSPF::stringToInt(myProperties[pos]); if(blend < 0 || blend > 100) myProperties[pos] = ourDefaultProperties[pos]; break; diff --git a/src/emucore/TIASurface.cxx b/src/emucore/TIASurface.cxx index b99f33be0..46b2250c0 100644 --- a/src/emucore/TIASurface.cxx +++ b/src/emucore/TIASurface.cxx @@ -91,11 +91,7 @@ void TIASurface::initialize(const Console& console, } else { - try - { - p_blend = stoi(console.properties().get(PropType::Display_PPBlend)); - } - catch (...) {} + p_blend = BSPF::stringToInt(console.properties().get(PropType::Display_PPBlend)); enable = console.properties().get(PropType::Display_Phosphor) == "YES"; } enablePhosphor(enable, p_blend); diff --git a/src/gui/GameInfoDialog.cxx b/src/gui/GameInfoDialog.cxx index 9ed16a231..2cfb80912 100644 --- a/src/gui/GameInfoDialog.cxx +++ b/src/gui/GameInfoDialog.cxx @@ -460,17 +460,10 @@ void GameInfoDialog::loadEmulationProperties(const Properties& props) myPPBlend->setEnabled(!alwaysPhosphor && usePhosphor); const string& blend = props.get(PropType::Display_PPBlend); - try - { - myPPBlend->setValue(stoi(blend)); - } - catch (...) - { - myPPBlend->setValue(0); - } + myPPBlend->setValue(BSPF::stringToInt(blend)); // set vertical center - Int32 vcenter = stoi(props.get(PropType::Display_VCenter)); + Int32 vcenter = BSPF::stringToInt(props.get(PropType::Display_VCenter)); myVCenter->setValueLabel(vcenter); myVCenter->setValue(vcenter); myVCenter->setValueUnit(vcenter ? "px" : ""); @@ -519,7 +512,7 @@ void GameInfoDialog::loadControllerProperties(const Properties& props) myMouseY->setEnabled(!autoAxis); if(m_axis >> m_range) { - myMouseRange->setValue(stoi(m_range)); + myMouseRange->setValue(BSPF::stringToInt(m_range)); } else { From 47fa6eb8bb7a3006f51412aebdeb82eda4a50031 Mon Sep 17 00:00:00 2001 From: thrust26 Date: Sat, 8 Feb 2020 11:16:21 +0100 Subject: [PATCH 2/4] change zero value for phosphor into "0" (not "Off") --- src/gui/VideoDialog.cxx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/gui/VideoDialog.cxx b/src/gui/VideoDialog.cxx index 90e126918..981e1a4e0 100644 --- a/src/gui/VideoDialog.cxx +++ b/src/gui/VideoDialog.cxx @@ -472,7 +472,8 @@ void VideoDialog::saveConfig() instance().settings().setValue("tv.phosphor", myTVPhosphor->getState() ? "always" : "byrom"); // TV phosphor blend - instance().settings().setValue("tv.phosblend", myTVPhosLevel->getValueLabel()); + instance().settings().setValue("tv.phosblend", myTVPhosLevel->getValueLabel() == "Off" + ? "0" : myTVPhosLevel->getValueLabel()); // TV scanline intensity instance().settings().setValue("tv.scanlines", myTVScanIntense->getValueLabel()); From 46e103970cc8aab106a54987bfdf58f064a43d74 Mon Sep 17 00:00:00 2001 From: Stephen Anthony Date: Sun, 9 Feb 2020 16:42:50 -0330 Subject: [PATCH 3/4] Use 'const' instead of 'constexpr' when generating properties file. --- src/tools/create_props.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/create_props.pl b/src/tools/create_props.pl index 1aac986a9..b212b10fa 100755 --- a/src/tools/create_props.pl +++ b/src/tools/create_props.pl @@ -68,7 +68,7 @@ print OUTFILE " regenerated and the application recompiled.\n"; print OUTFILE "*/\n"; print OUTFILE "\nstatic constexpr uInt32 DEF_PROPS_SIZE = " . $setsize . ";"; print OUTFILE "\n\n"; -print OUTFILE "static constexpr BSPF::array2D DefProps = {{\n"; +print OUTFILE "static const BSPF::array2D DefProps = {{\n"; # Walk the hash map and print each item in order of md5sum my $idx = 0; From 9d0d1c4cf299b8dec0602e5e007383fa0ab60109 Mon Sep 17 00:00:00 2001 From: Stephen Anthony Date: Sun, 9 Feb 2020 16:46:19 -0330 Subject: [PATCH 4/4] Apply changes to DefProps based on latest 'const' vs. 'constexpr' refactoring. --- src/emucore/DefProps.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/emucore/DefProps.hxx b/src/emucore/DefProps.hxx index b959f4699..6b09a99a7 100644 --- a/src/emucore/DefProps.hxx +++ b/src/emucore/DefProps.hxx @@ -27,7 +27,7 @@ static constexpr uInt32 DEF_PROPS_SIZE = 3507; -static constexpr BSPF::array2D DefProps = {{ +static const BSPF::array2D DefProps = {{ { "000509d1ed2b8d30a9d94be1b3b5febb", "Greg Zumwalt", "", "Jungle Jane (2003) (Greg Zumwalt) (Hack)", "Hack of Pitfall!", "Hack", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" }, { "0060a89b4c956b9c703a59b181cb3018", "CommaVid, Irwin Gaines - Ariola", "CM-008 - 712 008-720", "Cakewalk (1983) (CommaVid) (PAL)", "AKA Alarm in der Backstube", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" }, { "007d18dedc1f0565f09c42aa61a6f585", "CCE", "C-843", "Worm War I (1983) (CCE)", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },