converter.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 BOOST_LOCALE_SOURCE
  9. #include <locale>
  10. #include <stdexcept>
  11. #include <boost/locale/generator.hpp>
  12. #include <boost/locale/conversion.hpp>
  13. #include <boost/locale/encoding.hpp>
  14. #include <vector>
  15. #include <string.h>
  16. #include "api.hpp"
  17. #include "all_generator.hpp"
  18. namespace boost {
  19. namespace locale {
  20. namespace impl_win {
  21. class utf16_converter : public converter<wchar_t>
  22. {
  23. public:
  24. utf16_converter(winlocale const &lc,size_t refs = 0) :
  25. converter<wchar_t>(refs),
  26. lc_(lc)
  27. {
  28. }
  29. virtual std::wstring convert(converter_base::conversion_type how,wchar_t const *begin,wchar_t const *end,int flags = 0) const
  30. {
  31. switch(how) {
  32. case converter_base::upper_case:
  33. return towupper_l(begin,end,lc_);
  34. case converter_base::lower_case:
  35. return towlower_l(begin,end,lc_);
  36. case converter_base::case_folding:
  37. return wcsfold(begin,end);
  38. case converter_base::normalization:
  39. return wcsnormalize(static_cast<norm_type>(flags),begin,end);
  40. default:
  41. return std::wstring(begin,end-begin);
  42. }
  43. }
  44. private:
  45. winlocale lc_;
  46. };
  47. class utf8_converter : public converter<char> {
  48. public:
  49. utf8_converter(winlocale const &lc,size_t refs = 0) :
  50. converter<char>(refs),
  51. lc_(lc)
  52. {
  53. }
  54. virtual std::string convert(converter_base::conversion_type how,char const *begin,char const *end,int flags = 0) const
  55. {
  56. std::wstring tmp = conv::to_utf<wchar_t>(begin,end,"UTF-8");
  57. wchar_t const *wb=tmp.c_str();
  58. wchar_t const *we=wb+tmp.size();
  59. std::wstring res;
  60. switch(how) {
  61. case upper_case:
  62. res = towupper_l(wb,we,lc_);
  63. break;
  64. case lower_case:
  65. res = towlower_l(wb,we,lc_);
  66. break;
  67. case case_folding:
  68. res = wcsfold(wb,we);
  69. break;
  70. case normalization:
  71. res = wcsnormalize(static_cast<norm_type>(flags),wb,we);
  72. break;
  73. default:
  74. res = tmp; // make gcc happy
  75. }
  76. return conv::from_utf(res,"UTF-8");
  77. }
  78. private:
  79. winlocale lc_;
  80. };
  81. std::locale create_convert( std::locale const &in,
  82. winlocale const &lc,
  83. character_facet_type type)
  84. {
  85. switch(type) {
  86. case char_facet:
  87. return std::locale(in,new utf8_converter(lc));
  88. case wchar_t_facet:
  89. return std::locale(in,new utf16_converter(lc));
  90. default:
  91. return in;
  92. }
  93. }
  94. } // namespace impl_win32
  95. } // locale
  96. } // boost
  97. // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4