From 7e01a863302ec0a56831fa899d5b243dcc090758 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Sat, 10 Jun 2017 08:40:27 -1000 Subject: [PATCH] Windows fixes --- hecl/include/hecl/SystemChar.hpp | 60 ++---------------------------- hecl/include/hecl/hecl.hpp | 64 ++++++++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 60 deletions(-) diff --git a/hecl/include/hecl/SystemChar.hpp b/hecl/include/hecl/SystemChar.hpp index 07bef3c1e..ab1fa0a4b 100644 --- a/hecl/include/hecl/SystemChar.hpp +++ b/hecl/include/hecl/SystemChar.hpp @@ -18,6 +18,10 @@ namespace hecl { +#if _WIN32 && UNICODE +#define HECL_UCS2 1 +#endif + #if HECL_UCS2 typedef wchar_t SystemChar; static inline size_t StrLen(const SystemChar* str) {return wcslen(str);} @@ -26,34 +30,6 @@ static inline void ToLower(SystemString& str) {std::transform(str.begin(), str.end(), str.begin(), towlower);} static inline void ToUpper(SystemString& str) {std::transform(str.begin(), str.end(), str.begin(), towupper);} -class SystemUTF8View -{ - std::string m_utf8; -public: - explicit SystemUTF8View(const SystemString& str) - : m_utf8(WideToUTF8(str)) {} - operator const std::string&() const {return m_utf8;} - const std::string& str() const {return m_utf8;} - const char* c_str() const {return m_utf8.c_str();} - std::string operator+(const std::string& other) const {return m_utf8 + other;} - std::string operator+(const char* other) const {return m_utf8 + other;} -}; -inline std::string operator+(const std::string& lhs, const SystemUTF8View& rhs) {return lhs + std::string(rhs);} -inline std::string operator+(const char* lhs, const SystemUTF8View& rhs) {return lhs + std::string(rhs);} -class SystemStringView -{ - std::wstring m_sys; -public: - explicit SystemStringView(const std::string& str) - : m_sys(UTF8ToWide(str)) {} - operator const std::wstring&() const {return m_sys;} - const std::wstring& sys_str() const {return m_sys;} - const SystemChar* c_str() const {return m_sys.c_str();} - std::wstring operator+(const std::wstring& other) const {return m_sys + other;} - std::wstring operator+(const wchar_t* other) const {return m_sys + other;} -}; -inline std::wstring operator+(const std::wstring& lhs, const SystemStringView& rhs) {return lhs + std::wstring(rhs);} -inline std::wstring operator+(const wchar_t* lhs, const SystemStringView& rhs) {return lhs + std::wstring(rhs);} #ifndef _S #define _S(val) L ## val #endif @@ -66,34 +42,6 @@ static inline void ToLower(SystemString& str) {std::transform(str.begin(), str.end(), str.begin(), tolower);} static inline void ToUpper(SystemString& str) {std::transform(str.begin(), str.end(), str.begin(), toupper);} -class SystemUTF8View -{ - const std::string& m_utf8; -public: - explicit SystemUTF8View(const SystemString& str) - : m_utf8(str) {} - operator const std::string&() const {return m_utf8;} - const std::string& str() const {return m_utf8;} - const char* c_str() const {return m_utf8.c_str();} - std::string operator+(const std::string& other) const {return std::string(m_utf8) + other;} - std::string operator+(const char* other) const {return std::string(m_utf8) + other;} -}; -inline std::string operator+(const std::string& lhs, const SystemUTF8View& rhs) {return lhs + std::string(rhs);} -inline std::string operator+(const char* lhs, const SystemUTF8View& rhs) {return lhs + std::string(rhs);} -class SystemStringView -{ - const std::string& m_sys; -public: - explicit SystemStringView(const std::string& str) - : m_sys(str) {} - operator const std::string&() const {return m_sys;} - const std::string& sys_str() const {return m_sys;} - const SystemChar* c_str() const {return m_sys.c_str();} - std::string operator+(const std::string& other) const {return m_sys + other;} - std::string operator+(const char* other) const {return m_sys + other;} -}; -inline std::string operator+(const std::string& lhs, const SystemStringView& rhs) {return lhs + std::string(rhs);} -inline std::string operator+(const char* lhs, const SystemStringView& rhs) {return lhs + std::string(rhs);} #ifndef _S #define _S(val) val #endif diff --git a/hecl/include/hecl/hecl.hpp b/hecl/include/hecl/hecl.hpp index bca079c1c..0d7ca3375 100644 --- a/hecl/include/hecl/hecl.hpp +++ b/hecl/include/hecl/hecl.hpp @@ -49,10 +49,6 @@ struct DataSpecEntry; extern unsigned VerbosityLevel; extern logvisor::Module LogModule; -#if _WIN32 && UNICODE -#define HECL_UCS2 1 -#endif - std::string WideToUTF8(const std::wstring& src); std::string Char16ToUTF8(const std::u16string& src); std::wstring Char16ToWide(const std::u16string& src); @@ -80,6 +76,66 @@ ENABLE_BITWISE_ENUM(HNScale) std::string HumanizeNumber(int64_t quotient, size_t len, const char* suffix, int scale, HNFlags flags); +#if HECL_UCS2 +class SystemUTF8View +{ + std::string m_utf8; +public: + explicit SystemUTF8View(const SystemString& str) + : m_utf8(WideToUTF8(str)) {} + operator const std::string&() const {return m_utf8;} + const std::string& str() const {return m_utf8;} + const char* c_str() const {return m_utf8.c_str();} + std::string operator+(const std::string& other) const {return m_utf8 + other;} + std::string operator+(const char* other) const {return m_utf8 + other;} +}; +inline std::string operator+(const std::string& lhs, const SystemUTF8View& rhs) {return lhs + std::string(rhs);} +inline std::string operator+(const char* lhs, const SystemUTF8View& rhs) {return lhs + std::string(rhs);} +class SystemStringView +{ + std::wstring m_sys; +public: + explicit SystemStringView(const std::string& str) + : m_sys(UTF8ToWide(str)) {} + operator const std::wstring&() const {return m_sys;} + const std::wstring& sys_str() const {return m_sys;} + const SystemChar* c_str() const {return m_sys.c_str();} + std::wstring operator+(const std::wstring& other) const {return m_sys + other;} + std::wstring operator+(const wchar_t* other) const {return m_sys + other;} +}; +inline std::wstring operator+(const std::wstring& lhs, const SystemStringView& rhs) {return lhs + std::wstring(rhs);} +inline std::wstring operator+(const wchar_t* lhs, const SystemStringView& rhs) {return lhs + std::wstring(rhs);} +#else +class SystemUTF8View +{ + const std::string& m_utf8; +public: + explicit SystemUTF8View(const SystemString& str) + : m_utf8(str) {} + operator const std::string&() const {return m_utf8;} + const std::string& str() const {return m_utf8;} + const char* c_str() const {return m_utf8.c_str();} + std::string operator+(const std::string& other) const {return std::string(m_utf8) + other;} + std::string operator+(const char* other) const {return std::string(m_utf8) + other;} +}; +inline std::string operator+(const std::string& lhs, const SystemUTF8View& rhs) {return lhs + std::string(rhs);} +inline std::string operator+(const char* lhs, const SystemUTF8View& rhs) {return lhs + std::string(rhs);} +class SystemStringView +{ + const std::string& m_sys; +public: + explicit SystemStringView(const std::string& str) + : m_sys(str) {} + operator const std::string&() const {return m_sys;} + const std::string& sys_str() const {return m_sys;} + const SystemChar* c_str() const {return m_sys.c_str();} + std::string operator+(const std::string& other) const {return m_sys + other;} + std::string operator+(const char* other) const {return m_sys + other;} +}; +inline std::string operator+(const std::string& lhs, const SystemStringView& rhs) {return lhs + std::string(rhs);} +inline std::string operator+(const char* lhs, const SystemStringView& rhs) {return lhs + std::string(rhs);} +#endif + void SanitizePath(std::string& path); void SanitizePath(std::wstring& path);