Massive fmtlib refactor

This commit is contained in:
Jack Andersen 2019-07-19 18:19:44 -10:00
parent f3852eb0d2
commit 478d75f1cb
25 changed files with 204 additions and 277 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "extern/lzokay"] [submodule "extern/lzokay"]
path = extern/lzokay path = extern/lzokay
url = https://github.com/jackoalan/lzokay.git url = https://github.com/jackoalan/lzokay.git
[submodule "extern/fmt"]
path = extern/fmt
url = https://github.com/fmtlib/fmt

View File

@ -84,7 +84,7 @@ add_library(athena-core
target_include_directories(athena-core PUBLIC target_include_directories(athena-core PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${ZLIB_INCLUDE_DIR}>) $<BUILD_INTERFACE:${ZLIB_INCLUDE_DIR}>)
target_link_libraries(athena-core PUBLIC athena-libyaml) target_link_libraries(athena-core PUBLIC athena-libyaml fmt)
add_library(athena-sakura EXCLUDE_FROM_ALL add_library(athena-sakura EXCLUDE_FROM_ALL
src/athena/Sprite.cpp src/athena/Sprite.cpp
@ -191,7 +191,8 @@ foreach(p LIB INCLUDE CMAKE)
endforeach() endforeach()
# Define installs # 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 install(TARGETS athena-core
DESTINATION ${INSTALL_LIB_DIR} EXPORT AthenaTargets COMPONENT athena) DESTINATION ${INSTALL_LIB_DIR} EXPORT AthenaTargets COMPONENT athena)
if(WIN32 AND NOT CYGWIN) if(WIN32 AND NOT CYGWIN)
@ -208,7 +209,7 @@ endif()
################## ##################
# Add all targets to the build-tree export set # 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 # Export the package for use from the build-tree
# (this registers the build-tree with a global CMake-registry) # (this registers the build-tree with a global CMake-registry)

View File

@ -4,6 +4,8 @@
if(NOT CMAKE_CROSSCOMPILING) if(NOT CMAKE_CROSSCOMPILING)
string(REPLACE -stdlib=libc++ "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
get_directory_property(ATDNA_DEFINES COMPILE_DEFINITIONS) get_directory_property(ATDNA_DEFINES COMPILE_DEFINITIONS)
list(REMOVE_ITEM ATDNA_DEFINES _GLIBCXX_DEBUG=1) list(REMOVE_ITEM ATDNA_DEFINES _GLIBCXX_DEBUG=1)
set_directory_properties(PROPERTIES COMPILE_DEFINITIONS "${ATDNA_DEFINES}") set_directory_properties(PROPERTIES COMPILE_DEFINITIONS "${ATDNA_DEFINES}")

View File

@ -1,3 +1,6 @@
add_subdirectory(lzokay) add_subdirectory(lzokay)
add_subdirectory(zlib) add_subdirectory(zlib)
add_subdirectory(yaml) add_subdirectory(yaml)
if(NOT TARGET fmt)
add_subdirectory(fmt)
endif()

1
extern/fmt vendored Submodule

@ -0,0 +1 @@
Subproject commit 6bcc3fd21694b5634cc006915bd049cf460a9a8d

2
extern/lzokay vendored

@ -1 +1 @@
Subproject commit 343f9707f6ab2143d3455c93d8b7570a0ffcee97 Subproject commit 671e2b98642f95aaf29251367791f28e5597a0a7

View File

@ -9,17 +9,17 @@
namespace athena::io { namespace athena::io {
template <class T> template <class T>
static inline const char* __GetDNAName(const T& dna, typename std::enable_if_t<athena::io::__IsDNAVRecord_v<T>>* = 0) { inline const char* __GetDNAName(const T& dna, typename std::enable_if_t<athena::io::__IsDNAVRecord_v<T>>* = 0) {
return dna.DNATypeV(); return dna.DNATypeV();
} }
template <class T> template <class T>
static inline const char* __GetDNAName(const T& dna, typename std::enable_if_t<!athena::io::__IsDNAVRecord_v<T>>* = 0) { inline const char* __GetDNAName(const T& dna, typename std::enable_if_t<!athena::io::__IsDNAVRecord_v<T>>* = 0) {
return dna.DNAType(); return dna.DNAType();
} }
template <class T> template <class T>
static inline std::string ToYAMLString(const T& dna) { inline std::string ToYAMLString(const T& dna) {
YAMLDocWriter docWriter(__GetDNAName(dna)); YAMLDocWriter docWriter(__GetDNAName(dna));
std::string res; std::string res;
@ -35,7 +35,7 @@ static inline std::string ToYAMLString(const T& dna) {
} }
template <class T> template <class T>
static inline bool FromYAMLString(T& dna, std::string_view str) { inline bool FromYAMLString(T& dna, std::string_view str) {
YAMLStdStringViewReaderState reader(str); YAMLStdStringViewReaderState reader(str);
YAMLDocReader docReader; YAMLDocReader docReader;
yaml_parser_set_input(docReader.getParser(), (yaml_read_handler_t*)YAMLStdStringReader, &reader); 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 <class DNASubtype> template <class DNASubtype>
static inline bool ValidateFromYAMLString(std::string_view str) { inline bool ValidateFromYAMLString(std::string_view str) {
YAMLStdStringViewReaderState reader(str); YAMLStdStringViewReaderState reader(str);
YAMLDocReader docReader; YAMLDocReader docReader;
yaml_parser_set_input(docReader.getParser(), (yaml_read_handler_t*)YAMLStdStringReader, &reader); 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 <class T> template <class T>
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)); YAMLDocWriter docWriter(__GetDNAName(dna));
yaml_emitter_set_unicode(docWriter.getEmitter(), true); yaml_emitter_set_unicode(docWriter.getEmitter(), true);
@ -66,7 +66,7 @@ static inline bool ToYAMLStream(const T& dna, athena::io::IStreamWriter& fout) {
} }
template <class T> template <class T>
static inline bool ToYAMLStream(const T& dna, athena::io::IStreamWriter& fout, inline bool ToYAMLStream(const T& dna, athena::io::IStreamWriter& fout,
void (T::*fn)(YAMLDocWriter& out) const) { void (T::*fn)(YAMLDocWriter& out) const) {
YAMLDocWriter docWriter(__GetDNAName(dna)); YAMLDocWriter docWriter(__GetDNAName(dna));
@ -78,7 +78,7 @@ static inline bool ToYAMLStream(const T& dna, athena::io::IStreamWriter& fout,
} }
template <class T> template <class T>
static inline bool FromYAMLStream(T& dna, athena::io::IStreamReader& fin) { inline bool FromYAMLStream(T& dna, athena::io::IStreamReader& fin) {
YAMLDocReader docReader; YAMLDocReader docReader;
if (!docReader.parse(&fin)) if (!docReader.parse(&fin))
return false; return false;
@ -87,7 +87,7 @@ static inline bool FromYAMLStream(T& dna, athena::io::IStreamReader& fin) {
} }
template <class T> template <class T>
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; YAMLDocReader docReader;
if (!docReader.parse(&fin)) if (!docReader.parse(&fin))
return false; return false;
@ -96,7 +96,7 @@ static inline bool FromYAMLStream(T& dna, athena::io::IStreamReader& fin, void (
} }
template <class T, typename NameT> template <class T, typename NameT>
static inline bool MergeToYAMLFile(const T& dna, const NameT& filename) { inline bool MergeToYAMLFile(const T& dna, const NameT& filename) {
athena::io::FileReader r(filename); athena::io::FileReader r(filename);
YAMLDocWriter docWriter(__GetDNAName(dna), r.isOpen() ? &r : nullptr); YAMLDocWriter docWriter(__GetDNAName(dna), r.isOpen() ? &r : nullptr);
r.close(); r.close();
@ -109,7 +109,7 @@ static inline bool MergeToYAMLFile(const T& dna, const NameT& filename) {
} }
template <class DNASubtype> template <class DNASubtype>
static inline bool ValidateFromYAMLStream(athena::io::IStreamReader& fin) { inline bool ValidateFromYAMLStream(athena::io::IStreamReader& fin) {
YAMLDocReader reader; YAMLDocReader reader;
atUint64 pos = fin.position(); atUint64 pos = fin.position();
yaml_parser_set_input(reader.getParser(), (yaml_read_handler_t*)YAMLAthenaReader, &fin); yaml_parser_set_input(reader.getParser(), (yaml_read_handler_t*)YAMLAthenaReader, &fin);

View File

@ -3,6 +3,11 @@
#include <iostream> #include <iostream>
#include "athena/Types.hpp" #include "athena/Types.hpp"
#define FMT_STRING_ALIAS 1
#define FMT_ENFORCE_COMPILE_STRING 1
#define FMT_USE_GRISU 0
#include <fmt/format.h>
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning(disable : 4996) #pragma warning(disable : 4996)
@ -142,8 +147,8 @@ inline constexpr bool __IsDNAVRecord_v = __IsDNAVRecord<T>::value;
} // namespace io } // namespace io
} // namespace athena } // namespace athena
typedef void (*atEXCEPTION_HANDLER)(athena::error::Level level, const char* file, const char* function, int line, typedef void (*atEXCEPTION_HANDLER)(athena::error::Level level, const char* /*file*/, const char*, int /*line*/,
const char* fmt, ...); fmt::string_view fmt, fmt::format_args args);
atEXCEPTION_HANDLER atGetExceptionHandler(); 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::SeekOrigin& origin);
std::ostream& operator<<(std::ostream& os, const athena::Endian& endian); std::ostream& operator<<(std::ostream& os, const athena::Endian& endian);
#ifdef _MSC_VER template <typename First, typename... Rest>
constexpr auto __FIRST_ARG__(First first, Rest...) { return first; }
template <typename S, typename... Args>
inline auto __make_args_checked__(const S& format_str, Args&&... args) {
return fmt::internal::make_args_checked<Args...>(format_str, std::forward<Args>(args)...);
}
#ifndef NDEBUG #ifndef NDEBUG
#define atDebug(fmt, ...) \ #define atDebug(...) \
do { \ do { \
atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \ 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) } while (0)
#else #else
#define atDebug(fmt, ...) #define atDebug(...)
#endif #endif
#define atMessage(fmt, ...) \ #define atMessage(...) \
do { \ do { \
atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \ 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) } while (0)
#define atWarning(fmt, ...) \ #define atWarning(...) \
do { \ do { \
atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \ if (__handler) { \
__handler(athena::error::Level::Warning, __FILE__, AT_PRETTY_FUNCTION, __LINE__, fmt, ##__VA_ARGS__); \ __handler(athena::error::Level::Warning, __FILE__, AT_PRETTY_FUNCTION, __LINE__, __FIRST_ARG__(__VA_ARGS__), \
__make_args_checked__(__VA_ARGS__)); \
} \
} while (0) } while (0)
#define atError(fmt, ...) \ #define atError(...) \
do { \ do { \
atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \ 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) } while (0)
#define atFatal(fmt, ...) \ #define atFatal(...) \
do { \ do { \
atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \ atEXCEPTION_HANDLER __handler = atGetExceptionHandler(); \
if (__handler) \ 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) } 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__)

View File

@ -650,7 +650,7 @@ public:
utf8proc_int32_t wc; utf8proc_int32_t wc;
utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc); utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc);
if (len < 0) { if (len < 0) {
atWarning("invalid UTF-8 character while decoding"); atWarning(fmt("invalid UTF-8 character while decoding"));
return; return;
} }
buf += len; buf += len;
@ -664,7 +664,7 @@ public:
if (*buf) { if (*buf) {
utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc); utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc);
if (len < 0) { if (len < 0) {
atWarning("invalid UTF-8 character while decoding"); atWarning(fmt("invalid UTF-8 character while decoding"));
return; return;
} }
buf += len; buf += len;
@ -698,7 +698,7 @@ public:
utf8proc_int32_t wc; utf8proc_int32_t wc;
utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc); utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc);
if (len < 0) { if (len < 0) {
atWarning("invalid UTF-8 character while decoding"); atWarning(fmt("invalid UTF-8 character while decoding"));
return; return;
} }
buf += len; buf += len;
@ -712,7 +712,7 @@ public:
if (*buf) { if (*buf) {
utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc); utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc);
if (len < 0) { if (len < 0) {
atWarning("invalid UTF-8 character while decoding"); atWarning(fmt("invalid UTF-8 character while decoding"));
return; return;
} }
buf += len; buf += len;
@ -747,7 +747,7 @@ public:
utf8proc_int32_t wc; utf8proc_int32_t wc;
utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc); utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc);
if (len < 0) { if (len < 0) {
atWarning("invalid UTF-8 character while decoding"); atWarning(fmt("invalid UTF-8 character while decoding"));
return; return;
} }
buf += len; buf += len;
@ -761,7 +761,7 @@ public:
if (*buf) { if (*buf) {
utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc); utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc);
if (len < 0) { if (len < 0) {
atWarning("invalid UTF-8 character while decoding"); atWarning(fmt("invalid UTF-8 character while decoding"));
return; return;
} }
buf += len; buf += len;

View File

@ -10,16 +10,16 @@
#include "athena/Types.hpp" #include "athena/Types.hpp"
namespace athena::utility { 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 #if _WIN32
constexpr bool isSystemBigEndian() { return false; } constexpr bool isSystemBigEndian() { return false; }
#else #else
constexpr bool isSystemBigEndian() { return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__; } constexpr bool isSystemBigEndian() { return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__; }
#endif #endif
inline constexpr ::athena::Endian SystemEndian = isSystemBigEndian() ? Big : Little; constexpr ::athena::Endian SystemEndian = isSystemBigEndian() ? Big : Little;
inline constexpr ::athena::Endian NotSystemEndian = isSystemBigEndian() ? Little : Big; constexpr ::athena::Endian NotSystemEndian = isSystemBigEndian() ? Little : Big;
inline atInt16 swap16(atInt16 val) { constexpr atInt16 swap16(atInt16 val) {
#if __GNUC__ #if __GNUC__
return __builtin_bswap16(val); return __builtin_bswap16(val);
#elif _WIN32 #elif _WIN32
@ -28,8 +28,8 @@ inline atInt16 swap16(atInt16 val) {
return (val = (val << 8) | ((val >> 8) & 0xFF)); return (val = (val << 8) | ((val >> 8) & 0xFF));
#endif #endif
} }
inline atUint16 swapU16(atUint16 val) { return (atUint16)swap16(val); } constexpr atUint16 swapU16(atUint16 val) { return (atUint16)swap16(val); }
inline atInt32 swap32(atInt32 val) { constexpr atInt32 swap32(atInt32 val) {
#if __GNUC__ #if __GNUC__
return __builtin_bswap32(val); return __builtin_bswap32(val);
#elif _WIN32 #elif _WIN32
@ -40,8 +40,8 @@ inline atInt32 swap32(atInt32 val) {
return val; return val;
#endif #endif
} }
inline atUint32 swapU32(atUint32 val) { return (atUint32)swap32(val); } constexpr atUint32 swapU32(atUint32 val) { return (atUint32)swap32(val); }
inline atInt64 swap64(atInt64 val) { constexpr atInt64 swap64(atInt64 val) {
#if __GNUC__ #if __GNUC__
return __builtin_bswap64(val); return __builtin_bswap64(val);
#elif _WIN32 #elif _WIN32
@ -54,89 +54,89 @@ inline atInt64 swap64(atInt64 val) {
(((atInt64)(val)&0x000000000000FF00ULL) << 40) | (((atInt64)(val)&0x00000000000000FFULL) << 56)))); (((atInt64)(val)&0x000000000000FF00ULL) << 40) | (((atInt64)(val)&0x00000000000000FFULL) << 56))));
#endif #endif
} }
inline atUint64 swapU64(atUint64 val) { return (atUint64)swap64(val); } constexpr atUint64 swapU64(atUint64 val) { return (atUint64)swap64(val); }
inline float swapFloat(float val) { constexpr float swapFloat(float val) {
union { float f; atInt32 i; } uval1 = {val}; union { float f; atInt32 i; } uval1 = {val};
union { atInt32 i; float f; } uval2 = {swap32(uval1.i)}; union { atInt32 i; float f; } uval2 = {swap32(uval1.i)};
return uval2.f; return uval2.f;
} }
inline double swapDouble(double val) { constexpr double swapDouble(double val) {
union { double f; atInt64 i; } uval1 = {val}; union { double f; atInt64 i; } uval1 = {val};
union { atInt64 i; double f; } uval2 = {swap64(uval1.i)}; union { atInt64 i; double f; } uval2 = {swap64(uval1.i)};
return uval2.f; return uval2.f;
} }
inline atInt16 LittleInt16(atInt16& val) { constexpr atInt16 LittleInt16(atInt16& val) {
if (athena::utility::isSystemBigEndian()) if constexpr (athena::utility::isSystemBigEndian())
val = athena::utility::swap16(val); val = athena::utility::swap16(val);
return val; return val;
} }
inline atUint16 LittleUint16(atUint16& val) { constexpr atUint16 LittleUint16(atUint16& val) {
atInt16 ret = val; atInt16 ret = val;
LittleInt16(ret); LittleInt16(ret);
val = ret; val = ret;
return val; return val;
} }
inline atInt16 BigInt16(atInt16& val) { constexpr atInt16 BigInt16(atInt16& val) {
if (!athena::utility::isSystemBigEndian()) if constexpr (!athena::utility::isSystemBigEndian())
val = athena::utility::swap16(val); val = athena::utility::swap16(val);
return val; return val;
} }
inline atUint16 BigUint16(atUint16& val) { constexpr atUint16 BigUint16(atUint16& val) {
atInt16 ret = val; atInt16 ret = val;
BigInt16(ret); BigInt16(ret);
val = ret; val = ret;
return val; return val;
} }
inline atInt32 LittleInt32(atInt32& val) { constexpr atInt32 LittleInt32(atInt32& val) {
if (athena::utility::isSystemBigEndian()) if constexpr (athena::utility::isSystemBigEndian())
val = athena::utility::swap32(val); val = athena::utility::swap32(val);
return val; return val;
} }
inline atUint32 LittleUint32(atUint32& val) { constexpr atUint32 LittleUint32(atUint32& val) {
atInt32 ret = val; atInt32 ret = val;
LittleInt32(ret); LittleInt32(ret);
val = ret; val = ret;
return val; return val;
} }
inline atInt32 BigInt32(atInt32& val) { constexpr atInt32 BigInt32(atInt32& val) {
if (!athena::utility::isSystemBigEndian()) if constexpr (!athena::utility::isSystemBigEndian())
val = athena::utility::swap32(val); val = athena::utility::swap32(val);
return val; return val;
} }
inline atUint32 BigUint32(atUint32& val) { constexpr atUint32 BigUint32(atUint32& val) {
atInt32 ret = val; atInt32 ret = val;
BigInt32(ret); BigInt32(ret);
val = ret; val = ret;
return val; return val;
} }
inline atInt64 LittleInt64(atInt64& val) { constexpr atInt64 LittleInt64(atInt64& val) {
if (athena::utility::isSystemBigEndian()) if constexpr (athena::utility::isSystemBigEndian())
val = athena::utility::swap64(val); val = athena::utility::swap64(val);
return val; return val;
} }
inline atUint64 LittleUint64(atUint64& val) { constexpr atUint64 LittleUint64(atUint64& val) {
atInt64 ret = val; atInt64 ret = val;
LittleInt64(ret); LittleInt64(ret);
val = ret; val = ret;
return val; return val;
} }
inline atInt64 BigInt64(atInt64& val) { constexpr atInt64 BigInt64(atInt64& val) {
if (!athena::utility::isSystemBigEndian()) if constexpr (!athena::utility::isSystemBigEndian())
val = athena::utility::swap64(val); val = athena::utility::swap64(val);
return val; return val;
} }
inline atUint64 BigUint64(atUint64& val) { constexpr atUint64 BigUint64(atUint64& val) {
atInt64 ret = val; atInt64 ret = val;
BigInt64(ret); BigInt64(ret);
val = ret; val = ret;
@ -144,27 +144,27 @@ inline atUint64 BigUint64(atUint64& val) {
return val; return val;
} }
inline float LittleFloat(float val) { constexpr float LittleFloat(float val) {
if (athena::utility::isSystemBigEndian()) if constexpr (athena::utility::isSystemBigEndian())
val = athena::utility::swapFloat(val); return athena::utility::swapFloat(val);
return val; return val;
} }
inline float BigFloat(float val) { constexpr float BigFloat(float val) {
if (!athena::utility::isSystemBigEndian()) if constexpr (!athena::utility::isSystemBigEndian())
val = athena::utility::swapFloat(val); return athena::utility::swapFloat(val);
return val; return val;
} }
inline double LittleDouble(double val) { constexpr double LittleDouble(double val) {
if (athena::utility::isSystemBigEndian()) if constexpr (athena::utility::isSystemBigEndian())
val = athena::utility::swapDouble(val); return athena::utility::swapDouble(val);
return val; return val;
} }
inline double BigDouble(double val) { constexpr double BigDouble(double val) {
if (!athena::utility::isSystemBigEndian()) if constexpr (!athena::utility::isSystemBigEndian())
val = athena::utility::swapDouble(val); return athena::utility::swapDouble(val);
return val; return val;
} }
@ -175,8 +175,6 @@ atUint64 rand64();
std::string join(const std::vector<std::string>& elems, std::string_view delims); std::string join(const std::vector<std::string>& elems, std::string_view delims);
void tolower(std::string& str); void tolower(std::string& str);
void toupper(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); bool parseBool(std::string_view boolean, bool* valid = NULL);
int countChar(std::string_view str, const char chr, int* lastOccur = NULL); int countChar(std::string_view str, const char chr, int* lastOccur = NULL);

View File

@ -25,12 +25,12 @@ namespace athena {
static const uint8_t InCo[4] = {0xB, 0xD, 0x9, 0xE}; /* Inverse Coefficients */ 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 */ /* 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]; 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 */ /* unpack bytes from a word */
b[0] = (uint8_t)a; b[0] = (uint8_t)a;
b[1] = (uint8_t)(a >> 8); 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); 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 { static const struct SoftwareAESTables {
uint8_t fbsub[256]; uint8_t fbsub[256];

View File

@ -211,7 +211,7 @@ ALTTPDungeonItemFlags ALTTPFileReader::readDungeonFlags() {
flags.HyruleCastle = (flagsByte >> 6) & 1; flags.HyruleCastle = (flagsByte >> 6) & 1;
flags.SewerPassage = (flagsByte >> 7) & 1; flags.SewerPassage = (flagsByte >> 7) & 1;
atDebug("%x %x", flags.flags1, flags.flags2); atDebug("{:x} {:x}", flags.flags1, flags.flags2);
return flags; return flags;
} }

View File

@ -26,10 +26,8 @@ atInt8 NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(atInt8 val) { std::unique_ptr<YAMLNode> ValToNode(atInt8 val) {
char str[32];
snprintf(str, 32, "%d", int(val));
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE);
ret->m_scalarString = str; ret->m_scalarString = fmt::format(fmt("{}"), int(val));
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
} }
@ -39,10 +37,8 @@ atUint8 NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(atUint8 val) { std::unique_ptr<YAMLNode> ValToNode(atUint8 val) {
char str[32];
snprintf(str, 32, "0x%02X", val);
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE);
ret->m_scalarString = str; ret->m_scalarString = fmt::format(fmt("0x{:02X}"), val);
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
} }
@ -52,10 +48,8 @@ atInt16 NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(atInt16 val) { std::unique_ptr<YAMLNode> ValToNode(atInt16 val) {
char str[32];
snprintf(str, 32, "%d", int(val));
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE);
ret->m_scalarString = str; ret->m_scalarString = fmt::format(fmt("{}"), int(val));
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
} }
@ -65,10 +59,8 @@ atUint16 NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(atUint16 val) { std::unique_ptr<YAMLNode> ValToNode(atUint16 val) {
char str[32];
snprintf(str, 32, "0x%04X", val);
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE);
ret->m_scalarString = str; ret->m_scalarString = fmt::format(fmt("0x{:04X}"), val);
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
} }
@ -78,10 +70,8 @@ atInt32 NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(atInt32 val) { std::unique_ptr<YAMLNode> ValToNode(atInt32 val) {
char str[32];
snprintf(str, 32, "%d", int(val));
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE);
ret->m_scalarString = str; ret->m_scalarString = fmt::format(fmt("{}"), int(val));
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
} }
@ -91,10 +81,8 @@ atUint32 NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(atUint32 val) { std::unique_ptr<YAMLNode> ValToNode(atUint32 val) {
char str[32];
snprintf(str, 32, "0x%08X", val);
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE);
ret->m_scalarString = str; ret->m_scalarString = fmt::format(fmt("0x{:08X}"), val);
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
} }
@ -108,10 +96,8 @@ atInt64 NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(atInt64 val) { std::unique_ptr<YAMLNode> ValToNode(atInt64 val) {
char str[32];
snprintf(str, 32, "%" PRId64, val);
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE);
ret->m_scalarString = str; ret->m_scalarString = fmt::format(fmt("{}"), val);
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
} }
@ -125,10 +111,8 @@ atUint64 NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(atUint64 val) { std::unique_ptr<YAMLNode> ValToNode(atUint64 val) {
char str[32];
snprintf(str, 32, "0x%016" PRIX64, val);
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE);
ret->m_scalarString = str; ret->m_scalarString = fmt::format(fmt("0x{:016X}"), val);
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
} }
@ -138,10 +122,8 @@ float NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(float val) { std::unique_ptr<YAMLNode> ValToNode(float val) {
char str[64];
snprintf(str, 64, "%f", val);
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE);
ret->m_scalarString = str; ret->m_scalarString = fmt::format(fmt("{}"), val);
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
} }
@ -151,10 +133,8 @@ double NodeToVal(const YAMLNode* node) {
} }
std::unique_ptr<YAMLNode> ValToNode(double val) { std::unique_ptr<YAMLNode> ValToNode(double val) {
char str[64];
snprintf(str, 64, "%f", val);
YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE); YAMLNode* ret = new YAMLNode(YAML_SCALAR_NODE);
ret->m_scalarString = str; ret->m_scalarString = fmt::format(fmt("{}"), val);
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
} }
@ -189,10 +169,8 @@ std::unique_ptr<YAMLNode> ValToNode(const atVec2f& val) {
ret->m_seqChildren.reserve(2); ret->m_seqChildren.reserve(2);
simd_floats f(val.simd); simd_floats f(val.simd);
for (size_t i = 0; i < 2; ++i) { for (size_t i = 0; i < 2; ++i) {
char str[64];
snprintf(str, 64, "%f", f[i]);
YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); 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); ret->m_seqChildren.emplace_back(comp);
} }
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
@ -208,10 +186,8 @@ std::unique_ptr<YAMLNode> ValToNode(const atVec3f& val) {
ret->m_seqChildren.reserve(3); ret->m_seqChildren.reserve(3);
simd_floats f(val.simd); simd_floats f(val.simd);
for (size_t i = 0; i < 3; ++i) { for (size_t i = 0; i < 3; ++i) {
char str[64];
snprintf(str, 64, "%f", f[i]);
YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); 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); ret->m_seqChildren.emplace_back(comp);
} }
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
@ -227,10 +203,8 @@ std::unique_ptr<YAMLNode> ValToNode(const atVec4f& val) {
ret->m_seqChildren.reserve(4); ret->m_seqChildren.reserve(4);
simd_floats f(val.simd); simd_floats f(val.simd);
for (size_t i = 0; i < 4; ++i) { for (size_t i = 0; i < 4; ++i) {
char str[64];
snprintf(str, 64, "%f", f[i]);
YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); 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); ret->m_seqChildren.emplace_back(comp);
} }
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
@ -246,10 +220,8 @@ std::unique_ptr<YAMLNode> ValToNode(const atVec2d& val) {
ret->m_seqChildren.reserve(2); ret->m_seqChildren.reserve(2);
simd_doubles f(val.simd); simd_doubles f(val.simd);
for (size_t i = 0; i < 2; ++i) { for (size_t i = 0; i < 2; ++i) {
char str[64];
snprintf(str, 64, "%f", f[i]);
YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); 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); ret->m_seqChildren.emplace_back(comp);
} }
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
@ -265,10 +237,8 @@ std::unique_ptr<YAMLNode> ValToNode(const atVec3d& val) {
ret->m_seqChildren.reserve(3); ret->m_seqChildren.reserve(3);
simd_doubles f(val.simd); simd_doubles f(val.simd);
for (size_t i = 0; i < 3; ++i) { for (size_t i = 0; i < 3; ++i) {
char str[64];
snprintf(str, 64, "%f", f[i]);
YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); 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); ret->m_seqChildren.emplace_back(comp);
} }
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
@ -284,10 +254,8 @@ std::unique_ptr<YAMLNode> ValToNode(const atVec4d& val) {
ret->m_seqChildren.reserve(4); ret->m_seqChildren.reserve(4);
simd_doubles f(val.simd); simd_doubles f(val.simd);
for (size_t i = 0; i < 4; ++i) { for (size_t i = 0; i < 4; ++i) {
char str[64];
snprintf(str, 64, "%f", f[i]);
YAMLNode* comp = new YAMLNode(YAML_SCALAR_NODE); 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); ret->m_seqChildren.emplace_back(comp);
} }
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
@ -325,7 +293,7 @@ std::wstring NodeToVal(const YAMLNode* node) {
utf8proc_int32_t wc; utf8proc_int32_t wc;
utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc); utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc);
if (len < 0) { if (len < 0) {
atWarning("invalid UTF-8 character while decoding"); atWarning(fmt("invalid UTF-8 character while decoding"));
return retval; return retval;
} }
buf += len; buf += len;
@ -341,7 +309,7 @@ std::unique_ptr<YAMLNode> ValToNode(std::wstring_view val) {
utf8proc_uint8_t mb[4]; utf8proc_uint8_t mb[4];
utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb); utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb);
if (c < 0) { if (c < 0) {
atWarning("invalid UTF-8 character while encoding"); atWarning(fmt("invalid UTF-8 character while encoding"));
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
} }
ret->m_scalarString.append(reinterpret_cast<char*>(mb), c); ret->m_scalarString.append(reinterpret_cast<char*>(mb), c);
@ -356,7 +324,7 @@ std::unique_ptr<YAMLNode> ValToNode(std::u16string_view val) {
utf8proc_uint8_t mb[4]; utf8proc_uint8_t mb[4];
utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb); utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb);
if (c < 0) { if (c < 0) {
atWarning("invalid UTF-8 character while encoding"); atWarning(fmt("invalid UTF-8 character while encoding"));
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
} }
ret->m_scalarString.append(reinterpret_cast<char*>(mb), c); ret->m_scalarString.append(reinterpret_cast<char*>(mb), c);
@ -371,7 +339,7 @@ std::unique_ptr<YAMLNode> ValToNode(std::u32string_view val) {
utf8proc_uint8_t mb[4]; utf8proc_uint8_t mb[4];
utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb); utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb);
if (c < 0) { if (c < 0) {
atWarning("invalid UTF-8 character while encoding"); atWarning(fmt("invalid UTF-8 character while encoding"));
return std::unique_ptr<YAMLNode>(ret); return std::unique_ptr<YAMLNode>(ret);
} }
ret->m_scalarString.append(reinterpret_cast<char*>(mb), c); ret->m_scalarString.append(reinterpret_cast<char*>(mb), c);
@ -402,11 +370,11 @@ static const char* ErrorString(yaml_error_type_t errt) {
} }
void HandleYAMLParserError(yaml_parser_t* parser) { 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) { 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) { int YAMLStdStringReader(YAMLStdStringViewReaderState* reader, unsigned char* buffer, size_t size, size_t* size_read) {
@ -629,7 +597,7 @@ void YAMLDocWriter::writeU32String(const char* name, std::u32string_view val) {
void YAMLDocWriter::setStyle(YAMLNodeStyle s) { m_subStack.back()->m_style = s; } void YAMLDocWriter::setStyle(YAMLNodeStyle s) { m_subStack.back()->m_style = s; }
static inline void InsertNode(std::vector<YAMLNode*>& nodeStack, std::unique_ptr<YAMLNode>& mapKey, static void InsertNode(std::vector<YAMLNode*>& nodeStack, std::unique_ptr<YAMLNode>& mapKey,
std::unique_ptr<YAMLNode>& retVal, std::unique_ptr<YAMLNode>&& newNode) { std::unique_ptr<YAMLNode>& retVal, std::unique_ptr<YAMLNode>&& newNode) {
if (nodeStack.empty()) { if (nodeStack.empty()) {
retVal = std::move(newNode); retVal = std::move(newNode);
@ -670,7 +638,7 @@ std::unique_ptr<YAMLNode> YAMLDocReader::ParseEvents(athena::io::IStreamReader*
switch (event.type) { switch (event.type) {
case YAML_SCALAR_EVENT: { case YAML_SCALAR_EVENT: {
if (nodeStack.empty()) { if (nodeStack.empty()) {
atWarning("YAML parser stack empty; skipping scalar node"); atWarning(fmt("YAML parser stack empty; skipping scalar node"));
break; break;
} }
std::unique_ptr<YAMLNode> newScalar(new YAMLNode(YAML_SCALAR_NODE)); std::unique_ptr<YAMLNode> newScalar(new YAMLNode(YAML_SCALAR_NODE));
@ -805,7 +773,7 @@ YAMLDocReader::RecordRAII YAMLDocReader::enterSubRecord(const char* name) {
m_seqTrackerStack.push_back(0); m_seqTrackerStack.push_back(0);
return RecordRAII{this}; return RecordRAII{this};
} else if (!name) { } else if (!name) {
atError("Expected YAML sequence"); atError(fmt("Expected YAML sequence"));
} }
for (const auto& item : curSub->m_mapChildren) { for (const auto& item : curSub->m_mapChildren) {
if (!item.first.compare(name)) { 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) { if (nextSub->m_type == YAML_SEQUENCE_NODE) {
countOut = nextSub->m_seqChildren.size(); countOut = nextSub->m_seqChildren.size();
} else { } else {
atError("'%s' is not a vector field", name); atError(fmt("'{}' is not a vector field"), name);
countOut = 0; countOut = 0;
} }
m_subStack.push_back(nextSub); m_subStack.push_back(nextSub);
@ -878,7 +846,7 @@ RETURNTYPE YAMLDocReader::readVal(const char* name) {
} }
} }
if (name) if (name)
atWarning("Unable to find field '%s'; returning 0", name); atWarning(fmt("Unable to find field '{}'; returning 0"), name);
return RETURNTYPE(); return RETURNTYPE();
} }
@ -935,7 +903,7 @@ std::string YAMLDocReader::readString(const char* name) { return readVal<std::st
std::wstring YAMLDocReader::readWString(const char* name) { return readVal<std::wstring>(name); } std::wstring YAMLDocReader::readWString(const char* name) { return readVal<std::wstring>(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; yaml_event_t event;
if (!yaml_scalar_event_initialize(&event, nullptr, nullptr, (yaml_char_t*)val, strlen(val), true, true, if (!yaml_scalar_event_initialize(&event, nullptr, nullptr, (yaml_char_t*)val, strlen(val), true, true,
YAML_PLAIN_SCALAR_STYLE)) 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; 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) for (const auto& ch : node.m_scalarString)
if (ch == '\n') if (ch == '\n')
return YAML_LITERAL_SCALAR_STYLE; return YAML_LITERAL_SCALAR_STYLE;
return YAML_ANY_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) if (node.m_style == YAMLNodeStyle::Flow)
return YAML_FLOW_SEQUENCE_STYLE; return YAML_FLOW_SEQUENCE_STYLE;
else if (node.m_style == YAMLNodeStyle::Block) 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; 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) if (node.m_style == YAMLNodeStyle::Flow)
return YAML_FLOW_MAPPING_STYLE; return YAML_FLOW_MAPPING_STYLE;
else if (node.m_style == YAMLNodeStyle::Block) else if (node.m_style == YAMLNodeStyle::Block)
@ -1030,7 +998,7 @@ static const std::string base64_chars =
"abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz"
"0123456789+/"; "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 base64_encode(const atUint8* bytes_to_encode, size_t in_len) {
std::string ret; std::string ret;

View File

@ -47,7 +47,7 @@ bool Dir::rm(std::string_view path) { return !(remove((m_path + "/" + path.data(
bool Dir::touch() { bool Dir::touch() {
srand(time(NULL)); srand(time(NULL));
atUint64 tmp = utility::rand64(); 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(); bool ret = FileInfo(m_path + "/" + tmpFile).touch();
if (ret) if (ret)
return rm(tmpFile); return rm(tmpFile);

View File

@ -33,7 +33,7 @@ void FileReader::open() {
if (!m_fileHandle) { if (!m_fileHandle) {
std::string _filename = filename(); std::string _filename = filename();
if (m_globalErr) if (m_globalErr)
atError("File not found '%s'", _filename.c_str()); atError(fmt("File not found '{}'"), _filename);
setError(); setError();
return; return;
} }
@ -45,7 +45,7 @@ void FileReader::open() {
void FileReader::close() { void FileReader::close() {
if (!m_fileHandle) { if (!m_fileHandle) {
if (m_globalErr) if (m_globalErr)
atError("Cannot close an unopened stream"); atError(fmt("Cannot close an unopened stream"));
setError(); setError();
return; return;
} }
@ -74,7 +74,7 @@ void FileReader::seek(atInt64 pos, SeekOrigin origin) {
} }
if (m_offset > length()) { if (m_offset > length()) {
if (m_globalErr) if (m_globalErr)
atError("Unable to seek in file"); atError(fmt("Unable to seek in file"));
setError(); setError();
return; return;
} }
@ -87,7 +87,7 @@ void FileReader::seek(atInt64 pos, SeekOrigin origin) {
} }
} else if (fseeko64(m_fileHandle, pos, (int)origin) != 0) { } else if (fseeko64(m_fileHandle, pos, (int)origin) != 0) {
if (m_globalErr) if (m_globalErr)
atError("Unable to seek in file"); atError(fmt("Unable to seek in file"));
setError(); setError();
} }
} }
@ -95,7 +95,7 @@ void FileReader::seek(atInt64 pos, SeekOrigin origin) {
atUint64 FileReader::position() const { atUint64 FileReader::position() const {
if (!isOpen()) { if (!isOpen()) {
if (m_globalErr) if (m_globalErr)
atError("File not open"); atError(fmt("File not open"));
return 0; return 0;
} }
@ -108,7 +108,7 @@ atUint64 FileReader::position() const {
atUint64 FileReader::length() const { atUint64 FileReader::length() const {
if (!isOpen()) { if (!isOpen()) {
if (m_globalErr) if (m_globalErr)
atError("File not open"); atError(fmt("File not open"));
return 0; return 0;
} }
@ -118,7 +118,7 @@ atUint64 FileReader::length() const {
atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len) { atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len) {
if (!isOpen()) { if (!isOpen()) {
if (m_globalErr) if (m_globalErr)
atError("File not open for reading"); atError(fmt("File not open for reading"));
setError(); setError();
return 0; return 0;
} }

View File

@ -36,7 +36,7 @@ void FileReader::open() {
m_fileHandle = 0; m_fileHandle = 0;
std::string _filename = filename(); std::string _filename = filename();
if (m_globalErr) if (m_globalErr)
atError("File not found '%s'", _filename.c_str()); atError("File not found '{}'", _filename);
setError(); setError();
return; return;
} }

View File

@ -49,7 +49,7 @@ void FileWriter::open(bool overwrite) {
if (!m_fileHandle) { if (!m_fileHandle) {
if (m_globalErr) if (m_globalErr)
atError("Unable to open file '%s'", filename().c_str()); atError(fmt("Unable to open file '%s'"), filename().c_str());
setError(); setError();
return; return;
} }
@ -61,7 +61,7 @@ void FileWriter::open(bool overwrite) {
void FileWriter::close() { void FileWriter::close() {
if (!m_fileHandle) { if (!m_fileHandle) {
if (m_globalErr) if (m_globalErr)
atError("Cannot close an unopened stream"); atError(fmt("Cannot close an unopened stream"));
setError(); setError();
return; return;
} }
@ -81,14 +81,14 @@ void FileWriter::close() {
void FileWriter::seek(atInt64 pos, SeekOrigin origin) { void FileWriter::seek(atInt64 pos, SeekOrigin origin) {
if (!isOpen()) { if (!isOpen()) {
if (m_globalErr) if (m_globalErr)
atError("Unable to seek in file, not open"); atError(fmt("Unable to seek in file, not open"));
setError(); setError();
return; return;
} }
if (fseeko64(m_fileHandle, pos, (int)origin) != 0) { if (fseeko64(m_fileHandle, pos, (int)origin) != 0) {
if (m_globalErr) if (m_globalErr)
atError("Unable to seek in file"); atError(fmt("Unable to seek in file"));
setError(); setError();
} }
} }
@ -100,14 +100,14 @@ atUint64 FileWriter::length() const { return utility::fileSize(m_filename); }
void FileWriter::writeUBytes(const atUint8* data, atUint64 len) { void FileWriter::writeUBytes(const atUint8* data, atUint64 len) {
if (!isOpen()) { if (!isOpen()) {
if (m_globalErr) if (m_globalErr)
atError("File not open for writing"); atError(fmt("File not open for writing"));
setError(); setError();
return; return;
} }
if (fwrite(data, 1, len, m_fileHandle) != len) { if (fwrite(data, 1, len, m_fileHandle) != len) {
if (m_globalErr) if (m_globalErr)
atError("Unable to write to stream"); atError(fmt("Unable to write to stream"));
setError(); setError();
} }
} }

View File

@ -43,7 +43,7 @@ void FileWriter::open(bool overwrite) {
if (m_fileHandle == INVALID_HANDLE_VALUE) { if (m_fileHandle == INVALID_HANDLE_VALUE) {
m_fileHandle = 0; m_fileHandle = 0;
if (m_globalErr) if (m_globalErr)
atError("Unable to open file '%s'", filename().c_str()); atError("Unable to open file '{}'", filename());
setError(); setError();
return; return;
} }

View File

@ -1,9 +1,13 @@
#include "athena/Global.hpp" #include "athena/Global.hpp"
#include "athena/Utility.hpp" #include "athena/Utility.hpp"
#include <cstdio> #include <cstdio>
#include <cstdarg>
#include <cstdlib> #include <cstdlib>
#define FMT_STRING_ALIAS 1
#define FMT_ENFORCE_COMPILE_STRING 1
#define FMT_USE_GRISU 0
#include <fmt/format.h>
std::ostream& operator<<(std::ostream& os, const athena::SeekOrigin& origin) { std::ostream& operator<<(std::ostream& os, const athena::SeekOrigin& origin) {
switch (origin) { switch (origin) {
case athena::SeekOrigin::Begin: 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, 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; std::string levelStr;
switch (level) { switch (level) {
case athena::error::Level::Warning: case athena::error::Level::Warning:
@ -53,10 +57,7 @@ static void __defaultExceptionHandler(athena::error::Level level, const char* fi
break; break;
} }
va_list vl; std::string msg = fmt::internal::vformat(fmt, args);
va_start(vl, fmt);
std::string msg = athena::utility::vsprintf(fmt, vl);
va_end(vl);
std::cerr << levelStr << " " << file << " " << function << "(" << line << "): " << msg << std::endl; std::cerr << levelStr << " " << file << " " << function << "(" << line << "): " << msg << std::endl;
} }

View File

@ -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) { : m_data(data), m_length(length), m_position(0), m_owns(takeOwnership), m_globalErr(globalErr) {
if (!data) { if (!data) {
if (m_globalErr) if (m_globalErr)
atError("data cannot be NULL"); atError(fmt("data cannot be NULL"));
setError(); setError();
return; return;
} }
@ -33,7 +33,7 @@ MemoryReader::~MemoryReader() {
MemoryCopyReader::MemoryCopyReader(const void* data, atUint64 length) : MemoryReader(data, length, false) { MemoryCopyReader::MemoryCopyReader(const void* data, atUint64 length) : MemoryReader(data, length, false) {
if (!data) { if (!data) {
if (m_globalErr) if (m_globalErr)
atError("data cannot be NULL"); atError(fmt("data cannot be NULL"));
setError(); setError();
return; return;
} }
@ -48,7 +48,7 @@ void MemoryReader::seek(atInt64 position, SeekOrigin origin) {
case SeekOrigin::Begin: case SeekOrigin::Begin:
if ((position < 0 || (atInt64)position > (atInt64)m_length)) { if ((position < 0 || (atInt64)position > (atInt64)m_length)) {
if (m_globalErr) if (m_globalErr)
atFatal("Position %0.8X outside stream bounds ", position); atFatal(fmt("Position {:08X} outside stream bounds "), position);
m_position = m_length; m_position = m_length;
setError(); setError();
return; return;
@ -60,7 +60,7 @@ void MemoryReader::seek(atInt64 position, SeekOrigin origin) {
case SeekOrigin::Current: case SeekOrigin::Current:
if ((((atInt64)m_position + position) < 0 || (m_position + position) > m_length)) { if ((((atInt64)m_position + position) < 0 || (m_position + position) > m_length)) {
if (m_globalErr) if (m_globalErr)
atFatal("Position %0.8X outside stream bounds ", position); atFatal(fmt("Position {:08X} outside stream bounds "), position);
m_position = m_length; m_position = m_length;
setError(); setError();
return; return;
@ -72,7 +72,7 @@ void MemoryReader::seek(atInt64 position, SeekOrigin origin) {
case SeekOrigin::End: case SeekOrigin::End:
if ((((atInt64)m_length - position < 0) || (m_length - position) > m_length)) { if ((((atInt64)m_length - position < 0) || (m_length - position) > m_length)) {
if (m_globalErr) if (m_globalErr)
atFatal("Position %0.8X outside stream bounds ", position); atFatal(fmt("Position {:08X} outside stream bounds "), position);
m_position = m_length; m_position = m_length;
setError(); setError();
return; return;
@ -110,7 +110,7 @@ atUint8* MemoryReader::data() const {
atUint64 MemoryReader::readUBytesToBuf(void* buf, atUint64 length) { atUint64 MemoryReader::readUBytesToBuf(void* buf, atUint64 length) {
if (m_position >= m_length) { if (m_position >= m_length) {
if (m_globalErr) 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; m_position = m_length;
setError(); setError();
return 0; return 0;
@ -129,7 +129,7 @@ void MemoryCopyReader::loadData() {
if (!in) { if (!in) {
if (m_globalErr) if (m_globalErr)
atError("Unable to open file '%s'", m_filepath.c_str()); atError(fmt("Unable to open file '%s'"), m_filepath);
setError(); setError();
return; return;
} }
@ -151,7 +151,7 @@ void MemoryCopyReader::loadData() {
if (ret < 0) { if (ret < 0) {
if (m_globalErr) if (m_globalErr)
atError("Error reading data from disk"); atError(fmt("Error reading data from disk"));
setError(); setError();
return; return;
} else if (ret == 0) } else if (ret == 0)

View File

@ -14,7 +14,7 @@ namespace athena::io {
MemoryWriter::MemoryWriter(atUint8* data, atUint64 length, bool takeOwnership) MemoryWriter::MemoryWriter(atUint8* data, atUint64 length, bool takeOwnership)
: m_data((atUint8*)data), m_length(length), m_position(0), m_bufferOwned(takeOwnership) { : m_data((atUint8*)data), m_length(length), m_position(0), m_bufferOwned(takeOwnership) {
if (!data) { if (!data) {
atError("data cannot be NULL"); atError(fmt("data cannot be NULL"));
setError(); setError();
return; return;
} }
@ -34,7 +34,7 @@ MemoryCopyWriter::MemoryCopyWriter(atUint8* data, atUint64 length) {
m_bufferOwned = false; m_bufferOwned = false;
if (length == 0) { if (length == 0) {
atError("length cannot be 0"); atError(fmt("length cannot be 0"));
setError(); setError();
return; return;
} }
@ -53,7 +53,7 @@ MemoryCopyWriter::MemoryCopyWriter(std::string_view filename) {
m_bufferOwned = false; m_bufferOwned = false;
if (!m_data) { if (!m_data) {
atError("Could not allocate memory!"); atError(fmt("Could not allocate memory!"));
setError(); setError();
return; return;
} }
@ -63,13 +63,13 @@ void MemoryWriter::seek(atInt64 position, SeekOrigin origin) {
switch (origin) { switch (origin) {
case SeekOrigin::Begin: case SeekOrigin::Begin:
if (position < 0) { if (position < 0) {
atError("Position outside stream bounds"); atError(fmt("Position outside stream bounds"));
setError(); setError();
return; return;
} }
if ((atUint64)position > m_length) { if ((atUint64)position > m_length) {
atError("data exceeds available buffer space"); atError(fmt("data exceeds available buffer space"));
setError(); setError();
return; return;
} }
@ -79,13 +79,13 @@ void MemoryWriter::seek(atInt64 position, SeekOrigin origin) {
case SeekOrigin::Current: case SeekOrigin::Current:
if ((((atInt64)m_position + position) < 0)) { if ((((atInt64)m_position + position) < 0)) {
atError("Position outside stream bounds"); atError(fmt("Position outside stream bounds"));
setError(); setError();
return; return;
} }
if (m_position + position > m_length) { if (m_position + position > m_length) {
atError("data exceeds available buffer space"); atError(fmt("data exceeds available buffer space"));
setError(); setError();
return; return;
} }
@ -95,13 +95,13 @@ void MemoryWriter::seek(atInt64 position, SeekOrigin origin) {
case SeekOrigin::End: case SeekOrigin::End:
if (((atInt64)m_length - position) < 0) { if (((atInt64)m_length - position) < 0) {
atError("Position outside stream bounds"); atError(fmt("Position outside stream bounds"));
setError(); setError();
return; return;
} }
if ((atUint64)position > m_length) { if ((atUint64)position > m_length) {
atError("data exceeds available buffer space"); atError(fmt("data exceeds available buffer space"));
setError(); setError();
return; return;
} }
@ -115,7 +115,7 @@ void MemoryCopyWriter::seek(atInt64 position, SeekOrigin origin) {
switch (origin) { switch (origin) {
case SeekOrigin::Begin: case SeekOrigin::Begin:
if (position < 0) { if (position < 0) {
atError("Position outside stream bounds"); atError(fmt("Position outside stream bounds"));
setError(); setError();
return; return;
} }
@ -128,7 +128,7 @@ void MemoryCopyWriter::seek(atInt64 position, SeekOrigin origin) {
case SeekOrigin::Current: case SeekOrigin::Current:
if ((((atInt64)m_position + position) < 0)) { if ((((atInt64)m_position + position) < 0)) {
atError("Position outside stream bounds"); atError(fmt("Position outside stream bounds"));
setError(); setError();
return; return;
} }
@ -141,7 +141,7 @@ void MemoryCopyWriter::seek(atInt64 position, SeekOrigin origin) {
case SeekOrigin::End: case SeekOrigin::End:
if (((atInt64)m_length - position) < 0) { if (((atInt64)m_length - position) < 0) {
atError("Position outside stream bounds"); atError(fmt("Position outside stream bounds"));
setError(); setError();
return; return;
} }
@ -182,7 +182,7 @@ atUint8* MemoryWriter::data() const {
void MemoryWriter::save(std::string_view filename) { void MemoryWriter::save(std::string_view filename) {
if (filename.empty() && m_filepath.empty()) { if (filename.empty() && m_filepath.empty()) {
atError("No file specified, cannot save."); atError(fmt("No file specified, cannot save."));
setError(); setError();
return; return;
} }
@ -193,7 +193,7 @@ void MemoryWriter::save(std::string_view filename) {
FILE* out = fopen(m_filepath.c_str(), "wb"); FILE* out = fopen(m_filepath.c_str(), "wb");
if (!out) { if (!out) {
atError("Unable to open file '%s'", m_filepath.c_str()); atError(fmt("Unable to open file '{}'"), m_filepath);
setError(); setError();
return; return;
} }
@ -208,7 +208,7 @@ void MemoryWriter::save(std::string_view filename) {
atInt64 ret = fwrite(m_data + done, 1, blocksize, out); atInt64 ret = fwrite(m_data + done, 1, blocksize, out);
if (ret < 0) { if (ret < 0) {
atError("Error writing data to disk"); atError(fmt("Error writing data to disk"));
setError(); setError();
return; return;
} else if (ret == 0) } else if (ret == 0)
@ -222,13 +222,13 @@ void MemoryWriter::save(std::string_view filename) {
void MemoryWriter::writeUBytes(const atUint8* data, atUint64 length) { void MemoryWriter::writeUBytes(const atUint8* data, atUint64 length) {
if (!data) { if (!data) {
atError("data cannnot be NULL"); atError(fmt("data cannnot be NULL"));
setError(); setError();
return; return;
} }
if (m_position + length > m_length) { if (m_position + length > m_length) {
atError("data length exceeds available buffer space"); atError(fmt("data length exceeds available buffer space"));
setError(); setError();
return; return;
} }
@ -240,7 +240,7 @@ void MemoryWriter::writeUBytes(const atUint8* data, atUint64 length) {
void MemoryCopyWriter::writeUBytes(const atUint8* data, atUint64 length) { void MemoryCopyWriter::writeUBytes(const atUint8* data, atUint64 length) {
if (!data) { if (!data) {
atError("data cannnot be NULL"); atError(fmt("data cannnot be NULL"));
setError(); setError();
return; return;
} }
@ -255,7 +255,7 @@ void MemoryCopyWriter::writeUBytes(const atUint8* data, atUint64 length) {
void MemoryCopyWriter::resize(atUint64 newSize) { void MemoryCopyWriter::resize(atUint64 newSize) {
if (newSize < m_length) { 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; return;
} }

View File

@ -80,7 +80,7 @@ bool Socket::openSocket() {
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_socket == -1) { if (m_socket == -1) {
atError("Can't allocate socket"); atError(fmt("Can't allocate socket"));
return false; return false;
} }
@ -134,13 +134,13 @@ bool Socket::openAndListen(const IPAddress& address, uint32_t port) {
sockaddr_in addr = createAddress(address.toInteger(), port); sockaddr_in addr = createAddress(address.toInteger(), port);
if (bind(m_socket, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1) { if (bind(m_socket, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == -1) {
/* Not likely to happen, but... */ /* 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; return false;
} }
if (::listen(m_socket, 0) == -1) { if (::listen(m_socket, 0) == -1) {
/* Oops, socket is deaf */ /* Oops, socket is deaf */
atError("Failed to listen to port %d", port); atError(fmt("Failed to listen to port {}"), port);
return false; return false;
} }
@ -160,7 +160,7 @@ Socket::EResult Socket::accept(Socket& remoteSocketOut, sockaddr_in& fromAddress
#ifndef _WIN32 #ifndef _WIN32
EResult res = (errno == EAGAIN) ? EResult::Busy : EResult::Error; EResult res = (errno == EAGAIN) ? EResult::Busy : EResult::Error;
if (res == 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 #else
EResult res = LastWSAError(); EResult res = LastWSAError();
if (res == EResult::Error) if (res == EResult::Error)

View File

@ -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); } 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) { bool parseBool(std::string_view boolean, bool* valid) {
std::string val(boolean); std::string val(boolean);
// compare must be case insensitive // compare must be case insensitive
@ -192,7 +166,7 @@ std::string wideToUtf8(std::wstring_view src) {
utf8proc_uint8_t mb[4]; utf8proc_uint8_t mb[4];
utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb); utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb);
if (c < 0) { if (c < 0) {
atWarning("invalid UTF-8 character while encoding"); atWarning(fmt("invalid UTF-8 character while encoding"));
return retval; return retval;
} }
retval.append(reinterpret_cast<char*>(mb), c); retval.append(reinterpret_cast<char*>(mb), c);
@ -208,7 +182,7 @@ std::wstring utf8ToWide(std::string_view src) {
utf8proc_int32_t wc; utf8proc_int32_t wc;
utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc); utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc);
if (len < 0) { if (len < 0) {
atWarning("invalid UTF-8 character while decoding"); atWarning(fmt("invalid UTF-8 character while decoding"));
return retval; return retval;
} }
buf += len; buf += len;

View File

@ -12,7 +12,7 @@ void VectorWriter::seek(atInt64 position, SeekOrigin origin) {
switch (origin) { switch (origin) {
case SeekOrigin::Begin: case SeekOrigin::Begin:
if (position < 0) { if (position < 0) {
atError("Position outside stream bounds"); atError(fmt("Position outside stream bounds"));
setError(); setError();
return; return;
} }
@ -25,7 +25,7 @@ void VectorWriter::seek(atInt64 position, SeekOrigin origin) {
case SeekOrigin::Current: case SeekOrigin::Current:
if ((((atInt64)m_position + position) < 0)) { if ((((atInt64)m_position + position) < 0)) {
atError("Position outside stream bounds"); atError(fmt("Position outside stream bounds"));
setError(); setError();
return; return;
} }
@ -38,13 +38,13 @@ void VectorWriter::seek(atInt64 position, SeekOrigin origin) {
case SeekOrigin::End: case SeekOrigin::End:
if (((atInt64)m_data.size() - position) < 0) { if (((atInt64)m_data.size() - position) < 0) {
atError("Position outside stream bounds"); atError(fmt("Position outside stream bounds"));
setError(); setError();
return; return;
} }
if ((atUint64)position > m_data.size()) { if ((atUint64)position > m_data.size()) {
atError("data exceeds vector size"); atError(fmt("data exceeds vector size"));
setError(); setError();
return; return;
} }
@ -56,7 +56,7 @@ void VectorWriter::seek(atInt64 position, SeekOrigin origin) {
void VectorWriter::writeUBytes(const atUint8* data, atUint64 length) { void VectorWriter::writeUBytes(const atUint8* data, atUint64 length) {
if (!data) { if (!data) {
atError("data cannnot be NULL"); atError(fmt("data cannnot be NULL"));
setError(); setError();
return; return;
} }

View File

@ -78,7 +78,7 @@ bool WiiFile::isFile() const { return (m_type == WiiFile::File); }
void WiiFile::addChild(WiiFile* file) { void WiiFile::addChild(WiiFile* file) {
if (!isDirectory()) { if (!isDirectory()) {
atWarning("%s is not a directory", filename().c_str()); atWarning("{} is not a directory", filename());
return; return;
} }