diff options
author | hop311 <hop3114@gmail.com> | 2023-11-04 19:44:38 +0100 |
---|---|---|
committer | hop311 <hop3114@gmail.com> | 2023-11-07 19:34:51 +0100 |
commit | b5bbeb47febc823517a5baba4eca66f32fb3609c (patch) | |
tree | 1a78d296f22d1cd17a84ce824974f9438a197074 /src/openvic-simulation/utility | |
parent | c1b7cab254ac14a173477661047ad2492930ff8b (diff) |
Cross-platform file lookup (case and separator)
Diffstat (limited to 'src/openvic-simulation/utility')
-rw-r--r-- | src/openvic-simulation/utility/Logger.cpp | 34 | ||||
-rw-r--r-- | src/openvic-simulation/utility/Logger.hpp | 21 | ||||
-rw-r--r-- | src/openvic-simulation/utility/StringUtils.hpp | 68 |
3 files changed, 83 insertions, 40 deletions
diff --git a/src/openvic-simulation/utility/Logger.cpp b/src/openvic-simulation/utility/Logger.cpp deleted file mode 100644 index 63dfd6c..0000000 --- a/src/openvic-simulation/utility/Logger.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "Logger.hpp" - -#include <iostream> - -using namespace OpenVic; - -void Logger::set_logger_funcs() { - Logger::set_info_func([](std::string&& str) { - std::cout << "[INFO] " << str; - }); - Logger::set_warning_func([](std::string&& str) { - std::cerr << "[WARNING] " << str; - }); - Logger::set_error_func([](std::string&& str) { - std::cerr << "[ERROR] " << str; - }); -} - -char const* Logger::get_filename(char const* filepath, char const* default_path) { - if (filepath == nullptr) { - return default_path; - } - char const* last_slash = filepath; - while (*filepath != '\0') { - if (*filepath == '\\' || *filepath == '/') { - last_slash = filepath + 1; - } - filepath++; - } - 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 04306f7..20c7fdd 100644 --- a/src/openvic-simulation/utility/Logger.hpp +++ b/src/openvic-simulation/utility/Logger.hpp @@ -1,12 +1,16 @@ #pragma once #include <functional> +#include <iostream> #include <queue> #include <sstream> + #ifdef __cpp_lib_source_location #include <source_location> #endif +#include "openvic-simulation/utility/StringUtils.hpp" + namespace OpenVic { #ifndef __cpp_lib_source_location @@ -52,8 +56,17 @@ namespace OpenVic { #endif public: - static void set_logger_funcs(); - static char const* get_filename(char const* filepath, char const* default_path = nullptr); + static void set_logger_funcs() { + set_info_func([](std::string&& str) { + std::cout << "[INFO] " << str; + }); + set_warning_func([](std::string&& str) { + std::cerr << "[WARNING] " << str; + }); + set_error_func([](std::string&& str) { + std::cerr << "[ERROR] " << str; + }); + } private: struct log_channel_t { @@ -65,13 +78,13 @@ namespace OpenVic { struct log { log(log_channel_t& log_channel, Ts&&... ts, source_location const& location) { std::stringstream stream; - stream << get_filename(location.file_name()) << "(" + stream << StringUtils::get_filename(location.file_name()) << "(" /* 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 << "\n" << std::endl; + stream << std::endl; log_channel.queue.push(stream.str()); if (log_channel.func) { do { diff --git a/src/openvic-simulation/utility/StringUtils.hpp b/src/openvic-simulation/utility/StringUtils.hpp index d968bf6..ede1d6b 100644 --- a/src/openvic-simulation/utility/StringUtils.hpp +++ b/src/openvic-simulation/utility/StringUtils.hpp @@ -1,4 +1,7 @@ +#pragma once + #include <cstdint> +#include <cstring> #include <limits> #include <string_view> @@ -90,7 +93,7 @@ namespace OpenVic::StringUtils { return result; } - constexpr uint64_t string_to_uint64(char const* str, size_t length, bool* successful = nullptr, int base = 10) { + inline constexpr uint64_t string_to_uint64(char const* str, size_t length, bool* successful = nullptr, int base = 10) { return string_to_uint64(str, str + length, successful, base); } @@ -135,11 +138,72 @@ namespace OpenVic::StringUtils { } } - constexpr int64_t string_to_int64(char const* str, size_t length, bool* successful = nullptr, int base = 10) { + inline constexpr int64_t string_to_int64(char const* str, size_t length, bool* successful = nullptr, int base = 10) { return string_to_int64(str, str + length, successful, base); } inline int64_t string_to_int64(std::string_view str, bool* successful = nullptr, int base = 10) { return string_to_int64(str.data(), str.length(), successful, base); } + + inline constexpr std::string_view get_filename(std::string_view path) { + size_t pos = path.size(); + while (pos > 0 && path[pos - 1] != '/' && path[pos - 1] != '\\') { + --pos; + } + path.remove_prefix(pos); + return path; + } + + inline constexpr char const* get_filename(char const* filepath, char const* default_path = nullptr) { + const std::string_view filename { get_filename(std::string_view { filepath }) }; + if (!filename.empty()) { + return filename.data(); + } + return default_path; + } + + inline std::string make_forward_slash_path(std::string_view path) { + std::string ret { path }; + std::replace(ret.begin(), ret.end(), '\\', '/'); + for (char& c : ret) { + if (c == '\\') { + c = '/'; + } + } + return ret; + } + + inline constexpr std::string_view remove_leading_slashes(std::string_view path) { + size_t count = 0; + while (count < path.size() && (path[count] == '/' || path[count] == '\\')) { + ++count; + } + path.remove_prefix(count); + return path; + } + + template<typename... Args> + requires (std::is_same_v<std::string_view, Args> && ...) + inline std::string _append_string_views(Args... args) { + std::string ret; + ret.reserve((args.size() + ...)); + (ret.append(args), ...); + return ret; + } + + template<typename... Args> + requires (std::is_convertible_v<Args, std::string_view> && ...) + inline std::string append_string_views(Args... args) { + return _append_string_views(std::string_view { args }...); + } + + inline constexpr std::string_view remove_extension(std::string_view path) { + size_t pos = path.size(); + while (pos > 0 && path[--pos] != '.') {} + if (path[pos] == '.') { + path.remove_suffix(path.size() - pos); + } + return path; + } } |