convert narrow characters to wide characters

TAconverUTP.h

#ifndef TACONVERTUTF_H_ECEB822A_469E_49d3_B337_8F3397A22E3E #define TACONVERTUTF_H_ECEB822A_469E_49d3_B337_8F3397A22E3E #include "core/synchronisation/src/ReEntrantThreadLockable.h" #include "core/synchronisation/src/ThreadGuard.h" #include <string> namespace TA_Base_Core { char * convert (const wchar_t *wstr); wchar_t * convert (const char *str); class TAConvertUTF { public: #ifdef _UNICODE static wchar_t* toWideString(const char* pStr); static std::wstring toWideString(const std::string& Str); static std::string toNarrowString(const std::wstring& Str); static char* toNarrowString(const wchar_t* pStr); #else static const char* toWideString(const char* pStr); static std::string toWideString(const std::string& str); static std::string toNarrowString(const std::string& Str); static const char* toNarrowString(const char* pStr); #endif #ifdef _UNICODE static std::wstring wcserror(int errNo); #endif //static const int MAX_ARG_NUM; //static TA_Base_Core::ReEntrantThreadLockable m_lock; }; } #endif // TACONVERTUTF_H

 

 

 

TAconverUTP.cpp

  #include "core/utilities/src/TAConvertUTF.h" #include "core/utilities/src/DebugUtil.h" #ifdef SOLARIS #include <iconv.h> #else #include "ace/ACE.h" #endif #define MAX_ARG_NUM 100 using namespace std; namespace TA_Base_Core { #ifdef WIN32 char * convert (const wchar_t *wstr) { // Short circuit null pointer case if (wstr == 0) return 0; int len = ::WideCharToMultiByte (CP_OEMCP, 0, wstr, -1, 0, 0, 0, 0); #if defined (ACE_LACKS_WCSLEN) const wchar_t *wtemp = wstr; while (wtemp != 0) { ++wtemp; } int len = wtemp - wstr + 1; #endif char *str = new char[len]; ::WideCharToMultiByte (CP_OEMCP, 0, wstr, -1, str, len, 0, 0); return str; } wchar_t * convert (const char *str) { // Short circuit null pointer case if (str == 0) return 0; int len = ::MultiByteToWideChar (CP_OEMCP, 0, str, -1, 0, 0); wchar_t *wstr = new wchar_t[len]; ::MultiByteToWideChar (CP_OEMCP, 0, str, -1, wstr, len); return wstr; } #endif #if defined SOLARIS && _UNICODE const static char * LOCAL_PAGE_SET = "GBK"; const static char * UNICODE_PAGE_SET = "UTF-32BE"; //unicode code page name working with Windows OS. int wchar2char(/*in*/const wchar_t* in,int in_len,/*out*/char* out,int out_max) { size_t result; iconv_t env; env = iconv_open(LOCAL_PAGE_SET, UNICODE_PAGE_SET);//tocode_page, fromcode_page result = iconv(env,(const char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max); iconv_close(env); return (int) result; } int wstring2string(/*in*/const wstring& in,/*out*/string& out) { int len = in.length() + 1; int result; char* pBuffer = new char[len*3]; memset(pBuffer,0,len*3); result = wchar2char(in.c_str(),in.length() * sizeof(wchar_t),pBuffer,len*3); //printf("wstring2string result is %d,errno is %s/n",result,strerror(errno)); if(result >= 0) { out = pBuffer; } else { out = ""; } delete[] pBuffer; return result; } int char2wchar(/*in*/const char* in,int in_len, /*out*/wchar_t* out,int out_max) { size_t result; iconv_t env; env = iconv_open(UNICODE_PAGE_SET, LOCAL_PAGE_SET); result = iconv(env,(const char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max); iconv_close(env); return (int) result; } int string2wstring(/*in*/const string& in,/*out*/wstring& out) { int len = in.length() + 1; int result; //wstring temp; wchar_t* pBuffer = new wchar_t[len]; memset(pBuffer,0,len*sizeof(wchar_t)); //tocode_page, fromcode_page result = char2wchar(in.c_str(),in.length(),pBuffer,len*sizeof(wchar_t)); //printf("string2wstring result is %d,errno is %s/n",result,strerror(errno)); if(result >= 0) { out = pBuffer; } else { out.clear(); } delete[] pBuffer; return result; } #endif #ifdef _UNICODE std::wstring TAConvertUTF::toWideString(const std::string& Str) { try { #ifdef WIN32 wchar_t* pWchar = convert(Str.c_str()); std::wstring wstr(pWchar); delete []pWchar; return wstr; #elif defined SOLARIS std::wstring wStr; string2wstring(Str, wStr); return wStr; #elif TA_ASSERT(false, _T("toNarrowString does not support current OS platform.")); #endif } catch(...) { LOG_GENERIC(SourceInfo, TA_Base_Core::DebugUtil::DebugFatal, _T("an unknown error is in TAConvertUTF::toWideString(const std::string& Str).")); } return _T(""); } //the caller must release the memory after use this function. wchar_t* TAConvertUTF::toWideString(const char* pStr) { wchar_t* ret =NULL; int result = 0; try { #ifdef WIN32 ret = convert(pStr); #elif defined SOLARIS int len = strlen(pStr) + 1; wchar_t* pBuffer = new wchar_t[len]; memset(pBuffer,0,len*sizeof(wchar_t)); result = char2wchar(pStr, strlen(pStr), pBuffer, len*sizeof(wchar_t)); ret = pBuffer; #elif TA_ASSERT(false, _T("toWideString does not support current OS platform.")); #endif } catch(...) { LOG_GENERIC(SourceInfo, TA_Base_Core::DebugUtil::DebugFatal, _T("an unknown error is in TAConvertUTF::toWideString(const char* pStr).")); LOG_GENERIC(SourceInfo, TA_Base_Core::DebugUtil::ExceptionCatch, _T("toNarrowString result is %d,errno is %s/n"), result, strerror(errno) ); } return ret; } std::string TAConvertUTF::toNarrowString(const std::wstring& Str) { try { #ifdef WIN32 char* pChar = NULL; pChar = convert(Str.c_str()); std::string strNorrow(pChar); delete[] pChar; pChar = NULL; return strNorrow; #elif defined SOLARIS std::string strNorrow; wstring2string(Str, strNorrow); return strNorrow; #elif TA_ASSERT(false, _T("toNarrowString does not support current OS platform.")); #endif }catch(...) { LOG_GENERIC(SourceInfo, TA_Base_Core::DebugUtil::DebugFatal, _T("an unknown error is in TAConvertUTF::toNarrowString(const std::wstring& Str).")); } return ""; } //the caller must release the memory after use this function. char* TAConvertUTF::toNarrowString(const wchar_t* pStr) { //TA_Base_Core::ThreadGuard guard (m_lock); char * ret = NULL; int result = 0; try { #ifdef WIN32 ret = convert(pStr); #elif defined SOLARIS int len = wcslen(pStr) + 1; char* pBuffer = new char[len * 3]; //in case of that many language code want three bytes to store the one word. memset(pBuffer, 0, len * 3); result = wchar2char(pStr, wcslen(pStr) * sizeof(wchar_t), pBuffer, len*3); ret = pBuffer; #elif TA_ASSERT(false, _T("toNarrowString does not support current OS platform.")); #endif } catch(...) { LOG_GENERIC(SourceInfo, TA_Base_Core::DebugUtil::DebugFatal, _T("an unknown error is in TAConvertUTF::toNarrowString(const wchar_t* pStr).")); LOG_GENERIC(SourceInfo, TA_Base_Core::DebugUtil::ExceptionCatch, _T("toNarrowString result is %d,errno is %s/n"), result, strerror(errno) ); } return ret; } #else std::string TAConvertUTF::toWideString(const std::string& Str) { return Str; } const char* TAConvertUTF::toWideString(const char* pStr) { return pStr; } std::string TAConvertUTF::toNarrowString(const std::string& Str) { return Str; } const char* TAConvertUTF::toNarrowString(const char* pStr) { return pStr; } #endif #ifdef _UNICODE std::wstring TAConvertUTF::wcserror(int errNo) { std::string errorMsg = strerror( errno ); return TA_Base_Core::TAConvertUTF::toWideString(errorMsg); } #endif }

你可能感兴趣的:(convert narrow characters to wide characters)