From efa88c722fcb6c8fea7a86e1b3b8a83f1f59eb31 Mon Sep 17 00:00:00 2001 From: Hop311 Date: Thu, 24 Aug 2023 00:32:23 +0100 Subject: Big Dataloader Commit (openvic-simulation) --- src/openvic/utility/BMP.cpp | 2 +- src/openvic/utility/BMP.hpp | 6 ++- src/openvic/utility/FloatUtils.hpp | 11 +++++ src/openvic/utility/StringUtils.hpp | 93 +++++++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 src/openvic/utility/FloatUtils.hpp create mode 100644 src/openvic/utility/StringUtils.hpp (limited to 'src/openvic/utility') diff --git a/src/openvic/utility/BMP.cpp b/src/openvic/utility/BMP.cpp index 9a90d68..6859411 100644 --- a/src/openvic/utility/BMP.cpp +++ b/src/openvic/utility/BMP.cpp @@ -3,7 +3,7 @@ #include #include -#include "Logger.hpp" +#include "openvic/utility/Logger.hpp" using namespace OpenVic; diff --git a/src/openvic/utility/BMP.hpp b/src/openvic/utility/BMP.hpp index c6f5815..50b333b 100644 --- a/src/openvic/utility/BMP.hpp +++ b/src/openvic/utility/BMP.hpp @@ -1,6 +1,10 @@ #pragma once -#include "../Types.hpp" +#include +#include + +#include "openvic/types/Colour.hpp" +#include "openvic/types/Return.hpp" namespace OpenVic { class BMP { diff --git a/src/openvic/utility/FloatUtils.hpp b/src/openvic/utility/FloatUtils.hpp new file mode 100644 index 0000000..4fc83fd --- /dev/null +++ b/src/openvic/utility/FloatUtils.hpp @@ -0,0 +1,11 @@ +#include + +namespace OpenVic::FloatUtils { + constexpr int round_to_int(double num) { + return (num > 0.0) ? (num + 0.5) : (num - 0.5); + } + + constexpr int64_t round_to_int64(double num) { + return (num > 0.0) ? (num + 0.5) : (num - 0.5); + } +} diff --git a/src/openvic/utility/StringUtils.hpp b/src/openvic/utility/StringUtils.hpp new file mode 100644 index 0000000..72f4038 --- /dev/null +++ b/src/openvic/utility/StringUtils.hpp @@ -0,0 +1,93 @@ +#include +#include + +namespace OpenVic::StringUtils { + /* The constexpr function 'string_to_int64' will convert a string into a int64 integer value. + * The function takes four parameters: the input string (as a pair of pointers marking the start and + * end of the string), a bool pointer for reporting success, and the base for numerical conversion. + * The base parameter defaults to 10 (decimal), but it can be any value between 2 and 36. If the base + * given is 0, it will be set to 16 if the string starts with "0x" or "0X", otherwise 8 if the string + * still starts with "0", otherwise 10. The success bool pointer parameter is used to report whether + * or not conversion was successful. It can be nullptr if this information is not needed. + */ + constexpr int64_t string_to_int64(char const* str, const char* end, bool* successful, int base = 10) { + if (successful != nullptr) *successful = false; + + // Base value should be between 2 and 36. If it's not, return 0 as an invalid case. + if (str == nullptr || end <= str || base < 0 || base == 1 || base > 36) + return 0; + + // The result of the conversion will be stored in this variable. + int64_t result = 0; + // This flag will be set if the number is negative. + bool is_negative = false; + + // Check if there is a sign character. + if (*str == '+' || *str == '-') { + if (*str == '-') + is_negative = true; + ++str; + if (str == end) return 0; + } + + // If base is zero, base is determined by the string prefix. + if (base == 0) { + if (*str == '0') { + if (str + 1 != end && (str[1] == 'x' || str[1] == 'X')) { + base = 16; // Hexadecimal. + str += 2; // Skip '0x' or '0X' + if (str == end) return 0; + } else { + base = 8; // Octal. + } + } else { + base = 10; // Decimal. + } + } else if (base == 16) { + // If base is 16 and string starts with '0x' or '0X', skip these characters. + if (*str == '0' && str + 1 != end && (str[1] == 'x' || str[1] == 'X')) { + str += 2; + if (str == end) return 0; + } + } + + // Convert the number in the string. + for (; str != end; ++str) { + int digit; + if (*str >= '0' && *str <= '9') { + digit = *str - '0'; // Calculate digit value for '0'-'9'. + } else if (*str >= 'a' && *str <= 'z') { + digit = *str - 'a' + 10; // Calculate digit value for 'a'-'z'. + } else if (*str >= 'A' && *str <= 'Z') { + digit = *str - 'A' + 10; // Calculate digit value for 'A'-'Z'. + } else { + break; // Stop conversion if current character is not a digit. + } + + if (digit >= base) { + break; // Stop conversion if current digit is greater than or equal to the base. + } + + // Check for overflow on multiplication + if (result > std::numeric_limits::max() / base) { + return is_negative ? std::numeric_limits::min() : std::numeric_limits::max(); + } + + result *= base; + + // Check for overflow on addition + if (result > std::numeric_limits::max() - digit) { + return is_negative ? std::numeric_limits::min() : std::numeric_limits::max(); + } + + result += digit; + } + + // If successful is not null and the entire string was parsed, + // set *successful to true (if not it is already false). + if (successful != nullptr && str == end) *successful = true; + + // Return the result. If the number was negative, the result is negated. + return is_negative ? -result : result; + } +} -- cgit v1.2.3-56-ga3b1