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