1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
//
// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#if defined(__FreeBSD__)
# include <xlocale.h>
#endif
#include <boost/locale/encoding.hpp>
#include <boost/locale/formatting.hpp>
#include <boost/locale/generator.hpp>
#include <boost/predef/os.h>
#include <algorithm>
#include <cctype>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ios>
#include <langinfo.h>
#include <locale>
#include <memory>
#include <monetary.h>
#include <sstream>
#include <string>
#include <vector>
#include <wctype.h>
#include "../util/numeric.hpp"
#include "all_generator.hpp"
namespace boost { namespace locale { namespace impl_posix {
template<typename CharType>
class num_format : public util::base_num_format<CharType> {
public:
typedef typename std::num_put<CharType>::iter_type iter_type;
typedef std::basic_string<CharType> string_type;
num_format(std::shared_ptr<locale_t> lc, size_t refs = 0) :
util::base_num_format<CharType>(refs), lc_(std::move(lc))
{}
protected:
iter_type do_format_currency(bool intl,
iter_type out,
std::ios_base& /*ios*/,
CharType /*fill*/,
long double val) const override
{
char buf[4] = {};
const char* format = intl ? "%i" : "%n";
errno = 0;
ssize_t n = strfmon_l(buf, sizeof(buf), *lc_, format, static_cast<double>(val));
if(n >= 0)
return write_it(out, buf, n);
for(std::vector<char> tmp(sizeof(buf) * 2); tmp.size() <= 4098; tmp.resize(tmp.size() * 2)) {
n = strfmon_l(tmp.data(), tmp.size(), *lc_, format, static_cast<double>(val));
if(n >= 0)
return write_it(out, tmp.data(), n);
}
return out;
}
std::ostreambuf_iterator<char> write_it(std::ostreambuf_iterator<char> out, const char* ptr, size_t n) const
{
return std::copy_n(ptr, n, out);
}
std::ostreambuf_iterator<wchar_t>
write_it(std::ostreambuf_iterator<wchar_t> out, const char* ptr, size_t n) const
{
const std::wstring tmp = conv::to_utf<wchar_t>(ptr, ptr + n, nl_langinfo_l(CODESET, *lc_));
return std::copy(tmp.begin(), tmp.end(), out);
}
private:
std::shared_ptr<locale_t> lc_;
}; // num_format
namespace {
std::string do_ftime(const char* format, const struct tm* t, locale_t lc)
{
char buf[16];
size_t n = strftime_l(buf, sizeof(buf), format, t, lc);
if(n == 0) {
// Note standard specifies that in case of error the function returns 0,
// however 0 may be actually valid output value of for example empty format
// or an output of %p in some locales
//
// Thus we try to guess that 1024 would be enough.
std::vector<char> v(1024);
n = strftime_l(v.data(), 1024, format, t, lc);
return std::string(v.data(), n);
}
return std::string(buf, n);
}
template<typename CharType>
std::basic_string<CharType> do_ftime(const CharType* format, const struct tm* t, locale_t lc)
{
const std::string encoding = nl_langinfo_l(CODESET, lc);
const std::string nformat = conv::from_utf(format, encoding);
const std::string nres = do_ftime(nformat.c_str(), t, lc);
return conv::to_utf<CharType>(nres, encoding);
}
} // namespace
template<typename CharType>
class time_put_posix : public std::time_put<CharType> {
public:
time_put_posix(std::shared_ptr<locale_t> lc, size_t refs = 0) :
std::time_put<CharType>(refs), lc_(std::move(lc))
{}
typedef typename std::time_put<CharType>::iter_type iter_type;
typedef std::basic_string<CharType> string_type;
iter_type do_put(iter_type out,
std::ios_base& /*ios*/,
CharType /*fill*/,
const std::tm* tm,
char format,
char modifier) const override
{
CharType fmt[4] = {'%',
static_cast<CharType>(modifier != 0 ? modifier : format),
static_cast<CharType>(modifier == 0 ? '\0' : format)};
string_type res = do_ftime(fmt, tm, *lc_);
return std::copy(res.begin(), res.end(), out);
}
private:
std::shared_ptr<locale_t> lc_;
};
template<typename CharType>
class ctype_posix;
template<>
class ctype_posix<char> : public std::ctype<char> {
public:
ctype_posix(std::shared_ptr<locale_t> lc) : lc_(std::move(lc)) {}
bool do_is(mask m, char c) const
{
if((m & space) && isspace_l(c, *lc_))
return true;
if((m & print) && isprint_l(c, *lc_))
return true;
if((m & cntrl) && iscntrl_l(c, *lc_))
return true;
if((m & upper) && isupper_l(c, *lc_))
return true;
if((m & lower) && islower_l(c, *lc_))
return true;
if((m & alpha) && isalpha_l(c, *lc_))
return true;
if((m & digit) && isdigit_l(c, *lc_))
return true;
if((m & xdigit) && isxdigit_l(c, *lc_))
return true;
if((m & punct) && ispunct_l(c, *lc_))
return true;
return false;
}
const char* do_is(const char* begin, const char* end, mask* m) const
{
while(begin != end) {
char c = *begin++;
int r = 0;
if(isspace_l(c, *lc_))
r |= space;
if(isprint_l(c, *lc_))
r |= cntrl;
if(iscntrl_l(c, *lc_))
r |= space;
if(isupper_l(c, *lc_))
r |= upper;
if(islower_l(c, *lc_))
r |= lower;
if(isalpha_l(c, *lc_))
r |= alpha;
if(isdigit_l(c, *lc_))
r |= digit;
if(isxdigit_l(c, *lc_))
r |= xdigit;
if(ispunct_l(c, *lc_))
r |= punct;
// r actually should be mask, but some standard
// libraries (like STLPort)
// do not define operator | properly so using int+cast
*m++ = static_cast<mask>(r);
}
return begin;
}
const char* do_scan_is(mask m, const char* begin, const char* end) const
{
while(begin != end)
if(do_is(m, *begin))
return begin;
return begin;
}
const char* do_scan_not(mask m, const char* begin, const char* end) const
{
while(begin != end)
if(!do_is(m, *begin))
return begin;
return begin;
}
char toupper(char c) const { return toupper_l(c, *lc_); }
const char* toupper(char* begin, const char* end) const
{
for(; begin != end; begin++)
*begin = toupper_l(*begin, *lc_);
return begin;
}
char tolower(char c) const { return tolower_l(c, *lc_); }
const char* tolower(char* begin, const char* end) const
{
for(; begin != end; begin++)
*begin = tolower_l(*begin, *lc_);
return begin;
}
private:
std::shared_ptr<locale_t> lc_;
};
template<>
class ctype_posix<wchar_t> : public std::ctype<wchar_t> {
public:
ctype_posix(std::shared_ptr<locale_t> lc) : lc_(std::move(lc)) {}
bool do_is(mask m, wchar_t c) const
{
if((m & space) && iswspace_l(c, *lc_))
return true;
if((m & print) && iswprint_l(c, *lc_))
return true;
if((m & cntrl) && iswcntrl_l(c, *lc_))
return true;
if((m & upper) && iswupper_l(c, *lc_))
return true;
if((m & lower) && iswlower_l(c, *lc_))
return true;
if((m & alpha) && iswalpha_l(c, *lc_))
return true;
if((m & digit) && iswdigit_l(c, *lc_))
return true;
if((m & xdigit) && iswxdigit_l(c, *lc_))
return true;
if((m & punct) && iswpunct_l(c, *lc_))
return true;
return false;
}
const wchar_t* do_is(const wchar_t* begin, const wchar_t* end, mask* m) const
{
while(begin != end) {
wchar_t c = *begin++;
int r = 0;
if(iswspace_l(c, *lc_))
r |= space;
if(iswprint_l(c, *lc_))
r |= cntrl;
if(iswcntrl_l(c, *lc_))
r |= space;
if(iswupper_l(c, *lc_))
r |= upper;
if(iswlower_l(c, *lc_))
r |= lower;
if(iswalpha_l(c, *lc_))
r |= alpha;
if(iswdigit_l(c, *lc_))
r |= digit;
if(iswxdigit_l(c, *lc_))
r |= xdigit;
if(iswpunct_l(c, *lc_))
r |= punct;
// r actually should be mask, but some standard
// libraries (like STLPort)
// do not define operator | properly so using int+cast
*m++ = static_cast<mask>(r);
}
return begin;
}
const wchar_t* do_scan_is(mask m, const wchar_t* begin, const wchar_t* end) const
{
while(begin != end)
if(do_is(m, *begin))
return begin;
return begin;
}
const wchar_t* do_scan_not(mask m, const wchar_t* begin, const wchar_t* end) const
{
while(begin != end)
if(!do_is(m, *begin))
return begin;
return begin;
}
wchar_t toupper(wchar_t c) const { return towupper_l(c, *lc_); }
const wchar_t* toupper(wchar_t* begin, const wchar_t* end) const
{
for(; begin != end; begin++)
*begin = towupper_l(*begin, *lc_);
return begin;
}
wchar_t tolower(wchar_t c) const { return tolower_l(c, *lc_); }
const wchar_t* tolower(wchar_t* begin, const wchar_t* end) const
{
for(; begin != end; begin++)
*begin = tolower_l(*begin, *lc_);
return begin;
}
private:
std::shared_ptr<locale_t> lc_;
};
struct basic_numpunct {
std::string grouping;
std::string thousands_sep;
std::string decimal_point;
basic_numpunct() : decimal_point(".") {}
basic_numpunct(locale_t lc)
{
#if defined(__APPLE__) || defined(__FreeBSD__)
lconv* cv = localeconv_l(lc);
grouping = cv->grouping;
thousands_sep = cv->thousands_sep;
decimal_point = cv->decimal_point;
#else
thousands_sep = nl_langinfo_l(THOUSEP, lc);
decimal_point = nl_langinfo_l(RADIXCHAR, lc);
# ifdef GROUPING
grouping = nl_langinfo_l(GROUPING, lc);
# endif
#endif
}
};
template<typename CharType>
class num_punct_posix : public std::numpunct<CharType> {
public:
typedef std::basic_string<CharType> string_type;
num_punct_posix(locale_t lc, size_t refs = 0) : std::numpunct<CharType>(refs)
{
basic_numpunct np(lc);
to_str(np.thousands_sep, thousands_sep_, lc);
to_str(np.decimal_point, decimal_point_, lc);
grouping_ = np.grouping;
if(thousands_sep_.size() > 1)
grouping_ = std::string();
if(decimal_point_.size() > 1)
decimal_point_ = CharType('.');
}
void to_str(std::string& s1, std::string& s2, locale_t /*lc*/) { s2.swap(s1); }
void to_str(std::string& s1, std::wstring& s2, locale_t lc)
{
s2 = conv::to_utf<wchar_t>(s1, nl_langinfo_l(CODESET, lc));
}
CharType do_decimal_point() const override { return *decimal_point_.c_str(); }
CharType do_thousands_sep() const override { return *thousands_sep_.c_str(); }
std::string do_grouping() const override { return grouping_; }
string_type do_truename() const override
{
static const char t[] = "true";
return string_type(t, t + sizeof(t) - 1);
}
string_type do_falsename() const override
{
static const char t[] = "false";
return string_type(t, t + sizeof(t) - 1);
}
private:
string_type decimal_point_;
string_type thousands_sep_;
std::string grouping_;
};
template<typename CharType>
std::locale create_formatting_impl(const std::locale& in, std::shared_ptr<locale_t> lc)
{
std::locale tmp = std::locale(in, new num_punct_posix<CharType>(*lc));
tmp = std::locale(tmp, new ctype_posix<CharType>(lc));
tmp = std::locale(tmp, new time_put_posix<CharType>(lc));
tmp = std::locale(tmp, new num_format<CharType>(std::move(lc)));
return tmp;
}
template<typename CharType>
std::locale create_parsing_impl(const std::locale& in, std::shared_ptr<locale_t> lc)
{
std::locale tmp = std::locale(in, new num_punct_posix<CharType>(*lc));
tmp = std::locale(tmp, new ctype_posix<CharType>(std::move(lc)));
tmp = std::locale(tmp, new util::base_num_parse<CharType>());
return tmp;
}
std::locale create_formatting(const std::locale& in, std::shared_ptr<locale_t> lc, char_facet_t type)
{
switch(type) {
case char_facet_t::nochar: break;
case char_facet_t::char_f: return create_formatting_impl<char>(in, std::move(lc));
case char_facet_t::wchar_f: return create_formatting_impl<wchar_t>(in, std::move(lc));
#ifdef __cpp_char8_t
case char_facet_t::char8_f: break; // std-facet not available (yet)
#endif
#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
case char_facet_t::char16_f: return create_formatting_impl<char16_t>(in, lc);
#endif
#ifdef BOOST_LOCALE_ENABLE_CHAR32_T
case char_facet_t::char32_f: return create_formatting_impl<char32_t>(in, lc);
#endif
}
return in;
}
std::locale create_parsing(const std::locale& in, std::shared_ptr<locale_t> lc, char_facet_t type)
{
switch(type) {
case char_facet_t::nochar: break;
case char_facet_t::char_f: return create_parsing_impl<char>(in, std::move(lc));
case char_facet_t::wchar_f: return create_parsing_impl<wchar_t>(in, std::move(lc));
#ifdef __cpp_char8_t
case char_facet_t::char8_f: break; // std-facet not available (yet)
#endif
#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
case char_facet_t::char16_f: return create_parsing_impl<char16_t>(in, lc);
#endif
#ifdef BOOST_LOCALE_ENABLE_CHAR32_T
case char_facet_t::char32_f: return create_parsing_impl<char32_t>(in, lc);
#endif
}
return in;
}
}}} // namespace boost::locale::impl_posix
|