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
12 #include <boost/locale/formatting.hpp>
13 #include <boost/locale/generator.hpp>
14 #include <boost/locale/encoding.hpp>
15 #include <boost/shared_ptr.hpp>
23 #include "all_generator.hpp"
25 #include "../util/numeric.hpp"
32 std::ostreambuf_iterator
<wchar_t> write_it(std::ostreambuf_iterator
<wchar_t> out
,std::wstring
const &s
)
34 for(size_t i
=0;i
<s
.size();i
++)
39 std::ostreambuf_iterator
<char> write_it(std::ostreambuf_iterator
<char> out
,std::wstring
const &s
)
41 std::string tmp
= conv::from_utf(s
,"UTF-8");
42 for(size_t i
=0;i
<tmp
.size();i
++)
49 template<typename CharType
>
50 class num_format
: public util::base_num_format
<CharType
>
53 typedef typename
std::num_put
<CharType
>::iter_type iter_type
;
54 typedef std::basic_string
<CharType
> string_type
;
55 typedef CharType char_type
;
57 num_format(winlocale
const &lc
,size_t refs
= 0) :
58 util::base_num_format
<CharType
>(refs
),
65 iter_type
do_format_currency(bool /*intl*/,iter_type out
,std::ios_base
&ios
,char_type fill
,long double val
) const
68 std::locale loc
= ios
.getloc();
69 int digits
= std::use_facet
<std::moneypunct
<char_type
> >(loc
).frac_digits();
74 std::ios_base::fmtflags f
=ios
.flags();
75 ios
.flags(f
| std::ios_base::showbase
);
76 out
= std::use_facet
<std::money_put
<char_type
> >(loc
).put(out
,false,ios
,fill
,val
);
81 std::wstring cur
= wcsfmon_l(val
,lc_
);
82 return write_it(out
,cur
);
91 template<typename CharType
>
92 class time_put_win
: public std::time_put
<CharType
> {
94 time_put_win(winlocale
const &lc
, size_t refs
= 0) :
95 std::time_put
<CharType
>(refs
),
99 virtual ~time_put_win()
102 typedef typename
std::time_put
<CharType
>::iter_type iter_type
;
103 typedef CharType char_type
;
104 typedef std::basic_string
<char_type
> string_type
;
106 virtual iter_type
do_put( iter_type out
,
107 std::ios_base
&/*ios*/,
111 char /*modifier*/) const
113 return write_it(out
,wcsftime_l(format
,tm
,lc_
));
121 template<typename CharType
>
122 class num_punct_win
: public std::numpunct
<CharType
> {
124 typedef std::basic_string
<CharType
> string_type
;
125 num_punct_win(winlocale
const &lc
,size_t refs
= 0) :
126 std::numpunct
<CharType
>(refs
)
128 numeric_info np
= wcsnumformat_l(lc
) ;
129 if(sizeof(CharType
) == 1 && np
.thousands_sep
== L
"\xA0")
130 np
.thousands_sep
=L
" ";
132 to_str(np
.thousands_sep
,thousands_sep_
);
133 to_str(np
.decimal_point
,decimal_point_
);
134 grouping_
= np
.grouping
;
135 if(thousands_sep_
.size() > 1)
136 grouping_
= std::string();
137 if(decimal_point_
.size() > 1)
138 decimal_point_
= CharType('.');
141 void to_str(std::wstring
&s1
,std::wstring
&s2
)
146 void to_str(std::wstring
&s1
,std::string
&s2
)
148 s2
=conv::from_utf(s1
,"UTF-8");
150 virtual CharType
do_decimal_point() const
152 return *decimal_point_
.c_str();
154 virtual CharType
do_thousands_sep() const
156 return *thousands_sep_
.c_str();
158 virtual std::string
do_grouping() const
162 virtual string_type
do_truename() const
164 static const char t
[]="true";
165 return string_type(t
,t
+sizeof(t
)-1);
167 virtual string_type
do_falsename() const
169 static const char t
[]="false";
170 return string_type(t
,t
+sizeof(t
)-1);
173 string_type decimal_point_
;
174 string_type thousands_sep_
;
175 std::string grouping_
;
178 template<typename CharType
>
179 std::locale
create_formatting_impl(std::locale
const &in
,winlocale
const &lc
)
182 std::locale
tmp(in
,new std::numpunct_byname
<CharType
>("C"));
183 tmp
=std::locale(tmp
,new std::time_put_byname
<CharType
>("C"));
184 tmp
= std::locale(tmp
,new num_format
<CharType
>(lc
));
188 std::locale
tmp(in
,new num_punct_win
<CharType
>(lc
));
189 tmp
= std::locale(tmp
,new time_put_win
<CharType
>(lc
));
190 tmp
= std::locale(tmp
,new num_format
<CharType
>(lc
));
195 template<typename CharType
>
196 std::locale
create_parsing_impl(std::locale
const &in
,winlocale
const &lc
)
198 std::numpunct
<CharType
> *np
= 0;
200 np
= new std::numpunct_byname
<CharType
>("C");
202 np
= new num_punct_win
<CharType
>(lc
);
203 std::locale
tmp(in
,np
);
204 tmp
= std::locale(tmp
,new util::base_num_parse
<CharType
>());
209 std::locale
create_formatting( std::locale
const &in
,
211 character_facet_type type
)
215 return create_formatting_impl
<char>(in
,lc
);
217 return create_formatting_impl
<wchar_t>(in
,lc
);
223 std::locale
create_parsing( std::locale
const &in
,
225 character_facet_type type
)
229 return create_parsing_impl
<char>(in
,lc
);
231 return create_parsing_impl
<wchar_t>(in
,lc
);
245 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4