diff options
Diffstat (limited to 'src/openvic-simulation/utility')
-rw-r--r-- | src/openvic-simulation/utility/BMP.cpp | 15 | ||||
-rw-r--r-- | src/openvic-simulation/utility/ConstexprIntToStr.hpp | 6 | ||||
-rw-r--r-- | src/openvic-simulation/utility/Logger.cpp | 24 | ||||
-rw-r--r-- | src/openvic-simulation/utility/Logger.hpp | 29 | ||||
-rw-r--r-- | src/openvic-simulation/utility/StringUtils.hpp | 42 |
5 files changed, 82 insertions, 34 deletions
diff --git a/src/openvic-simulation/utility/BMP.cpp b/src/openvic-simulation/utility/BMP.cpp index 2fa9417..d4e7cf7 100644 --- a/src/openvic-simulation/utility/BMP.cpp +++ b/src/openvic-simulation/utility/BMP.cpp @@ -69,8 +69,10 @@ bool BMP::read_header() { // Validate sizes and dimensions // TODO - image_size_bytes can be 0 for non-compressed BMPs if (header.file_size != header.offset + header.image_size_bytes) { - Logger::error("Invalid BMP memory sizes: file size = ", header.file_size, " != ", header.offset + header.image_size_bytes, - " = ", header.offset, " + ", header.image_size_bytes, " = image data offset + image data size"); + Logger::error( + "Invalid BMP memory sizes: file size = ", header.file_size, " != ", header.offset + header.image_size_bytes, " = ", + header.offset, " + ", header.image_size_bytes, " = image data offset + image data size" + ); header_validated = false; } // TODO - support negative widths (i.e. horizontal flip) @@ -98,15 +100,18 @@ bool BMP::read_header() { #undef STR static constexpr uint16_t PALETTE_BITS_PER_PIXEL_LIMIT = 8; if (header.num_colours != 0 && header.bits_per_pixel > PALETTE_BITS_PER_PIXEL_LIMIT) { - Logger::error("Invalid BMP palette size: ", header.num_colours, " (should be 0 as bits per pixel is ", header.bits_per_pixel, " > 8)"); + Logger::error( + "Invalid BMP palette size: ", header.num_colours, " (should be 0 as bits per pixel is ", header.bits_per_pixel, + " > 8)" + ); header_validated = false; } // TODO - validate important_colours palette_size = header.bits_per_pixel > PALETTE_BITS_PER_PIXEL_LIMIT ? 0 // Use header.num_colours if it's greater than 0 and at most 1 << header.bits_per_pixel - : 0 < header.num_colours && header.num_colours - 1 >> header.bits_per_pixel == 0 ? header.num_colours - : 1 << header.bits_per_pixel; + : (0 < header.num_colours && header.num_colours - 1 >> header.bits_per_pixel == 0 + ? header.num_colours : 1 << header.bits_per_pixel); const uint32_t expected_offset = palette_size * PALETTE_COLOUR_SIZE + sizeof(header); if (header.offset != expected_offset) { diff --git a/src/openvic-simulation/utility/ConstexprIntToStr.hpp b/src/openvic-simulation/utility/ConstexprIntToStr.hpp index e383365..e346ebe 100644 --- a/src/openvic-simulation/utility/ConstexprIntToStr.hpp +++ b/src/openvic-simulation/utility/ConstexprIntToStr.hpp @@ -33,7 +33,9 @@ namespace OpenVic::ConstexprIntToStr { constexpr std::size_t next_value = value / 10; if constexpr (next_value != 0) { - return append_sequence(integer_to_string_sequence<next_value>(), std::integer_sequence<char, digits()[remainder]> {}); + return append_sequence( + integer_to_string_sequence<next_value>(), std::integer_sequence<char, digits()[remainder]> {} + ); } else { return std::integer_sequence<char, digits()[remainder]> {}; } @@ -53,4 +55,4 @@ namespace OpenVic::ConstexprIntToStr { constexpr auto make_itosv_array() { return generate_itosv_array(std::make_integer_sequence<std::size_t, N>()); } -}
\ No newline at end of file +} diff --git a/src/openvic-simulation/utility/Logger.cpp b/src/openvic-simulation/utility/Logger.cpp index 68c43dd..5e25c98 100644 --- a/src/openvic-simulation/utility/Logger.cpp +++ b/src/openvic-simulation/utility/Logger.cpp @@ -5,18 +5,30 @@ using namespace OpenVic; void Logger::set_logger_funcs() { - Logger::set_info_func([](std::string&& str) { std::cout << str; }); - Logger::set_warning_func([](std::string&& str) { std::cerr << str; }); - Logger::set_error_func([](std::string&& str) { std::cerr << str; }); + Logger::set_info_func([](std::string&& str) { + std::cout << str; + }); + Logger::set_warning_func([](std::string&& str) { + std::cerr << str; + }); + Logger::set_error_func([](std::string&& str) { + std::cerr << str; + }); } char const* Logger::get_filename(char const* filepath, char const* default_path) { - if (filepath == nullptr) return default_path; + if (filepath == nullptr) { + return default_path; + } char const* last_slash = filepath; while (*filepath != '\0') { - if (*filepath == '\\' || *filepath == '/') last_slash = filepath + 1; + if (*filepath == '\\' || *filepath == '/') { + last_slash = filepath + 1; + } filepath++; } - if (*last_slash == '\0') return default_path; + if (*last_slash == '\0') { + return default_path; + } return last_slash; } diff --git a/src/openvic-simulation/utility/Logger.hpp b/src/openvic-simulation/utility/Logger.hpp index d60e59e..a1c599d 100644 --- a/src/openvic-simulation/utility/Logger.hpp +++ b/src/openvic-simulation/utility/Logger.hpp @@ -20,15 +20,24 @@ namespace OpenVic { int _line; std::string _function; - public: source_location(std::string f, int l, std::string n) : _file(f), _line(l), _function(n) {} - static source_location current(std::string f = __builtin_FILE(), int l = __builtin_LINE(), std::string n = __builtin_FUNCTION()) { + + public: + static source_location current( + std::string f = __builtin_FILE(), int l = __builtin_LINE(), std::string n = __builtin_FUNCTION() + ) { return source_location(f, l, n); } - inline char const* file_name() const { return _file.c_str(); } - inline int line() const { return _line; } - inline char const* function_name() const { return _function.c_str(); } + inline char const* file_name() const { + return _file.c_str(); + } + inline int line() const { + return _line; + } + inline char const* function_name() const { + return _function.c_str(); + } }; #endif @@ -57,8 +66,10 @@ namespace OpenVic { log(log_channel_t& log_channel, Ts&&... ts, source_location const& location) { std::stringstream stream; stream << "\n" << get_filename(location.file_name()) << "(" - //<< location.line() << ") `" << location.function_name() << "`: "; - << location.line() << "): "; + /* Function name removed to reduce clutter. It is already included + * in Godot's print functions, so this was repeating it. */ + //<< location.line() << ") `" << location.function_name() << "`: "; + << location.line() << "): "; ((stream << std::forward<Ts>(ts)), ...); stream << std::endl; log_channel.queue.push(stream.str()); @@ -73,7 +84,7 @@ namespace OpenVic { #define LOG_FUNC(name) \ private: \ - static inline log_channel_t name##_channel{}; \ + static inline log_channel_t name##_channel {}; \ public: \ static inline void set_##name##_func(log_func_t log_func) { \ name##_channel.func = log_func; \ @@ -81,7 +92,7 @@ namespace OpenVic { template<typename... Ts> \ struct name { \ name(Ts&&... ts, source_location const& location = source_location::current()) { \ - log<Ts...>{ name##_channel, std::forward<Ts>(ts)..., location }; \ + log<Ts...> { name##_channel, std::forward<Ts>(ts)..., location }; \ } \ }; \ template<typename... Ts> \ diff --git a/src/openvic-simulation/utility/StringUtils.hpp b/src/openvic-simulation/utility/StringUtils.hpp index 5784208..d968bf6 100644 --- a/src/openvic-simulation/utility/StringUtils.hpp +++ b/src/openvic-simulation/utility/StringUtils.hpp @@ -12,11 +12,14 @@ namespace OpenVic::StringUtils { * or not conversion was successful. It can be nullptr if this information is not needed. */ constexpr uint64_t string_to_uint64(char const* str, const char* const end, bool* successful = nullptr, int base = 10) { - if (successful != nullptr) *successful = false; + 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) + if (str == nullptr || end <= str || base < 0 || base == 1 || base > 36) { return 0; + } // The result of the conversion will be stored in this variable. uint64_t result = 0; @@ -26,8 +29,10 @@ namespace OpenVic::StringUtils { 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; + str += 2; // Skip '0x' or '0X' + if (str == end) { + return 0; + } } else { base = 8; // Octal. } @@ -38,7 +43,9 @@ namespace OpenVic::StringUtils { // 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; + if (str == end) { + return 0; + } } } @@ -76,7 +83,9 @@ namespace OpenVic::StringUtils { // 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; + if (successful != nullptr && str == end) { + *successful = true; + } return result; } @@ -90,29 +99,38 @@ namespace OpenVic::StringUtils { } constexpr int64_t string_to_int64(char const* str, const char* const end, bool* successful = nullptr, int base = 10) { - if (successful != nullptr) *successful = false; + if (successful != nullptr) { + *successful = false; + } - if (str == nullptr || end <= str) return 0; + if (str == nullptr || end <= str) { + return 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 == '-') + if (*str == '-') { is_negative = true; + } ++str; - if (str == end) return 0; + if (str == end) { + return 0; + } } const uint64_t result = string_to_uint64(str, end, successful, base); if (!is_negative) { - if (result >= std::numeric_limits<int64_t>::max()) + if (result >= std::numeric_limits<int64_t>::max()) { return std::numeric_limits<int64_t>::max(); + } return result; } else { - if (result > std::numeric_limits<int64_t>::max()) + if (result > std::numeric_limits<int64_t>::max()) { return std::numeric_limits<int64_t>::min(); + } return -result; } } |