From 027efc59791c0917ad6a76520f05b76bbcfae474 Mon Sep 17 00:00:00 2001 From: Stephen Anthony Date: Sat, 24 Dec 2022 16:42:08 -0330 Subject: [PATCH] Consolidate and refactor some code (mostly string conversions). --- src/cheat/BankRomCheat.cxx | 8 ++++---- src/cheat/Cheat.hxx | 17 ----------------- src/cheat/CheetahCheat.cxx | 6 +++--- src/cheat/RamCheat.cxx | 4 ++-- src/common/HighScoresManager.cxx | 5 +++-- src/common/bspf.hxx | 14 ++++++++++---- src/debugger/CartDebug.cxx | 4 ++-- src/emucore/Console.cxx | 14 +++++++------- src/emucore/EventHandler.cxx | 2 +- src/emucore/FrameBuffer.cxx | 2 +- src/emucore/ProfilingRunner.cxx | 2 +- src/emucore/Props.cxx | 2 +- src/gui/GameInfoDialog.cxx | 26 +++++++++++++------------- 13 files changed, 48 insertions(+), 58 deletions(-) diff --git a/src/cheat/BankRomCheat.cxx b/src/cheat/BankRomCheat.cxx index 236d83dd1..8fd6d95dc 100644 --- a/src/cheat/BankRomCheat.cxx +++ b/src/cheat/BankRomCheat.cxx @@ -27,10 +27,10 @@ BankRomCheat::BankRomCheat(OSystem& os, string_view name, string_view code) if(myCode.length() == 7) myCode = "0" + string{code}; - bank = unhex(myCode.substr(0, 2)); - address = 0xf000 + unhex(myCode.substr(2, 3)); - value = static_cast(unhex(myCode.substr(5, 2))); - count = static_cast(unhex(myCode.substr(7, 1)) + 1); + bank = BSPF::stoi_16(myCode.substr(0, 2)); + address = 0xf000 + BSPF::stoi_16(myCode.substr(2, 3)); + value = static_cast(BSPF::stoi_16(myCode.substr(5, 2))); + count = static_cast(BSPF::stoi_16(myCode.substr(7, 1)) + 1); // Back up original data; we need this if the cheat is ever disabled for(int i = 0; i < count; ++i) diff --git a/src/cheat/Cheat.hxx b/src/cheat/Cheat.hxx index b21187f34..1d9fb719e 100644 --- a/src/cheat/Cheat.hxx +++ b/src/cheat/Cheat.hxx @@ -40,23 +40,6 @@ class Cheat virtual void evaluate() = 0; - protected: - static uInt16 unhex(string_view hex) - { - int ret = 0; - for(const auto c: hex) - { - ret *= 16; - if(c >= '0' && c <= '9') - ret += c - '0'; - else if(c >= 'A' && c <= 'F') - ret += c - 'A' + 10; - else - ret += c - 'a' + 10; - } - return ret; - } - protected: OSystem& myOSystem; diff --git a/src/cheat/CheetahCheat.cxx b/src/cheat/CheetahCheat.cxx index 271da4fa1..59f302c73 100644 --- a/src/cheat/CheetahCheat.cxx +++ b/src/cheat/CheetahCheat.cxx @@ -23,9 +23,9 @@ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - CheetahCheat::CheetahCheat(OSystem& os, string_view name, string_view code) : Cheat(os, name, code), - address{static_cast(0xf000 + unhex(code.substr(0, 3)))}, - value{static_cast(unhex(code.substr(3, 2)))}, - count{static_cast(unhex(code.substr(5, 1)) + 1)} + address{static_cast(0xf000 + BSPF::stoi_16(code.substr(0, 3)))}, + value{static_cast(BSPF::stoi_16(code.substr(3, 2)))}, + count{static_cast(BSPF::stoi_16(code.substr(5, 1)) + 1)} { // Back up original data; we need this if the cheat is ever disabled for(int i = 0; i < count; ++i) diff --git a/src/cheat/RamCheat.cxx b/src/cheat/RamCheat.cxx index 2d07ab8bb..a39b0995e 100644 --- a/src/cheat/RamCheat.cxx +++ b/src/cheat/RamCheat.cxx @@ -25,8 +25,8 @@ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - RamCheat::RamCheat(OSystem& os, string_view name, string_view code) : Cheat(os, name, code), - address{static_cast(unhex(myCode.substr(0, 2)))}, - value{static_cast(unhex(myCode.substr(2, 2)))} + address{static_cast(BSPF::stoi_16(myCode.substr(0, 2)))}, + value{static_cast(BSPF::stoi_16(myCode.substr(2, 2)))} { } diff --git a/src/common/HighScoresManager.cxx b/src/common/HighScoresManager.cxx index 1fae42462..66c1a5476 100644 --- a/src/common/HighScoresManager.cxx +++ b/src/common/HighScoresManager.cxx @@ -539,12 +539,13 @@ HSM::ScoreAddresses HighScoresManager::getPropScoreAddr(const json& jprops) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - uInt16 HighScoresManager::fromHexStr(string_view addr) { + // TODO: convert away from using string string naked{addr}; - if(const int pos = naked.find("0x") != std::string::npos) + if(const auto pos = naked.find("0x") != std::string::npos) naked = naked.substr(pos + 1); - return stringToIntBase16(naked); + return static_cast(BSPF::stoi_16(naked)); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/common/bspf.hxx b/src/common/bspf.hxx index 8b8392073..19aa7cf47 100644 --- a/src/common/bspf.hxx +++ b/src/common/bspf.hxx @@ -193,20 +193,26 @@ namespace BSPF } // Convert string to integer, using default value on any error - inline int stringToInt(string_view s, const int defaultValue = 0) + // TODO: reimplement stoi so only 'string_view' version is needed + inline int stoi(string_view s, const int defaultValue = 0) { try { return std::stoi(string{s}); } catch(...) { return defaultValue; } } - // TODO: remove this once we reimplement stoi - inline int stringToInt(const string& s, const int defaultValue = 0) + inline int stoi(const string& s, const int defaultValue = 0) { try { return std::stoi(s); } catch(...) { return defaultValue; } } // Convert string with base 16 to integer, using default value on any error - inline int stringToIntBase16(const string& s, const int defaultValue = 0) + // TODO: reimplement stoi so only 'string_view' version is needed + inline int stoi_16(string_view s, const int defaultValue = 0) + { + try { return std::stoi(string{s}, nullptr, 16); } + catch(...) { return defaultValue; } + } + inline int stoi_16(const string& s, const int defaultValue = 0) { try { return std::stoi(s, nullptr, 16); } catch(...) { return defaultValue; } diff --git a/src/debugger/CartDebug.cxx b/src/debugger/CartDebug.cxx index 3186886a4..0583dbca3 100644 --- a/src/debugger/CartDebug.cxx +++ b/src/debugger/CartDebug.cxx @@ -822,8 +822,8 @@ string CartDebug::loadListFile() buf >> addr >> addr_s; if(addr_s.length() == 0) continue; - const char* const p = addr_s[0] == 'U' ? addr_s.c_str() + 1 : addr_s.c_str(); - addr = static_cast(strtoul(p, nullptr, 16)); + + addr = BSPF::stoi_16(addr_s[0] == 'U' ? addr_s.substr(1) : addr_s); // For now, completely ignore ROM addresses if(!(addr & 0x1000)) diff --git a/src/emucore/Console.cxx b/src/emucore/Console.cxx index 3415ecc3b..31cf2b722 100644 --- a/src/emucore/Console.cxx +++ b/src/emucore/Console.cxx @@ -151,7 +151,7 @@ Console::Console(OSystem& osystem, unique_ptr& cart, myCart->setStartBankFromPropsFunc([this]() { const string_view startbank = myProperties.get(PropType::Cart_StartBank); return (startbank == EmptyString || BSPF::equalsIgnoreCase(startbank, "AUTO")) - ? -1 : BSPF::stringToInt(startbank); + ? -1 : BSPF::stoi(startbank); }); // We can only initialize after all the devices/components have been created @@ -636,7 +636,7 @@ void Console::togglePhosphor() // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Console::changePhosphor(int direction) { - int blend = BSPF::stringToInt(myProperties.get(PropType::Display_PPBlend)); + int blend = BSPF::stoi(myProperties.get(PropType::Display_PPBlend)); if(direction) { @@ -801,7 +801,7 @@ void Console::toggleCorrectAspectRatio(bool toggle) void Console::setTIAProperties() { const Int32 vcenter = BSPF::clamp( - static_cast(BSPF::stringToInt(myProperties.get(PropType::Display_VCenter))), TIAConstants::minVcenter, TIAConstants::maxVcenter + static_cast(BSPF::stoi(myProperties.get(PropType::Display_VCenter))), TIAConstants::minVcenter, TIAConstants::maxVcenter ); if(gameRefreshRate() == 60) @@ -976,8 +976,8 @@ unique_ptr Console::getControllerPort( else if(type == Controller::Type::PaddlesIAxDr) swapAxis = swapDir = true; - Paddles::setAnalogXCenter(BSPF::stringToInt(myProperties.get(PropType::Controller_PaddlesXCenter))); - Paddles::setAnalogYCenter(BSPF::stringToInt(myProperties.get(PropType::Controller_PaddlesYCenter))); + Paddles::setAnalogXCenter(BSPF::stoi(myProperties.get(PropType::Controller_PaddlesXCenter))); + Paddles::setAnalogYCenter(BSPF::stoi(myProperties.get(PropType::Controller_PaddlesYCenter))); Paddles::setAnalogSensitivity(myOSystem.settings().getInt("psense")); controller = make_unique(port, myEvent, *mySystem, @@ -1108,7 +1108,7 @@ void Console::toggleSwapPaddles(bool toggle) void Console::changePaddleCenterX(int direction) { const int center = - BSPF::clamp(BSPF::stringToInt(myProperties.get(PropType::Controller_PaddlesXCenter)) + direction, + BSPF::clamp(BSPF::stoi(myProperties.get(PropType::Controller_PaddlesXCenter)) + direction, Paddles::MIN_ANALOG_CENTER, Paddles::MAX_ANALOG_CENTER); myProperties.set(PropType::Controller_PaddlesXCenter, std::to_string(center)); Paddles::setAnalogXCenter(center); @@ -1123,7 +1123,7 @@ void Console::changePaddleCenterX(int direction) void Console::changePaddleCenterY(int direction) { const int center = - BSPF::clamp(BSPF::stringToInt(myProperties.get(PropType::Controller_PaddlesYCenter)) + direction, + BSPF::clamp(BSPF::stoi(myProperties.get(PropType::Controller_PaddlesYCenter)) + direction, Paddles::MIN_ANALOG_CENTER, Paddles::MAX_ANALOG_CENTER); myProperties.set(PropType::Controller_PaddlesYCenter, std::to_string(center)); Paddles::setAnalogYCenter(center); diff --git a/src/emucore/EventHandler.cxx b/src/emucore/EventHandler.cxx index 293fb1d0a..c00639d4a 100644 --- a/src/emucore/EventHandler.cxx +++ b/src/emucore/EventHandler.cxx @@ -2345,7 +2345,7 @@ void EventHandler::setComboListForEvent(Event::Type event, const StringList& eve const int combo = event - Event::Combo1; for(uInt32 i = 0; i < EVENTS_PER_COMBO; ++i) { - const uInt32 idx = BSPF::stringToInt(events[i]); + const uInt32 idx = BSPF::stoi(events[i]); if(idx < ourEmulActionList.size()) myComboTable[combo][i] = EventHandler::ourEmulActionList[idx].event; else diff --git a/src/emucore/FrameBuffer.cxx b/src/emucore/FrameBuffer.cxx index a149ea4fc..1f6c0fa39 100644 --- a/src/emucore/FrameBuffer.cxx +++ b/src/emucore/FrameBuffer.cxx @@ -332,7 +332,7 @@ FBInitStatus FrameBuffer::createDisplay(string_view title, BufferType type, } else { - p_blend = BSPF::stringToInt(myOSystem.console().properties().get(PropType::Display_PPBlend)); + p_blend = BSPF::stoi(myOSystem.console().properties().get(PropType::Display_PPBlend)); enable = myOSystem.console().properties().get(PropType::Display_Phosphor) == "YES"; } myTIASurface->enablePhosphor(enable, p_blend); diff --git a/src/emucore/ProfilingRunner.cxx b/src/emucore/ProfilingRunner.cxx index acea8691c..220f67f8c 100644 --- a/src/emucore/ProfilingRunner.cxx +++ b/src/emucore/ProfilingRunner.cxx @@ -67,7 +67,7 @@ ProfilingRunner::ProfilingRunner(int argc, char* argv[]) if (splitPoint == string::npos) run.runtime = RUNTIME_DEFAULT; else { - const int runtime = BSPF::stringToInt(arg.substr(splitPoint+1, string::npos)); + const int runtime = BSPF::stoi(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 54aa085f0..4f0edfe62 100644 --- a/src/emucore/Props.cxx +++ b/src/emucore/Props.cxx @@ -95,7 +95,7 @@ void Properties::set(PropType key, string_view value) case PropType::Display_PPBlend: { - const int blend = BSPF::stringToInt(myProperties[pos]); + const int blend = BSPF::stoi(myProperties[pos]); if(blend < 0 || blend > 100) myProperties[pos] = ourDefaultProperties[pos]; break; diff --git a/src/gui/GameInfoDialog.cxx b/src/gui/GameInfoDialog.cxx index fd7417baf..3536153d2 100644 --- a/src/gui/GameInfoDialog.cxx +++ b/src/gui/GameInfoDialog.cxx @@ -796,10 +796,10 @@ void GameInfoDialog::loadEmulationProperties(const Properties& props) myPhosphor->setLabel("Phosphor"); const string& blend = props.get(PropType::Display_PPBlend); - myPPBlend->setValue(stringToInt(blend)); + myPPBlend->setValue(BSPF::stoi(blend)); // set vertical center - const Int32 vcenter = stringToInt(props.get(PropType::Display_VCenter)); + const Int32 vcenter = BSPF::stoi(props.get(PropType::Display_VCenter)); myVCenter->setValueLabel(vcenter); myVCenter->setValue(vcenter); myVCenter->setValueUnit(vcenter ? "px" : ""); @@ -827,8 +827,8 @@ void GameInfoDialog::loadControllerProperties(const Properties& props) mySwapPaddles->setState(props.get(PropType::Controller_SwapPaddles) == "YES"); // Paddle centers - myPaddleXCenter->setValue(BSPF::stringToInt(props.get(PropType::Controller_PaddlesXCenter))); - myPaddleYCenter->setValue(BSPF::stringToInt(props.get(PropType::Controller_PaddlesYCenter))); + myPaddleXCenter->setValue(BSPF::stoi(props.get(PropType::Controller_PaddlesXCenter))); + myPaddleYCenter->setValue(BSPF::stoi(props.get(PropType::Controller_PaddlesYCenter))); // MouseAxis property (potentially contains 'range' information) istringstream m_axis(props.get(PropType::Controller_MouseAxis)); @@ -850,7 +850,7 @@ void GameInfoDialog::loadControllerProperties(const Properties& props) myMouseY->setEnabled(!autoAxis); if(m_axis >> m_range) { - myMouseRange->setValue(stringToInt(m_range)); + myMouseRange->setValue(BSPF::stoi(m_range)); } else { @@ -1061,19 +1061,19 @@ void GameInfoDialog::saveHighScoresProperties() string strAddr; strAddr = myVarAddress->getText(); - info.varsAddr = stringToIntBase16(strAddr, HSM::DEFAULT_ADDRESS); + info.varsAddr = BSPF::stoi_16(strAddr, HSM::DEFAULT_ADDRESS); strAddr = mySpecialAddress->getText(); - info.specialAddr = stringToIntBase16(strAddr, HSM::DEFAULT_ADDRESS); + info.specialAddr = BSPF::stoi_16(strAddr, HSM::DEFAULT_ADDRESS); for (uInt32 a = 0; a < HSM::MAX_SCORE_ADDR; ++a) { strAddr = myScoreAddress[a]->getText(); - info.scoreAddr[a] = stringToIntBase16(strAddr, HSM::DEFAULT_ADDRESS); + info.scoreAddr[a] = BSPF::stoi_16(strAddr, HSM::DEFAULT_ADDRESS); } const string strVars = myVariations->getText(); - HighScoresManager::set(myGameProperties, stringToInt(strVars, + HighScoresManager::set(myGameProperties, BSPF::stoi(strVars, HSM::DEFAULT_VARIATION), info); } else @@ -1347,7 +1347,7 @@ void GameInfoDialog::updateHighScoresWidgets() myVarAddress->setEnabled(enableVars); myVarAddress->setEditable(enableVars); myVarAddressVal->setEnabled(enableVars && enableConsole); - myVarsBCD->setEnabled(enableVars && stringToInt(myVariations->getText(), 1) >= 10); + myVarsBCD->setEnabled(enableVars && BSPF::stoi(myVariations->getText(), 1) >= 10); myVarsZeroBased->setEnabled(enableVars); myScoreLabel->setEnabled(enable); @@ -1386,7 +1386,7 @@ void GameInfoDialog::updateHighScoresWidgets() // update variations RAM value setAddressVal(myVarAddress, myVarAddressVal, myVarsBCD->getState(), - myVarsZeroBased->getState(), stringToInt(myVariations->getText(), 1)); + myVarsZeroBased->getState(), BSPF::stoi(myVariations->getText(), 1)); setAddressVal(mySpecialAddress, mySpecialAddressVal, mySpecialBCD->getState(), mySpecialZeroBased->getState()); @@ -1400,7 +1400,7 @@ void GameInfoDialog::updateHighScoresWidgets() { setAddressVal(myScoreAddress[a], myScoreAddressVal[a]); const string strAddr = myScoreAddress[a]->getText(); - scoreAddr[a] = stringToIntBase16(strAddr, HSM::DEFAULT_ADDRESS); + scoreAddr[a] = BSPF::stoi_16(strAddr, HSM::DEFAULT_ADDRESS); } else myScoreAddressVal[a]->setText(""); @@ -1427,7 +1427,7 @@ void GameInfoDialog::setAddressVal(const EditTextWidget* addressWidget, EditText ostringstream ss; // convert to number and read from memory - const uInt16 addr = stringToIntBase16(strAddr, HSM::DEFAULT_ADDRESS); + const uInt16 addr = BSPF::stoi_16(strAddr, HSM::DEFAULT_ADDRESS); uInt8 val = instance().highScores().peek(addr); val = HighScoresManager::convert(val, maxVal, isBCD, zeroBased);