From 63a58265eff1c53dffe6d93af5f5fb1ecc14c86a Mon Sep 17 00:00:00 2001 From: Phillip Date: Fri, 13 Nov 2015 21:53:13 -0800 Subject: [PATCH] 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