ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

201 lines
7.5 KiB

  1. //
  2. // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. #define BOOSTER_SOURCE
  9. #include <booster/config.h>
  10. #if defined(BOOSTER_WIN_NATIVE) || defined(__CYGWIN__)
  11. #define BOOSTER_LOCALE_WITH_WCONV
  12. #endif
  13. #ifdef BOOSTER_LOCALE_WITH_ICONV
  14. #include "iconv_codepage.ipp"
  15. #endif
  16. #ifdef BOOSTER_LOCALE_WITH_ICU
  17. #include "uconv_codepage.ipp"
  18. #endif
  19. #ifdef BOOSTER_LOCALE_WITH_WCONV
  20. #include "wconv_codepage.ipp"
  21. #endif
  22. #include <booster/locale/encoding.h>
  23. #include <string>
  24. #include <cstring>
  25. #include <booster/auto_ptr_inc.h>
  26. namespace booster {
  27. namespace locale {
  28. namespace conv {
  29. namespace impl {
  30. std::string convert_between(char const *begin,
  31. char const *end,
  32. char const *to_charset,
  33. char const *from_charset,
  34. method_type how)
  35. {
  36. std::auto_ptr<converter_between> cvt;
  37. #ifdef BOOSTER_LOCALE_WITH_ICONV
  38. cvt.reset(new iconv_between());
  39. if(cvt->open(to_charset,from_charset,how))
  40. return cvt->convert(begin,end);
  41. #endif
  42. #ifdef BOOSTER_LOCALE_WITH_ICU
  43. cvt.reset(new uconv_between());
  44. if(cvt->open(to_charset,from_charset,how))
  45. return cvt->convert(begin,end);
  46. #endif
  47. #ifdef BOOSTER_LOCALE_WITH_WCONV
  48. cvt.reset(new wconv_between());
  49. if(cvt->open(to_charset,from_charset,how))
  50. return cvt->convert(begin,end);
  51. #endif
  52. throw invalid_charset_error(std::string(to_charset) + " or " + from_charset);
  53. }
  54. template<typename CharType>
  55. std::basic_string<CharType> convert_to(
  56. char const *begin,
  57. char const *end,
  58. char const *charset,
  59. method_type how)
  60. {
  61. std::auto_ptr<converter_to_utf<CharType> > cvt;
  62. #ifdef BOOSTER_LOCALE_WITH_ICONV
  63. cvt.reset(new iconv_to_utf<CharType>());
  64. if(cvt->open(charset,how))
  65. return cvt->convert(begin,end);
  66. #endif
  67. #ifdef BOOSTER_LOCALE_WITH_ICU
  68. cvt.reset(new uconv_to_utf<CharType>());
  69. if(cvt->open(charset,how))
  70. return cvt->convert(begin,end);
  71. #endif
  72. #ifdef BOOSTER_LOCALE_WITH_WCONV
  73. cvt.reset(new wconv_to_utf<CharType>());
  74. if(cvt->open(charset,how))
  75. return cvt->convert(begin,end);
  76. #endif
  77. throw invalid_charset_error(charset);
  78. }
  79. template<typename CharType>
  80. std::string convert_from(
  81. CharType const *begin,
  82. CharType const *end,
  83. char const *charset,
  84. method_type how)
  85. {
  86. std::auto_ptr<converter_from_utf<CharType> > cvt;
  87. #ifdef BOOSTER_LOCALE_WITH_ICONV
  88. cvt.reset(new iconv_from_utf<CharType>());
  89. if(cvt->open(charset,how))
  90. return cvt->convert(begin,end);
  91. #endif
  92. #ifdef BOOSTER_LOCALE_WITH_ICU
  93. cvt.reset(new uconv_from_utf<CharType>());
  94. if(cvt->open(charset,how))
  95. return cvt->convert(begin,end);
  96. #endif
  97. #ifdef BOOSTER_LOCALE_WITH_WCONV
  98. cvt.reset(new wconv_from_utf<CharType>());
  99. if(cvt->open(charset,how))
  100. return cvt->convert(begin,end);
  101. #endif
  102. throw invalid_charset_error(charset);
  103. }
  104. std::string normalize_encoding(char const *ccharset)
  105. {
  106. std::string charset;
  107. charset.reserve(std::strlen(ccharset));
  108. while(*ccharset!=0) {
  109. char c=*ccharset++;
  110. if('0' <= c && c<= '9')
  111. charset+=c;
  112. else if('a' <=c && c <='z')
  113. charset+=c;
  114. else if('A' <=c && c <='Z')
  115. charset+=char(c-'A'+'a');
  116. }
  117. return charset;
  118. }
  119. } // impl
  120. using namespace impl;
  121. std::string between(char const *begin,char const *end,
  122. std::string const &to_charset,std::string const &from_charset,method_type how)
  123. {
  124. return convert_between(begin,end,to_charset.c_str(),from_charset.c_str(),how);
  125. }
  126. template<>
  127. std::basic_string<char> to_utf(char const *begin,char const *end,std::string const &charset,method_type how)
  128. {
  129. return convert_to<char>(begin,end,charset.c_str(),how);
  130. }
  131. template<>
  132. std::string from_utf(char const *begin,char const *end,std::string const &charset,method_type how)
  133. {
  134. return convert_from<char>(begin,end,charset.c_str(),how);
  135. }
  136. template<>
  137. std::basic_string<wchar_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how)
  138. {
  139. return convert_to<wchar_t>(begin,end,charset.c_str(),how);
  140. }
  141. template<>
  142. std::string from_utf(wchar_t const *begin,wchar_t const *end,std::string const &charset,method_type how)
  143. {
  144. return convert_from<wchar_t>(begin,end,charset.c_str(),how);
  145. }
  146. #ifdef BOOSTER_HAS_CHAR16_T
  147. template<>
  148. std::basic_string<char16_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how)
  149. {
  150. return convert_to<char16_t>(begin,end,charset.c_str(),how);
  151. }
  152. template<>
  153. std::string from_utf(char16_t const *begin,char16_t const *end,std::string const &charset,method_type how)
  154. {
  155. return convert_from<char16_t>(begin,end,charset.c_str(),how);
  156. }
  157. #endif
  158. #ifdef BOOSTER_HAS_CHAR32_T
  159. template<>
  160. std::basic_string<char32_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how)
  161. {
  162. return convert_to<char32_t>(begin,end,charset.c_str(),how);
  163. }
  164. template<>
  165. std::string from_utf(char32_t const *begin,char32_t const *end,std::string const &charset,method_type how)
  166. {
  167. return convert_from<char32_t>(begin,end,charset.c_str(),how);
  168. }
  169. #endif
  170. }
  171. }
  172. }
  173. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4