[ox/std] Make ox_atoi return an ox::Result

This commit is contained in:
Gary Talent 2021-03-03 00:30:16 -06:00
parent 28e68adc75
commit c5773202b5
3 changed files with 14 additions and 7 deletions

View File

@ -28,7 +28,9 @@ ClArgs::ClArgs(int argc, const char **args) {
m_bools[arg] = false; m_bools[arg] = false;
} }
m_strings[arg] = val; m_strings[arg] = val;
m_ints[arg] = ox_atoi(val.c_str()); if (auto r = ox_atoi(val.c_str()); r.error == 0) {
m_ints[arg] = r.value;
}
i++; i++;
} }
} }

View File

@ -49,7 +49,9 @@ Result<ClawHeader> readHeader(const char *buff, std::size_t buffLen) noexcept {
return OxError(1); return OxError(1);
} }
hdr.typeName = typeName; hdr.typeName = typeName;
hdr.typeVersion = ox_atoi(versionStr.c_str()); if (auto r = ox_atoi(versionStr.c_str()); r.error == 0) {
hdr.typeVersion = r.value;
}
hdr.data = buff; hdr.data = buff;
hdr.dataSize = buffLen; hdr.dataSize = buffLen;
return hdr; return hdr;

View File

@ -8,6 +8,7 @@
#pragma once #pragma once
#include "error.hpp"
#include "math.hpp" #include "math.hpp"
#include "types.hpp" #include "types.hpp"
#include "typetraits.hpp" #include "typetraits.hpp"
@ -125,15 +126,17 @@ template<typename T1, typename T2>
return retval; return retval;
} }
[[nodiscard]] constexpr int ox_atoi(const char *str) noexcept { [[nodiscard]] constexpr ox::Result<int> ox_atoi(const char *str) noexcept {
int total = 0; int total = 0;
int multiplier = 1; int multiplier = 1;
for (auto i = static_cast<int64_t>(ox_strlen(str)) - 1; i != -1; i--) { for (auto i = static_cast<int64_t>(ox_strlen(str)) - 1; i != -1; i--) {
if (str[i] >= '0' && str[i] <= '9') {
total += (str[i] - '0') * multiplier; total += (str[i] - '0') * multiplier;
multiplier *= 10; multiplier *= 10;
} else {
return OxError(1);
}
} }
return total; return total;
} }