CStringLocaleHelper : 一个用于UTF8和UNICODE字符串转换的辅助类

PomeloWu原创©,转载请注明出处

微软抛弃MBCS看来是必然的选择。VS2005默认创建的工程都是UNICODE的。但无论是std::string/std::wstring还是CString,都没有提供不同字符集间转换的包装。

写CStringLocaleHelper本意是辅助Linux和Windows通讯时UTF-8和UNICODE的转换,实际上对它稍加改变,就能面向更多的code page,让人从WINDOWS程序中无趣的字符集转换之中解放出来。

CStringLocaleHelper其实就是对调用WideCharToMultiByte/MultiByteToWideChar的封装,可以返回指定字符集(目前是UTF-8或者UNICODE)的字符串或者std::string和std::wstring。此外,重载了针对LPCTSTR、std::string和std::wstring的operator (),方便类型强制转化;重载了operator =以便增加CStringLocaleHelper的灵活性。


 以下是具体代码:

   
/* ------------------------------------------------------------- @filename: StringLocaleHelper.h @author: Pomelo Wu @purpose: A helper string class, handling string conversions between UTF-8 and UNICODE @revision history: -- Date Ver. Modifier Description-- 2006/03/15 0.1 Pomelo created 2006/03/29 0.2 Pomelo return type modified 2006/04/03 0.3 Pomelo cast operator added // -----------------------------------------------------------*/ #ifndef __STRINGLOCALEHELPER_H__ #define __STRINGLOCALEHELPER_H__ using namespace std; class CStringLocaleHelper { public: CStringLocaleHelper(); CStringLocaleHelper(string str); CStringLocaleHelper(wstring str); ~CStringLocaleHelper(void); public: const char * GetLocaleBuffer() const { return m_strLocale.c_str(); } const wchar_t * GetUnicodeBuffer() const { return m_strUnicode.c_str(); } const string& GetLocaleString() const { return m_strLocale; } const wstring& GetUnicodeString() const { return m_strUnicode; } operator LPCTSTR () const; operator string () const; operator wstring () const; CStringLocaleHelper & operator = (string strSrc); CStringLocaleHelper & operator = (wstring strSrc); protected: string m_strLocale; wstring m_strUnicode; }; #endif // __STRINGLOCALEHELPER_H__ /* ------------------------------------------------------------- @filename: StringLocaleHelper.cpp @author: Pomelo Wu @purpose: The implementation of CStringLocaleHelper @revision history: -- Date Ver. Modifier Description-- 2006/03/15 0.1 Pomelo created 2006/03/29 0.2 Pomelo return type modified 2006/04/03 0.3 Pomelo cast operator added // -----------------------------------------------------------*/ // ----------------------- Using strings ----------------------- #include <string> // --------------------- Unicode functions --------------------- #include <Windows.h> #include "StringLocaleHelper.h" // // // native functions int Unicode2Locale(const wchar_t *pSrc, char *pDest = NULL, int nCount = 0) { if (pSrc == NULL) // invalid parameter return -1; if (pDest == NULL) // queries the count return WideCharToMultiByte(CP_UTF8, 0, pSrc, -1, NULL, 0, NULL, NULL); memset(pDest, 0, sizeof(char) * (nCount + 1)); return WideCharToMultiByte(CP_UTF8, 0, pSrc, -1, pDest, nCount, NULL, NULL); } int Locale2Unicode(const char *pSrc, wchar_t *pDest = NULL, int nCount = 0) { if (pSrc == NULL) // invalid parameter return -1; if (pDest == NULL) // queries the count return MultiByteToWideChar(CP_UTF8, NULL, pSrc, -1, NULL, 0); memset(pDest, 0, sizeof(wchar_t) * (nCount + 1)); return MultiByteToWideChar(CP_UTF8, NULL, pSrc, -1, pDest, nCount + 1); } // // // constructors / destructors CStringLocaleHelper::CStringLocaleHelper() { m_strLocale.clear(); m_strUnicode.clear(); } CStringLocaleHelper::CStringLocaleHelper(string str) { m_strLocale = str; int nCount = Locale2Unicode(m_strLocale.c_str()); wchar_t *pwcText = new wchar_t [nCount + 1]; if (pwcText) { nCount = Locale2Unicode(str.c_str(), pwcText, nCount); m_strUnicode = pwcText; delete [] pwcText; } } CStringLocaleHelper::CStringLocaleHelper(wstring str) { m_strUnicode = str; int nCount = Unicode2Locale(str.c_str()); char *pBufferAnsi = new char[nCount + 1]; if (pBufferAnsi) { nCount = Unicode2Locale(str.c_str(), pBufferAnsi, nCount); m_strLocale = pBufferAnsi; delete [] pBufferAnsi; } } CStringLocaleHelper::~CStringLocaleHelper(void) { } // // // operators CStringLocaleHelper::operator LPCTSTR() const { #ifdef _UNICODE return m_strUnicode.c_str(); #else return m_strLocale.c_str(); #endif } CStringLocaleHelper::operator string () const { return m_strLocale; } CStringLocaleHelper::operator wstring () const { return m_strUnicode; } CStringLocaleHelper & CStringLocaleHelper::operator =(std::string strSrc) { m_strLocale = strSrc; int nCount = Locale2Unicode(strSrc.c_str()); wchar_t *pwcText = new wchar_t [nCount + 1]; if (pwcText) { nCount = Locale2Unicode(strSrc.c_str(), pwcText, nCount); m_strUnicode = pwcText; delete [] pwcText; } return *this; } CStringLocaleHelper & CStringLocaleHelper::operator =(std::wstring strSrc) { m_strUnicode = strSrc; int nCount = Unicode2Locale(strSrc.c_str()); char *pcText = new char [nCount + 1]; if (pcText) { nCount = Unicode2Locale(strSrc.c_str(), pcText, nCount); m_strLocale = pcText; delete [] pcText; } return *this; }
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页