Consolidate and refactor some code (mostly string conversions).

This commit is contained in:
Stephen Anthony 2022-12-24 16:42:08 -03:30
parent da0b491429
commit 027efc5979
13 changed files with 48 additions and 58 deletions

View File

@ -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<uInt8>(unhex(myCode.substr(5, 2)));
count = static_cast<uInt8>(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<uInt8>(BSPF::stoi_16(myCode.substr(5, 2)));
count = static_cast<uInt8>(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)

View File

@ -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;

View File

@ -23,9 +23,9 @@
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CheetahCheat::CheetahCheat(OSystem& os, string_view name, string_view code)
: Cheat(os, name, code),
address{static_cast<uInt16>(0xf000 + unhex(code.substr(0, 3)))},
value{static_cast<uInt8>(unhex(code.substr(3, 2)))},
count{static_cast<uInt8>(unhex(code.substr(5, 1)) + 1)}
address{static_cast<uInt16>(0xf000 + BSPF::stoi_16(code.substr(0, 3)))},
value{static_cast<uInt8>(BSPF::stoi_16(code.substr(3, 2)))},
count{static_cast<uInt8>(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)

View File

@ -25,8 +25,8 @@
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
RamCheat::RamCheat(OSystem& os, string_view name, string_view code)
: Cheat(os, name, code),
address{static_cast<uInt16>(unhex(myCode.substr(0, 2)))},
value{static_cast<uInt8>(unhex(myCode.substr(2, 2)))}
address{static_cast<uInt16>(BSPF::stoi_16(myCode.substr(0, 2)))},
value{static_cast<uInt8>(BSPF::stoi_16(myCode.substr(2, 2)))}
{
}

View File

@ -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<uInt16>(BSPF::stoi_16(naked));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -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; }

View File

@ -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<int>(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))

View File

@ -151,7 +151,7 @@ Console::Console(OSystem& osystem, unique_ptr<Cartridge>& 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<Int32>(BSPF::stringToInt(myProperties.get(PropType::Display_VCenter))), TIAConstants::minVcenter, TIAConstants::maxVcenter
static_cast<Int32>(BSPF::stoi(myProperties.get(PropType::Display_VCenter))), TIAConstants::minVcenter, TIAConstants::maxVcenter
);
if(gameRefreshRate() == 60)
@ -976,8 +976,8 @@ unique_ptr<Controller> 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<Paddles>(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);

View File

@ -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

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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);