diff --git a/core/Common/String.cpp b/core/Common/String.cpp index 45821af1..864b3a30 100644 --- a/core/Common/String.cpp +++ b/core/Common/String.cpp @@ -4,21 +4,6 @@ #pragma comment(lib, "comsuppw.lib") -// 将 Unicode 字符串转化为 ANSI 字符串 -static std::string ConvertWide2Ansi(const wchar_t* wstr) -{ - std::string str = static_cast(_bstr_t(wstr)); - return std::move(str); -} - -// 将 ANSI 字符串转化为 Unicode 字符串 -static std::wstring ConvertAnsi2Wide(const char* cstr) -{ - std::wstring str = static_cast(_bstr_t(cstr)); - return std::move(str); -} - - e2d::String::String() : m_str(L"") { @@ -30,12 +15,7 @@ e2d::String::String(const wchar_t *str) } e2d::String::String(const char *cstr) - : m_str(ConvertAnsi2Wide(cstr)) -{ -} - -e2d::String::String(const std::string &str) - : m_str(ConvertAnsi2Wide(str.c_str())) + : m_str(static_cast(_bstr_t(cstr))) { } @@ -49,11 +29,6 @@ e2d::String::String(const e2d::String &str) { } -e2d::String::String(const std::wstring &str) - : m_str(str) -{ -} - e2d::String::~String() { m_str.clear(); @@ -67,7 +42,7 @@ e2d::String &e2d::String::operator=(const wchar_t *str) e2d::String & e2d::String::operator=(const char *cstr) { - m_str = ConvertAnsi2Wide(cstr); + m_str = static_cast(_bstr_t(cstr)); return *this; } @@ -77,18 +52,6 @@ e2d::String & e2d::String::operator=(const String &str) return *this; } -e2d::String & e2d::String::operator=(const std::wstring &str) -{ - m_str = str; - return *this; -} - -e2d::String & e2d::String::operator=(const std::string &str) -{ - m_str = ConvertAnsi2Wide(str.c_str()); - return *this; -} - bool e2d::String::operator==(const wchar_t *str) { if (str) @@ -119,11 +82,6 @@ bool e2d::String::operator ==(const e2d::String &str) return m_str == str.m_str; } -bool e2d::String::operator==(const std::wstring &str) -{ - return m_str == str; -} - bool e2d::String::operator!=(const wchar_t *str) { if (str) @@ -154,11 +112,6 @@ bool e2d::String::operator!=(const e2d::String &str) return m_str != str.m_str; } -bool e2d::String::operator!=(const std::wstring &str) -{ - return m_str != str; -} - wchar_t &e2d::String::operator[](int index) { return m_str[static_cast(index)]; @@ -174,7 +127,7 @@ e2d::String e2d::String::operator+(const wchar_t *str) e2d::String e2d::String::operator+(const char *str) { String temp; - temp.m_str = m_str + ::ConvertAnsi2Wide(str); + temp.m_str = m_str + static_cast(_bstr_t(str)); return std::move(temp); } @@ -185,13 +138,6 @@ e2d::String e2d::String::operator+(const e2d::String &str) return std::move(temp); } -e2d::String e2d::String::operator+(const std::wstring &str) -{ - String temp; - temp.m_str = m_str + str; - return std::move(temp); -} - e2d::String e2d::operator+(const wchar_t *str1, const e2d::String &str2) { String temp; @@ -202,14 +148,7 @@ e2d::String e2d::operator+(const wchar_t *str1, const e2d::String &str2) e2d::String e2d::operator+(const char *str1, const String &str2) { String temp; - temp.m_str = ::ConvertAnsi2Wide(str1) + str2.m_str; - return std::move(temp); -} - -e2d::String e2d::operator+(const std::wstring &str1, const e2d::String &str2) -{ - String temp; - temp.m_str = str1 + str2.m_str; + temp.m_str = static_cast(_bstr_t(str1)) + str2.m_str; return std::move(temp); } @@ -221,7 +160,7 @@ e2d::String & e2d::String::operator+=(const wchar_t *str) e2d::String & e2d::String::operator+=(const char *str) { - m_str += ::ConvertAnsi2Wide(str); + m_str += static_cast(_bstr_t(str)); return (*this); } @@ -231,12 +170,6 @@ e2d::String & e2d::String::operator+=(const String &str) return (*this); } -e2d::String & e2d::String::operator+=(const std::wstring &str) -{ - m_str += str; - return (*this); -} - bool e2d::String::operator>(const String &str) const { return m_str > str.m_str; @@ -292,11 +225,6 @@ e2d::String::operator wchar_t*() const return const_cast(m_str.c_str()); } -e2d::String::operator const char*() const -{ - return ::ConvertWide2Ansi(m_str.c_str()).c_str(); -} - bool e2d::String::isEmpty() const { return m_str.empty(); @@ -319,6 +247,17 @@ unsigned int e2d::String::getHashCode() const return hash; } +std::wstring e2d::String::getWString() const +{ + return m_str; +} + +std::string e2d::String::getCString() const +{ + std::string str = static_cast(_bstr_t(m_str.c_str())); + return std::move(str); +} + e2d::String e2d::String::toUpper() const { String str(*this); @@ -427,13 +366,13 @@ e2d::String & e2d::String::append(wchar_t * str) e2d::String & e2d::String::append(const char * str) { - m_str += ::ConvertAnsi2Wide(str); + m_str += static_cast(_bstr_t(str)); return *this; } e2d::String & e2d::String::append(char * str) { - m_str += ::ConvertAnsi2Wide(str); + m_str += static_cast(_bstr_t(str)); return *this; } @@ -443,28 +382,29 @@ e2d::String & e2d::String::append(const e2d::String & str) return (*this); } -std::wostream & e2d::operator<<(std::wostream &cout, String &str) +std::wostream & e2d::operator<<(std::wostream &cout, const String &str) { cout << str.m_str; return cout; } -std::wistream & e2d::operator>>(std::wistream &cin, e2d::String &str) +std::wistream & e2d::operator>>(std::wistream &cin, String &str) { cin >> str.m_str; return cin; } -std::ostream & e2d::operator<<(std::ostream &cout, String &str) +std::ostream & e2d::operator<<(std::ostream &cout, const String &str) { - cout << ::ConvertWide2Ansi(str.m_str.c_str()); + std::string cstr = static_cast(_bstr_t(str.m_str.c_str())); + cout << cstr; return cout; } -std::istream & e2d::operator>>(std::istream &cin, e2d::String &str) +std::istream & e2d::operator>>(std::istream &cin, String &str) { std::string temp; cin >> temp; - str.m_str = ::ConvertAnsi2Wide(temp.c_str()); + str.m_str = static_cast(_bstr_t(temp.c_str())); return cin; } \ No newline at end of file diff --git a/core/Tool/Data.cpp b/core/Tool/Data.cpp index 669486d8..d6cd2ef4 100644 --- a/core/Tool/Data.cpp +++ b/core/Tool/Data.cpp @@ -49,7 +49,7 @@ e2d::String e2d::Data::getString(const String & key, const String & defaultValue return temp; } -void e2d::Data::setDefaultFileName(const String & fileName) +void e2d::Data::setDataFilePath(const String & fileName) { if (!fileName.isEmpty()) { diff --git a/core/Tool/File.cpp b/core/Tool/File.cpp index bb33a671..026efa41 100644 --- a/core/Tool/File.cpp +++ b/core/Tool/File.cpp @@ -39,7 +39,7 @@ e2d::String e2d::Path::getTempPath() // 创建临时文件目录 e2d::String tempFilePath; - tempFilePath << path << L"Easy2DGameTemp"; + tempFilePath << path << L"Easy2DGameTemp\\"; // 创建文件夹 if (!Path::createFolder(tempFilePath)) { @@ -51,13 +51,12 @@ e2d::String e2d::Path::getTempPath() if (!sAppName.isEmpty()) { // 创建文件夹 - if (!Path::createFolder(tempFilePath + L"\\" + sAppName)) + if (!Path::createFolder(tempFilePath + sAppName + L"\\")) { return std::move(tempFilePath); } - tempFilePath << L"\\" << sAppName; + tempFilePath << sAppName << L"\\"; } - tempFilePath << L"\\"; return std::move(tempFilePath); } diff --git a/core/ecommon.h b/core/ecommon.h index 205a5a7f..525dbd63 100644 --- a/core/ecommon.h +++ b/core/ecommon.h @@ -89,9 +89,9 @@ class String { public: String(); - String(const wchar_t *); String(const String &); - String(const std::wstring &); + String(const char *); + String(const wchar_t *); String(String &&); ~String(); @@ -105,6 +105,27 @@ public: // 获取该字符串的散列值 unsigned int getHashCode() const; + // 获取 Unicode 字符串 + std::wstring getWString() const; + + // 获取 ANSI 字符串 + std::string getCString() const; + + // 后接字符串 + String& append( + const String &str + ); + + // 后接字符串 + String& append( + const char * cstr + ); + + // 后接字符串 + String& append( + char * cstr + ); + // 后接字符串 String& append( const wchar_t *str @@ -115,20 +136,9 @@ public: wchar_t *str ); - // 后接字符串 - String& append( - const String &str - ); - // 后接字符串 template - String& append(const T &value) - { - std::wostringstream ss; - ss << value; - m_str += ss.str(); - return (*this); - } + String& append(const T &value); // 获取裁剪字符串 String subtract( @@ -166,76 +176,58 @@ public: // 将任意类型转化为字符串 template - static String toString(const T value) - { - std::wostringstream ss; - ss << value; - return ss.str(); - } + static String toString(const T value); - static String toString(const wchar_t * str) { return std::move(String(str)); } - static String toString(const char * str) { return std::move(String(str)); } - - String& operator= (const wchar_t *); + // 赋值运算符 String& operator= (const String &); - String& operator= (const std::wstring &); + String& operator= (const char *); + String& operator= (const wchar_t *); - bool operator== (const wchar_t *); - bool operator== (const String &); - bool operator== (const std::wstring &); - - bool operator!= (const wchar_t *); - bool operator!= (const String &); - bool operator!= (const std::wstring &); - - wchar_t &operator[] (int); - - String operator+ (const wchar_t *); - String operator+ (const String &); - String operator+ (const std::wstring &); - - friend String operator+ (const wchar_t*, const String &); - friend String operator+ (const std::wstring &, const String &); - - String& operator+= (const wchar_t *); + // 运算符 String& operator+= (const String &); - String& operator+= (const std::wstring &); + String& operator+= (const char *); + String& operator+= (const wchar_t *); + String operator+ (const String &); + String operator+ (const char *); + String operator+ (const wchar_t *); + // 友元运算符 + friend String operator+ (const char *, const String &); + friend String operator+ (const wchar_t*, const String &); + + // 类型转换操作符 + operator const wchar_t* () const; + operator wchar_t* () const; + + // 比较运算符 + bool operator== (const String &); + bool operator== (const char *); + bool operator== (const wchar_t *); + bool operator!= (const String &); + bool operator!= (const char *); + bool operator!= (const wchar_t *); bool operator> (const String &) const; bool operator>= (const String &) const; bool operator< (const String &) const; bool operator<= (const String &) const; + // << 运算符 String& operator<< (const String &); + String& operator<< (const char *); + String& operator<< (char *); String& operator<< (const wchar_t *); String& operator<< (wchar_t *); template String& operator<< (const T value) { return this->append<>(value); } - operator const wchar_t* () const; - operator wchar_t* () const; + // 其他运算符 + wchar_t &operator[] (int); - friend std::wostream& operator<< (std::wostream &, String &); - friend std::wistream& operator>> (std::wistream &, String &); + friend std::ostream& operator<< (std::ostream &, const String &); + friend std::wostream& operator<< (std::wostream &, const String &); - // 为 ANSI 作出的适应 - String(const char *); - String(const std::string &); - operator const char* () const; - String& operator= (const char *); - String& operator= (const std::string &); - bool operator== (const char *); - bool operator!= (const char *); - String operator+ (const char *); - friend String operator+ (const char *, const String &); - String& operator+= (const char *); - String& operator<< (const char *); - String& operator<< (char *); - String& append(const char *); - String& append(char *); - - friend std::ostream& operator<< (std::ostream &, String &); friend std::istream& operator>> (std::istream &, String &); + friend std::wistream& operator>> (std::wistream &, String &); private: std::wstring m_str; @@ -741,4 +733,23 @@ protected: VoidFunction m_callback; }; +// String 类模板函数定义 +template +inline e2d::String & e2d::String::append(const T & value) +{ + std::wostringstream ss; + ss << value; + m_str += ss.str(); + return (*this); +} + +template +inline e2d::String e2d::String::toString(const T value) +{ + std::wostringstream ss; + ss << value; + String str = ss.str().c_str(); + return std::move(str); +} + } \ No newline at end of file diff --git a/core/etools.h b/core/etools.h index f72a50e1..54e2ed4e 100644 --- a/core/etools.h +++ b/core/etools.h @@ -191,7 +191,7 @@ public: ); // 修改数据文件的默认名称 - static void setDefaultFileName( + static void setDataFilePath( const String & fileName );