From 478d75f1cb6ff25fbbd0d22357b6de395c3ab662 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Fri, 19 Jul 2019 18:19:44 -1000 Subject: [PATCH] Massive fmtlib refactor --- .gitmodules | 3 + CMakeLists.txt | 7 ++- atdna/CMakeLists.txt | 2 + extern/CMakeLists.txt | 3 + extern/fmt | 1 + extern/lzokay | 2 +- include/athena/DNAYaml.hpp | 24 ++++---- include/athena/Global.hpp | 88 +++++++++++----------------- include/athena/IStreamWriter.hpp | 12 ++-- include/athena/Utility.hpp | 84 +++++++++++++-------------- src/aes.cpp | 6 +- src/athena/ALTTPFileReader.cpp | 2 +- src/athena/DNAYaml.cpp | 98 +++++++++++--------------------- src/athena/Dir.cpp | 2 +- src/athena/FileReader.cpp | 14 ++--- src/athena/FileReaderWin32.cpp | 2 +- src/athena/FileWriterNix.cpp | 12 ++-- src/athena/FileWriterWin32.cpp | 2 +- src/athena/Global.cpp | 13 +++-- src/athena/MemoryReader.cpp | 16 +++--- src/athena/MemoryWriter.cpp | 38 ++++++------- src/athena/Socket.cpp | 8 +-- src/athena/Utility.cpp | 30 +--------- src/athena/VectorWriter.cpp | 10 ++-- src/athena/WiiFile.cpp | 2 +- 25 files changed, 204 insertions(+), 277 deletions(-) create mode 160000 extern/fmt diff --git a/.gitmodules b/.gitmodules index c6727f2..6d2172e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "extern/lzokay"] path = extern/lzokay url = https://github.com/jackoalan/lzokay.git +[submodule "extern/fmt"] + path = extern/fmt + url = https://github.com/fmtlib/fmt diff --git a/CMakeLists.txt b/CMakeLists.txt index 0282804..908be97 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,7 +84,7 @@ add_library(athena-core target_include_directories(athena-core PUBLIC $ $) -target_link_libraries(athena-core PUBLIC athena-libyaml) +target_link_libraries(athena-core PUBLIC athena-libyaml fmt) add_library(athena-sakura EXCLUDE_FROM_ALL src/athena/Sprite.cpp @@ -191,7 +191,8 @@ foreach(p LIB INCLUDE CMAKE) endforeach() # Define installs -install(DIRECTORY include/ DESTINATION ${INSTALL_INCLUDE_DIR}/athena COMPONENT athena) +install(DIRECTORY include DESTINATION ${INSTALL_INCLUDE_DIR}/athena COMPONENT athena) +install(DIRECTORY extern/fmt/include DESTINATION ${INSTALL_INCLUDE_DIR}/fmt COMPONENT athena) install(TARGETS athena-core DESTINATION ${INSTALL_LIB_DIR} EXPORT AthenaTargets COMPONENT athena) if(WIN32 AND NOT CYGWIN) @@ -208,7 +209,7 @@ endif() ################## # Add all targets to the build-tree export set -export(TARGETS athena-core athena-libyaml FILE "${CMAKE_CURRENT_BINARY_DIR}/AthenaTargets.cmake") +export(TARGETS athena-core athena-libyaml fmt FILE "${CMAKE_CURRENT_BINARY_DIR}/AthenaTargets.cmake") # Export the package for use from the build-tree # (this registers the build-tree with a global CMake-registry) diff --git a/atdna/CMakeLists.txt b/atdna/CMakeLists.txt index ea65ae0..c5bbdf7 100644 --- a/atdna/CMakeLists.txt +++ b/atdna/CMakeLists.txt @@ -4,6 +4,8 @@ if(NOT CMAKE_CROSSCOMPILING) +string(REPLACE -stdlib=libc++ "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + get_directory_property(ATDNA_DEFINES COMPILE_DEFINITIONS) list(REMOVE_ITEM ATDNA_DEFINES _GLIBCXX_DEBUG=1) set_directory_properties(PROPERTIES COMPILE_DEFINITIONS "${ATDNA_DEFINES}") diff --git a/extern/CMakeLists.txt b/extern/CMakeLists.txt index 71cb76b..2ae7b25 100644 --- a/extern/CMakeLists.txt +++ b/extern/CMakeLists.txt @@ -1,3 +1,6 @@ add_subdirectory(lzokay) add_subdirectory(zlib) add_subdirectory(yaml) +if(NOT TARGET fmt) + add_subdirectory(fmt) +endif() \ No newline at end of file diff --git a/extern/fmt b/extern/fmt new file mode 160000 index 0000000..6bcc3fd --- /dev/null +++ b/extern/fmt @@ -0,0 +1 @@ +Subproject commit 6bcc3fd21694b5634cc006915bd049cf460a9a8d diff --git a/extern/lzokay b/extern/lzokay index 343f970..671e2b9 160000 --- a/extern/lzokay +++ b/extern/lzokay @@ -1 +1 @@ -Subproject commit 343f9707f6ab2143d3455c93d8b7570a0ffcee97 +Subproject commit 671e2b98642f95aaf29251367791f28e5597a0a7 diff --git a/include/athena/DNAYaml.hpp b/include/athena/DNAYaml.hpp index 99117cf..01d8dc7 100644 --- a/include/athena/DNAYaml.hpp +++ b/include/athena/DNAYaml.hpp @@ -9,17 +9,17 @@ namespace athena::io { template -static inline const char* __GetDNAName(const T& dna, typename std::enable_if_t>* = 0) { +inline const char* __GetDNAName(const T& dna, typename std::enable_if_t>* = 0) { return dna.DNATypeV(); } template -static inline const char* __GetDNAName(const T& dna, typename std::enable_if_t>* = 0) { +inline const char* __GetDNAName(const T& dna, typename std::enable_if_t>* = 0) { return dna.DNAType(); } template -static inline std::string ToYAMLString(const T& dna) { +inline std::string ToYAMLString(const T& dna) { YAMLDocWriter docWriter(__GetDNAName(dna)); std::string res; @@ -35,7 +35,7 @@ static inline std::string ToYAMLString(const T& dna) { } template -static inline bool FromYAMLString(T& dna, std::string_view str) { +inline bool FromYAMLString(T& dna, std::string_view str) { YAMLStdStringViewReaderState reader(str); YAMLDocReader docReader; yaml_parser_set_input(docReader.getParser(), (yaml_read_handler_t*)YAMLStdStringReader, &reader); @@ -46,7 +46,7 @@ static inline bool FromYAMLString(T& dna, std::string_view str) { } template -static inline bool ValidateFromYAMLString(std::string_view str) { +inline bool ValidateFromYAMLString(std::string_view str) { YAMLStdStringViewReaderState reader(str); YAMLDocReader docReader; yaml_parser_set_input(docReader.getParser(), (yaml_read_handler_t*)YAMLStdStringReader, &reader); @@ -55,7 +55,7 @@ static inline bool ValidateFromYAMLString(std::string_view str) { } template -static inline bool ToYAMLStream(const T& dna, athena::io::IStreamWriter& fout) { +inline bool ToYAMLStream(const T& dna, athena::io::IStreamWriter& fout) { YAMLDocWriter docWriter(__GetDNAName(dna)); yaml_emitter_set_unicode(docWriter.getEmitter(), true); @@ -66,8 +66,8 @@ static inline bool ToYAMLStream(const T& dna, athena::io::IStreamWriter& fout) { } template -static inline bool ToYAMLStream(const T& dna, athena::io::IStreamWriter& fout, - void (T::*fn)(YAMLDocWriter& out) const) { +inline bool ToYAMLStream(const T& dna, athena::io::IStreamWriter& fout, + void (T::*fn)(YAMLDocWriter& out) const) { YAMLDocWriter docWriter(__GetDNAName(dna)); yaml_emitter_set_unicode(docWriter.getEmitter(), true); @@ -78,7 +78,7 @@ static inline bool ToYAMLStream(const T& dna, athena::io::IStreamWriter& fout, } template -static inline bool FromYAMLStream(T& dna, athena::io::IStreamReader& fin) { +inline bool FromYAMLStream(T& dna, athena::io::IStreamReader& fin) { YAMLDocReader docReader; if (!docReader.parse(&fin)) return false; @@ -87,7 +87,7 @@ static inline bool FromYAMLStream(T& dna, athena::io::IStreamReader& fin) { } template -static inline bool FromYAMLStream(T& dna, athena::io::IStreamReader& fin, void (T::*fn)(YAMLDocReader& in)) { +inline bool FromYAMLStream(T& dna, athena::io::IStreamReader& fin, void (T::*fn)(YAMLDocReader& in)) { YAMLDocReader docReader; if (!docReader.parse(&fin)) return false; @@ -96,7 +96,7 @@ static inline bool FromYAMLStream(T& dna, athena::io::IStreamReader& fin, void ( } template -static inline bool MergeToYAMLFile(const T& dna, const NameT& filename) { +inline bool MergeToYAMLFile(const T& dna, const NameT& filename) { athena::io::FileReader r(filename); YAMLDocWriter docWriter(__GetDNAName(dna), r.isOpen() ? &r : nullptr); r.close(); @@ -109,7 +109,7 @@ static inline bool MergeToYAMLFile(const T& dna, const NameT& filename) { } template -static inline bool ValidateFromYAMLStream(athena::io::IStreamReader& fin) { +inline bool ValidateFromYAMLStream(athena::io::IStreamReader& fin) { YAMLDocReader reader; atUint64 pos = fin.position(); yaml_parser_set_input(reader.getParser(), (yaml_read_handler_t*)YAMLAthenaReader, &fin); diff --git a/include/athena/Global.hpp b/include/athena/Global.hpp index af99af2..02b5309 100644 --- a/include/athena/Global.hpp +++ b/include/athena/Global.hpp @@ -3,6 +3,11 @@ #include #include "athena/Types.hpp" +#define FMT_STRING_ALIAS 1 +#define FMT_ENFORCE_COMPILE_STRING 1 +#define FMT_USE_GRISU 0 +#include + #ifdef _MSC_VER #pragma warning(disable : 4996) @@ -142,8 +147,8 @@ inline constexpr bool __IsDNAVRecord_v = __IsDNAVRecord::value; } // namespace io } // namespace athena -typedef void (*atEXCEPTION_HANDLER)(athena::error::Level level, const char* file, const char* function, int line, - const char* fmt, ...); +typedef void (*atEXCEPTION_HANDLER)(athena::error::Level level, const char* /*file*/, const char*, int /*line*/, + fmt::string_view fmt, fmt::format_args args); atEXCEPTION_HANDLER atGetExceptionHandler(); /** @@ -155,83 +160,54 @@ void atSetExceptionHandler(atEXCEPTION_HANDLER func); std::ostream& operator<<(std::ostream& os, const athena::SeekOrigin& origin); std::ostream& operator<<(std::ostream& os, const athena::Endian& endian); -#ifdef _MSC_VER +template +constexpr auto __FIRST_ARG__(First first, Rest...) { return first; } +template +inline auto __make_args_checked__(const S& format_str, Args&&... args) { + return fmt::internal::make_args_checked(format_str, std::forward(args)...); +} + #ifndef NDEBUG -#define atDebug(fmt, ...) \ +#define atDebug(...) \ do { \ atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ if (__handler) \ - __handler(athena::error::Level::Message, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \ + __handler(athena::error::Level::Message, __FILE__, AT_PRETTY_FUNCTION, __LINE__, __FIRST_ARG__(__VA_ARGS__), \ + __make_args_checked__(__VA_ARGS__)); \ } while (0) #else -#define atDebug(fmt, ...) +#define atDebug(...) #endif -#define atMessage(fmt, ...) \ +#define atMessage(...) \ do { \ atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ if (__handler) \ - __handler(athena::error::Level::Message, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \ + __handler(athena::error::Level::Message, __FILE__, AT_PRETTY_FUNCTION, __LINE__, __FIRST_ARG__(__VA_ARGS__), \ + __make_args_checked__(__VA_ARGS__)); \ } while (0) -#define atWarning(fmt, ...) \ +#define atWarning(...) \ do { \ atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ - if (__handler) \ - __handler(athena::error::Level::Warning, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \ + if (__handler) { \ + __handler(athena::error::Level::Warning, __FILE__, AT_PRETTY_FUNCTION, __LINE__, __FIRST_ARG__(__VA_ARGS__), \ + __make_args_checked__(__VA_ARGS__)); \ + } \ } while (0) -#define atError(fmt, ...) \ +#define atError(...) \ do { \ atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ if (__handler) \ - __handler(athena::error::Level::Error, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \ + __handler(athena::error::Level::Error, __FILE__, AT_PRETTY_FUNCTION, __LINE__, __FIRST_ARG__(__VA_ARGS__), \ + __make_args_checked__(__VA_ARGS__)); \ } while (0) -#define atFatal(fmt, ...) \ +#define atFatal(...) \ do { \ atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ if (__handler) \ - __handler(athena::error::Level::Fatal, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \ + __handler(athena::error::Level::Fatal, __FILE__, AT_PRETTY_FUNCTION, __LINE__, __FIRST_ARG__(__VA_ARGS__), \ + __make_args_checked__(__VA_ARGS__)); \ } while (0) -#elif defined(__GNUC__) - -#ifndef NDEBUG -#define atDebug(fmt...) \ - do { \ - atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ - if (__handler) \ - __handler(athena::error::Level::Message, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \ - } while (0) -#else // _MSC_VER -#define atDebug(fmt, ...) -#endif // NDEBUG - -#define atMessage(fmt...) \ - do { \ - atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ - if (__handler) \ - __handler(athena::error::Level::Message, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \ - } while (0) - -#define atWarning(fmt...) \ - do { \ - atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ - if (__handler) \ - __handler(athena::error::Level::Warning, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \ - } while (0) - -#define atError(fmt...) \ - do { \ - atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ - if (__handler) \ - __handler(athena::error::Level::Error, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \ - } while (0) - -#define atFatal(fmt...) \ - do { \ - atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ - if (__handler) \ - __handler(athena::error::Level::Fatal, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt); \ - } while (0) -#endif // defined(__GNUC__) diff --git a/include/athena/IStreamWriter.hpp b/include/athena/IStreamWriter.hpp index f10dd68..e10beaf 100644 --- a/include/athena/IStreamWriter.hpp +++ b/include/athena/IStreamWriter.hpp @@ -650,7 +650,7 @@ public: utf8proc_int32_t wc; utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc); if (len < 0) { - atWarning("invalid UTF-8 character while decoding"); + atWarning(fmt("invalid UTF-8 character while decoding")); return; } buf += len; @@ -664,7 +664,7 @@ public: if (*buf) { utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc); if (len < 0) { - atWarning("invalid UTF-8 character while decoding"); + atWarning(fmt("invalid UTF-8 character while decoding")); return; } buf += len; @@ -698,7 +698,7 @@ public: utf8proc_int32_t wc; utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc); if (len < 0) { - atWarning("invalid UTF-8 character while decoding"); + atWarning(fmt("invalid UTF-8 character while decoding")); return; } buf += len; @@ -712,7 +712,7 @@ public: if (*buf) { utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc); if (len < 0) { - atWarning("invalid UTF-8 character while decoding"); + atWarning(fmt("invalid UTF-8 character while decoding")); return; } buf += len; @@ -747,7 +747,7 @@ public: utf8proc_int32_t wc; utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc); if (len < 0) { - atWarning("invalid UTF-8 character while decoding"); + atWarning(fmt("invalid UTF-8 character while decoding")); return; } buf += len; @@ -761,7 +761,7 @@ public: if (*buf) { utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc); if (len < 0) { - atWarning("invalid UTF-8 character while decoding"); + atWarning(fmt("invalid UTF-8 character while decoding")); return; } buf += len; diff --git a/include/athena/Utility.hpp b/include/athena/Utility.hpp index 85bcf88..e6b83bc 100644 --- a/include/athena/Utility.hpp +++ b/include/athena/Utility.hpp @@ -10,16 +10,16 @@ #include "athena/Types.hpp" namespace athena::utility { -inline bool isEmpty(atInt8* buf, atUint32 size) { return !memcmp(buf, buf + 1, size - 1); } +inline bool isEmpty(atInt8* buf, atUint32 size) { return !std::memcmp(buf, buf + 1, size - 1); } #if _WIN32 constexpr bool isSystemBigEndian() { return false; } #else constexpr bool isSystemBigEndian() { return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__; } #endif -inline constexpr ::athena::Endian SystemEndian = isSystemBigEndian() ? Big : Little; -inline constexpr ::athena::Endian NotSystemEndian = isSystemBigEndian() ? Little : Big; +constexpr ::athena::Endian SystemEndian = isSystemBigEndian() ? Big : Little; +constexpr ::athena::Endian NotSystemEndian = isSystemBigEndian() ? Little : Big; -inline atInt16 swap16(atInt16 val) { +constexpr atInt16 swap16(atInt16 val) { #if __GNUC__ return __builtin_bswap16(val); #elif _WIN32 @@ -28,8 +28,8 @@ inline atInt16 swap16(atInt16 val) { return (val = (val << 8) | ((val >> 8) & 0xFF)); #endif } -inline atUint16 swapU16(atUint16 val) { return (atUint16)swap16(val); } -inline atInt32 swap32(atInt32 val) { +constexpr atUint16 swapU16(atUint16 val) { return (atUint16)swap16(val); } +constexpr atInt32 swap32(atInt32 val) { #if __GNUC__ return __builtin_bswap32(val); #elif _WIN32 @@ -40,8 +40,8 @@ inline atInt32 swap32(atInt32 val) { return val; #endif } -inline atUint32 swapU32(atUint32 val) { return (atUint32)swap32(val); } -inline atInt64 swap64(atInt64 val) { +constexpr atUint32 swapU32(atUint32 val) { return (atUint32)swap32(val); } +constexpr atInt64 swap64(atInt64 val) { #if __GNUC__ return __builtin_bswap64(val); #elif _WIN32 @@ -54,89 +54,89 @@ inline atInt64 swap64(atInt64 val) { (((atInt64)(val)&0x000000000000FF00ULL) << 40) | (((atInt64)(val)&0x00000000000000FFULL) << 56)))); #endif } -inline atUint64 swapU64(atUint64 val) { return (atUint64)swap64(val); } -inline float swapFloat(float val) { +constexpr atUint64 swapU64(atUint64 val) { return (atUint64)swap64(val); } +constexpr float swapFloat(float val) { union { float f; atInt32 i; } uval1 = {val}; union { atInt32 i; float f; } uval2 = {swap32(uval1.i)}; return uval2.f; } -inline double swapDouble(double val) { +constexpr double swapDouble(double val) { union { double f; atInt64 i; } uval1 = {val}; union { atInt64 i; double f; } uval2 = {swap64(uval1.i)}; return uval2.f; } -inline atInt16 LittleInt16(atInt16& val) { - if (athena::utility::isSystemBigEndian()) +constexpr atInt16 LittleInt16(atInt16& val) { + if constexpr (athena::utility::isSystemBigEndian()) val = athena::utility::swap16(val); return val; } -inline atUint16 LittleUint16(atUint16& val) { +constexpr atUint16 LittleUint16(atUint16& val) { atInt16 ret = val; LittleInt16(ret); val = ret; return val; } -inline atInt16 BigInt16(atInt16& val) { - if (!athena::utility::isSystemBigEndian()) +constexpr atInt16 BigInt16(atInt16& val) { + if constexpr (!athena::utility::isSystemBigEndian()) val = athena::utility::swap16(val); return val; } -inline atUint16 BigUint16(atUint16& val) { +constexpr atUint16 BigUint16(atUint16& val) { atInt16 ret = val; BigInt16(ret); val = ret; return val; } -inline atInt32 LittleInt32(atInt32& val) { - if (athena::utility::isSystemBigEndian()) +constexpr atInt32 LittleInt32(atInt32& val) { + if constexpr (athena::utility::isSystemBigEndian()) val = athena::utility::swap32(val); return val; } -inline atUint32 LittleUint32(atUint32& val) { +constexpr atUint32 LittleUint32(atUint32& val) { atInt32 ret = val; LittleInt32(ret); val = ret; return val; } -inline atInt32 BigInt32(atInt32& val) { - if (!athena::utility::isSystemBigEndian()) +constexpr atInt32 BigInt32(atInt32& val) { + if constexpr (!athena::utility::isSystemBigEndian()) val = athena::utility::swap32(val); return val; } -inline atUint32 BigUint32(atUint32& val) { +constexpr atUint32 BigUint32(atUint32& val) { atInt32 ret = val; BigInt32(ret); val = ret; return val; } -inline atInt64 LittleInt64(atInt64& val) { - if (athena::utility::isSystemBigEndian()) +constexpr atInt64 LittleInt64(atInt64& val) { + if constexpr (athena::utility::isSystemBigEndian()) val = athena::utility::swap64(val); return val; } -inline atUint64 LittleUint64(atUint64& val) { +constexpr atUint64 LittleUint64(atUint64& val) { atInt64 ret = val; LittleInt64(ret); val = ret; return val; } -inline atInt64 BigInt64(atInt64& val) { - if (!athena::utility::isSystemBigEndian()) +constexpr atInt64 BigInt64(atInt64& val) { + if constexpr (!athena::utility::isSystemBigEndian()) val = athena::utility::swap64(val); return val; } -inline atUint64 BigUint64(atUint64& val) { +constexpr atUint64 BigUint64(atUint64& val) { atInt64 ret = val; BigInt64(ret); val = ret; @@ -144,27 +144,27 @@ inline atUint64 BigUint64(atUint64& val) { return val; } -inline float LittleFloat(float val) { - if (athena::utility::isSystemBigEndian()) - val = athena::utility::swapFloat(val); +constexpr float LittleFloat(float val) { + if constexpr (athena::utility::isSystemBigEndian()) + return athena::utility::swapFloat(val); return val; } -inline float BigFloat(float val) { - if (!athena::utility::isSystemBigEndian()) - val = athena::utility::swapFloat(val); +constexpr float BigFloat(float val) { + if constexpr (!athena::utility::isSystemBigEndian()) + return athena::utility::swapFloat(val); return val; } -inline double LittleDouble(double val) { - if (athena::utility::isSystemBigEndian()) - val = athena::utility::swapDouble(val); +constexpr double LittleDouble(double val) { + if constexpr (athena::utility::isSystemBigEndian()) + return athena::utility::swapDouble(val); return val; } -inline double BigDouble(double val) { - if (!athena::utility::isSystemBigEndian()) - val = athena::utility::swapDouble(val); +constexpr double BigDouble(double val) { + if constexpr (!athena::utility::isSystemBigEndian()) + return athena::utility::swapDouble(val); return val; } @@ -175,8 +175,6 @@ atUint64 rand64(); std::string join(const std::vector& elems, std::string_view delims); void tolower(std::string& str); void toupper(std::string& str); -std::string vsprintf(const char* fmt, va_list list); -std::string sprintf(const char* fmt, ...); bool parseBool(std::string_view boolean, bool* valid = NULL); int countChar(std::string_view str, const char chr, int* lastOccur = NULL); diff --git a/src/aes.cpp b/src/aes.cpp index e70caad..122efbe 100644 --- a/src/aes.cpp +++ b/src/aes.cpp @@ -25,12 +25,12 @@ namespace athena { static const uint8_t InCo[4] = {0xB, 0xD, 0x9, 0xE}; /* Inverse Coefficients */ -static inline uint32_t pack(const uint8_t* b) { +static uint32_t pack(const uint8_t* b) { /* pack bytes into a 32-bit Word */ return ((uint32_t)b[3] << 24) | ((uint32_t)b[2] << 16) | ((uint32_t)b[1] << 8) | (uint32_t)b[0]; } -static inline void unpack(uint32_t a, uint8_t* b) { +static void unpack(uint32_t a, uint8_t* b) { /* unpack bytes from a word */ b[0] = (uint8_t)a; b[1] = (uint8_t)(a >> 8); @@ -38,7 +38,7 @@ static inline void unpack(uint32_t a, uint8_t* b) { b[3] = (uint8_t)(a >> 24); } -static inline uint8_t xtime(uint8_t a) { return ((a << 1) ^ (((a >> 7) & 1) * 0x11B)); } +constexpr uint8_t xtime(uint8_t a) { return ((a << 1) ^ (((a >> 7) & 1) * 0x11B)); } static const struct SoftwareAESTables { uint8_t fbsub[256]; diff --git a/src/athena/ALTTPFileReader.cpp b/src/athena/ALTTPFileReader.cpp index a229848..feeea49 100644 --- a/src/athena/ALTTPFileReader.cpp +++ b/src/athena/ALTTPFileReader.cpp @@ -211,7 +211,7 @@ ALTTPDungeonItemFlags ALTTPFileReader::readDungeonFlags() { flags.HyruleCastle = (flagsByte >> 6) & 1; flags.SewerPassage = (flagsByte >> 7) & 1; - atDebug("%x %x", flags.flags1, flags.flags2); + atDebug("{:x} {:x}", flags.flags1, flags.flags2); return flags; } diff --git a/src/athena/DNAYaml.cpp b/src/athena/DNAYaml.cpp index 7c2f7d7..705ee48 100644 --- a/src/athena/DNAYaml.cpp +++ b/src/athena/DNAYaml.cpp @@ -26,10 +26,8 @@ atInt8 NodeToVal(const YAMLNode* node) { } std::unique_ptr ValToNode(atInt8 val) { - char str[32]; - snprintf(str, 32, "%d", int(val)); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); - ret->m_scalarString = str; + ret->m_scalarString = fmt::format(fmt("{}"), int(val)); return std::unique_ptr(ret); } @@ -39,10 +37,8 @@ atUint8 NodeToVal(const YAMLNode* node) { } std::unique_ptr ValToNode(atUint8 val) { - char str[32]; - snprintf(str, 32, "0x%02X", val); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); - ret->m_scalarString = str; + ret->m_scalarString = fmt::format(fmt("0x{:02X}"), val); return std::unique_ptr(ret); } @@ -52,10 +48,8 @@ atInt16 NodeToVal(const YAMLNode* node) { } std::unique_ptr ValToNode(atInt16 val) { - char str[32]; - snprintf(str, 32, "%d", int(val)); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); - ret->m_scalarString = str; + ret->m_scalarString = fmt::format(fmt("{}"), int(val)); return std::unique_ptr(ret); } @@ -65,10 +59,8 @@ atUint16 NodeToVal(const YAMLNode* node) { } std::unique_ptr ValToNode(atUint16 val) { - char str[32]; - snprintf(str, 32, "0x%04X", val); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); - ret->m_scalarString = str; + ret->m_scalarString = fmt::format(fmt("0x{:04X}"), val); return std::unique_ptr(ret); } @@ -78,10 +70,8 @@ atInt32 NodeToVal(const YAMLNode* node) { } std::unique_ptr ValToNode(atInt32 val) { - char str[32]; - snprintf(str, 32, "%d", int(val)); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); - ret->m_scalarString = str; + ret->m_scalarString = fmt::format(fmt("{}"), int(val)); return std::unique_ptr(ret); } @@ -91,10 +81,8 @@ atUint32 NodeToVal(const YAMLNode* node) { } std::unique_ptr ValToNode(atUint32 val) { - char str[32]; - snprintf(str, 32, "0x%08X", val); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); - ret->m_scalarString = str; + ret->m_scalarString = fmt::format(fmt("0x{:08X}"), val); return std::unique_ptr(ret); } @@ -108,10 +96,8 @@ atInt64 NodeToVal(const YAMLNode* node) { } std::unique_ptr ValToNode(atInt64 val) { - char str[32]; - snprintf(str, 32, "%" PRId64, val); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); - ret->m_scalarString = str; + ret->m_scalarString = fmt::format(fmt("{}"), val); return std::unique_ptr(ret); } @@ -125,10 +111,8 @@ atUint64 NodeToVal(const YAMLNode* node) { } std::unique_ptr ValToNode(atUint64 val) { - char str[32]; - snprintf(str, 32, "0x%016" PRIX64, val); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); - ret->m_scalarString = str; + ret->m_scalarString = fmt::format(fmt("0x{:016X}"), val); return std::unique_ptr(ret); } @@ -138,10 +122,8 @@ float NodeToVal(const YAMLNode* node) { } std::unique_ptr ValToNode(float val) { - char str[64]; - snprintf(str, 64, "%f", val); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); - ret->m_scalarString = str; + ret->m_scalarString = fmt::format(fmt("{}"), val); return std::unique_ptr(ret); } @@ -151,10 +133,8 @@ double NodeToVal(const YAMLNode* node) { } std::unique_ptr ValToNode(double val) { - char str[64]; - snprintf(str, 64, "%f", val); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); - ret->m_scalarString = str; + ret->m_scalarString = fmt::format(fmt("{}"), val); return std::unique_ptr(ret); } @@ -189,10 +169,8 @@ std::unique_ptr ValToNode(const atVec2f& val) { ret->m_seqChildren.reserve(2); simd_floats f(val.simd); for (size_t i = 0; i < 2; ++i) { - char str[64]; - snprintf(str, 64, "%f", f[i]); YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); - comp->m_scalarString = str; + comp->m_scalarString = fmt::format(fmt("{}"), f[i]); ret->m_seqChildren.emplace_back(comp); } return std::unique_ptr(ret); @@ -208,10 +186,8 @@ std::unique_ptr ValToNode(const atVec3f& val) { ret->m_seqChildren.reserve(3); simd_floats f(val.simd); for (size_t i = 0; i < 3; ++i) { - char str[64]; - snprintf(str, 64, "%f", f[i]); YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); - comp->m_scalarString = str; + comp->m_scalarString = fmt::format(fmt("{}"), f[i]); ret->m_seqChildren.emplace_back(comp); } return std::unique_ptr(ret); @@ -227,10 +203,8 @@ std::unique_ptr ValToNode(const atVec4f& val) { ret->m_seqChildren.reserve(4); simd_floats f(val.simd); for (size_t i = 0; i < 4; ++i) { - char str[64]; - snprintf(str, 64, "%f", f[i]); YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); - comp->m_scalarString = str; + comp->m_scalarString = fmt::format(fmt("{}"), f[i]); ret->m_seqChildren.emplace_back(comp); } return std::unique_ptr(ret); @@ -246,10 +220,8 @@ std::unique_ptr ValToNode(const atVec2d& val) { ret->m_seqChildren.reserve(2); simd_doubles f(val.simd); for (size_t i = 0; i < 2; ++i) { - char str[64]; - snprintf(str, 64, "%f", f[i]); YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); - comp->m_scalarString = str; + comp->m_scalarString = fmt::format(fmt("{}"), f[i]); ret->m_seqChildren.emplace_back(comp); } return std::unique_ptr(ret); @@ -265,10 +237,8 @@ std::unique_ptr ValToNode(const atVec3d& val) { ret->m_seqChildren.reserve(3); simd_doubles f(val.simd); for (size_t i = 0; i < 3; ++i) { - char str[64]; - snprintf(str, 64, "%f", f[i]); YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); - comp->m_scalarString = str; + comp->m_scalarString = fmt::format(fmt("{}"), f[i]); ret->m_seqChildren.emplace_back(comp); } return std::unique_ptr(ret); @@ -284,10 +254,8 @@ std::unique_ptr ValToNode(const atVec4d& val) { ret->m_seqChildren.reserve(4); simd_doubles f(val.simd); for (size_t i = 0; i < 4; ++i) { - char str[64]; - snprintf(str, 64, "%f", f[i]); YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); - comp->m_scalarString = str; + comp->m_scalarString = fmt::format(fmt("{}"), f[i]); ret->m_seqChildren.emplace_back(comp); } return std::unique_ptr(ret); @@ -325,7 +293,7 @@ std::wstring NodeToVal(const YAMLNode* node) { utf8proc_int32_t wc; utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc); if (len < 0) { - atWarning("invalid UTF-8 character while decoding"); + atWarning(fmt("invalid UTF-8 character while decoding")); return retval; } buf += len; @@ -341,7 +309,7 @@ std::unique_ptr ValToNode(std::wstring_view val) { utf8proc_uint8_t mb[4]; utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb); if (c < 0) { - atWarning("invalid UTF-8 character while encoding"); + atWarning(fmt("invalid UTF-8 character while encoding")); return std::unique_ptr(ret); } ret->m_scalarString.append(reinterpret_cast(mb), c); @@ -356,7 +324,7 @@ std::unique_ptr ValToNode(std::u16string_view val) { utf8proc_uint8_t mb[4]; utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb); if (c < 0) { - atWarning("invalid UTF-8 character while encoding"); + atWarning(fmt("invalid UTF-8 character while encoding")); return std::unique_ptr(ret); } ret->m_scalarString.append(reinterpret_cast(mb), c); @@ -371,7 +339,7 @@ std::unique_ptr ValToNode(std::u32string_view val) { utf8proc_uint8_t mb[4]; utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb); if (c < 0) { - atWarning("invalid UTF-8 character while encoding"); + atWarning(fmt("invalid UTF-8 character while encoding")); return std::unique_ptr(ret); } ret->m_scalarString.append(reinterpret_cast(mb), c); @@ -402,11 +370,11 @@ static const char* ErrorString(yaml_error_type_t errt) { } void HandleYAMLParserError(yaml_parser_t* parser) { - atError("YAML error: %s: %s", ErrorString(parser->error), parser->problem ? parser->problem : ""); + atError(fmt("YAML error: {}: {}"), ErrorString(parser->error), parser->problem ? parser->problem : ""); } void HandleYAMLEmitterError(yaml_emitter_t* emitter) { - atError("YAML error: %s: %s", ErrorString(emitter->error), emitter->problem ? emitter->problem : ""); + atError(fmt("YAML error: {}: {}"), ErrorString(emitter->error), emitter->problem ? emitter->problem : ""); } int YAMLStdStringReader(YAMLStdStringViewReaderState* reader, unsigned char* buffer, size_t size, size_t* size_read) { @@ -629,8 +597,8 @@ void YAMLDocWriter::writeU32String(const char* name, std::u32string_view val) { void YAMLDocWriter::setStyle(YAMLNodeStyle s) { m_subStack.back()->m_style = s; } -static inline void InsertNode(std::vector& nodeStack, std::unique_ptr& mapKey, - std::unique_ptr& retVal, std::unique_ptr&& newNode) { +static void InsertNode(std::vector& nodeStack, std::unique_ptr& mapKey, + std::unique_ptr& retVal, std::unique_ptr&& newNode) { if (nodeStack.empty()) { retVal = std::move(newNode); return; @@ -670,7 +638,7 @@ std::unique_ptr YAMLDocReader::ParseEvents(athena::io::IStreamReader* switch (event.type) { case YAML_SCALAR_EVENT: { if (nodeStack.empty()) { - atWarning("YAML parser stack empty; skipping scalar node"); + atWarning(fmt("YAML parser stack empty; skipping scalar node")); break; } std::unique_ptr newScalar(new YAMLNode(YAML_SCALAR_NODE)); @@ -805,7 +773,7 @@ YAMLDocReader::RecordRAII YAMLDocReader::enterSubRecord(const char* name) { m_seqTrackerStack.push_back(0); return RecordRAII{this}; } else if (!name) { - atError("Expected YAML sequence"); + atError(fmt("Expected YAML sequence")); } for (const auto& item : curSub->m_mapChildren) { if (!item.first.compare(name)) { @@ -840,7 +808,7 @@ YAMLDocReader::VectorRAII YAMLDocReader::enterSubVector(const char* name, size_t if (nextSub->m_type == YAML_SEQUENCE_NODE) { countOut = nextSub->m_seqChildren.size(); } else { - atError("'%s' is not a vector field", name); + atError(fmt("'{}' is not a vector field"), name); countOut = 0; } m_subStack.push_back(nextSub); @@ -878,7 +846,7 @@ RETURNTYPE YAMLDocReader::readVal(const char* name) { } } if (name) - atWarning("Unable to find field '%s'; returning 0", name); + atWarning(fmt("Unable to find field '{}'; returning 0"), name); return RETURNTYPE(); } @@ -935,7 +903,7 @@ std::string YAMLDocReader::readString(const char* name) { return readVal(name); } -static inline bool EmitKeyScalar(yaml_emitter_t* doc, const char* val) { +static bool EmitKeyScalar(yaml_emitter_t* doc, const char* val) { yaml_event_t event; if (!yaml_scalar_event_initialize(&event, nullptr, nullptr, (yaml_char_t*)val, strlen(val), true, true, YAML_PLAIN_SCALAR_STYLE)) @@ -943,14 +911,14 @@ static inline bool EmitKeyScalar(yaml_emitter_t* doc, const char* val) { return yaml_emitter_emit(doc, &event) != 0; } -static inline yaml_scalar_style_t ScalarStyle(const YAMLNode& node) { +static yaml_scalar_style_t ScalarStyle(const YAMLNode& node) { for (const auto& ch : node.m_scalarString) if (ch == '\n') return YAML_LITERAL_SCALAR_STYLE; return YAML_ANY_SCALAR_STYLE; } -static inline yaml_sequence_style_t SequenceStyle(const YAMLNode& node) { +static yaml_sequence_style_t SequenceStyle(const YAMLNode& node) { if (node.m_style == YAMLNodeStyle::Flow) return YAML_FLOW_SEQUENCE_STYLE; else if (node.m_style == YAMLNodeStyle::Block) @@ -969,7 +937,7 @@ static inline yaml_sequence_style_t SequenceStyle(const YAMLNode& node) { return (count > 6) ? YAML_BLOCK_SEQUENCE_STYLE : YAML_FLOW_SEQUENCE_STYLE; } -static inline yaml_mapping_style_t MappingStyle(const YAMLNode& node) { +static yaml_mapping_style_t MappingStyle(const YAMLNode& node) { if (node.m_style == YAMLNodeStyle::Flow) return YAML_FLOW_MAPPING_STYLE; else if (node.m_style == YAMLNodeStyle::Block) @@ -1030,7 +998,7 @@ static const std::string base64_chars = "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; -static inline bool is_base64(unsigned char c) { return (isalnum(c) || (c == '+') || (c == '/')); } +inline bool is_base64(unsigned char c) { return (isalnum(c) || (c == '+') || (c == '/')); } std::string base64_encode(const atUint8* bytes_to_encode, size_t in_len) { std::string ret; diff --git a/src/athena/Dir.cpp b/src/athena/Dir.cpp index 0d432cf..6f6a02c 100644 --- a/src/athena/Dir.cpp +++ b/src/athena/Dir.cpp @@ -47,7 +47,7 @@ bool Dir::rm(std::string_view path) { return !(remove((m_path + "/" + path.data( bool Dir::touch() { srand(time(NULL)); atUint64 tmp = utility::rand64(); - std::string tmpFile = utility::sprintf("%" PRIX64 ".tmp", tmp); + std::string tmpFile = fmt::format(fmt("{:016X}.tmp"), tmp); bool ret = FileInfo(m_path + "/" + tmpFile).touch(); if (ret) return rm(tmpFile); diff --git a/src/athena/FileReader.cpp b/src/athena/FileReader.cpp index 9922154..7a887e3 100644 --- a/src/athena/FileReader.cpp +++ b/src/athena/FileReader.cpp @@ -33,7 +33,7 @@ void FileReader::open() { if (!m_fileHandle) { std::string _filename = filename(); if (m_globalErr) - atError("File not found '%s'", _filename.c_str()); + atError(fmt("File not found '{}'"), _filename); setError(); return; } @@ -45,7 +45,7 @@ void FileReader::open() { void FileReader::close() { if (!m_fileHandle) { if (m_globalErr) - atError("Cannot close an unopened stream"); + atError(fmt("Cannot close an unopened stream")); setError(); return; } @@ -74,7 +74,7 @@ void FileReader::seek(atInt64 pos, SeekOrigin origin) { } if (m_offset > length()) { if (m_globalErr) - atError("Unable to seek in file"); + atError(fmt("Unable to seek in file")); setError(); return; } @@ -87,7 +87,7 @@ void FileReader::seek(atInt64 pos, SeekOrigin origin) { } } else if (fseeko64(m_fileHandle, pos, (int)origin) != 0) { if (m_globalErr) - atError("Unable to seek in file"); + atError(fmt("Unable to seek in file")); setError(); } } @@ -95,7 +95,7 @@ void FileReader::seek(atInt64 pos, SeekOrigin origin) { atUint64 FileReader::position() const { if (!isOpen()) { if (m_globalErr) - atError("File not open"); + atError(fmt("File not open")); return 0; } @@ -108,7 +108,7 @@ atUint64 FileReader::position() const { atUint64 FileReader::length() const { if (!isOpen()) { if (m_globalErr) - atError("File not open"); + atError(fmt("File not open")); return 0; } @@ -118,7 +118,7 @@ atUint64 FileReader::length() const { atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len) { if (!isOpen()) { if (m_globalErr) - atError("File not open for reading"); + atError(fmt("File not open for reading")); setError(); return 0; } diff --git a/src/athena/FileReaderWin32.cpp b/src/athena/FileReaderWin32.cpp index fd71e83..9af417d 100644 --- a/src/athena/FileReaderWin32.cpp +++ b/src/athena/FileReaderWin32.cpp @@ -36,7 +36,7 @@ void FileReader::open() { m_fileHandle = 0; std::string _filename = filename(); if (m_globalErr) - atError("File not found '%s'", _filename.c_str()); + atError("File not found '{}'", _filename); setError(); return; } diff --git a/src/athena/FileWriterNix.cpp b/src/athena/FileWriterNix.cpp index a1ca793..89b1bfb 100644 --- a/src/athena/FileWriterNix.cpp +++ b/src/athena/FileWriterNix.cpp @@ -49,7 +49,7 @@ void FileWriter::open(bool overwrite) { if (!m_fileHandle) { if (m_globalErr) - atError("Unable to open file '%s'", filename().c_str()); + atError(fmt("Unable to open file '%s'"), filename().c_str()); setError(); return; } @@ -61,7 +61,7 @@ void FileWriter::open(bool overwrite) { void FileWriter::close() { if (!m_fileHandle) { if (m_globalErr) - atError("Cannot close an unopened stream"); + atError(fmt("Cannot close an unopened stream")); setError(); return; } @@ -81,14 +81,14 @@ void FileWriter::close() { void FileWriter::seek(atInt64 pos, SeekOrigin origin) { if (!isOpen()) { if (m_globalErr) - atError("Unable to seek in file, not open"); + atError(fmt("Unable to seek in file, not open")); setError(); return; } if (fseeko64(m_fileHandle, pos, (int)origin) != 0) { if (m_globalErr) - atError("Unable to seek in file"); + atError(fmt("Unable to seek in file")); setError(); } } @@ -100,14 +100,14 @@ atUint64 FileWriter::length() const { return utility::fileSize(m_filename); } void FileWriter::writeUBytes(const atUint8* data, atUint64 len) { if (!isOpen()) { if (m_globalErr) - atError("File not open for writing"); + atError(fmt("File not open for writing")); setError(); return; } if (fwrite(data, 1, len, m_fileHandle) != len) { if (m_globalErr) - atError("Unable to write to stream"); + atError(fmt("Unable to write to stream")); setError(); } } diff --git a/src/athena/FileWriterWin32.cpp b/src/athena/FileWriterWin32.cpp index de66e56..98dcf36 100644 --- a/src/athena/FileWriterWin32.cpp +++ b/src/athena/FileWriterWin32.cpp @@ -43,7 +43,7 @@ void FileWriter::open(bool overwrite) { if (m_fileHandle == INVALID_HANDLE_VALUE) { m_fileHandle = 0; if (m_globalErr) - atError("Unable to open file '%s'", filename().c_str()); + atError("Unable to open file '{}'", filename()); setError(); return; } diff --git a/src/athena/Global.cpp b/src/athena/Global.cpp index afecbf3..ca82e30 100644 --- a/src/athena/Global.cpp +++ b/src/athena/Global.cpp @@ -1,9 +1,13 @@ #include "athena/Global.hpp" #include "athena/Utility.hpp" #include -#include #include +#define FMT_STRING_ALIAS 1 +#define FMT_ENFORCE_COMPILE_STRING 1 +#define FMT_USE_GRISU 0 +#include + std::ostream& operator<<(std::ostream& os, const athena::SeekOrigin& origin) { switch (origin) { case athena::SeekOrigin::Begin: @@ -37,7 +41,7 @@ std::ostream& operator<<(std::ostream& os, const athena::Endian& endian) { } static void __defaultExceptionHandler(athena::error::Level level, const char* file, const char* function, int line, - const char* fmt, ...) { + fmt::string_view fmt, fmt::format_args args) { std::string levelStr; switch (level) { case athena::error::Level::Warning: @@ -53,10 +57,7 @@ static void __defaultExceptionHandler(athena::error::Level level, const char* fi break; } - va_list vl; - va_start(vl, fmt); - std::string msg = athena::utility::vsprintf(fmt, vl); - va_end(vl); + std::string msg = fmt::internal::vformat(fmt, args); std::cerr << levelStr << " " << file << " " << function << "(" << line << "): " << msg << std::endl; } diff --git a/src/athena/MemoryReader.cpp b/src/athena/MemoryReader.cpp index d7e57ef..ba45b41 100644 --- a/src/athena/MemoryReader.cpp +++ b/src/athena/MemoryReader.cpp @@ -19,7 +19,7 @@ MemoryReader::MemoryReader(const void* data, atUint64 length, bool takeOwnership : m_data(data), m_length(length), m_position(0), m_owns(takeOwnership), m_globalErr(globalErr) { if (!data) { if (m_globalErr) - atError("data cannot be NULL"); + atError(fmt("data cannot be NULL")); setError(); return; } @@ -33,7 +33,7 @@ MemoryReader::~MemoryReader() { MemoryCopyReader::MemoryCopyReader(const void* data, atUint64 length) : MemoryReader(data, length, false) { if (!data) { if (m_globalErr) - atError("data cannot be NULL"); + atError(fmt("data cannot be NULL")); setError(); return; } @@ -48,7 +48,7 @@ void MemoryReader::seek(atInt64 position, SeekOrigin origin) { case SeekOrigin::Begin: if ((position < 0 || (atInt64)position > (atInt64)m_length)) { if (m_globalErr) - atFatal("Position %0.8X outside stream bounds ", position); + atFatal(fmt("Position {:08X} outside stream bounds "), position); m_position = m_length; setError(); return; @@ -60,7 +60,7 @@ void MemoryReader::seek(atInt64 position, SeekOrigin origin) { case SeekOrigin::Current: if ((((atInt64)m_position + position) < 0 || (m_position + position) > m_length)) { if (m_globalErr) - atFatal("Position %0.8X outside stream bounds ", position); + atFatal(fmt("Position {:08X} outside stream bounds "), position); m_position = m_length; setError(); return; @@ -72,7 +72,7 @@ void MemoryReader::seek(atInt64 position, SeekOrigin origin) { case SeekOrigin::End: if ((((atInt64)m_length - position < 0) || (m_length - position) > m_length)) { if (m_globalErr) - atFatal("Position %0.8X outside stream bounds ", position); + atFatal(fmt("Position {:08X} outside stream bounds "), position); m_position = m_length; setError(); return; @@ -110,7 +110,7 @@ atUint8* MemoryReader::data() const { atUint64 MemoryReader::readUBytesToBuf(void* buf, atUint64 length) { if (m_position >= m_length) { if (m_globalErr) - atFatal("Position %0.8X outside stream bounds ", m_position); + atFatal(fmt("Position {:08X} outside stream bounds "), m_position); m_position = m_length; setError(); return 0; @@ -129,7 +129,7 @@ void MemoryCopyReader::loadData() { if (!in) { if (m_globalErr) - atError("Unable to open file '%s'", m_filepath.c_str()); + atError(fmt("Unable to open file '%s'"), m_filepath); setError(); return; } @@ -151,7 +151,7 @@ void MemoryCopyReader::loadData() { if (ret < 0) { if (m_globalErr) - atError("Error reading data from disk"); + atError(fmt("Error reading data from disk")); setError(); return; } else if (ret == 0) diff --git a/src/athena/MemoryWriter.cpp b/src/athena/MemoryWriter.cpp index 36876ac..15f4216 100644 --- a/src/athena/MemoryWriter.cpp +++ b/src/athena/MemoryWriter.cpp @@ -14,7 +14,7 @@ namespace athena::io { MemoryWriter::MemoryWriter(atUint8* data, atUint64 length, bool takeOwnership) : m_data((atUint8*)data), m_length(length), m_position(0), m_bufferOwned(takeOwnership) { if (!data) { - atError("data cannot be NULL"); + atError(fmt("data cannot be NULL")); setError(); return; } @@ -34,7 +34,7 @@ MemoryCopyWriter::MemoryCopyWriter(atUint8* data, atUint64 length) { m_bufferOwned = false; if (length == 0) { - atError("length cannot be 0"); + atError(fmt("length cannot be 0")); setError(); return; } @@ -53,7 +53,7 @@ MemoryCopyWriter::MemoryCopyWriter(std::string_view filename) { m_bufferOwned = false; if (!m_data) { - atError("Could not allocate memory!"); + atError(fmt("Could not allocate memory!")); setError(); return; } @@ -63,13 +63,13 @@ void MemoryWriter::seek(atInt64 position, SeekOrigin origin) { switch (origin) { case SeekOrigin::Begin: if (position < 0) { - atError("Position outside stream bounds"); + atError(fmt("Position outside stream bounds")); setError(); return; } if ((atUint64)position > m_length) { - atError("data exceeds available buffer space"); + atError(fmt("data exceeds available buffer space")); setError(); return; } @@ -79,13 +79,13 @@ void MemoryWriter::seek(atInt64 position, SeekOrigin origin) { case SeekOrigin::Current: if ((((atInt64)m_position + position) < 0)) { - atError("Position outside stream bounds"); + atError(fmt("Position outside stream bounds")); setError(); return; } if (m_position + position > m_length) { - atError("data exceeds available buffer space"); + atError(fmt("data exceeds available buffer space")); setError(); return; } @@ -95,13 +95,13 @@ void MemoryWriter::seek(atInt64 position, SeekOrigin origin) { case SeekOrigin::End: if (((atInt64)m_length - position) < 0) { - atError("Position outside stream bounds"); + atError(fmt("Position outside stream bounds")); setError(); return; } if ((atUint64)position > m_length) { - atError("data exceeds available buffer space"); + atError(fmt("data exceeds available buffer space")); setError(); return; } @@ -115,7 +115,7 @@ void MemoryCopyWriter::seek(atInt64 position, SeekOrigin origin) { switch (origin) { case SeekOrigin::Begin: if (position < 0) { - atError("Position outside stream bounds"); + atError(fmt("Position outside stream bounds")); setError(); return; } @@ -128,7 +128,7 @@ void MemoryCopyWriter::seek(atInt64 position, SeekOrigin origin) { case SeekOrigin::Current: if ((((atInt64)m_position + position) < 0)) { - atError("Position outside stream bounds"); + atError(fmt("Position outside stream bounds")); setError(); return; } @@ -141,7 +141,7 @@ void MemoryCopyWriter::seek(atInt64 position, SeekOrigin origin) { case SeekOrigin::End: if (((atInt64)m_length - position) < 0) { - atError("Position outside stream bounds"); + atError(fmt("Position outside stream bounds")); setError(); return; } @@ -182,7 +182,7 @@ atUint8* MemoryWriter::data() const { void MemoryWriter::save(std::string_view filename) { if (filename.empty() && m_filepath.empty()) { - atError("No file specified, cannot save."); + atError(fmt("No file specified, cannot save.")); setError(); return; } @@ -193,7 +193,7 @@ void MemoryWriter::save(std::string_view filename) { FILE* out = fopen(m_filepath.c_str(), "wb"); if (!out) { - atError("Unable to open file '%s'", m_filepath.c_str()); + atError(fmt("Unable to open file '{}'"), m_filepath); setError(); return; } @@ -208,7 +208,7 @@ void MemoryWriter::save(std::string_view filename) { atInt64 ret = fwrite(m_data + done, 1, blocksize, out); if (ret < 0) { - atError("Error writing data to disk"); + atError(fmt("Error writing data to disk")); setError(); return; } else if (ret == 0) @@ -222,13 +222,13 @@ void MemoryWriter::save(std::string_view filename) { void MemoryWriter::writeUBytes(const atUint8* data, atUint64 length) { if (!data) { - atError("data cannnot be NULL"); + atError(fmt("data cannnot be NULL")); setError(); return; } if (m_position + length > m_length) { - atError("data length exceeds available buffer space"); + atError(fmt("data length exceeds available buffer space")); setError(); return; } @@ -240,7 +240,7 @@ void MemoryWriter::writeUBytes(const atUint8* data, atUint64 length) { void MemoryCopyWriter::writeUBytes(const atUint8* data, atUint64 length) { if (!data) { - atError("data cannnot be NULL"); + atError(fmt("data cannnot be NULL")); setError(); return; } @@ -255,7 +255,7 @@ void MemoryCopyWriter::writeUBytes(const atUint8* data, atUint64 length) { void MemoryCopyWriter::resize(atUint64 newSize) { if (newSize < m_length) { - atError("New size cannot be less to the old size."); + atError(fmt("New size cannot be less to the old size.")); return; } diff --git a/src/athena/Socket.cpp b/src/athena/Socket.cpp index 4add125..9b2662e 100644 --- a/src/athena/Socket.cpp +++ b/src/athena/Socket.cpp @@ -80,7 +80,7 @@ bool Socket::openSocket() { m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (m_socket == -1) { - atError("Can't allocate socket"); + atError(fmt("Can't allocate socket")); return false; } @@ -134,13 +134,13 @@ bool Socket::openAndListen(const IPAddress& address, uint32_t port) { sockaddr_in addr = createAddress(address.toInteger(), port); if (bind(m_socket, reinterpret_cast(&addr), sizeof(addr)) == -1) { /* Not likely to happen, but... */ - atError("Failed to bind listener socket to port %d", port); + atError(fmt("Failed to bind listener socket to port {}"), port); return false; } if (::listen(m_socket, 0) == -1) { /* Oops, socket is deaf */ - atError("Failed to listen to port %d", port); + atError(fmt("Failed to listen to port {}"), port); return false; } @@ -160,7 +160,7 @@ Socket::EResult Socket::accept(Socket& remoteSocketOut, sockaddr_in& fromAddress #ifndef _WIN32 EResult res = (errno == EAGAIN) ? EResult::Busy : EResult::Error; if (res == EResult::Error) - atError("Failed to accept incoming connection: %s", strerror(errno)); + atError(fmt("Failed to accept incoming connection: {}"), strerror(errno)); #else EResult res = LastWSAError(); if (res == EResult::Error) diff --git a/src/athena/Utility.cpp b/src/athena/Utility.cpp index f51f8ea..da36334 100644 --- a/src/athena/Utility.cpp +++ b/src/athena/Utility.cpp @@ -63,32 +63,6 @@ void tolower(std::string& str) { std::transform(str.begin(), str.end(), str.begi void toupper(std::string& str) { std::transform(str.begin(), str.end(), str.begin(), ::toupper); } -std::string vsprintf(const char* fmt, va_list list) { - int size = 512; - char* buffer = 0; - buffer = new char[size]; - int nsize = ::vsprintf(buffer, fmt, list); - - while (size <= nsize) { - // fail delete buffer and try again - delete[] buffer; - buffer = 0; - buffer = new char[nsize + 1]; //+1 for /0 - nsize = ::vsprintf(buffer, fmt, list); - } - - std::string ret(buffer); - delete[] buffer; - return ret; -} -std::string sprintf(const char* fmt, ...) { - va_list vl; - va_start(vl, fmt); - std::string ret = vsprintf(fmt, vl); - va_end(vl); - return ret; -} - bool parseBool(std::string_view boolean, bool* valid) { std::string val(boolean); // compare must be case insensitive @@ -192,7 +166,7 @@ std::string wideToUtf8(std::wstring_view src) { utf8proc_uint8_t mb[4]; utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb); if (c < 0) { - atWarning("invalid UTF-8 character while encoding"); + atWarning(fmt("invalid UTF-8 character while encoding")); return retval; } retval.append(reinterpret_cast(mb), c); @@ -208,7 +182,7 @@ std::wstring utf8ToWide(std::string_view src) { utf8proc_int32_t wc; utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc); if (len < 0) { - atWarning("invalid UTF-8 character while decoding"); + atWarning(fmt("invalid UTF-8 character while decoding")); return retval; } buf += len; diff --git a/src/athena/VectorWriter.cpp b/src/athena/VectorWriter.cpp index 7822e04..2e36c9d 100644 --- a/src/athena/VectorWriter.cpp +++ b/src/athena/VectorWriter.cpp @@ -12,7 +12,7 @@ void VectorWriter::seek(atInt64 position, SeekOrigin origin) { switch (origin) { case SeekOrigin::Begin: if (position < 0) { - atError("Position outside stream bounds"); + atError(fmt("Position outside stream bounds")); setError(); return; } @@ -25,7 +25,7 @@ void VectorWriter::seek(atInt64 position, SeekOrigin origin) { case SeekOrigin::Current: if ((((atInt64)m_position + position) < 0)) { - atError("Position outside stream bounds"); + atError(fmt("Position outside stream bounds")); setError(); return; } @@ -38,13 +38,13 @@ void VectorWriter::seek(atInt64 position, SeekOrigin origin) { case SeekOrigin::End: if (((atInt64)m_data.size() - position) < 0) { - atError("Position outside stream bounds"); + atError(fmt("Position outside stream bounds")); setError(); return; } if ((atUint64)position > m_data.size()) { - atError("data exceeds vector size"); + atError(fmt("data exceeds vector size")); setError(); return; } @@ -56,7 +56,7 @@ void VectorWriter::seek(atInt64 position, SeekOrigin origin) { void VectorWriter::writeUBytes(const atUint8* data, atUint64 length) { if (!data) { - atError("data cannnot be NULL"); + atError(fmt("data cannnot be NULL")); setError(); return; } diff --git a/src/athena/WiiFile.cpp b/src/athena/WiiFile.cpp index a0eccfb..a83e210 100644 --- a/src/athena/WiiFile.cpp +++ b/src/athena/WiiFile.cpp @@ -78,7 +78,7 @@ bool WiiFile::isFile() const { return (m_type == WiiFile::File); } void WiiFile::addChild(WiiFile* file) { if (!isDirectory()) { - atWarning("%s is not a directory", filename().c_str()); + atWarning("{} is not a directory", filename()); return; }