[util] Support parsing floating point arguments

This commit is contained in:
Philip Rebohle 2022-08-01 12:05:43 +02:00
parent 8db2eb51fa
commit 9671055538
No known key found for this signature in database
GPG Key ID: C8CC613427A31C99
2 changed files with 64 additions and 0 deletions

View File

@ -755,6 +755,66 @@ namespace dxvk {
}
bool Config::parseOptionValue(
const std::string& value,
float& result) {
if (value.size() == 0)
return false;
// Parse sign
size_t pos = 0;
bool negate = false;
if (value[0] == '-') {
negate = true;
pos += 1;
}
// Parse integer part
uint64_t intPart = 0;
if (value[pos] == '.')
return false;
while (pos < value.size()) {
if (value[pos] == '.') {
if (++pos == value.size())
return false;
break;
}
if (value[pos] < '0' || value[pos] > '9')
return false;
intPart *= 10;
intPart += value[pos] - '0';
pos += 1;
}
// Parse fractional part
uint64_t fractPart = 0;
uint64_t fractDivisor = 1;
while (pos < value.size()) {
if (value[pos] < '0' || value[pos] > '9')
return false;
fractDivisor *= 10;
fractPart *= 10;
fractPart += value[pos] - '0';
pos += 1;
}
// Compute final number, not super accurate but this should do
result = float((double(fractPart) / double(fractDivisor)) + double(intPart));
if (negate)
result = -result;
return true;
}
bool Config::parseOptionValue(
const std::string& value,
Tristate& result) {

View File

@ -123,6 +123,10 @@ namespace dxvk {
const std::string& value,
int32_t& result);
static bool parseOptionValue(
const std::string& value,
float& result);
static bool parseOptionValue(
const std::string& value,
Tristate& result);