From 20bbc25b8afe819dc5b3e08b38c24bb86d7bede1 Mon Sep 17 00:00:00 2001 From: hhvrc Date: Fri, 6 Sep 2024 20:45:39 +0200 Subject: [PATCH] Move and rename IntConv --- include/Convert.h | 16 ++++++++++++++ include/intconv.h | 15 ------------- src/{intconv.cpp => Convert.cpp} | 36 +++++++++++++++++++++++-------- src/serial/SerialInputHandler.cpp | 26 +++------------------- src/util/IPAddressUtils.cpp | 4 ++-- 5 files changed, 48 insertions(+), 49 deletions(-) create mode 100644 include/Convert.h delete mode 100644 include/intconv.h rename src/{intconv.cpp => Convert.cpp} (90%) diff --git a/include/Convert.h b/include/Convert.h new file mode 100644 index 00000000..e3defdb5 --- /dev/null +++ b/include/Convert.h @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +namespace OpenShock::Convert { + bool ToInt8(std::string_view str, int8_t& val); + bool ToUInt8(std::string_view str, uint8_t& val); + bool ToInt16(std::string_view str, int16_t& val); + bool ToUInt16(std::string_view str, uint16_t& val); + bool ToInt32(std::string_view str, int32_t& val); + bool ToUInt32(std::string_view str, uint32_t& val); + bool ToInt64(std::string_view str, int64_t& val); + bool ToUInt64(std::string_view str, uint64_t& val); + bool ToBool(std::string_view str, bool& val); +} // namespace OpenShock::Convert diff --git a/include/intconv.h b/include/intconv.h deleted file mode 100644 index f0074383..00000000 --- a/include/intconv.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include -#include - -namespace OpenShock::IntConv { - bool stoi8(std::string_view str, int8_t& val); - bool stou8(std::string_view str, uint8_t& val); - bool stoi16(std::string_view str, int16_t& val); - bool stou16(std::string_view str, uint16_t& val); - bool stoi32(std::string_view str, int32_t& val); - bool stou32(std::string_view str, uint32_t& val); - bool stoi64(std::string_view str, int64_t& val); - bool stou64(std::string_view str, uint64_t& val); -} // namespace OpenShock::IntConv diff --git a/src/intconv.cpp b/src/Convert.cpp similarity index 90% rename from src/intconv.cpp rename to src/Convert.cpp index caa64410..85dd2a28 100644 --- a/src/intconv.cpp +++ b/src/Convert.cpp @@ -1,6 +1,7 @@ -#include "intconv.h" +#include "Convert.h" #include +#include #include #include #include @@ -102,30 +103,47 @@ constexpr bool spanToST(std::string_view str, T& val) { using namespace OpenShock; // Specific converters -bool IntConv::stoi8(std::string_view str, int8_t& val) { +bool Convert::ToInt8(std::string_view str, int8_t& val) { return spanToST(str, val); } -bool IntConv::stou8(std::string_view str, uint8_t& val) { +bool Convert::ToUInt8(std::string_view str, uint8_t& val) { return spanToUT(str, val); } -bool IntConv::stoi16(std::string_view str, int16_t& val) { +bool Convert::ToInt16(std::string_view str, int16_t& val) { return spanToST(str, val); } -bool IntConv::stou16(std::string_view str, uint16_t& val) { +bool Convert::ToUInt16(std::string_view str, uint16_t& val) { return spanToUT(str, val); } -bool IntConv::stoi32(std::string_view str, int32_t& val) { +bool Convert::ToInt32(std::string_view str, int32_t& val) { return spanToST(str, val); } -bool IntConv::stou32(std::string_view str, uint32_t& val) { +bool Convert::ToUInt32(std::string_view str, uint32_t& val) { return spanToUT(str, val); } -bool IntConv::stoi64(std::string_view str, int64_t& val) { +bool Convert::ToInt64(std::string_view str, int64_t& val) { return spanToST(str, val); } -bool IntConv::stou64(std::string_view str, uint64_t& val) { +bool Convert::ToUInt64(std::string_view str, uint64_t& val) { return spanToUT(str, val); } +bool Convert::ToBool(std::string_view str, bool& val) { + if (str.length() > 5) { + return false; + } + + if (strncasecmp(str.data(), "true", str.length()) == 0) { + val = true; + return true; + } + + if (strncasecmp(str.data(), "false", str.length()) == 0) { + val = false; + return true; + } + + return false; +} static_assert(NumDigits() == 3, "NumDigits test for uint8_t failed"); static_assert(NumDigits() == 5, "NumDigits test for uint16_t failed"); diff --git a/src/serial/SerialInputHandler.cpp b/src/serial/SerialInputHandler.cpp index 4d6bdaec..173a7512 100644 --- a/src/serial/SerialInputHandler.cpp +++ b/src/serial/SerialInputHandler.cpp @@ -8,7 +8,7 @@ const char* const TAG = "SerialInputHandler"; #include "config/SerialInputConfig.h" #include "FormatHelpers.h" #include "http/HTTPRequestManager.h" -#include "intconv.h" +#include "Convert.h" #include "Logging.h" #include "serialization/JsonAPI.h" #include "serialization/JsonSerial.h" @@ -74,27 +74,7 @@ static std::unordered_map 5) { - return false; - } - - if (strncasecmp(str.data(), "true", str.length()) == 0) { - out = true; - return true; - } - - if (strncasecmp(str.data(), "false", str.length()) == 0) { - out = false; - return true; - } - - return false; + return OpenShock::Convert::ToBool(OpenShock::StringTrim(str), out); } void _handleVersionCommand(std::string_view arg) { @@ -134,7 +114,7 @@ void _handleRfTxPinCommand(std::string_view arg) { } uint8_t pin; - if (!OpenShock::IntConv::stou8(arg, pin)) { + if (!OpenShock::Convert::ToUInt8(arg, pin)) { SERPR_ERROR("Invalid argument (number invalid or out of range)"); } diff --git a/src/util/IPAddressUtils.cpp b/src/util/IPAddressUtils.cpp index 61023d37..f5d0801d 100644 --- a/src/util/IPAddressUtils.cpp +++ b/src/util/IPAddressUtils.cpp @@ -1,6 +1,6 @@ #include "util/IPAddressUtils.h" -#include "intconv.h" +#include "Convert.h" #include "util/StringUtils.h" const char* const TAG = "Util::IPAddressUtils"; @@ -16,7 +16,7 @@ bool OpenShock::IPV4AddressFromStringView(IPAddress& ip, std::string_view sv) { } std::uint8_t octets[4]; - if (!IntConv::stou8(parts[0], octets[0]) || !IntConv::stou8(parts[1], octets[1]) || !IntConv::stou8(parts[2], octets[2]) || !IntConv::stou8(parts[3], octets[3])) { + if (!Convert::ToUInt8(parts[0], octets[0]) || !Convert::ToUInt8(parts[1], octets[1]) || !Convert::ToUInt8(parts[2], octets[2]) || !Convert::ToUInt8(parts[3], octets[3])) { return false; }