2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
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)
8 #define BOOST_LOCALE_SOURCE
10 #if defined(__FreeBSD__)
15 #include <boost/locale/generator.hpp>
16 #include <boost/locale/conversion.hpp>
17 #include <boost/locale/encoding.hpp>
18 #include <boost/shared_ptr.hpp>
24 #include "all_generator.hpp"
28 namespace impl_posix
{
30 template<typename CharType
>
34 struct case_traits
<char> {
35 static char lower(char c
,locale_t lc
)
37 return tolower_l(c
,lc
);
39 static char upper(char c
,locale_t lc
)
41 return toupper_l(c
,lc
);
46 struct case_traits
<wchar_t> {
47 static wchar_t lower(wchar_t c
,locale_t lc
)
49 return towlower_l(c
,lc
);
51 static wchar_t upper(wchar_t c
,locale_t lc
)
53 return towupper_l(c
,lc
);
58 template<typename CharType
>
59 class std_converter
: public converter
<CharType
>
62 typedef CharType char_type
;
63 typedef std::basic_string
<char_type
> string_type
;
64 typedef std::ctype
<char_type
> ctype_type
;
65 std_converter(boost::shared_ptr
<locale_t
> lc
,size_t refs
= 0) :
66 converter
<CharType
>(refs
),
70 virtual string_type
convert(converter_base::conversion_type how
,char_type
const *begin
,char_type
const *end
,int /*flags*/ = 0) const
73 case converter_base::upper_case
:
76 res
.reserve(end
-begin
);
78 res
+=case_traits
<char_type
>::upper(*begin
++,*lc_
);
82 case converter_base::lower_case
:
83 case converter_base::case_folding
:
86 res
.reserve(end
-begin
);
88 res
+=case_traits
<char_type
>::lower(*begin
++,*lc_
);
93 return string_type(begin
,end
-begin
);
97 boost::shared_ptr
<locale_t
> lc_
;
100 class utf8_converter
: public converter
<char> {
102 utf8_converter(boost::shared_ptr
<locale_t
> lc
,size_t refs
= 0) :
103 converter
<char>(refs
),
107 virtual std::string
convert(converter_base::conversion_type how
,char const *begin
,char const *end
,int /*flags*/ = 0) const
112 std::wstring tmp
= conv::to_utf
<wchar_t>(begin
,end
,"UTF-8");
114 wres
.reserve(tmp
.size());
115 for(unsigned i
=0;i
<tmp
.size();i
++)
116 wres
+=towupper_l(tmp
[i
],*lc_
);
117 return conv::from_utf
<wchar_t>(wres
,"UTF-8");
123 std::wstring tmp
= conv::to_utf
<wchar_t>(begin
,end
,"UTF-8");
125 wres
.reserve(tmp
.size());
126 for(unsigned i
=0;i
<tmp
.size();i
++)
127 wres
+=towlower_l(tmp
[i
],*lc_
);
128 return conv::from_utf
<wchar_t>(wres
,"UTF-8");
131 return std::string(begin
,end
-begin
);
135 boost::shared_ptr
<locale_t
> lc_
;
138 std::locale
create_convert( std::locale
const &in
,
139 boost::shared_ptr
<locale_t
> lc
,
140 character_facet_type type
)
145 std::string encoding
= nl_langinfo_l(CODESET
,*lc
);
146 for(unsigned i
=0;i
<encoding
.size();i
++)
147 if('A'<=encoding
[i
] && encoding
[i
]<='Z')
148 encoding
[i
]=encoding
[i
]-'A'+'a';
149 if(encoding
=="utf-8" || encoding
=="utf8" || encoding
=="utf_8") {
150 return std::locale(in
,new utf8_converter(lc
));
152 return std::locale(in
,new std_converter
<char>(lc
));
155 return std::locale(in
,new std_converter
<wchar_t>(lc
));
162 } // namespace impl_std
165 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4