diff --git a/common/libex/include/ex/ex_str.h b/common/libex/include/ex/ex_str.h index 4fc9ce4..ca9cea9 100644 --- a/common/libex/include/ex/ex_str.h +++ b/common/libex/include/ex/ex_str.h @@ -65,7 +65,7 @@ typedef std::wostringstream ex_woss; typedef std::vector ex_astrs; typedef std::vector ex_wstrs; -typedef std::vector ex_str_utf16le; +//typedef std::vector ex_str_utf16le; bool ex_wstr2astr(const ex_wstr& in_str, ex_astr& out_str, int code_page = EX_CODEPAGE_DEFAULT); bool ex_wstr2astr(const wchar_t* in_str, ex_astr& out_str, int code_page = EX_CODEPAGE_DEFAULT); @@ -81,8 +81,22 @@ void ex_remove_white_space(ex_wstr& str_fix, int ulFlag = EX_RSC_ALL); ex_astr& ex_replace_all(ex_astr& str, const ex_astr& old_value, const ex_astr& new_value); ex_wstr& ex_replace_all(ex_wstr& str, const ex_wstr& old_value, const ex_wstr& new_value); +class ex_str_utf16le { +public: + ex_str_utf16le(); + ~ex_str_utf16le(); + + size_t length() const; + bool from_utf8(const ex_astr& from); + + const uint16_t* c_str() const; + +protected: + std::vector m_data; +}; + // 将UTF8字符串转换为UTF16-LE字符串(输出结果包含\0结束符) -bool ex_utf8_to_utf16le(const std::string& from, ex_str_utf16le& to); +//bool ex_utf8_to_utf16le(const ex_astr& from, ex_str_utf16le& to); #endif diff --git a/common/libex/src/ex_str.cpp b/common/libex/src/ex_str.cpp index 1aa6046..b88fabc 100644 --- a/common/libex/src/ex_str.cpp +++ b/common/libex/src/ex_str.cpp @@ -835,21 +835,48 @@ static int MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteS #endif - - -bool ex_utf8_to_utf16le(const std::string& from, ex_str_utf16le& to) -{ - int iSize = MultiByteToWideChar(CP_UTF8, 0, from.c_str(), -1, NULL, 0); - if (iSize <= 0) - return false; - - //++iSize; - to.resize(iSize); - memset(&to[0], 0, sizeof(ex_utf16)); - - MultiByteToWideChar(CP_UTF8, 0, from.c_str(), -1, &to[0], iSize); - - return true; +ex_str_utf16le::ex_str_utf16le() { + m_data.resize(1); + memset(&m_data[0], 0, 1); } +ex_str_utf16le::~ex_str_utf16le() {} + +size_t ex_str_utf16le::length() const { + return m_data.size() - 1; +} + +bool ex_str_utf16le::from_utf8(const ex_astr& from) { + int iSize = MultiByteToWideChar(CP_UTF8, 0, from.c_str(), -1, NULL, 0); + // 注意iSize包括\0结束符 + if (iSize <= 0) + return false; + + m_data.resize(iSize); + memset(&m_data[0], 0, sizeof(ex_utf16)); + + MultiByteToWideChar(CP_UTF8, 0, from.c_str(), -1, (LPWSTR)&m_data[0], iSize); + + return true; +} + +const uint16_t* ex_str_utf16le::c_str() const { + return &m_data[0]; +} + +// bool ex_utf8_to_utf16le(const ex_astr& from, ex_str_utf16le& to) +// { +// int iSize = MultiByteToWideChar(CP_UTF8, 0, from.c_str(), -1, NULL, 0); +// if (iSize <= 0) +// return false; +// +// //++iSize; +// to.resize(iSize); +// memset(&to[0], 0, sizeof(ex_utf16)); +// +// MultiByteToWideChar(CP_UTF8, 0, from.c_str(), -1, &to[0], iSize); +// +// return true; +// } + #endif diff --git a/common/teleport/teleport_const.h b/common/teleport/teleport_const.h index 1f33d72..df55075 100644 --- a/common/teleport/teleport_const.h +++ b/common/teleport/teleport_const.h @@ -113,7 +113,8 @@ #define TPE_UNKNOWN_CMD 124 // 未知的命令 #define TPE_JSON_FORMAT 125 // 错误的JSON格式(需要JSON格式数据,但是却无法按JSON格式解码) #define TPE_PARAM 126 // 参数错误 -#define TPE_DATA 127 // 数据错误 +#define TPE_INVALID_DATA 127 // 数据错误 +#define TPE_UNEXPECTED_DATA 128 // 不是期望的数据 // #define TPE_OPENFILE_ERROR 0x1007 // 无法打开文件 // #define TPE_GETTEMPPATH_ERROR 0x1007