字符集转换(更新)

当工程中混用stl与非stl模式的时候,原来的代码容易产生问题。

主要原因是因为stl与非stl版本的函数名相同。故此稍作修改,让他们可以一定程度并存下来。

C++代码
  1. #pragma once   
  2.   
  3. #include <malloc.h>   
  4. #include <windows.h>   
  5.   
  6. #define USE_LIN_CHAR_SET_VARIANT char* _lin_dst = 0; wchar_t* _lin_wdst = 0; int _lin_dst_size = 0;   
  7.   
  8. #define LIN_CHAR_SET_NO_WARNING  _lin_dst; _lin_wdst;_lin_dst_size;   
  9.   
  10. #define USE_LIN_CHAR_SET USE_LIN_CHAR_SET_VARIANT;LIN_CHAR_SET_NO_WARNING   
  11.   
  12.   
  13. #ifdef LIN_CHAR_SET_USE_STL   
  14.   
  15. #include <string>   
  16. std::string _w2a_s( const std::wstring& pwcsSrc, int codepage)   
  17. {   
  18.     USE_LIN_CHAR_SET;   
  19.     _lin_dst_size = pwcsSrc.size()*3;   
  20.     _lin_dst = new (std::nothrowchar[ _lin_dst_size];   
  21.     if( _lin_dst == 0) return std::string();   
  22.   
  23.     _lin_dst_size = WideCharToMultiByte( codepage, 0, pwcsSrc.c_str(), pwcsSrc.length(), _lin_dst, _lin_dst_size, 0, 0);   
  24.   
  25.     if( _lin_dst_size == 0)    
  26.     {   
  27.         delete[] _lin_dst;   
  28.         return std::string();   
  29.     }   
  30.   
  31.     _lin_dst[_lin_dst_size] = 0;   
  32.     std::string result = _lin_dst;   
  33.     delete[] _lin_dst;   
  34.     return result;   
  35. }   
  36.   
  37. std::wstring _a2w_s( const std::string& pszSrc, int codepage)   
  38. {   
  39.     USE_LIN_CHAR_SET;   
  40.     _lin_dst_size = pszSrc.size()*2;   
  41.     _lin_wdst = new (std::nothrowwchar_t [ _lin_dst_size];   
  42.     if( _lin_wdst == 0) return std::wstring();   
  43.   
  44.     _lin_dst_size = MultiByteToWideChar( codepage, 0, pszSrc.c_str(), pszSrc.length(), _lin_wdst,_lin_dst_size);   
  45.   
  46.     if( _lin_dst_size == 0)   
  47.     {   
  48.         delete[] _lin_wdst;   
  49.         return std::wstring();   
  50.     }   
  51.   
  52.     _lin_wdst[_lin_dst_size] = 0;   
  53.     std::wstring result = _lin_wdst;   
  54.     delete[] _lin_wdst;   
  55.     return result;   
  56. }   
  57.   
  58. #define _utf82w( pszSrc) _a2w_s( pszSrc, CP_UTF8)   
  59. #define _w2utf8( pszSrc) _w2a_s( pszSrc, CP_UTF8)   
  60.   
  61. #define _utf72w( pszSrc) _a2w_s( pszSrc, CP_UTF7)   
  62. #define _w2utf7( pszSrc) _w2a_s( pszSrc, CP_UTF7)   
  63.   
  64.   
  65. // 导出的宏   
  66. #define a2w(pszSrc) _a2w_s(pszSrc, CP_ACP)   
  67. #define w2a(tcsSrc) _w2a_s(tcsSrc, CP_ACP)   
  68.   
  69. #else   
  70. #define _w2a( pwcsSrc, codepage) ( pwcsSrc == 0? 0:( \   
  71.     (_lin_dst = (char*)_alloca( ((_lin_dst_size = (int)wcslen( pwcsSrc))+ 1)*3))== 0? 0 : (\   
  72.     (_lin_dst_size = WideCharToMultiByte( codepage, 0, pwcsSrc, _lin_dst_size, _lin_dst, _lin_dst_size*3, 0, 0))==0? \   
  73.     ((_lin_dst[0]=0)==0 ? _lin_dst:0): (\   
  74.     (_lin_dst[_lin_dst_size] = 0) == 0? _lin_dst : 0))))   
  75.   
  76. #define _a2w( pszSrc, codepage) (pszSrc == 0? 0 : (\   
  77.     (_lin_wdst = (wchar_t*)_alloca( ((_lin_dst_size = (int)strlen( pszSrc)) + 1)*2)) == 0? 0 : (\   
  78.     (_lin_dst_size = MultiByteToWideChar( codepage, 0, pszSrc, _lin_dst_size, _lin_wdst,_lin_dst_size + sizeof(wchar_t))) == 0? \   
  79.     ((_lin_wdst[0]=0)==0? _lin_wdst:0) : (\   
  80.     (_lin_wdst[ _lin_dst_size] = 0) == 0? _lin_wdst: 0))))   
  81.   
  82. #define _utf82w( pszSrc) _a2w( pszSrc, CP_UTF8)   
  83. #define _w2utf8( pszSrc) _w2a( pszSrc, CP_UTF8)   
  84.   
  85. #define _utf72w( pszSrc) _a2w( pszSrc, CP_UTF7)   
  86. #define _w2utf7( pszSrc) _w2a( pszSrc, CP_UTF7)   
  87.   
  88.   
  89. // 导出的宏   
  90. #define a2w(pszSrc) _a2w(pszSrc, CP_ACP)   
  91. #define w2a(tcsSrc) _w2a(tcsSrc, CP_ACP)   
  92.   
  93. #endif   
  94.   
  95. #ifdef UNICODE   
  96. #define a2t(pszSrc) a2w(pszSrc)   
  97. #define t2a(tcsSrc) w2a(tcsSrc)   
  98. #define w2t(wcsSrc) wcsSrc;   
  99. #define t2w(tcsSrc) tcsSrc;   
  100. #else   
  101. #define a2t(pszSrc) pszSrc   
  102. #define t2a(tcsSrc) tcsSrc   
  103. #define w2t(wcsSrc) wta(wcsSrc)   
  104. #define t2w(tcsSrc) a2w(tcsSrc)   
  105. #endif   
  106.   
  107. #define w2utf8(wcsSrc) _w2utf8(wcsSrc)   
  108. #define utf82w(pszSrc) _utf82w(pszSrc)   
  109. #define w2utf7(wcsSrc) _w2utf7(wcsSrc)   
  110. #define utf72w(pszSrc) _utf72w(pszSrc)   
  111.   
  112. #define a2utf8(pszSrc) w2utf8(a2w(pszSrc))   
  113. #define a2utf7(pszSrc) w2utf7(a2w(pszSrc))   
  114. #define utf82a(pszSrc) w2a(utf82w(pszSrc))   
  115. #define utf72a(pszSrc) w2a(utf72w(pszSrc))   
  116. #define t2utf8(tcsSrc) w2utf8(tcsSrc)   
  117. #define t2utf7(tcsSrc) w2utf7(tcsSrc)   
  118. #define utf82t(pszSrc) w2t(utf82w(pszSrc))   
  119. #define utf72t(pszSrc) w2t(utf72w(pszSrc))   


[本日志由 ClassyK 于 2009-08-17 01:51 PM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.