From bedcf128acfcc3307e896ccab903a4dcd082639b Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Tue, 10 Nov 2015 21:01:41 -1000 Subject: [PATCH 1/7] Macro update for OS X --- atdna/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atdna/CMakeLists.txt b/atdna/CMakeLists.txt index ac53c7a..b60adb9 100644 --- a/atdna/CMakeLists.txt +++ b/atdna/CMakeLists.txt @@ -167,7 +167,7 @@ macro(atdna out) # OSX Extra elseif(APPLE) - list(APPEND extraargs -isysroot ${CMAKE_OSX_SYSROOT} -isysroot + list(APPEND extraargs -isysroot ${CMAKE_OSX_SYSROOT} -I ${CMAKE_OSX_SYSROOT}/usr/include -isysroot ${CMAKE_OSX_SYSROOT}/../../../../../Toolchains/XcodeDefault.xctoolchain) endif() From b6b54d092130aa9d31e845c768faf8f08b7a30b1 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Thu, 12 Nov 2015 16:10:50 -1000 Subject: [PATCH 2/7] Minor file I/O tweaks --- src/Athena/FileReader.cpp | 4 ++-- src/Athena/FileWriter.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Athena/FileReader.cpp b/src/Athena/FileReader.cpp index 10a48bc..cb0c115 100644 --- a/src/Athena/FileReader.cpp +++ b/src/Athena/FileReader.cpp @@ -59,8 +59,8 @@ void FileReader::open() return; } - // ensure we're at the beginning of the file - rewind(m_fileHandle); + // reset error + m_hasError = false; } void FileReader::close() diff --git a/src/Athena/FileWriter.cpp b/src/Athena/FileWriter.cpp index 6a2eb1c..12cf013 100644 --- a/src/Athena/FileWriter.cpp +++ b/src/Athena/FileWriter.cpp @@ -70,8 +70,8 @@ void FileWriter::open(bool overwrite) return; } - // ensure we're at the beginning of the file - rewind(m_fileHandle); + // reset error + m_hasError = false; } void FileWriter::close() From 63a58265eff1c53dffe6d93af5f5fb1ecc14c86a Mon Sep 17 00:00:00 2001 From: Phillip Date: Fri, 13 Nov 2015 21:53:13 -0800 Subject: [PATCH 3/7] Squash MSVC warnings Unify filename usage --- include/Athena/DNAYaml.hpp | 4 +- include/Athena/FileReader.hpp | 29 +++- include/Athena/FileWriter.hpp | 24 ++- include/Athena/IStreamWriter.hpp | 270 ++++++++++++++++--------------- include/Athena/Types.hpp | 2 +- include/Athena/Utility.hpp | 5 + src/Athena/FileReader.cpp | 29 ++-- src/Athena/FileWriter.cpp | 46 +++--- src/Athena/Utility.cpp | 39 +++++ 9 files changed, 273 insertions(+), 175 deletions(-) diff --git a/include/Athena/DNAYaml.hpp b/include/Athena/DNAYaml.hpp index a6488eb..0e74e83 100644 --- a/include/Athena/DNAYaml.hpp +++ b/include/Athena/DNAYaml.hpp @@ -903,7 +903,7 @@ public: } template - void writeVal(const char* name, INTYPE val) + void writeVal(const char* name, const INTYPE& val) { YAMLNode* curSub = m_subStack.back(); if (curSub->m_type == YAML_MAPPING_NODE) @@ -913,7 +913,7 @@ public: } template - void writeVal(const char* name, INTYPE val, size_t byteCount) + void writeVal(const char* name, const INTYPE& val, size_t byteCount) { YAMLNode* curSub = m_subStack.back(); if (curSub->m_type == YAML_MAPPING_NODE) diff --git a/include/Athena/FileReader.hpp b/include/Athena/FileReader.hpp index bddd682..53a4727 100644 --- a/include/Athena/FileReader.hpp +++ b/include/Athena/FileReader.hpp @@ -14,12 +14,26 @@ class FileReader : public IStreamReader { public: FileReader(const std::string& filename, atInt32 cacheSize = (32 * 1024)); -#if _WIN32 FileReader(const std::wstring& filename, atInt32 cacheSize = (32 * 1024)); -#endif virtual ~FileReader(); - inline const std::string& filename() const - {return m_filename;} + + inline std::string filename() const + { +#if _WIN32 + return utility::wideToUtf8(m_filename); +#else + return m_filename; +#endif + } + + inline std::wstring wfilename() const + { +#if _WIN32 + return m_filename; +#else + return utility::utf8ToWide(m_filename); +#endif + } void open(); void close(); @@ -34,13 +48,14 @@ public: void setCacheSize(const atInt32 blockSize); FILE* _fileHandle() {return m_fileHandle;} protected: - std::string m_filename; #if _WIN32 - std::wstring m_wfilename; + std::wstring m_filename; +#else + std::string m_filename; #endif FILE* m_fileHandle; std::unique_ptr m_cacheData; - atInt32 m_blockSize; + atInt32 m_blockSize; atInt32 m_curBlock; atUint64 m_offset; }; diff --git a/include/Athena/FileWriter.hpp b/include/Athena/FileWriter.hpp index 320e081..9088846 100644 --- a/include/Athena/FileWriter.hpp +++ b/include/Athena/FileWriter.hpp @@ -12,11 +12,26 @@ class FileWriter : public IStreamWriter { public: FileWriter(const std::string& filename, bool overwrite = true); -#if _WIN32 FileWriter(const std::wstring& filename, bool overwrite = true); -#endif virtual ~FileWriter(); + inline std::string filename() const + { +#if _WIN32 + return utility::wideToUtf8(m_filename); +#else + return m_filename; +#endif + } + inline std::wstring wfilename() const + { +#if _WIN32 + return m_filename; +#else + return utility::utf8ToWide(m_filename); +#endif + } + void open(bool overwrite = true); void close(); inline bool isOpen() const @@ -28,9 +43,10 @@ public: FILE* _fileHandle() {return m_fileHandle;} private: - std::string m_filename; #if _WIN32 - std::wstring m_wfilename; + std::wstring m_filename; +#else + std::string m_filename; #endif FILE* m_fileHandle; atUint8 m_currentByte; diff --git a/include/Athena/IStreamWriter.hpp b/include/Athena/IStreamWriter.hpp index a4e9a2d..2d02ed3 100644 --- a/include/Athena/IStreamWriter.hpp +++ b/include/Athena/IStreamWriter.hpp @@ -383,306 +383,324 @@ public: * * @param vec The value to write to the buffer */ - inline void writeVec2f(atVec2f vec) + inline void writeVec2f(const atVec2f& vec) { + atVec2f tmp = vec; if (m_endian == BigEndian) { - utility::BigFloat(vec.vec[0]); - utility::BigFloat(vec.vec[1]); + utility::BigFloat(tmp.vec[0]); + utility::BigFloat(tmp.vec[1]); } else { - utility::LittleFloat(vec.vec[0]); - utility::LittleFloat(vec.vec[1]); + utility::LittleFloat(tmp.vec[0]); + utility::LittleFloat(tmp.vec[1]); } - writeUBytes((atUint8*)&vec, 8); + writeUBytes((atUint8*)&tmp, 8); } - inline void writeVal(atVec2f val) {writeVec2f(val);} + inline void writeVal(const atVec2f& val) {writeVec2f(val);} /** @brief Writes an atVec2f (8 bytes) to the buffer and advances the buffer. * It also swaps the bytes against little depending on the platform. * * @param vec The value to write to the buffer */ - inline void writeVec2fLittle(atVec2f vec) + inline void writeVec2fLittle(const atVec2f& vec) { - utility::LittleFloat(vec.vec[0]); - utility::LittleFloat(vec.vec[1]); - writeUBytes((atUint8*)&vec, 8); + atVec2f tmp = vec; + utility::LittleFloat(tmp.vec[0]); + utility::LittleFloat(tmp.vec[1]); + writeUBytes((atUint8*)&tmp, 8); } - inline void writeValLittle(atVec2f val) {writeVec2fLittle(val);} + inline void writeValLittle(const atVec2f& val) {writeVec2fLittle(val);} /** @brief Writes an atVec2f (8 bytes) to the buffer and advances the buffer. * It also swaps the bytes against big depending on the platform. * * @param vec The value to write to the buffer */ - inline void writeVec2fBig(atVec2f vec) + inline void writeVec2fBig(const atVec2f& vec) { - utility::BigFloat(vec.vec[0]); - utility::BigFloat(vec.vec[1]); - writeUBytes((atUint8*)&vec, 8); + atVec2f tmp = vec; + utility::BigFloat(tmp.vec[0]); + utility::BigFloat(tmp.vec[1]); + writeUBytes((atUint8*)&tmp, 8); } - inline void writeValBig(atVec2f val) {writeVec2fBig(val);} + inline void writeValBig(const atVec2f& val) {writeVec2fBig(val);} /** @brief Writes an atVec3f (12 bytes) to the buffer and advances the buffer. * It also swaps the bytes depending on the platform and Stream settings. * * @param vec The value to write to the buffer */ - inline void writeVec3f(atVec3f vec) + inline void writeVec3f(const atVec3f& vec) { + atVec3f tmp = vec; if (m_endian == BigEndian) { - utility::BigFloat(vec.vec[0]); - utility::BigFloat(vec.vec[1]); - utility::BigFloat(vec.vec[2]); + utility::BigFloat(tmp.vec[0]); + utility::BigFloat(tmp.vec[1]); + utility::BigFloat(tmp.vec[2]); } else { - utility::LittleFloat(vec.vec[0]); - utility::LittleFloat(vec.vec[1]); - utility::LittleFloat(vec.vec[2]); + utility::LittleFloat(tmp.vec[0]); + utility::LittleFloat(tmp.vec[1]); + utility::LittleFloat(tmp.vec[2]); } - writeUBytes((atUint8*)&vec, 12); + writeUBytes((atUint8*)&tmp, 12); } - inline void writeVal(atVec3f val) {writeVec3f(val);} + inline void writeVal(const atVec3f& val) {writeVec3f(val);} /** @brief Writes an atVec3f (12 bytes) to the buffer and advances the buffer. * It also swaps the bytes against little depending on the platform. * * @param vec The value to write to the buffer */ - inline void writeVec3fLittle(atVec3f vec) + inline void writeVec3fLittle(const atVec3f& vec) { - utility::LittleFloat(vec.vec[0]); - utility::LittleFloat(vec.vec[1]); - utility::LittleFloat(vec.vec[2]); - writeUBytes((atUint8*)&vec, 12); + atVec3f tmp = vec; + utility::LittleFloat(tmp.vec[0]); + utility::LittleFloat(tmp.vec[1]); + utility::LittleFloat(tmp.vec[2]); + writeUBytes((atUint8*)&tmp, 12); } - inline void writeValLittle(atVec3f val) {writeVec3fLittle(val);} + inline void writeValLittle(const atVec3f& val) {writeVec3fLittle(val);} /** @brief Writes an atVec3f (12 bytes) to the buffer and advances the buffer. * It also swaps the bytes against big depending on the platform. * * @param vec The value to write to the buffer */ - inline void writeVec3fBig(atVec3f vec) + inline void writeVec3fBig(const atVec3f& vec) { - utility::BigFloat(vec.vec[0]); - utility::BigFloat(vec.vec[1]); - utility::BigFloat(vec.vec[2]); - writeUBytes((atUint8*)&vec, 12); + atVec3f tmp = vec; + utility::BigFloat(tmp.vec[0]); + utility::BigFloat(tmp.vec[1]); + utility::BigFloat(tmp.vec[2]); + writeUBytes((atUint8*)&tmp, 12); } - inline void writeValBig(atVec3f val) {writeVec3fBig(val);} + inline void writeValBig(const atVec3f& val) {writeVec3fBig(val);} /** @brief Writes an atVec4f (16 bytes) to the buffer and advances the buffer. * It also swaps the bytes depending on the platform and Stream settings. * * @param vec The value to write to the buffer */ - inline void writeVec4f(atVec4f vec) + inline void writeVec4f(const atVec4f& vec) { + atVec4f tmp = vec; if (m_endian == BigEndian) { - utility::BigFloat(vec.vec[0]); - utility::BigFloat(vec.vec[1]); - utility::BigFloat(vec.vec[2]); - utility::BigFloat(vec.vec[3]); + utility::BigFloat(tmp.vec[0]); + utility::BigFloat(tmp.vec[1]); + utility::BigFloat(tmp.vec[2]); + utility::BigFloat(tmp.vec[3]); } else { - utility::LittleFloat(vec.vec[0]); - utility::LittleFloat(vec.vec[1]); - utility::LittleFloat(vec.vec[2]); - utility::LittleFloat(vec.vec[3]); + utility::LittleFloat(tmp.vec[0]); + utility::LittleFloat(tmp.vec[1]); + utility::LittleFloat(tmp.vec[2]); + utility::LittleFloat(tmp.vec[3]); } - writeUBytes((atUint8*)&vec, 16); + writeUBytes((atUint8*)&tmp, 16); } - inline void writeVal(atVec4f val) {writeVec4f(val);} + inline void writeVal(const atVec4f& val) {writeVec4f(val);} /** @brief Writes an atVec4f (16 bytes) to the buffer and advances the buffer. * It also swaps the bytes against little depending on the platform. * * @param vec The value to write to the buffer */ - inline void writeVec4fLittle(atVec4f vec) + inline void writeVec4fLittle(const atVec4f& vec) { - utility::LittleFloat(vec.vec[0]); - utility::LittleFloat(vec.vec[1]); - utility::LittleFloat(vec.vec[2]); - utility::LittleFloat(vec.vec[3]); - writeUBytes((atUint8*)&vec, 16); + atVec4f tmp = vec; + utility::LittleFloat(tmp.vec[0]); + utility::LittleFloat(tmp.vec[1]); + utility::LittleFloat(tmp.vec[2]); + utility::LittleFloat(tmp.vec[3]); + writeUBytes((atUint8*)&tmp, 16); } - inline void writeValLittle(atVec4f val) {writeVec4fLittle(val);} + inline void writeValLittle(const atVec4f& val) {writeVec4fLittle(val);} /** @brief Writes an atVec4f (16 bytes) to the buffer and advances the buffer. * It also swaps the bytes against big depending on the platform. * * @param vec The value to write to the buffer */ - inline void writeVec4fBig(atVec4f vec) + inline void writeVec4fBig(const atVec4f& vec) { - utility::BigFloat(vec.vec[0]); - utility::BigFloat(vec.vec[1]); - utility::BigFloat(vec.vec[2]); - utility::BigFloat(vec.vec[3]); - writeUBytes((atUint8*)&vec, 16); + atVec4f tmp = vec; + utility::BigFloat(tmp.vec[0]); + utility::BigFloat(tmp.vec[1]); + utility::BigFloat(tmp.vec[2]); + utility::BigFloat(tmp.vec[3]); + writeUBytes((atUint8*)&tmp, 16); } - inline void writeValBig(atVec4f val) {writeVec4fBig(val);} + inline void writeValBig(const atVec4f& val) {writeVec4fBig(val);} /** @brief Writes an atVec2d (16 bytes) to the buffer and advances the buffer. * It also swaps the bytes depending on the platform and Stream settings. * * @param vec The value to write to the buffer */ - inline void writeVec2d(atVec2d vec) + inline void writeVec2d(const atVec2d& vec) { + atVec2d tmp = vec; if (m_endian == BigEndian) { - utility::BigDouble(vec.vec[0]); - utility::BigDouble(vec.vec[1]); + utility::BigDouble(tmp.vec[0]); + utility::BigDouble(tmp.vec[1]); } else { - utility::LittleDouble(vec.vec[0]); - utility::LittleDouble(vec.vec[1]); + utility::LittleDouble(tmp.vec[0]); + utility::LittleDouble(tmp.vec[1]); } - writeUBytes((atUint8*)&vec, 16); + writeUBytes((atUint8*)&tmp, 16); } - inline void writeVal(atVec2d val) {writeVec2d(val);} + inline void writeVal(const atVec2d& val) {writeVec2d(val);} /** @brief Writes an atVec2d (16 bytes) to the buffer and advances the buffer. * It also swaps the bytes against little depending on the platform. * * @param vec The value to write to the buffer */ - inline void writeVec2dLittle(atVec2d vec) + inline void writeVec2dLittle(const atVec2d& vec) { - utility::LittleDouble(vec.vec[0]); - utility::LittleDouble(vec.vec[1]); - writeUBytes((atUint8*)&vec, 16); + atVec2d tmp = vec; + utility::LittleDouble(tmp.vec[0]); + utility::LittleDouble(tmp.vec[1]); + writeUBytes((atUint8*)&tmp, 16); } - inline void writeValLittle(atVec2d val) {writeVec2dLittle(val);} + inline void writeValLittle(const atVec2d& val) {writeVec2dLittle(val);} /** @brief Writes an atVec2d (16 bytes) to the buffer and advances the buffer. * It also swaps the bytes against big depending on the platform. * * @param vec The value to write to the buffer */ - inline void writeVec2dBig(atVec2d vec) + inline void writeVec2dBig(const atVec2d& vec) { - utility::BigDouble(vec.vec[0]); - utility::BigDouble(vec.vec[1]); - writeUBytes((atUint8*)&vec, 16); + atVec2d tmp = vec; + utility::BigDouble(tmp.vec[0]); + utility::BigDouble(tmp.vec[1]); + writeUBytes((atUint8*)&tmp, 16); } - inline void writeValBig(atVec2d val) {writeVec2dBig(val);} + inline void writeValBig(const atVec2d& val) {writeVec2dBig(val);} /** @brief Writes an atVec3d (24 bytes) to the buffer and advances the buffer. * It also swaps the bytes depending on the platform and Stream settings. * * @param vec The value to write to the buffer */ - inline void writeVec3d(atVec3d vec) + inline void writeVec3d(const atVec3d& vec) { + atVec3d tmp = vec; if (m_endian == BigEndian) { - utility::BigDouble(vec.vec[0]); - utility::BigDouble(vec.vec[1]); - utility::BigDouble(vec.vec[2]); + utility::BigDouble(tmp.vec[0]); + utility::BigDouble(tmp.vec[1]); + utility::BigDouble(tmp.vec[2]); } else { - utility::LittleDouble(vec.vec[0]); - utility::LittleDouble(vec.vec[1]); - utility::LittleDouble(vec.vec[2]); + utility::LittleDouble(tmp.vec[0]); + utility::LittleDouble(tmp.vec[1]); + utility::LittleDouble(tmp.vec[2]); } - writeUBytes((atUint8*)&vec, 24); + writeUBytes((atUint8*)&tmp, 24); } - inline void writeVal(atVec3d val) {writeVec3d(val);} + inline void writeVal(const atVec3d& val) {writeVec3d(val);} /** @brief Writes an atVec3d (24 bytes) to the buffer and advances the buffer. * It also swaps the bytes against little depending on the platform. * * @param vec The value to write to the buffer */ - inline void writeVec3dLittle(atVec3d vec) + inline void writeVec3dLittle(const atVec3d& vec) { - utility::LittleDouble(vec.vec[0]); - utility::LittleDouble(vec.vec[1]); - utility::LittleDouble(vec.vec[2]); - writeUBytes((atUint8*)&vec, 24); + atVec3d tmp = vec; + utility::LittleDouble(tmp.vec[0]); + utility::LittleDouble(tmp.vec[1]); + utility::LittleDouble(tmp.vec[2]); + writeUBytes((atUint8*)&tmp, 24); } - inline void writeValLittle(atVec3d val) {writeVec3dLittle(val);} + inline void writeValLittle(const atVec3d& val) {writeVec3dLittle(val);} /** @brief Writes an atVec3d (24 bytes) to the buffer and advances the buffer. * It also swaps the bytes against big depending on the platform. * * @param vec The value to write to the buffer */ - inline void writeVec3dBig(atVec3d vec) + inline void writeVec3dBig(const atVec3d& vec) { - utility::BigDouble(vec.vec[0]); - utility::BigDouble(vec.vec[1]); - utility::BigDouble(vec.vec[2]); - writeUBytes((atUint8*)&vec, 24); + atVec3d tmp = vec; + utility::BigDouble(tmp.vec[0]); + utility::BigDouble(tmp.vec[1]); + utility::BigDouble(tmp.vec[2]); + writeUBytes((atUint8*)&tmp, 24); } - inline void writeValBig(atVec3d val) {writeVec3dBig(val);} + inline void writeValBig(const atVec3d& val) {writeVec3dBig(val);} /** @brief Writes an atVec4d (32 bytes) to the buffer and advances the buffer. * It also swaps the bytes depending on the platform and Stream settings. * * @param vec The value to write to the buffer */ - inline void writeVec4d(atVec4d vec) + inline void writeVec4d(const atVec4d& vec) { + atVec4d tmp = vec; if (m_endian == BigEndian) { - utility::BigDouble(vec.vec[0]); - utility::BigDouble(vec.vec[1]); - utility::BigDouble(vec.vec[2]); - utility::BigDouble(vec.vec[3]); + utility::BigDouble(tmp.vec[0]); + utility::BigDouble(tmp.vec[1]); + utility::BigDouble(tmp.vec[2]); + utility::BigDouble(tmp.vec[3]); } else { - utility::LittleDouble(vec.vec[0]); - utility::LittleDouble(vec.vec[1]); - utility::LittleDouble(vec.vec[2]); - utility::LittleDouble(vec.vec[3]); + utility::LittleDouble(tmp.vec[0]); + utility::LittleDouble(tmp.vec[1]); + utility::LittleDouble(tmp.vec[2]); + utility::LittleDouble(tmp.vec[3]); } - writeUBytes((atUint8*)&vec, 32); + writeUBytes((atUint8*)&tmp, 32); } - inline void writeVal(atVec4d val) {writeVec4d(val);} + inline void writeVal(const atVec4d& val) {writeVec4d(val);} /** @brief Writes an atVec4d (32 bytes) to the buffer and advances the buffer. * It also swaps the bytes against little depending on the platform. * * @param vec The value to write to the buffer */ - inline void writeVec4dLittle(atVec4d vec) + inline void writeVec4dLittle(const atVec4d& vec) { - utility::LittleDouble(vec.vec[0]); - utility::LittleDouble(vec.vec[1]); - utility::LittleDouble(vec.vec[2]); - utility::LittleDouble(vec.vec[3]); - writeUBytes((atUint8*)&vec, 32); + atVec4d tmp = vec; + utility::LittleDouble(tmp.vec[0]); + utility::LittleDouble(tmp.vec[1]); + utility::LittleDouble(tmp.vec[2]); + utility::LittleDouble(tmp.vec[3]); + writeUBytes((atUint8*)&tmp, 32); } - inline void writeValLittle(atVec4d val) {writeVec4dLittle(val);} + inline void writeValLittle(const atVec4d& val) {writeVec4dLittle(val);} /** @brief Writes an atVec4d (32 bytes) to the buffer and advances the buffer. * It also swaps the bytes against big depending on the platform. * * @param vec The value to write to the buffer */ - inline void writeVec4dBig(atVec4d vec) + inline void writeVec4dBig(const atVec4d& vec) { - utility::BigDouble(vec.vec[0]); - utility::BigDouble(vec.vec[1]); - utility::BigDouble(vec.vec[2]); - utility::BigDouble(vec.vec[3]); - writeUBytes((atUint8*)&vec, 32); + atVec4d tmp = vec; + utility::BigDouble(tmp.vec[0]); + utility::BigDouble(tmp.vec[1]); + utility::BigDouble(tmp.vec[2]); + utility::BigDouble(tmp.vec[3]); + writeUBytes((atUint8*)&tmp, 32); } - inline void writeValBig(atVec4d val) {writeVec4dBig(val);} + inline void writeValBig(const atVec4d& val) {writeVec4dBig(val);} /** @brief Converts a UTF8 string to a wide-char string in the buffer and advances the buffer. * It also swaps the bytes depending on the platform and Stream settings. diff --git a/include/Athena/Types.hpp b/include/Athena/Types.hpp index 1278acb..26d8495 100644 --- a/include/Athena/Types.hpp +++ b/include/Athena/Types.hpp @@ -58,7 +58,7 @@ void* operator new[](size_t bytes) noexcept \ void operator delete(void* buf) noexcept \ {_mm_free(buf);} \ void operator delete[](void* buf) noexcept \ -{_mm_free(buf);} \ +{_mm_free(buf);} typedef union alignas(16) { diff --git a/include/Athena/Utility.hpp b/include/Athena/Utility.hpp index 13e4a14..b629e7e 100644 --- a/include/Athena/Utility.hpp +++ b/include/Athena/Utility.hpp @@ -208,6 +208,11 @@ std::string& rtrim(std::string& s); // trim from both ends std::string& trim(std::string& s); atUint64 fileSize(const std::string& filename); + +std::string wideToUtf8(const std::wstring& src); + +std::wstring utf8ToWide(const std::string& src); + } // utility } // Athena #endif diff --git a/src/Athena/FileReader.cpp b/src/Athena/FileReader.cpp index cb0c115..a94bbac 100644 --- a/src/Athena/FileReader.cpp +++ b/src/Athena/FileReader.cpp @@ -14,26 +14,32 @@ namespace Athena namespace io { FileReader::FileReader(const std::string& filename, atInt32 cacheSize) - : m_filename(filename), - m_fileHandle(nullptr), + : m_fileHandle(nullptr), m_cacheData(nullptr), m_offset(0) { +#if _WIN32 + m_filename = utility::utf8ToWide(filename); +#else + m_filename = fiename; +#endif open(); setCacheSize(cacheSize); } -#if _WIN32 FileReader::FileReader(const std::wstring& filename, atInt32 cacheSize) - : m_wfilename(filename), - m_fileHandle(nullptr), + : m_fileHandle(nullptr), m_cacheData(nullptr), m_offset(0) { +#if _WIN32 + m_filename = filename; +#else + m_filename = utility::wideToUtf8(filename); +#endif open(); setCacheSize(cacheSize); } -#endif FileReader::~FileReader() { @@ -44,17 +50,14 @@ FileReader::~FileReader() void FileReader::open() { #if _WIN32 - if (m_wfilename.size()) - m_fileHandle = _wfopen(m_wfilename.c_str(), L"rb"); - else - m_fileHandle = fopen(m_filename.c_str(), "rb"); + m_fileHandle = _wfopen(m_filename.c_str(), L"rb"); #else m_fileHandle = fopen(m_filename.c_str(), "rb"); #endif if (!m_fileHandle) { - atError("File not found '%s'", m_filename.c_str()); + atError("File not found '%s'", filename()); setError(); return; } @@ -136,7 +139,11 @@ atUint64 FileReader::length() const return 0; } +#if _WIN32 + return utility::fileSize(utility::wideToUtf8(m_filename)); +#else return utility::fileSize(m_filename); +#endif } atUint64 FileReader::readUBytesToBuf(void* buf, atUint64 len) diff --git a/src/Athena/FileWriter.cpp b/src/Athena/FileWriter.cpp index 12cf013..a59635d 100644 --- a/src/Athena/FileWriter.cpp +++ b/src/Athena/FileWriter.cpp @@ -14,22 +14,28 @@ namespace Athena namespace io { FileWriter::FileWriter(const std::string& filename, bool overwrite) - : m_filename(filename), - m_fileHandle(NULL), + : m_fileHandle(NULL), m_bytePosition(0) { +#if _WIN32 + m_filename = utility::utf8ToWide(filename); +#else + m_filename = fiename; +#endif open(overwrite); } -#if _WIN32 FileWriter::FileWriter(const std::wstring& filename, bool overwrite) - : m_wfilename(filename), - m_fileHandle(NULL), - m_bytePosition(0) + : m_fileHandle(NULL), + m_bytePosition(0) { +#if _WIN32 + m_filename = filename; +#else + m_filename = utility::wideToUtf8(filename); +#endif open(overwrite); } -#endif FileWriter::~FileWriter() { @@ -40,32 +46,20 @@ FileWriter::~FileWriter() void FileWriter::open(bool overwrite) { #if _WIN32 - if (m_wfilename.size()) - { - if (overwrite) - m_fileHandle = _wfopen(m_wfilename.c_str(), L"w+b"); - else - m_fileHandle = _wfopen(m_wfilename.c_str(), L"r+b"); - } + if (overwrite) + m_fileHandle = _wfopen(m_filename.c_str(), L"w+b"); else - { - if (overwrite) - m_fileHandle = fopen(m_filename.c_str(), "w+b"); - else - m_fileHandle = fopen(m_filename.c_str(), "r+b"); - } + m_fileHandle = _wfopen(m_filename.c_str(), L"r+b"); #else if (overwrite) m_fileHandle = fopen(m_filename.c_str(), "w+b"); else - m_fileHandle = fopen(m_filename.c_str(), "r+b"); + m_fileHandle = fopen(m_filename.c_str(), "r+b"); #endif - - if (!m_fileHandle) { - atError("Unable to open file '%s'", m_filename.c_str()); + atError("Unable to open file '%s'", filename()); setError(); return; } @@ -104,7 +98,11 @@ atUint64 FileWriter::position() const atUint64 FileWriter::length() const { +#if _WIN32 + return utility::fileSize(utility::wideToUtf8(m_filename)); +#else return utility::fileSize(m_filename); +#endif } void FileWriter::writeUBytes(const atUint8* data, atUint64 len) diff --git a/src/Athena/Utility.cpp b/src/Athena/Utility.cpp index 0ef8a72..81f5568 100644 --- a/src/Athena/Utility.cpp +++ b/src/Athena/Utility.cpp @@ -10,6 +10,7 @@ #include #include #include +#include "utf8proc.h" #ifdef _MSC_VER #include @@ -207,5 +208,43 @@ atUint64 rand64() return r0 | r1 | r2 | r3; } +std::string wideToUtf8(const std::wstring& src) +{ + std::string retval; + retval.reserve(src.length()); + for (wchar_t ch : 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"); + return retval; + } + retval.append(reinterpret_cast(mb), c); + } + return retval; +} + +std::wstring utf8ToWide(const std::string& src) +{ + std::wstring retval; + retval.reserve(src.length()); + const utf8proc_uint8_t* buf = reinterpret_cast(src.c_str()); + while (*buf) + { + utf8proc_int32_t wc; + utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc); + if (len < 0) + { + atWarning("invalid UTF-8 character while decoding"); + return retval; + } + buf += len; + retval += wchar_t(wc); + } + return retval; +} + } // utility } // Athena From ab86e2195fceab0fcfddfe2795922609c87ff714 Mon Sep 17 00:00:00 2001 From: Phillip Date: Fri, 13 Nov 2015 21:59:17 -0800 Subject: [PATCH 4/7] Fix Typo --- src/Athena/FileReader.cpp | 24 ++++++++++++------------ src/Athena/FileWriter.cpp | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Athena/FileReader.cpp b/src/Athena/FileReader.cpp index a94bbac..e788db8 100644 --- a/src/Athena/FileReader.cpp +++ b/src/Athena/FileReader.cpp @@ -21,7 +21,7 @@ FileReader::FileReader(const std::string& filename, atInt32 cacheSize) #if _WIN32 m_filename = utility::utf8ToWide(filename); #else - m_filename = fiename; + m_filename = filename; #endif open(); setCacheSize(cacheSize); @@ -29,8 +29,8 @@ FileReader::FileReader(const std::string& filename, atInt32 cacheSize) FileReader::FileReader(const std::wstring& filename, atInt32 cacheSize) : m_fileHandle(nullptr), - m_cacheData(nullptr), - m_offset(0) + m_cacheData(nullptr), + m_offset(0) { #if _WIN32 m_filename = filename; @@ -88,15 +88,15 @@ void FileReader::seek(atInt64 pos, SeekOrigin origin) atUint64 oldOff = m_offset; switch(origin) { - case SeekOrigin::Begin: - m_offset = pos; - break; - case SeekOrigin::Current: - m_offset += pos; - break; - case SeekOrigin::End: - m_offset = length() - pos; - break; + case SeekOrigin::Begin: + m_offset = pos; + break; + case SeekOrigin::Current: + m_offset += pos; + break; + case SeekOrigin::End: + m_offset = length() - pos; + break; } if (m_offset > length()) { diff --git a/src/Athena/FileWriter.cpp b/src/Athena/FileWriter.cpp index a59635d..abd3549 100644 --- a/src/Athena/FileWriter.cpp +++ b/src/Athena/FileWriter.cpp @@ -20,7 +20,7 @@ FileWriter::FileWriter(const std::string& filename, bool overwrite) #if _WIN32 m_filename = utility::utf8ToWide(filename); #else - m_filename = fiename; + m_filename = filename; #endif open(overwrite); } From 64f34cc9030145dc8ddeadc80d90f42ed3162675 Mon Sep 17 00:00:00 2001 From: Phillip Date: Sat, 14 Nov 2015 00:47:05 -0800 Subject: [PATCH 5/7] Fix error handling --- src/Athena/FileReader.cpp | 6 +++++- src/Athena/FileWriter.cpp | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Athena/FileReader.cpp b/src/Athena/FileReader.cpp index e788db8..5cdf8e3 100644 --- a/src/Athena/FileReader.cpp +++ b/src/Athena/FileReader.cpp @@ -57,7 +57,8 @@ void FileReader::open() if (!m_fileHandle) { - atError("File not found '%s'", filename()); + std::string _filename = filename(); + atError("File not found '%s'", _filename.c_str()); setError(); return; } @@ -82,6 +83,9 @@ void FileReader::close() void FileReader::seek(atInt64 pos, SeekOrigin origin) { + if (!isOpen()) + return; + // check block position if (m_blockSize > 0) { diff --git a/src/Athena/FileWriter.cpp b/src/Athena/FileWriter.cpp index abd3549..d6f579a 100644 --- a/src/Athena/FileWriter.cpp +++ b/src/Athena/FileWriter.cpp @@ -84,6 +84,13 @@ void FileWriter::close() void FileWriter::seek(atInt64 pos, SeekOrigin origin) { + if (!isOpen()) + { + atError("Unable to seek in file, not open"); + setError(); + return; + } + if (fseeko64(m_fileHandle, pos, (int)origin) != 0) { atError("Unable to seek in file"); From f248d1746eefcb9f6990b1f4a48c07a951b8d357 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sat, 14 Nov 2015 13:39:18 -1000 Subject: [PATCH 6/7] proper string-size computation for binarySize --- atdna/main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/atdna/main.cpp b/atdna/main.cpp index 114f07c..1073022 100644 --- a/atdna/main.cpp +++ b/atdna/main.cpp @@ -841,7 +841,7 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor } } - if (sizeExprStr.size()) + if (sizeExprStr.size() && sizeExprStr.compare("-1")) fileOut << " __isz += (" << sizeExprStr << ");\n"; else fileOut << " __isz += " << fieldName << ".size() + 1;\n"; @@ -879,7 +879,7 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor ++idx; } - if (sizeExprStr.size()) + if (sizeExprStr.size() && sizeExprStr.compare("-1")) fileOut << " __isz += (" << sizeExprStr << ") * 2;\n"; else fileOut << " __isz += (" << fieldName << ".size() + 1) * 2;\n"; @@ -913,7 +913,7 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor } - if (sizeExprStr.size()) + if (sizeExprStr.size() && sizeExprStr.compare("-1")) fileOut << " __isz += (" << sizeExprStr << ") * 2;\n"; else fileOut << " __isz += (" << fieldName << ".size() + 1) * 2;\n"; From df43b72f8ac860c6662896cd97498a4091a6174d Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sat, 14 Nov 2015 13:59:10 -1000 Subject: [PATCH 7/7] Minor fix; added PRISize macro --- include/Athena/Global.hpp | 6 ++++++ src/Athena/FileWriter.cpp | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/Athena/Global.hpp b/include/Athena/Global.hpp index 9659c12..304f189 100644 --- a/include/Athena/Global.hpp +++ b/include/Athena/Global.hpp @@ -22,6 +22,12 @@ #define S_ISLNK(m) 0 #endif +#define PRISize "Iu" + +#else + +#define PRISize "zu" + #endif #ifndef AT_PRETTY_FUNCTION diff --git a/src/Athena/FileWriter.cpp b/src/Athena/FileWriter.cpp index d6f579a..3d916d5 100644 --- a/src/Athena/FileWriter.cpp +++ b/src/Athena/FileWriter.cpp @@ -59,7 +59,7 @@ void FileWriter::open(bool overwrite) if (!m_fileHandle) { - atError("Unable to open file '%s'", filename()); + atError("Unable to open file '%s'", filename().c_str()); setError(); return; }