blob: 09faccd0738fbbd8dd9be23f27950b5e79e2c231 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- locale ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_LOCALE
12#define _LIBCPP_LOCALE
13
14/*
15 locale synopsis
16
17namespace std
18{
19
20class locale
21{
22public:
23 // types:
24 class facet;
25 class id;
26
27 typedef int category;
28 static const category // values assigned here are for exposition only
29 none = 0x000,
30 collate = 0x010,
31 ctype = 0x020,
32 monetary = 0x040,
33 numeric = 0x080,
34 time = 0x100,
35 messages = 0x200,
36 all = collate | ctype | monetary | numeric | time | messages;
37
38 // construct/copy/destroy:
39 locale() throw();
40 locale(const locale& other) throw();
41 explicit locale(const char* std_name);
42 explicit locale(const string& std_name);
43 locale(const locale& other, const char* std_name, category);
44 locale(const locale& other, const string& std_name, category);
45 template <class Facet> locale(const locale& other, Facet* f);
46 locale(const locale& other, const locale& one, category);
47
48 ~locale() throw(); // not virtual
49
50 const locale& operator=(const locale& other) throw();
51
52 template <class Facet> locale combine(const locale& other) const;
53
54 // locale operations:
55 basic_string<char> name() const;
56 bool operator==(const locale& other) const;
57 bool operator!=(const locale& other) const;
58 template <class charT, class Traits, class Allocator>
59 bool operator()(const basic_string<charT,Traits,Allocator>& s1,
60 const basic_string<charT,Traits,Allocator>& s2) const;
61
62 // global locale objects:
63 static locale global(const locale&);
64 static const locale& classic();
65};
66
67template <class Facet> const Facet& use_facet(const locale&);
68template <class Facet> bool has_facet(const locale&) throw();
69
70// 22.3.3, convenience interfaces:
71template <class charT> bool isspace (charT c, const locale& loc);
72template <class charT> bool isprint (charT c, const locale& loc);
73template <class charT> bool iscntrl (charT c, const locale& loc);
74template <class charT> bool isupper (charT c, const locale& loc);
75template <class charT> bool islower (charT c, const locale& loc);
76template <class charT> bool isalpha (charT c, const locale& loc);
77template <class charT> bool isdigit (charT c, const locale& loc);
78template <class charT> bool ispunct (charT c, const locale& loc);
79template <class charT> bool isxdigit(charT c, const locale& loc);
80template <class charT> bool isalnum (charT c, const locale& loc);
81template <class charT> bool isgraph (charT c, const locale& loc);
82template <class charT> charT toupper(charT c, const locale& loc);
83template <class charT> charT tolower(charT c, const locale& loc);
84template <class Codecvt, class Elem = wchar_t> class wstring_convert;
85template <class Codecvt, class Elem = wchar_t, class Tr = char_traits<Elem>>
86 class wbuffer_convert;
87
88// 22.4.1 and 22.4.1.3, ctype:
89class ctype_base;
90template <class charT> class ctype;
91template <> class ctype<char>; // specialization
92template <class charT> class ctype_byname;
93template <> class ctype_byname<char>; // specialization
94
95class codecvt_base;
96template <class internT, class externT, class stateT> class codecvt;
97template <class internT, class externT, class stateT> class codecvt_byname;
98
99// 22.4.2 and 22.4.3, numeric:
100template <class charT, class InputIterator> class num_get;
101template <class charT, class OutputIterator> class num_put;
102template <class charT> class numpunct;
103template <class charT> class numpunct_byname;
104
105// 22.4.4, col lation:
106template <class charT> class collate;
107template <class charT> class collate_byname;
108
109// 22.4.5, date and time:
110class time_base;
111template <class charT, class InputIterator> class time_get;
112template <class charT, class InputIterator> class time_get_byname;
113template <class charT, class OutputIterator> class time_put;
114template <class charT, class OutputIterator> class time_put_byname;
115
116// 22.4.6, money:
117class money_base;
118template <class charT, class InputIterator> class money_get;
119template <class charT, class OutputIterator> class money_put;
120template <class charT, bool Intl> class moneypunct;
121template <class charT, bool Intl> class moneypunct_byname;
122
123// 22.4.7, message retrieval:
124class messages_base;
125template <class charT> class messages;
126template <class charT> class messages_byname;
127
128} // std
129
130*/
131
132#include <__config>
133#include <__locale>
134#include <algorithm>
135#include <memory>
136#include <ios>
137#include <streambuf>
138#include <iterator>
139#include <limits>
Howard Hinnantadff4892010-05-24 17:49:41 +0000140#if !__APPLE__
141#include <cstdarg>
142#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000143#include <cstdlib>
144#include <ctime>
145#include <nl_types.h>
146
147#pragma GCC system_header
148
149_LIBCPP_BEGIN_NAMESPACE_STD
150
Howard Hinnantadff4892010-05-24 17:49:41 +0000151// OSX has nice foo_l() functions that let you turn off use of the global
152// locale. Linux, not so much. The following functions avoid the locale when
153// that's possible and otherwise do the wrong thing. FIXME.
154#if __APPLE__
155
156template <class _Tp>
157inline
158int
159__nolocale_sprintf(char* __restrict __str,
160 const char* __restrict __format, _Tp __v)
161{
162 return sprintf_l(__str, 0, __format, __v);
163}
164
165template <class _Tp>
166inline
167int
168__nolocale_snprintf(char* __restrict __str, size_t __size,
169 const char* __restrict __format, _Tp __v)
170{
171 return snprintf_l(__str, __size, 0, __format, __v);
172}
173
174template <class _Tp>
175inline
176int
177__nolocale_snprintf(char* __restrict __str, size_t __size,
178 const char* __restrict __format, int __prec, _Tp __v)
179{
180 return snprintf_l(__str, __size, 0, __format, __prec, __v);
181}
182
183template <class _Tp>
184inline
185int
186__nolocale_asprintf(char** __ret, const char* __restrict __format, _Tp __v)
187{
188 return asprintf_l(__ret, 0, __format, __v);
189}
190
191template <class _Tp>
192inline
193int
194__nolocale_asprintf(char** __ret, const char* __restrict __format, int __prec,
195 _Tp __v)
196{
197 return asprintf_l(__ret, 0, __format, __prec, __v);
198}
199
200template <class _Tp>
201inline
202int
203__nolocale_sscanf(const char* __restrict __str,
204 const char* __restrict __format, _Tp* __v)
205{
206 return sscanf_l(__str, 0, __format, __v);
207}
208
209inline
210int
211__nolocale_isxdigit(int __c)
212{
213 return isxdigit_l(__c, 0);
214}
215
216inline
217int
218__nolocale_isdigit(int __c)
219{
220 return isdigit_l(__c, 0);
221}
222
223#else /* !__APPLE__ */
224inline int
225__nolocale_sprintf(char* __restrict __str,
226 const char* __restrict __format, ...)
227{
228 va_list __ap;
229 va_start(__ap, __format);
230 int __result = vsprintf(__str, __format, __ap);
231 va_end(__ap);
232 return __result;
233}
234inline int
235__nolocale_snprintf(char* __restrict __str, size_t __size,
236 const char* __restrict __format, ...)
237{
238 va_list __ap;
239 va_start(__ap, __format);
240 int __result = vsnprintf(__str, __size, __format, __ap);
241 va_end(__ap);
242 return __result;
243}
244inline int
245__nolocale_asprintf(char** __ret,
246 const char* __restrict __format, ...)
247{
248 va_list __ap;
249 va_start(__ap, __format);
250 int __result = vasprintf(__ret, __format, __ap);
251 va_end(__ap);
252 return __result;
253}
254inline int
255__nolocale_sscanf(const char* __restrict __str,
256 const char* __restrict __format, ...)
257{
258 va_list __ap;
259 va_start(__ap, __format);
260 int __result = vsscanf(__str, __format, __ap);
261 va_end(__ap);
262 return __result;
263}
264inline int
265__nolocale_isxdigit(int __c)
266{
267 return isxdigit(__c);
268}
269inline int
270__nolocale_isdigit(int __c)
271{
272 return isdigit(__c);
273}
274#endif /* __APPLE__ */
275
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000276// __scan_keyword
277// Scans [__b, __e) until a match is found in the basic_strings range
278// [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
279// __b will be incremented (visibly), consuming CharT until a match is found
280// or proved to not exist. A keyword may be "", in which will match anything.
281// If one keyword is a prefix of another, and the next CharT in the input
282// might match another keyword, the algorithm will attempt to find the longest
283// matching keyword. If the longer matching keyword ends up not matching, then
284// no keyword match is found. If no keyword match is found, __ke is returned
285// and failbit is set in __err.
286// Else an iterator pointing to the matching keyword is found. If more than
287// one keyword matches, an iterator to the first matching keyword is returned.
288// If on exit __b == __e, eofbit is set in __err. If __case_senstive is false,
289// __ct is used to force to lower case before comparing characters.
290// Examples:
291// Keywords: "a", "abb"
292// If the input is "a", the first keyword matches and eofbit is set.
293// If the input is "abc", no match is found and "ab" are consumed.
294template <class _InputIterator, class _ForwardIterator, class _Ctype>
295_LIBCPP_HIDDEN
296_ForwardIterator
297__scan_keyword(_InputIterator& __b, _InputIterator __e,
298 _ForwardIterator __kb, _ForwardIterator __ke,
299 const _Ctype& __ct, ios_base::iostate& __err,
300 bool __case_sensitive = true)
301{
302 typedef typename iterator_traits<_InputIterator>::value_type _CharT;
303 size_t __nkw = _STD::distance(__kb, __ke);
304 const unsigned char __doesnt_match = '\0';
305 const unsigned char __might_match = '\1';
306 const unsigned char __does_match = '\2';
307 unsigned char __statbuf[100];
308 unsigned char* __status = __statbuf;
309 unique_ptr<unsigned char, void(*)(void*)> __stat_hold(0, free);
310 if (__nkw > sizeof(__statbuf))
311 {
312 __status = (unsigned char*)malloc(__nkw);
313 if (__status == 0)
314 __throw_bad_alloc();
315 __stat_hold.reset(__status);
316 }
317 size_t __n_might_match = __nkw; // At this point, any keyword might match
318 size_t __n_does_match = 0; // but none of them definitely do
319 // Initialize all statuses to __might_match, except for "" keywords are __does_match
320 unsigned char* __st = __status;
321 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
322 {
323 if (!__ky->empty())
324 *__st = __might_match;
325 else
326 {
327 *__st = __does_match;
328 --__n_might_match;
329 ++__n_does_match;
330 }
331 }
332 // While there might be a match, test keywords against the next CharT
333 for (size_t __indx = 0; __b != __e && __n_might_match > 0; ++__indx)
334 {
335 // Peek at the next CharT but don't consume it
336 _CharT __c = *__b;
337 if (!__case_sensitive)
338 __c = __ct.toupper(__c);
339 bool __consume = false;
340 // For each keyword which might match, see if the __indx character is __c
341 // If a match if found, consume __c
342 // If a match is found, and that is the last character in the keyword,
343 // then that keyword matches.
344 // If the keyword doesn't match this character, then change the keyword
345 // to doesn't match
346 __st = __status;
347 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
348 {
349 if (*__st == __might_match)
350 {
351 _CharT __kc = (*__ky)[__indx];
352 if (!__case_sensitive)
353 __kc = __ct.toupper(__kc);
354 if (__c == __kc)
355 {
356 __consume = true;
357 if (__ky->size() == __indx+1)
358 {
359 *__st = __does_match;
360 --__n_might_match;
361 ++__n_does_match;
362 }
363 }
364 else
365 {
366 *__st = __doesnt_match;
367 --__n_might_match;
368 }
369 }
370 }
371 // consume if we matched a character
372 if (__consume)
373 {
374 ++__b;
375 // If we consumed a character and there might be a matched keyword that
376 // was marked matched on a previous iteration, then such keywords
377 // which are now marked as not matching.
378 if (__n_might_match + __n_does_match > 1)
379 {
380 __st = __status;
381 for (_ForwardIterator __ky = __kb; __ky != __ke; ++__ky, ++__st)
382 {
383 if (*__st == __does_match && __ky->size() != __indx+1)
384 {
385 *__st = __doesnt_match;
386 --__n_does_match;
387 }
388 }
389 }
390 }
391 }
392 // We've exited the loop because we hit eof and/or we have no more "might matches".
393 if (__b == __e)
394 __err |= ios_base::eofbit;
395 // Return the first matching result
396 for (__st = __status; __kb != __ke; ++__kb, ++__st)
397 if (*__st == __does_match)
398 break;
399 if (__kb == __ke)
400 __err |= ios_base::failbit;
401 return __kb;
402}
403
404struct __num_get_base
405{
406 static const int __num_get_buf_sz = 40;
407
408 static int __get_base(ios_base&);
409 static const char __src[33];
410};
411
412void __check_grouping(const string& __grouping, unsigned* __g, unsigned* __g_end,
413 ios_base::iostate& __err);
414
415
416template <class _CharT>
417struct __num_get
418 : protected __num_get_base
419{
420 static string __stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep);
421 static string __stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
422 _CharT& __thousands_sep);
423 static int __stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
424 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
425 unsigned* __g, unsigned*& __g_end, _CharT* __atoms);
426 static int __stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp,
427 char* __a, char*& __a_end,
428 _CharT __decimal_point, _CharT __thousands_sep,
429 const string& __grouping, unsigned* __g,
430 unsigned*& __g_end, unsigned& __dc, _CharT* __atoms);
431};
432
433template <class _CharT>
434string
435__num_get<_CharT>::__stage2_int_prep(ios_base& __iob, _CharT* __atoms, _CharT& __thousands_sep)
436{
437 locale __loc = __iob.getloc();
438 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 26, __atoms);
439 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
440 __thousands_sep = __np.thousands_sep();
441 return __np.grouping();
442}
443
444template <class _CharT>
445string
446__num_get<_CharT>::__stage2_float_prep(ios_base& __iob, _CharT* __atoms, _CharT& __decimal_point,
447 _CharT& __thousands_sep)
448{
449 locale __loc = __iob.getloc();
450 use_facet<ctype<_CharT> >(__loc).widen(__src, __src + 32, __atoms);
451 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__loc);
452 __decimal_point = __np.decimal_point();
453 __thousands_sep = __np.thousands_sep();
454 return __np.grouping();
455}
456
457template <class _CharT>
458int
459__num_get<_CharT>::__stage2_int_loop(_CharT __ct, int __base, char* __a, char*& __a_end,
460 unsigned& __dc, _CharT __thousands_sep, const string& __grouping,
461 unsigned* __g, unsigned*& __g_end, _CharT* __atoms)
462{
463 if (__ct == __thousands_sep && __grouping.size() != 0)
464 {
465 if (__g_end-__g < __num_get_buf_sz)
466 {
467 *__g_end++ = __dc;
468 __dc = 0;
469 }
470 return 0;
471 }
472 ptrdiff_t __f = find(__atoms, __atoms + 26, __ct) - __atoms;
473 if (__f >= 26)
474 return -1;
475 if (__a_end-__a < __num_get_buf_sz - 1)
476 *__a_end++ = __src[__f];
477 switch (__base)
478 {
479 case 8:
480 case 10:
481 if (__f >= __base)
482 return 0;
483 break;
484 default:
485 if (__f >= 22)
486 return 0;
487 break;
488 }
489 ++__dc;
490 return 0;
491}
492
493template <class _CharT>
494int
495__num_get<_CharT>::__stage2_float_loop(_CharT __ct, bool& __in_units, char& __exp, char* __a, char*& __a_end,
496 _CharT __decimal_point, _CharT __thousands_sep, const string& __grouping,
497 unsigned* __g, unsigned*& __g_end, unsigned& __dc, _CharT* __atoms)
498{
499 if (__ct == __decimal_point)
500 {
501 if (!__in_units)
502 return -1;
503 __in_units = false;
504 *__a_end++ = '.';
505 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
506 *__g_end++ = __dc;
507 return 0;
508 }
509 if (__ct == __thousands_sep && __grouping.size() != 0)
510 {
511 if (!__in_units)
512 return -1;
513 if (__g_end-__g < __num_get_buf_sz)
514 {
515 *__g_end++ = __dc;
516 __dc = 0;
517 }
518 return 0;
519 }
520 ptrdiff_t __f = find(__atoms, __atoms + 32, __ct) - __atoms;
521 if (__f >= 32)
522 return -1;
523 char __x = __src[__f];
524 if (__a_end-__a < __num_get_buf_sz - 1)
525 *__a_end++ = __x;
526 if (__x == 'x' || __x == 'X')
527 __exp = 'P';
528 else if ((__x & 0xDF) == __exp)
529 {
530 __in_units = false;
531 if (__grouping.size() != 0 && __g_end-__g < __num_get_buf_sz)
532 *__g_end++ = __dc;
533 }
534 if (__f >= 22)
535 return 0;
536 ++__dc;
537 return 0;
538}
539
540extern template class __num_get<char>;
541extern template class __num_get<wchar_t>;
542
543template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
544class num_get
545 : public locale::facet,
546 private __num_get<_CharT>
547{
548public:
549 typedef _CharT char_type;
550 typedef _InputIterator iter_type;
551
552 _LIBCPP_ALWAYS_INLINE
553 explicit num_get(size_t __refs = 0)
554 : locale::facet(__refs) {}
555
556 _LIBCPP_ALWAYS_INLINE
557 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
558 ios_base::iostate& __err, bool& __v) const
559 {
560 return do_get(__b, __e, __iob, __err, __v);
561 }
562
563 _LIBCPP_ALWAYS_INLINE
564 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
565 ios_base::iostate& __err, long& __v) const
566 {
567 return do_get(__b, __e, __iob, __err, __v);
568 }
569
570 _LIBCPP_ALWAYS_INLINE
571 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
572 ios_base::iostate& __err, long long& __v) const
573 {
574 return do_get(__b, __e, __iob, __err, __v);
575 }
576
577 _LIBCPP_ALWAYS_INLINE
578 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
579 ios_base::iostate& __err, unsigned short& __v) const
580 {
581 return do_get(__b, __e, __iob, __err, __v);
582 }
583
584 _LIBCPP_ALWAYS_INLINE
585 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
586 ios_base::iostate& __err, unsigned int& __v) const
587 {
588 return do_get(__b, __e, __iob, __err, __v);
589 }
590
591 _LIBCPP_ALWAYS_INLINE
592 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
593 ios_base::iostate& __err, unsigned long& __v) const
594 {
595 return do_get(__b, __e, __iob, __err, __v);
596 }
597
598 _LIBCPP_ALWAYS_INLINE
599 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
600 ios_base::iostate& __err, unsigned long long& __v) const
601 {
602 return do_get(__b, __e, __iob, __err, __v);
603 }
604
605 _LIBCPP_ALWAYS_INLINE
606 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
607 ios_base::iostate& __err, float& __v) const
608 {
609 return do_get(__b, __e, __iob, __err, __v);
610 }
611
612 _LIBCPP_ALWAYS_INLINE
613 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
614 ios_base::iostate& __err, double& __v) const
615 {
616 return do_get(__b, __e, __iob, __err, __v);
617 }
618
619 _LIBCPP_ALWAYS_INLINE
620 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
621 ios_base::iostate& __err, long double& __v) const
622 {
623 return do_get(__b, __e, __iob, __err, __v);
624 }
625
626 _LIBCPP_ALWAYS_INLINE
627 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
628 ios_base::iostate& __err, void*& __v) const
629 {
630 return do_get(__b, __e, __iob, __err, __v);
631 }
632
633 static locale::id id;
634
635protected:
636 ~num_get() {}
637
638 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
639 ios_base::iostate& __err, bool& __v) const;
640 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
641 ios_base::iostate& __err, long& __v) const;
642 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
643 ios_base::iostate& __err, long long& __v) const;
644 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
645 ios_base::iostate& __err, unsigned short& __v) const;
646 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
647 ios_base::iostate& __err, unsigned int& __v) const;
648 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
649 ios_base::iostate& __err, unsigned long& __v) const;
650 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
651 ios_base::iostate& __err, unsigned long long& __v) const;
652 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
653 ios_base::iostate& __err, float& __v) const;
654 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
655 ios_base::iostate& __err, double& __v) const;
656 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
657 ios_base::iostate& __err, long double& __v) const;
658 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
659 ios_base::iostate& __err, void*& __v) const;
660};
661
662template <class _CharT, class _InputIterator>
663locale::id
664num_get<_CharT, _InputIterator>::id;
665
666template <class _Tp>
667_Tp
668__num_get_signed_integral(const char* __a, const char* __a_end,
669 ios_base::iostate& __err, int __base)
670{
671 if (__a != __a_end)
672 {
673 char *__p2;
674 long long __ll = strtoll_l(__a, &__p2, __base, 0);
675 if (__p2 != __a_end)
676 {
677 __err = ios_base::failbit;
678 return 0;
679 }
680 else if (__ll > numeric_limits<_Tp>::max())
681 {
682 __err = ios_base::failbit;
683 return numeric_limits<_Tp>::max();
684 }
685 else if (__ll < numeric_limits<_Tp>::min())
686 {
687 __err = ios_base::failbit;
688 return numeric_limits<_Tp>::min();
689 }
690 return static_cast<_Tp>(__ll);
691 }
692 __err = ios_base::failbit;
693 return 0;
694}
695
696template <class _Tp>
697_Tp
698__num_get_unsigned_integral(const char* __a, const char* __a_end,
699 ios_base::iostate& __err, int __base)
700{
701 if (__a != __a_end)
702 {
703 char *__p2;
704 unsigned long long __ll = strtoull_l(__a, &__p2, __base, 0);
705 if (__p2 != __a_end)
706 {
707 __err = ios_base::failbit;
708 return 0;
709 }
710 else if (__ll > numeric_limits<_Tp>::max())
711 {
712 __err = ios_base::failbit;
713 return numeric_limits<_Tp>::max();
714 }
715 return static_cast<_Tp>(__ll);
716 }
717 __err = ios_base::failbit;
718 return 0;
719}
720
721template <class _Tp>
722_Tp
723__num_get_float(const char* __a, const char* __a_end, ios_base::iostate& __err)
724{
725 if (__a != __a_end)
726 {
727 char *__p2;
728 long double __ld = strtold_l(__a, &__p2, 0);
729 if (__p2 != __a_end)
730 {
731 __err = ios_base::failbit;
732 return 0;
733 }
734 return static_cast<_Tp>(__ld);
735 }
736 __err = ios_base::failbit;
737 return 0;
738}
739
740template <class _CharT, class _InputIterator>
741_InputIterator
742num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
743 ios_base& __iob,
744 ios_base::iostate& __err,
745 bool& __v) const
746{
747 if ((__iob.flags() & ios_base::boolalpha) == 0)
748 {
749 long __lv = -1;
750 __b = do_get(__b, __e, __iob, __err, __lv);
751 switch (__lv)
752 {
753 case 0:
754 __v = false;
755 break;
756 case 1:
757 __v = true;
758 break;
759 default:
760 __v = true;
761 __err = ios_base::failbit;
762 break;
763 }
764 return __b;
765 }
766 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__iob.getloc());
767 const numpunct<_CharT>& __np = use_facet<numpunct<_CharT> >(__iob.getloc());
768 typedef typename numpunct<_CharT>::string_type string_type;
769 const string_type __names[2] = {__np.truename(), __np.falsename()};
770 const string_type* __i = __scan_keyword(__b, __e, __names, __names+2,
771 __ct, __err);
772 __v = __i == __names;
773 return __b;
774}
775
776template <class _CharT, class _InputIterator>
777_InputIterator
778num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
779 ios_base& __iob,
780 ios_base::iostate& __err,
781 long& __v) const
782{
783 // Stage 1
784 int __base = this->__get_base(__iob);
785 // Stage 2
786 char_type __atoms[26];
787 char_type __thousands_sep;
788 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
789 char __a[__num_get_base::__num_get_buf_sz] = {0};
790 char* __a_end = __a;
791 unsigned __g[__num_get_base::__num_get_buf_sz];
792 unsigned* __g_end = __g;
793 unsigned __dc = 0;
794 for (; __b != __e; ++__b)
795 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
796 __thousands_sep, __grouping, __g, __g_end,
797 __atoms))
798 break;
799 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
800 *__g_end++ = __dc;
801 // Stage 3
802 __v = __num_get_signed_integral<long>(__a, __a_end, __err, __base);
803 // Digit grouping checked
804 __check_grouping(__grouping, __g, __g_end, __err);
805 // EOF checked
806 if (__b == __e)
807 __err |= ios_base::eofbit;
808 return __b;
809}
810
811template <class _CharT, class _InputIterator>
812_InputIterator
813num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
814 ios_base& __iob,
815 ios_base::iostate& __err,
816 long long& __v) const
817{
818 // Stage 1
819 int __base = this->__get_base(__iob);
820 // Stage 2
821 char_type __atoms[26];
822 char_type __thousands_sep;
823 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
824 char __a[__num_get_base::__num_get_buf_sz] = {0};
825 char* __a_end = __a;
826 unsigned __g[__num_get_base::__num_get_buf_sz];
827 unsigned* __g_end = __g;
828 unsigned __dc = 0;
829 for (; __b != __e; ++__b)
830 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
831 __thousands_sep, __grouping, __g, __g_end,
832 __atoms))
833 break;
834 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
835 *__g_end++ = __dc;
836 // Stage 3
837 __v = __num_get_signed_integral<long long>(__a, __a_end, __err, __base);
838 // Digit grouping checked
839 __check_grouping(__grouping, __g, __g_end, __err);
840 // EOF checked
841 if (__b == __e)
842 __err |= ios_base::eofbit;
843 return __b;
844}
845
846template <class _CharT, class _InputIterator>
847_InputIterator
848num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
849 ios_base& __iob,
850 ios_base::iostate& __err,
851 unsigned short& __v) const
852{
853 // Stage 1
854 int __base = this->__get_base(__iob);
855 // Stage 2
856 char_type __atoms[26];
857 char_type __thousands_sep;
858 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
859 char __a[__num_get_base::__num_get_buf_sz] = {0};
860 char* __a_end = __a;
861 unsigned __g[__num_get_base::__num_get_buf_sz];
862 unsigned* __g_end = __g;
863 unsigned __dc = 0;
864 for (; __b != __e; ++__b)
865 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
866 __thousands_sep, __grouping, __g, __g_end,
867 __atoms))
868 break;
869 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
870 *__g_end++ = __dc;
871 // Stage 3
872 __v = __num_get_unsigned_integral<unsigned short>(__a, __a_end, __err, __base);
873 // Digit grouping checked
874 __check_grouping(__grouping, __g, __g_end, __err);
875 // EOF checked
876 if (__b == __e)
877 __err |= ios_base::eofbit;
878 return __b;
879}
880
881template <class _CharT, class _InputIterator>
882_InputIterator
883num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
884 ios_base& __iob,
885 ios_base::iostate& __err,
886 unsigned int& __v) const
887{
888 // Stage 1
889 int __base = this->__get_base(__iob);
890 // Stage 2
891 char_type __atoms[26];
892 char_type __thousands_sep;
893 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
894 char __a[__num_get_base::__num_get_buf_sz] = {0};
895 char* __a_end = __a;
896 unsigned __g[__num_get_base::__num_get_buf_sz];
897 unsigned* __g_end = __g;
898 unsigned __dc = 0;
899 for (; __b != __e; ++__b)
900 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
901 __thousands_sep, __grouping, __g, __g_end,
902 __atoms))
903 break;
904 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
905 *__g_end++ = __dc;
906 // Stage 3
907 __v = __num_get_unsigned_integral<unsigned int>(__a, __a_end, __err, __base);
908 // Digit grouping checked
909 __check_grouping(__grouping, __g, __g_end, __err);
910 // EOF checked
911 if (__b == __e)
912 __err |= ios_base::eofbit;
913 return __b;
914}
915
916template <class _CharT, class _InputIterator>
917_InputIterator
918num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
919 ios_base& __iob,
920 ios_base::iostate& __err,
921 unsigned long& __v) const
922{
923 // Stage 1
924 int __base = this->__get_base(__iob);
925 // Stage 2
926 char_type __atoms[26];
927 char_type __thousands_sep;
928 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
929 char __a[__num_get_base::__num_get_buf_sz] = {0};
930 char* __a_end = __a;
931 unsigned __g[__num_get_base::__num_get_buf_sz];
932 unsigned* __g_end = __g;
933 unsigned __dc = 0;
934 for (; __b != __e; ++__b)
935 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
936 __thousands_sep, __grouping, __g, __g_end,
937 __atoms))
938 break;
939 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
940 *__g_end++ = __dc;
941 // Stage 3
942 __v = __num_get_unsigned_integral<unsigned long>(__a, __a_end, __err, __base);
943 // Digit grouping checked
944 __check_grouping(__grouping, __g, __g_end, __err);
945 // EOF checked
946 if (__b == __e)
947 __err |= ios_base::eofbit;
948 return __b;
949}
950
951template <class _CharT, class _InputIterator>
952_InputIterator
953num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
954 ios_base& __iob,
955 ios_base::iostate& __err,
956 unsigned long long& __v) const
957{
958 // Stage 1
959 int __base = this->__get_base(__iob);
960 // Stage 2
961 char_type __atoms[26];
962 char_type __thousands_sep;
963 string __grouping = this->__stage2_int_prep(__iob, __atoms, __thousands_sep);
964 char __a[__num_get_base::__num_get_buf_sz] = {0};
965 char* __a_end = __a;
966 unsigned __g[__num_get_base::__num_get_buf_sz];
967 unsigned* __g_end = __g;
968 unsigned __dc = 0;
969 for (; __b != __e; ++__b)
970 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
971 __thousands_sep, __grouping, __g, __g_end,
972 __atoms))
973 break;
974 if (__grouping.size() != 0 && __g_end-__g < __num_get_base::__num_get_buf_sz)
975 *__g_end++ = __dc;
976 // Stage 3
977 __v = __num_get_unsigned_integral<unsigned long long>(__a, __a_end, __err, __base);
978 // Digit grouping checked
979 __check_grouping(__grouping, __g, __g_end, __err);
980 // EOF checked
981 if (__b == __e)
982 __err |= ios_base::eofbit;
983 return __b;
984}
985
986template <class _CharT, class _InputIterator>
987_InputIterator
988num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
989 ios_base& __iob,
990 ios_base::iostate& __err,
991 float& __v) const
992{
993 // Stage 1, nothing to do
994 // Stage 2
995 char_type __atoms[32];
996 char_type __decimal_point;
997 char_type __thousands_sep;
998 string __grouping = this->__stage2_float_prep(__iob, __atoms,
999 __decimal_point,
1000 __thousands_sep);
1001 char __a[__num_get_base::__num_get_buf_sz] = {0};
1002 char* __a_end = __a;
1003 unsigned __g[__num_get_base::__num_get_buf_sz];
1004 unsigned* __g_end = __g;
1005 unsigned __dc = 0;
1006 bool __in_units = true;
1007 char __exp = 'E';
1008 for (; __b != __e; ++__b)
1009 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1010 __decimal_point, __thousands_sep,
1011 __grouping, __g, __g_end,
1012 __dc, __atoms))
1013 break;
1014 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1015 *__g_end++ = __dc;
1016 // Stage 3
1017 __v = __num_get_float<float>(__a, __a_end, __err);
1018 // Digit grouping checked
1019 __check_grouping(__grouping, __g, __g_end, __err);
1020 // EOF checked
1021 if (__b == __e)
1022 __err |= ios_base::eofbit;
1023 return __b;
1024}
1025
1026template <class _CharT, class _InputIterator>
1027_InputIterator
1028num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1029 ios_base& __iob,
1030 ios_base::iostate& __err,
1031 double& __v) const
1032{
1033 // Stage 1, nothing to do
1034 // Stage 2
1035 char_type __atoms[32];
1036 char_type __decimal_point;
1037 char_type __thousands_sep;
1038 string __grouping = this->__stage2_float_prep(__iob, __atoms,
1039 __decimal_point,
1040 __thousands_sep);
1041 char __a[__num_get_base::__num_get_buf_sz] = {0};
1042 char* __a_end = __a;
1043 unsigned __g[__num_get_base::__num_get_buf_sz];
1044 unsigned* __g_end = __g;
1045 unsigned __dc = 0;
1046 bool __in_units = true;
1047 char __exp = 'E';
1048 for (; __b != __e; ++__b)
1049 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1050 __decimal_point, __thousands_sep,
1051 __grouping, __g, __g_end,
1052 __dc, __atoms))
1053 break;
1054 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1055 *__g_end++ = __dc;
1056 // Stage 3
1057 __v = __num_get_float<double>(__a, __a_end, __err);
1058 // Digit grouping checked
1059 __check_grouping(__grouping, __g, __g_end, __err);
1060 // EOF checked
1061 if (__b == __e)
1062 __err |= ios_base::eofbit;
1063 return __b;
1064}
1065
1066template <class _CharT, class _InputIterator>
1067_InputIterator
1068num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1069 ios_base& __iob,
1070 ios_base::iostate& __err,
1071 long double& __v) const
1072{
1073 // Stage 1, nothing to do
1074 // Stage 2
1075 char_type __atoms[32];
1076 char_type __decimal_point;
1077 char_type __thousands_sep;
1078 string __grouping = this->__stage2_float_prep(__iob, __atoms,
1079 __decimal_point,
1080 __thousands_sep);
1081 char __a[__num_get_base::__num_get_buf_sz] = {0};
1082 char* __a_end = __a;
1083 unsigned __g[__num_get_base::__num_get_buf_sz];
1084 unsigned* __g_end = __g;
1085 unsigned __dc = 0;
1086 bool __in_units = true;
1087 char __exp = 'E';
1088 for (; __b != __e; ++__b)
1089 if (this->__stage2_float_loop(*__b, __in_units, __exp, __a, __a_end,
1090 __decimal_point, __thousands_sep,
1091 __grouping, __g, __g_end,
1092 __dc, __atoms))
1093 break;
1094 if (__grouping.size() != 0 && __in_units && __g_end-__g < __num_get_base::__num_get_buf_sz)
1095 *__g_end++ = __dc;
1096 // Stage 3
1097 __v = __num_get_float<long double>(__a, __a_end, __err);
1098 // Digit grouping checked
1099 __check_grouping(__grouping, __g, __g_end, __err);
1100 // EOF checked
1101 if (__b == __e)
1102 __err |= ios_base::eofbit;
1103 return __b;
1104}
1105
1106template <class _CharT, class _InputIterator>
1107_InputIterator
1108num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
1109 ios_base& __iob,
1110 ios_base::iostate& __err,
1111 void*& __v) const
1112{
1113 // Stage 1
1114 int __base = 16;
1115 // Stage 2
1116 char_type __atoms[26];
1117 char_type __thousands_sep;
1118 string __grouping;
1119 use_facet<ctype<_CharT> >(__iob.getloc()).widen(__num_get_base::__src,
1120 __num_get_base::__src + 26, __atoms);
1121 char __a[__num_get_base::__num_get_buf_sz] = {0};
1122 char* __a_end = __a;
1123 unsigned __g[__num_get_base::__num_get_buf_sz];
1124 unsigned* __g_end = __g;
1125 unsigned __dc = 0;
1126 for (; __b != __e; ++__b)
1127 if (this->__stage2_int_loop(*__b, __base, __a, __a_end, __dc,
1128 __thousands_sep, __grouping,
1129 __g, __g_end, __atoms))
1130 break;
1131 // Stage 3
1132 __a[sizeof(__a)-1] = 0;
Howard Hinnantadff4892010-05-24 17:49:41 +00001133 if (__nolocale_sscanf(__a, "%p", &__v) != 1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001134 __err = ios_base::failbit;
1135 // EOF checked
1136 if (__b == __e)
1137 __err |= ios_base::eofbit;
1138 return __b;
1139}
1140
1141extern template class num_get<char>;
1142extern template class num_get<wchar_t>;
1143
1144struct __num_put_base
1145{
1146protected:
1147 static void __format_int(char* __fmt, const char* __len, bool __signd,
1148 ios_base::fmtflags __flags);
1149 static bool __format_float(char* __fmt, const char* __len,
1150 ios_base::fmtflags __flags);
1151 static char* __identify_padding(char* __nb, char* __ne,
1152 const ios_base& __iob);
1153};
1154
1155template <class _CharT>
1156struct __num_put
1157 : protected __num_put_base
1158{
1159 static void __widen_and_group_int(char* __nb, char* __np, char* __ne,
1160 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1161 const locale& __loc);
1162 static void __widen_and_group_float(char* __nb, char* __np, char* __ne,
1163 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1164 const locale& __loc);
1165};
1166
1167template <class _CharT>
1168void
1169__num_put<_CharT>::__widen_and_group_int(char* __nb, char* __np, char* __ne,
1170 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1171 const locale& __loc)
1172{
1173 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1174 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1175 string __grouping = __npt.grouping();
1176 if (__grouping.empty())
1177 {
1178 __ct.widen(__nb, __ne, __ob);
1179 __oe = __ob + (__ne - __nb);
1180 }
1181 else
1182 {
1183 __oe = __ob;
1184 char* __nf = __nb;
1185 if (*__nf == '-' || *__nf == '+')
1186 *__oe++ = __ct.widen(*__nf++);
1187 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1188 __nf[1] == 'X'))
1189 {
1190 *__oe++ = __ct.widen(*__nf++);
1191 *__oe++ = __ct.widen(*__nf++);
1192 }
1193 reverse(__nf, __ne);
1194 _CharT __thousands_sep = __npt.thousands_sep();
1195 unsigned __dc = 0;
1196 unsigned __dg = 0;
1197 for (char* __p = __nf; __p < __ne; ++__p)
1198 {
1199 if (static_cast<unsigned>(__grouping[__dg]) > 0 &&
1200 __dc == static_cast<unsigned>(__grouping[__dg]))
1201 {
1202 *__oe++ = __thousands_sep;
1203 __dc = 0;
1204 if (__dg < __grouping.size()-1)
1205 ++__dg;
1206 }
1207 *__oe++ = __ct.widen(*__p);
1208 ++__dc;
1209 }
1210 reverse(__ob + (__nf - __nb), __oe);
1211 }
1212 if (__np == __ne)
1213 __op = __oe;
1214 else
1215 __op = __ob + (__np - __nb);
1216}
1217
1218template <class _CharT>
1219void
1220__num_put<_CharT>::__widen_and_group_float(char* __nb, char* __np, char* __ne,
1221 _CharT* __ob, _CharT*& __op, _CharT*& __oe,
1222 const locale& __loc)
1223{
1224 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> > (__loc);
1225 const numpunct<_CharT>& __npt = use_facet<numpunct<_CharT> >(__loc);
1226 string __grouping = __npt.grouping();
1227 __oe = __ob;
1228 char* __nf = __nb;
1229 if (*__nf == '-' || *__nf == '+')
1230 *__oe++ = __ct.widen(*__nf++);
1231 char* __ns;
1232 if (__ne - __nf >= 2 && __nf[0] == '0' && (__nf[1] == 'x' ||
1233 __nf[1] == 'X'))
1234 {
1235 *__oe++ = __ct.widen(*__nf++);
1236 *__oe++ = __ct.widen(*__nf++);
1237 for (__ns = __nf; __ns < __ne; ++__ns)
Howard Hinnantadff4892010-05-24 17:49:41 +00001238 if (!__nolocale_isxdigit(*__ns))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001239 break;
1240 }
1241 else
1242 {
1243 for (__ns = __nf; __ns < __ne; ++__ns)
Howard Hinnantadff4892010-05-24 17:49:41 +00001244 if (!__nolocale_isdigit(*__ns))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245 break;
1246 }
1247 if (__grouping.empty())
1248 {
1249 __ct.widen(__nf, __ns, __oe);
1250 __oe += __ns - __nf;
1251 }
1252 else
1253 {
1254 reverse(__nf, __ns);
1255 _CharT __thousands_sep = __npt.thousands_sep();
1256 unsigned __dc = 0;
1257 unsigned __dg = 0;
1258 for (char* __p = __nf; __p < __ns; ++__p)
1259 {
1260 if (__grouping[__dg] > 0 && __dc == static_cast<unsigned>(__grouping[__dg]))
1261 {
1262 *__oe++ = __thousands_sep;
1263 __dc = 0;
1264 if (__dg < __grouping.size()-1)
1265 ++__dg;
1266 }
1267 *__oe++ = __ct.widen(*__p);
1268 ++__dc;
1269 }
1270 reverse(__ob + (__nf - __nb), __oe);
1271 }
1272 for (__nf = __ns; __nf < __ne; ++__nf)
1273 {
1274 if (*__nf == '.')
1275 {
1276 *__oe++ = __npt.decimal_point();
1277 ++__nf;
1278 break;
1279 }
1280 else
1281 *__oe++ = __ct.widen(*__nf);
1282 }
1283 __ct.widen(__nf, __ne, __oe);
1284 __oe += __ne - __nf;
1285 if (__np == __ne)
1286 __op = __oe;
1287 else
1288 __op = __ob + (__np - __nb);
1289}
1290
1291extern template class __num_put<char>;
1292extern template class __num_put<wchar_t>;
1293
1294template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
1295class num_put
1296 : public locale::facet,
1297 private __num_put<_CharT>
1298{
1299public:
1300 typedef _CharT char_type;
1301 typedef _OutputIterator iter_type;
1302
1303 _LIBCPP_ALWAYS_INLINE
1304 explicit num_put(size_t __refs = 0)
1305 : locale::facet(__refs) {}
1306
1307 _LIBCPP_ALWAYS_INLINE
1308 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1309 bool __v) const
1310 {
1311 return do_put(__s, __iob, __fl, __v);
1312 }
1313
1314 _LIBCPP_ALWAYS_INLINE
1315 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1316 long __v) const
1317 {
1318 return do_put(__s, __iob, __fl, __v);
1319 }
1320
1321 _LIBCPP_ALWAYS_INLINE
1322 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1323 long long __v) const
1324 {
1325 return do_put(__s, __iob, __fl, __v);
1326 }
1327
1328 _LIBCPP_ALWAYS_INLINE
1329 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1330 unsigned long __v) const
1331 {
1332 return do_put(__s, __iob, __fl, __v);
1333 }
1334
1335 _LIBCPP_ALWAYS_INLINE
1336 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1337 unsigned long long __v) const
1338 {
1339 return do_put(__s, __iob, __fl, __v);
1340 }
1341
1342 _LIBCPP_ALWAYS_INLINE
1343 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1344 double __v) const
1345 {
1346 return do_put(__s, __iob, __fl, __v);
1347 }
1348
1349 _LIBCPP_ALWAYS_INLINE
1350 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1351 long double __v) const
1352 {
1353 return do_put(__s, __iob, __fl, __v);
1354 }
1355
1356 _LIBCPP_ALWAYS_INLINE
1357 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
1358 const void* __v) const
1359 {
1360 return do_put(__s, __iob, __fl, __v);
1361 }
1362
1363 static locale::id id;
1364
1365protected:
1366 ~num_put() {}
1367
1368 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1369 bool __v) const;
1370 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1371 long __v) const;
1372 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1373 long long __v) const;
1374 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1375 unsigned long) const;
1376 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1377 unsigned long long) const;
1378 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1379 double __v) const;
1380 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1381 long double __v) const;
1382 virtual iter_type do_put(iter_type __s, ios_base& __iob, char_type __fl,
1383 const void* __v) const;
1384};
1385
1386template <class _CharT, class _OutputIterator>
1387locale::id
1388num_put<_CharT, _OutputIterator>::id;
1389
1390template <class _CharT, class _OutputIterator>
1391_LIBCPP_HIDDEN
1392_OutputIterator
1393__pad_and_output(_OutputIterator __s,
1394 const _CharT* __ob, const _CharT* __op, const _CharT* __oe,
1395 ios_base& __iob, _CharT __fl)
1396{
1397 streamsize __sz = __oe - __ob;
1398 streamsize __ns = __iob.width();
1399 if (__ns > __sz)
1400 __ns -= __sz;
1401 else
1402 __ns = 0;
1403 for (;__ob < __op; ++__ob, ++__s)
1404 *__s = *__ob;
1405 for (; __ns; --__ns, ++__s)
1406 *__s = __fl;
1407 for (; __ob < __oe; ++__ob, ++__s)
1408 *__s = *__ob;
1409 __iob.width(0);
1410 return __s;
1411}
1412
1413template <class _CharT, class _OutputIterator>
1414_OutputIterator
1415num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1416 char_type __fl, bool __v) const
1417{
1418 if ((__iob.flags() & ios_base::boolalpha) == 0)
1419 return do_put(__s, __iob, __fl, (unsigned long)__v);
1420 const numpunct<char_type>& __np = use_facet<numpunct<char_type> >(__iob.getloc());
1421 typedef typename numpunct<char_type>::string_type string_type;
1422 string_type __nm = __v ? __np.truename() : __np.falsename();
1423 for (typename string_type::iterator __i = __nm.begin(); __i != __nm.end(); ++__i, ++__s)
1424 *__s = *__i;
1425 return __s;
1426}
1427
1428template <class _CharT, class _OutputIterator>
1429_OutputIterator
1430num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1431 char_type __fl, long __v) const
1432{
1433 // Stage 1 - Get number in narrow char
1434 char __fmt[6] = {'%', 0};
1435 const char* __len = "l";
1436 this->__format_int(__fmt+1, __len, true, __iob.flags());
1437 const unsigned __nbuf = (numeric_limits<long>::digits / 3)
1438 + ((numeric_limits<long>::digits % 3) != 0)
1439 + 1;
1440 char __nar[__nbuf];
Howard Hinnantadff4892010-05-24 17:49:41 +00001441 int __nc = __nolocale_sprintf(__nar, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001442 char* __ne = __nar + __nc;
1443 char* __np = this->__identify_padding(__nar, __ne, __iob);
1444 // Stage 2 - Widen __nar while adding thousands separators
1445 char_type __o[2*(__nbuf-1) - 1];
1446 char_type* __op; // pad here
1447 char_type* __oe; // end of output
1448 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1449 // [__o, __oe) contains thousands_sep'd wide number
1450 // Stage 3 & 4
1451 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1452}
1453
1454template <class _CharT, class _OutputIterator>
1455_OutputIterator
1456num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1457 char_type __fl, long long __v) const
1458{
1459 // Stage 1 - Get number in narrow char
1460 char __fmt[8] = {'%', 0};
1461 const char* __len = "ll";
1462 this->__format_int(__fmt+1, __len, true, __iob.flags());
1463 const unsigned __nbuf = (numeric_limits<long long>::digits / 3)
1464 + ((numeric_limits<long long>::digits % 3) != 0)
1465 + 1;
1466 char __nar[__nbuf];
Howard Hinnantadff4892010-05-24 17:49:41 +00001467 int __nc = __nolocale_sprintf(__nar, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001468 char* __ne = __nar + __nc;
1469 char* __np = this->__identify_padding(__nar, __ne, __iob);
1470 // Stage 2 - Widen __nar while adding thousands separators
1471 char_type __o[2*(__nbuf-1) - 1];
1472 char_type* __op; // pad here
1473 char_type* __oe; // end of output
1474 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1475 // [__o, __oe) contains thousands_sep'd wide number
1476 // Stage 3 & 4
1477 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1478}
1479
1480template <class _CharT, class _OutputIterator>
1481_OutputIterator
1482num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1483 char_type __fl, unsigned long __v) const
1484{
1485 // Stage 1 - Get number in narrow char
1486 char __fmt[6] = {'%', 0};
1487 const char* __len = "l";
1488 this->__format_int(__fmt+1, __len, false, __iob.flags());
1489 const unsigned __nbuf = (numeric_limits<unsigned long>::digits / 3)
1490 + ((numeric_limits<unsigned long>::digits % 3) != 0)
1491 + 1;
1492 char __nar[__nbuf];
Howard Hinnantadff4892010-05-24 17:49:41 +00001493 int __nc = __nolocale_sprintf(__nar, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001494 char* __ne = __nar + __nc;
1495 char* __np = this->__identify_padding(__nar, __ne, __iob);
1496 // Stage 2 - Widen __nar while adding thousands separators
1497 char_type __o[2*(__nbuf-1) - 1];
1498 char_type* __op; // pad here
1499 char_type* __oe; // end of output
1500 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1501 // [__o, __oe) contains thousands_sep'd wide number
1502 // Stage 3 & 4
1503 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1504}
1505
1506template <class _CharT, class _OutputIterator>
1507_OutputIterator
1508num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1509 char_type __fl, unsigned long long __v) const
1510{
1511 // Stage 1 - Get number in narrow char
1512 char __fmt[8] = {'%', 0};
1513 const char* __len = "ll";
1514 this->__format_int(__fmt+1, __len, false, __iob.flags());
1515 const unsigned __nbuf = (numeric_limits<unsigned long long>::digits / 3)
1516 + ((numeric_limits<unsigned long long>::digits % 3) != 0)
1517 + 1;
1518 char __nar[__nbuf];
Howard Hinnantadff4892010-05-24 17:49:41 +00001519 int __nc = __nolocale_sprintf(__nar, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001520 char* __ne = __nar + __nc;
1521 char* __np = this->__identify_padding(__nar, __ne, __iob);
1522 // Stage 2 - Widen __nar while adding thousands separators
1523 char_type __o[2*(__nbuf-1) - 1];
1524 char_type* __op; // pad here
1525 char_type* __oe; // end of output
1526 this->__widen_and_group_int(__nar, __np, __ne, __o, __op, __oe, __iob.getloc());
1527 // [__o, __oe) contains thousands_sep'd wide number
1528 // Stage 3 & 4
1529 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1530}
1531
1532template <class _CharT, class _OutputIterator>
1533_OutputIterator
1534num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1535 char_type __fl, double __v) const
1536{
1537 // Stage 1 - Get number in narrow char
1538 char __fmt[8] = {'%', 0};
1539 const char* __len = "";
1540 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1541 const unsigned __nbuf = 30;
1542 char __nar[__nbuf];
1543 char* __nb = __nar;
1544 int __nc;
1545 if (__specify_precision)
Howard Hinnantadff4892010-05-24 17:49:41 +00001546 __nc = __nolocale_snprintf(__nb, __nbuf, __fmt,
1547 (int)__iob.precision(), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001548 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001549 __nc = __nolocale_snprintf(__nb, __nbuf, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001550 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1551 if (__nc > static_cast<int>(__nbuf-1))
1552 {
1553 if (__specify_precision)
Howard Hinnantadff4892010-05-24 17:49:41 +00001554 __nc = __nolocale_asprintf(&__nb, __fmt, (int)__iob.precision(),
1555 __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001556 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001557 __nc = __nolocale_asprintf(&__nb, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001558 if (__nb == 0)
1559 __throw_bad_alloc();
1560 __nbh.reset(__nb);
1561 }
1562 char* __ne = __nb + __nc;
1563 char* __np = this->__identify_padding(__nb, __ne, __iob);
1564 // Stage 2 - Widen __nar while adding thousands separators
1565 char_type __o[2*(__nbuf-1) - 1];
1566 char_type* __ob = __o;
1567 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1568 if (__nb != __nar)
1569 {
1570 __ob = (char_type*)malloc((2*__nc)*sizeof(char_type));
1571 if (__ob == 0)
1572 __throw_bad_alloc();
1573 __obh.reset(__ob);
1574 }
1575 char_type* __op; // pad here
1576 char_type* __oe; // end of output
1577 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1578 // [__o, __oe) contains thousands_sep'd wide number
1579 // Stage 3 & 4
1580 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1581 return __s;
1582}
1583
1584template <class _CharT, class _OutputIterator>
1585_OutputIterator
1586num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1587 char_type __fl, long double __v) const
1588{
1589 // Stage 1 - Get number in narrow char
1590 char __fmt[8] = {'%', 0};
1591 const char* __len = "L";
1592 bool __specify_precision = this->__format_float(__fmt+1, __len, __iob.flags());
1593 const unsigned __nbuf = 30;
1594 char __nar[__nbuf];
1595 char* __nb = __nar;
1596 int __nc;
1597 if (__specify_precision)
Howard Hinnantadff4892010-05-24 17:49:41 +00001598 __nc = __nolocale_snprintf(__nb, __nbuf, __fmt,
1599 (int)__iob.precision(), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001600 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001601 __nc = __nolocale_snprintf(__nb, __nbuf, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001602 unique_ptr<char, void(*)(void*)> __nbh(0, free);
1603 if (__nc > static_cast<int>(__nbuf-1))
1604 {
1605 if (__specify_precision)
Howard Hinnantadff4892010-05-24 17:49:41 +00001606 __nc = __nolocale_asprintf(&__nb, __fmt, (int)__iob.precision(),
1607 __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001608 else
Howard Hinnantadff4892010-05-24 17:49:41 +00001609 __nc = __nolocale_asprintf(&__nb, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001610 if (__nb == 0)
1611 __throw_bad_alloc();
1612 __nbh.reset(__nb);
1613 }
1614 char* __ne = __nb + __nc;
1615 char* __np = this->__identify_padding(__nb, __ne, __iob);
1616 // Stage 2 - Widen __nar while adding thousands separators
1617 char_type __o[2*(__nbuf-1) - 1];
1618 char_type* __ob = __o;
1619 unique_ptr<char_type, void(*)(void*)> __obh(0, free);
1620 if (__nb != __nar)
1621 {
1622 __ob = (char_type*)malloc((2*__nc)*sizeof(char_type));
1623 if (__ob == 0)
1624 __throw_bad_alloc();
1625 __obh.reset(__ob);
1626 }
1627 char_type* __op; // pad here
1628 char_type* __oe; // end of output
1629 this->__widen_and_group_float(__nb, __np, __ne, __ob, __op, __oe, __iob.getloc());
1630 // [__o, __oe) contains thousands_sep'd wide number
1631 // Stage 3 & 4
1632 __s = __pad_and_output(__s, __ob, __op, __oe, __iob, __fl);
1633 return __s;
1634}
1635
1636template <class _CharT, class _OutputIterator>
1637_OutputIterator
1638num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
1639 char_type __fl, const void* __v) const
1640{
1641 // Stage 1 - Get pointer in narrow char
1642 char __fmt[6] = "%p";
1643 const unsigned __nbuf = 20;
1644 char __nar[__nbuf];
Howard Hinnantadff4892010-05-24 17:49:41 +00001645 int __nc = __nolocale_sprintf(__nar, __fmt, __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001646 char* __ne = __nar + __nc;
1647 char* __np = this->__identify_padding(__nar, __ne, __iob);
1648 // Stage 2 - Widen __nar
1649 char_type __o[2*(__nbuf-1) - 1];
1650 char_type* __op; // pad here
1651 char_type* __oe; // end of output
1652 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
1653 __ct.widen(__nar, __ne, __o);
1654 __oe = __o + (__ne - __nar);
1655 if (__np == __ne)
1656 __op = __oe;
1657 else
1658 __op = __o + (__np - __nar);
1659 // [__o, __oe) contains wide number
1660 // Stage 3 & 4
1661 return __pad_and_output(__s, __o, __op, __oe, __iob, __fl);
1662}
1663
1664extern template class num_put<char>;
1665extern template class num_put<wchar_t>;
1666
1667template <class _CharT, class _InputIterator>
1668_LIBCPP_HIDDEN
1669int
1670__get_up_to_n_digits(_InputIterator& __b, _InputIterator __e,
1671 ios_base::iostate& __err, const ctype<_CharT>& __ct, int __n)
1672{
1673 // Precondition: __n >= 1
1674 if (__b == __e)
1675 {
1676 __err |= ios_base::eofbit | ios_base::failbit;
1677 return 0;
1678 }
1679 // get first digit
1680 _CharT __c = *__b;
1681 if (!__ct.is(ctype_base::digit, __c))
1682 {
1683 __err |= ios_base::failbit;
1684 return 0;
1685 }
1686 int __r = __ct.narrow(__c, 0) - '0';
1687 for (++__b, --__n; __b != __e && __n > 0; ++__b, --__n)
1688 {
1689 // get next digit
1690 __c = *__b;
1691 if (!__ct.is(ctype_base::digit, __c))
1692 return __r;
1693 __r = __r * 10 + __ct.narrow(__c, 0) - '0';
1694 }
1695 if (__b == __e)
1696 __err |= ios_base::eofbit;
1697 return __r;
1698}
1699
1700class time_base
1701{
1702public:
1703 enum dateorder {no_order, dmy, mdy, ymd, ydm};
1704};
1705
1706template <class _CharT>
1707class __time_get_c_storage
1708{
1709protected:
1710 typedef basic_string<_CharT> string_type;
1711
1712 virtual const string_type* __weeks() const;
1713 virtual const string_type* __months() const;
1714 virtual const string_type* __am_pm() const;
1715 virtual const string_type& __c() const;
1716 virtual const string_type& __r() const;
1717 virtual const string_type& __x() const;
1718 virtual const string_type& __X() const;
1719};
1720
1721template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
1722class time_get
1723 : public locale::facet,
1724 public time_base,
1725 private __time_get_c_storage<_CharT>
1726{
1727public:
1728 typedef _CharT char_type;
1729 typedef _InputIterator iter_type;
1730 typedef time_base::dateorder dateorder;
1731 typedef basic_string<char_type> string_type;
1732
1733 _LIBCPP_ALWAYS_INLINE
1734 explicit time_get(size_t __refs = 0)
1735 : locale::facet(__refs) {}
1736
1737 _LIBCPP_ALWAYS_INLINE
1738 dateorder date_order() const
1739 {
1740 return this->do_date_order();
1741 }
1742
1743 _LIBCPP_ALWAYS_INLINE
1744 iter_type get_time(iter_type __b, iter_type __e, ios_base& __iob,
1745 ios_base::iostate& __err, tm* __tm) const
1746 {
1747 return do_get_time(__b, __e, __iob, __err, __tm);
1748 }
1749
1750 _LIBCPP_ALWAYS_INLINE
1751 iter_type get_date(iter_type __b, iter_type __e, ios_base& __iob,
1752 ios_base::iostate& __err, tm* __tm) const
1753 {
1754 return do_get_date(__b, __e, __iob, __err, __tm);
1755 }
1756
1757 _LIBCPP_ALWAYS_INLINE
1758 iter_type get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1759 ios_base::iostate& __err, tm* __tm) const
1760 {
1761 return do_get_weekday(__b, __e, __iob, __err, __tm);
1762 }
1763
1764 _LIBCPP_ALWAYS_INLINE
1765 iter_type get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1766 ios_base::iostate& __err, tm* __tm) const
1767 {
1768 return do_get_monthname(__b, __e, __iob, __err, __tm);
1769 }
1770
1771 _LIBCPP_ALWAYS_INLINE
1772 iter_type get_year(iter_type __b, iter_type __e, ios_base& __iob,
1773 ios_base::iostate& __err, tm* __tm) const
1774 {
1775 return do_get_year(__b, __e, __iob, __err, __tm);
1776 }
1777
1778 _LIBCPP_ALWAYS_INLINE
1779 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1780 ios_base::iostate& __err, tm *__tm,
1781 char __fmt, char __mod = 0) const
1782 {
1783 return do_get(__b, __e, __iob, __err, __tm, __fmt, __mod);
1784 }
1785
1786 iter_type get(iter_type __b, iter_type __e, ios_base& __iob,
1787 ios_base::iostate& __err, tm* __tm,
1788 const char_type* __fmtb, const char_type* __fmte) const;
1789
1790 static locale::id id;
1791
1792protected:
1793 ~time_get() {}
1794
1795 virtual dateorder do_date_order() const;
1796 virtual iter_type do_get_time(iter_type __b, iter_type __e, ios_base& __iob,
1797 ios_base::iostate& __err, tm* __tm) const;
1798 virtual iter_type do_get_date(iter_type __b, iter_type __e, ios_base& __iob,
1799 ios_base::iostate& __err, tm* __tm) const;
1800 virtual iter_type do_get_weekday(iter_type __b, iter_type __e, ios_base& __iob,
1801 ios_base::iostate& __err, tm* __tm) const;
1802 virtual iter_type do_get_monthname(iter_type __b, iter_type __e, ios_base& __iob,
1803 ios_base::iostate& __err, tm* __tm) const;
1804 virtual iter_type do_get_year(iter_type __b, iter_type __e, ios_base& __iob,
1805 ios_base::iostate& __err, tm* __tm) const;
1806 virtual iter_type do_get(iter_type __b, iter_type __e, ios_base& __iob,
1807 ios_base::iostate& __err, tm* __tm,
1808 char __fmt, char __mod) const;
1809private:
1810 void __get_white_space(iter_type& __b, iter_type __e,
1811 ios_base::iostate& __err, const ctype<char_type>& __ct) const;
1812 void __get_percent(iter_type& __b, iter_type __e, ios_base::iostate& __err,
1813 const ctype<char_type>& __ct) const;
1814
1815 void __get_weekdayname(int& __m,
1816 iter_type& __b, iter_type __e,
1817 ios_base::iostate& __err,
1818 const ctype<char_type>& __ct) const;
1819 void __get_monthname(int& __m,
1820 iter_type& __b, iter_type __e,
1821 ios_base::iostate& __err,
1822 const ctype<char_type>& __ct) const;
1823 void __get_day(int& __d,
1824 iter_type& __b, iter_type __e,
1825 ios_base::iostate& __err,
1826 const ctype<char_type>& __ct) const;
1827 void __get_month(int& __m,
1828 iter_type& __b, iter_type __e,
1829 ios_base::iostate& __err,
1830 const ctype<char_type>& __ct) const;
1831 void __get_year(int& __y,
1832 iter_type& __b, iter_type __e,
1833 ios_base::iostate& __err,
1834 const ctype<char_type>& __ct) const;
1835 void __get_year4(int& __y,
1836 iter_type& __b, iter_type __e,
1837 ios_base::iostate& __err,
1838 const ctype<char_type>& __ct) const;
1839 void __get_hour(int& __d,
1840 iter_type& __b, iter_type __e,
1841 ios_base::iostate& __err,
1842 const ctype<char_type>& __ct) const;
1843 void __get_12_hour(int& __h,
1844 iter_type& __b, iter_type __e,
1845 ios_base::iostate& __err,
1846 const ctype<char_type>& __ct) const;
1847 void __get_am_pm(int& __h,
1848 iter_type& __b, iter_type __e,
1849 ios_base::iostate& __err,
1850 const ctype<char_type>& __ct) const;
1851 void __get_minute(int& __m,
1852 iter_type& __b, iter_type __e,
1853 ios_base::iostate& __err,
1854 const ctype<char_type>& __ct) const;
1855 void __get_second(int& __s,
1856 iter_type& __b, iter_type __e,
1857 ios_base::iostate& __err,
1858 const ctype<char_type>& __ct) const;
1859 void __get_weekday(int& __w,
1860 iter_type& __b, iter_type __e,
1861 ios_base::iostate& __err,
1862 const ctype<char_type>& __ct) const;
1863 void __get_day_year_num(int& __w,
1864 iter_type& __b, iter_type __e,
1865 ios_base::iostate& __err,
1866 const ctype<char_type>& __ct) const;
1867};
1868
1869template <class _CharT, class _InputIterator>
1870locale::id
1871time_get<_CharT, _InputIterator>::id;
1872
1873// time_get primatives
1874
1875template <class _CharT, class _InputIterator>
1876void
1877time_get<_CharT, _InputIterator>::__get_weekdayname(int& __w,
1878 iter_type& __b, iter_type __e,
1879 ios_base::iostate& __err,
1880 const ctype<char_type>& __ct) const
1881{
1882 // Note: ignoring case comes from the POSIX strptime spec
1883 const string_type* __wk = this->__weeks();
1884 int __i = __scan_keyword(__b, __e, __wk, __wk+14, __ct, __err, false) - __wk;
1885 if (__i < 14)
1886 __w = __i % 7;
1887}
1888
1889template <class _CharT, class _InputIterator>
1890void
1891time_get<_CharT, _InputIterator>::__get_monthname(int& __m,
1892 iter_type& __b, iter_type __e,
1893 ios_base::iostate& __err,
1894 const ctype<char_type>& __ct) const
1895{
1896 // Note: ignoring case comes from the POSIX strptime spec
1897 const string_type* __month = this->__months();
1898 int __i = __scan_keyword(__b, __e, __month, __month+24, __ct, __err, false) - __month;
1899 if (__i < 24)
1900 __m = __i % 12;
1901}
1902
1903template <class _CharT, class _InputIterator>
1904void
1905time_get<_CharT, _InputIterator>::__get_day(int& __d,
1906 iter_type& __b, iter_type __e,
1907 ios_base::iostate& __err,
1908 const ctype<char_type>& __ct) const
1909{
1910 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1911 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 31)
1912 __d = __t;
1913 else
1914 __err |= ios_base::failbit;
1915}
1916
1917template <class _CharT, class _InputIterator>
1918void
1919time_get<_CharT, _InputIterator>::__get_month(int& __m,
1920 iter_type& __b, iter_type __e,
1921 ios_base::iostate& __err,
1922 const ctype<char_type>& __ct) const
1923{
1924 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2) - 1;
1925 if (!(__err & ios_base::failbit) && __t <= 11)
1926 __m = __t;
1927 else
1928 __err |= ios_base::failbit;
1929}
1930
1931template <class _CharT, class _InputIterator>
1932void
1933time_get<_CharT, _InputIterator>::__get_year(int& __y,
1934 iter_type& __b, iter_type __e,
1935 ios_base::iostate& __err,
1936 const ctype<char_type>& __ct) const
1937{
1938 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
1939 if (!(__err & ios_base::failbit))
1940 {
1941 if (__t < 69)
1942 __t += 2000;
1943 else if (69 <= __t && __t <= 99)
1944 __t += 1900;
1945 __y = __t - 1900;
1946 }
1947}
1948
1949template <class _CharT, class _InputIterator>
1950void
1951time_get<_CharT, _InputIterator>::__get_year4(int& __y,
1952 iter_type& __b, iter_type __e,
1953 ios_base::iostate& __err,
1954 const ctype<char_type>& __ct) const
1955{
1956 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 4);
1957 if (!(__err & ios_base::failbit))
1958 __y = __t - 1900;
1959}
1960
1961template <class _CharT, class _InputIterator>
1962void
1963time_get<_CharT, _InputIterator>::__get_hour(int& __h,
1964 iter_type& __b, iter_type __e,
1965 ios_base::iostate& __err,
1966 const ctype<char_type>& __ct) const
1967{
1968 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1969 if (!(__err & ios_base::failbit) && __t <= 23)
1970 __h = __t;
1971 else
1972 __err |= ios_base::failbit;
1973}
1974
1975template <class _CharT, class _InputIterator>
1976void
1977time_get<_CharT, _InputIterator>::__get_12_hour(int& __h,
1978 iter_type& __b, iter_type __e,
1979 ios_base::iostate& __err,
1980 const ctype<char_type>& __ct) const
1981{
1982 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1983 if (!(__err & ios_base::failbit) && 1 <= __t && __t <= 12)
1984 __h = __t;
1985 else
1986 __err |= ios_base::failbit;
1987}
1988
1989template <class _CharT, class _InputIterator>
1990void
1991time_get<_CharT, _InputIterator>::__get_minute(int& __m,
1992 iter_type& __b, iter_type __e,
1993 ios_base::iostate& __err,
1994 const ctype<char_type>& __ct) const
1995{
1996 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
1997 if (!(__err & ios_base::failbit) && __t <= 59)
1998 __m = __t;
1999 else
2000 __err |= ios_base::failbit;
2001}
2002
2003template <class _CharT, class _InputIterator>
2004void
2005time_get<_CharT, _InputIterator>::__get_second(int& __s,
2006 iter_type& __b, iter_type __e,
2007 ios_base::iostate& __err,
2008 const ctype<char_type>& __ct) const
2009{
2010 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 2);
2011 if (!(__err & ios_base::failbit) && __t <= 60)
2012 __s = __t;
2013 else
2014 __err |= ios_base::failbit;
2015}
2016
2017template <class _CharT, class _InputIterator>
2018void
2019time_get<_CharT, _InputIterator>::__get_weekday(int& __w,
2020 iter_type& __b, iter_type __e,
2021 ios_base::iostate& __err,
2022 const ctype<char_type>& __ct) const
2023{
2024 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 1);
2025 if (!(__err & ios_base::failbit) && __t <= 6)
2026 __w = __t;
2027 else
2028 __err |= ios_base::failbit;
2029}
2030
2031template <class _CharT, class _InputIterator>
2032void
2033time_get<_CharT, _InputIterator>::__get_day_year_num(int& __d,
2034 iter_type& __b, iter_type __e,
2035 ios_base::iostate& __err,
2036 const ctype<char_type>& __ct) const
2037{
2038 int __t = __get_up_to_n_digits(__b, __e, __err, __ct, 3);
2039 if (!(__err & ios_base::failbit) && __t <= 365)
2040 __d = __t;
2041 else
2042 __err |= ios_base::failbit;
2043}
2044
2045template <class _CharT, class _InputIterator>
2046void
2047time_get<_CharT, _InputIterator>::__get_white_space(iter_type& __b, iter_type __e,
2048 ios_base::iostate& __err,
2049 const ctype<char_type>& __ct) const
2050{
2051 for (; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2052 ;
2053 if (__b == __e)
2054 __err |= ios_base::eofbit;
2055}
2056
2057template <class _CharT, class _InputIterator>
2058void
2059time_get<_CharT, _InputIterator>::__get_am_pm(int& __h,
2060 iter_type& __b, iter_type __e,
2061 ios_base::iostate& __err,
2062 const ctype<char_type>& __ct) const
2063{
2064 const string_type* __ap = this->__am_pm();
2065 if (__ap[0].size() + __ap[1].size() == 0)
2066 {
2067 __err |= ios_base::failbit;
2068 return;
2069 }
2070 int __i = __scan_keyword(__b, __e, __ap, __ap+2, __ct, __err, false) - __ap;
2071 if (__i == 0 && __h == 12)
2072 __h = 0;
2073 else if (__i == 1 && __h < 12)
2074 __h += 12;
2075}
2076
2077template <class _CharT, class _InputIterator>
2078void
2079time_get<_CharT, _InputIterator>::__get_percent(iter_type& __b, iter_type __e,
2080 ios_base::iostate& __err,
2081 const ctype<char_type>& __ct) const
2082{
2083 if (__b == __e)
2084 {
2085 __err |= ios_base::eofbit | ios_base::failbit;
2086 return;
2087 }
2088 if (__ct.narrow(*__b, 0) != '%')
2089 __err |= ios_base::failbit;
2090 else if(++__b == __e)
2091 __err |= ios_base::eofbit;
2092}
2093
2094// time_get end primatives
2095
2096template <class _CharT, class _InputIterator>
2097_InputIterator
2098time_get<_CharT, _InputIterator>::get(iter_type __b, iter_type __e,
2099 ios_base& __iob,
2100 ios_base::iostate& __err, tm* __tm,
2101 const char_type* __fmtb, const char_type* __fmte) const
2102{
2103 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2104 __err = ios_base::goodbit;
2105 while (__fmtb != __fmte && __err == ios_base::goodbit)
2106 {
2107 if (__b == __e)
2108 {
2109 __err = ios_base::failbit;
2110 break;
2111 }
2112 if (__ct.narrow(*__fmtb, 0) == '%')
2113 {
2114 if (++__fmtb == __fmte)
2115 {
2116 __err = ios_base::failbit;
2117 break;
2118 }
2119 char __cmd = __ct.narrow(*__fmtb, 0);
2120 char __opt = '\0';
2121 if (__cmd == 'E' || __cmd == '0')
2122 {
2123 if (++__fmtb == __fmte)
2124 {
2125 __err = ios_base::failbit;
2126 break;
2127 }
2128 __opt = __cmd;
2129 __cmd = __ct.narrow(*__fmtb, 0);
2130 }
2131 __b = do_get(__b, __e, __iob, __err, __tm, __cmd, __opt);
2132 ++__fmtb;
2133 }
2134 else if (__ct.is(ctype_base::space, *__fmtb))
2135 {
2136 for (++__fmtb; __fmtb != __fmte && __ct.is(ctype_base::space, *__fmtb); ++__fmtb)
2137 ;
2138 for ( ; __b != __e && __ct.is(ctype_base::space, *__b); ++__b)
2139 ;
2140 }
2141 else if (__ct.toupper(*__b) == __ct.toupper(*__fmtb))
2142 {
2143 ++__b;
2144 ++__fmtb;
2145 }
2146 else
2147 __err = ios_base::failbit;
2148 }
2149 if (__b == __e)
2150 __err |= ios_base::eofbit;
2151 return __b;
2152}
2153
2154template <class _CharT, class _InputIterator>
2155typename time_get<_CharT, _InputIterator>::dateorder
2156time_get<_CharT, _InputIterator>::do_date_order() const
2157{
2158 return mdy;
2159}
2160
2161template <class _CharT, class _InputIterator>
2162_InputIterator
2163time_get<_CharT, _InputIterator>::do_get_time(iter_type __b, iter_type __e,
2164 ios_base& __iob,
2165 ios_base::iostate& __err,
2166 tm* __tm) const
2167{
2168 const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2169 return get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2170}
2171
2172template <class _CharT, class _InputIterator>
2173_InputIterator
2174time_get<_CharT, _InputIterator>::do_get_date(iter_type __b, iter_type __e,
2175 ios_base& __iob,
2176 ios_base::iostate& __err,
2177 tm* __tm) const
2178{
2179 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2180 const string_type& __fmt = this->__x();
2181 return get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
2182}
2183
2184template <class _CharT, class _InputIterator>
2185_InputIterator
2186time_get<_CharT, _InputIterator>::do_get_weekday(iter_type __b, iter_type __e,
2187 ios_base& __iob,
2188 ios_base::iostate& __err,
2189 tm* __tm) const
2190{
2191 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2192 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2193 return __b;
2194}
2195
2196template <class _CharT, class _InputIterator>
2197_InputIterator
2198time_get<_CharT, _InputIterator>::do_get_monthname(iter_type __b, iter_type __e,
2199 ios_base& __iob,
2200 ios_base::iostate& __err,
2201 tm* __tm) const
2202{
2203 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2204 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2205 return __b;
2206}
2207
2208template <class _CharT, class _InputIterator>
2209_InputIterator
2210time_get<_CharT, _InputIterator>::do_get_year(iter_type __b, iter_type __e,
2211 ios_base& __iob,
2212 ios_base::iostate& __err,
2213 tm* __tm) const
2214{
2215 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2216 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2217 return __b;
2218}
2219
2220template <class _CharT, class _InputIterator>
2221_InputIterator
2222time_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
2223 ios_base& __iob,
2224 ios_base::iostate& __err, tm* __tm,
2225 char __fmt, char) const
2226{
2227 __err = ios_base::goodbit;
2228 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2229 switch (__fmt)
2230 {
2231 case 'a':
2232 case 'A':
2233 __get_weekdayname(__tm->tm_wday, __b, __e, __err, __ct);
2234 break;
2235 case 'b':
2236 case 'B':
2237 case 'h':
2238 __get_monthname(__tm->tm_mon, __b, __e, __err, __ct);
2239 break;
2240 case 'c':
2241 {
2242 const string_type& __fmt = this->__c();
2243 __b = get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
2244 }
2245 break;
2246 case 'd':
2247 case 'e':
2248 __get_day(__tm->tm_mday, __b, __e, __err, __ct);
2249 break;
2250 case 'D':
2251 {
2252 const char_type __fmt[] = {'%', 'm', '/', '%', 'd', '/', '%', 'y'};
2253 __b = get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2254 }
2255 break;
2256 case 'H':
2257 __get_hour(__tm->tm_hour, __b, __e, __err, __ct);
2258 break;
2259 case 'I':
2260 __get_12_hour(__tm->tm_hour, __b, __e, __err, __ct);
2261 break;
2262 case 'j':
2263 __get_day_year_num(__tm->tm_yday, __b, __e, __err, __ct);
2264 break;
2265 case 'm':
2266 __get_month(__tm->tm_mon, __b, __e, __err, __ct);
2267 break;
2268 case 'M':
2269 __get_minute(__tm->tm_min, __b, __e, __err, __ct);
2270 break;
2271 case 'n':
2272 case 't':
2273 __get_white_space(__b, __e, __err, __ct);
2274 break;
2275 case 'p':
2276 __get_am_pm(__tm->tm_hour, __b, __e, __err, __ct);
2277 break;
2278 case 'r':
2279 {
2280 const char_type __fmt[] = {'%', 'I', ':', '%', 'M', ':', '%', 'S', ' ', '%', 'p'};
2281 __b = get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2282 }
2283 break;
2284 case 'R':
2285 {
2286 const char_type __fmt[] = {'%', 'H', ':', '%', 'M'};
2287 __b = get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2288 }
2289 break;
2290 case 'S':
2291 __get_second(__tm->tm_sec, __b, __e, __err, __ct);
2292 break;
2293 case 'T':
2294 {
2295 const char_type __fmt[] = {'%', 'H', ':', '%', 'M', ':', '%', 'S'};
2296 __b = get(__b, __e, __iob, __err, __tm, __fmt, __fmt + sizeof(__fmt)/sizeof(__fmt[0]));
2297 }
2298 break;
2299 case 'w':
2300 __get_weekday(__tm->tm_wday, __b, __e, __err, __ct);
2301 break;
2302 case 'x':
2303 return do_get_date(__b, __e, __iob, __err, __tm);
2304 case 'X':
2305 {
2306 const string_type& __fmt = this->__X();
2307 __b = get(__b, __e, __iob, __err, __tm, __fmt.data(), __fmt.data() + __fmt.size());
2308 }
2309 break;
2310 case 'y':
2311 __get_year(__tm->tm_year, __b, __e, __err, __ct);
2312 break;
2313 case 'Y':
2314 __get_year4(__tm->tm_year, __b, __e, __err, __ct);
2315 break;
2316 case '%':
2317 __get_percent(__b, __e, __err, __ct);
2318 break;
2319 default:
2320 __err |= ios_base::failbit;
2321 }
2322 return __b;
2323}
2324
2325extern template class time_get<char>;
2326extern template class time_get<wchar_t>;
2327
2328class __time_get
2329{
2330protected:
2331 locale_t __loc_;
2332
2333 __time_get(const char* __nm);
2334 __time_get(const string& __nm);
2335 ~__time_get();
2336};
2337
2338template <class _CharT>
2339class __time_get_storage
2340 : public __time_get
2341{
2342protected:
2343 typedef basic_string<_CharT> string_type;
2344
2345 string_type __weeks_[14];
2346 string_type __months_[24];
2347 string_type __am_pm_[2];
2348 string_type __c_;
2349 string_type __r_;
2350 string_type __x_;
2351 string_type __X_;
2352
2353 explicit __time_get_storage(const char* __nm);
2354 explicit __time_get_storage(const string& __nm);
2355
2356 _LIBCPP_ALWAYS_INLINE ~__time_get_storage() {}
2357
2358 time_base::dateorder __do_date_order() const;
2359
2360private:
2361 void init(const ctype<_CharT>&);
2362 string_type __analyze(char __fmt, const ctype<_CharT>&);
2363};
2364
2365template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2366class time_get_byname
2367 : public time_get<_CharT, _InputIterator>,
2368 private __time_get_storage<_CharT>
2369{
2370public:
2371 typedef time_base::dateorder dateorder;
2372 typedef _InputIterator iter_type;
2373 typedef _CharT char_type;
2374 typedef basic_string<char_type> string_type;
2375
2376 explicit time_get_byname(const char* __nm, size_t __refs = 0)
2377 : time_get<_CharT, _InputIterator>(__refs),
2378 __time_get_storage<_CharT>(__nm) {}
2379 explicit time_get_byname(const string& __nm, size_t __refs = 0)
2380 : time_get<_CharT, _InputIterator>(__refs),
2381 __time_get_storage<_CharT>(__nm) {}
2382
2383protected:
2384 ~time_get_byname() {}
2385
2386 virtual dateorder do_date_order() const {return this->__do_date_order();}
2387private:
2388 virtual const string_type* __weeks() const {return this->__weeks_;}
2389 virtual const string_type* __months() const {return this->__months_;}
2390 virtual const string_type* __am_pm() const {return this->__am_pm_;}
2391 virtual const string_type& __c() const {return this->__c_;}
2392 virtual const string_type& __r() const {return this->__r_;}
2393 virtual const string_type& __x() const {return this->__x_;}
2394 virtual const string_type& __X() const {return this->__X_;}
2395};
2396
2397extern template class time_get_byname<char>;
2398extern template class time_get_byname<wchar_t>;
2399
2400class __time_put
2401{
2402 locale_t __loc_;
2403protected:
2404 _LIBCPP_ALWAYS_INLINE __time_put() : __loc_(0) {}
2405 __time_put(const char* __nm);
2406 __time_put(const string& __nm);
2407 ~__time_put();
2408 void __do_put(char* __nb, char*& __ne, const tm* __tm,
2409 char __fmt, char __mod) const;
2410 void __do_put(wchar_t* __wb, wchar_t*& __we, const tm* __tm,
2411 char __fmt, char __mod) const;
2412};
2413
2414template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2415class time_put
2416 : public locale::facet,
2417 private __time_put
2418{
2419public:
2420 typedef _CharT char_type;
2421 typedef _OutputIterator iter_type;
2422
2423 _LIBCPP_ALWAYS_INLINE
2424 explicit time_put(size_t __refs = 0)
2425 : locale::facet(__refs) {}
2426
2427 iter_type put(iter_type __s, ios_base& __iob, char_type __fl, const tm* __tm,
2428 const char_type* __pb, const char_type* __pe) const;
2429
2430 _LIBCPP_ALWAYS_INLINE
2431 iter_type put(iter_type __s, ios_base& __iob, char_type __fl,
2432 const tm* __tm, char __fmt, char __mod = 0) const
2433 {
2434 return do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2435 }
2436
2437 static locale::id id;
2438
2439protected:
2440 ~time_put() {}
2441 virtual iter_type do_put(iter_type __s, ios_base&, char_type, const tm* __tm,
2442 char __fmt, char __mod) const;
2443
2444 explicit time_put(const char* __nm, size_t __refs)
2445 : locale::facet(__refs),
2446 __time_put(__nm) {}
2447 explicit time_put(const string& __nm, size_t __refs)
2448 : locale::facet(__refs),
2449 __time_put(__nm) {}
2450};
2451
2452template <class _CharT, class _OutputIterator>
2453locale::id
2454time_put<_CharT, _OutputIterator>::id;
2455
2456template <class _CharT, class _OutputIterator>
2457_OutputIterator
2458time_put<_CharT, _OutputIterator>::put(iter_type __s, ios_base& __iob,
2459 char_type __fl, const tm* __tm,
2460 const char_type* __pb,
2461 const char_type* __pe) const
2462{
2463 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__iob.getloc());
2464 for (; __pb != __pe; ++__pb)
2465 {
2466 if (__ct.narrow(*__pb, 0) == '%')
2467 {
2468 if (++__pb == __pe)
2469 {
2470 *__s++ = __pb[-1];
2471 break;
2472 }
2473 char __mod = 0;
2474 char __fmt = __ct.narrow(*__pb, 0);
2475 if (__fmt == 'E' || __fmt == 'O')
2476 {
2477 if (++__pb == __pe)
2478 {
2479 *__s++ = __pb[-2];
2480 *__s++ = __pb[-1];
2481 break;
2482 }
2483 __mod = __fmt;
2484 __fmt = __ct.narrow(*__pb, 0);
2485 }
2486 __s = do_put(__s, __iob, __fl, __tm, __fmt, __mod);
2487 }
2488 else
2489 *__s++ = *__pb;
2490 }
2491 return __s;
2492}
2493
2494template <class _CharT, class _OutputIterator>
2495_OutputIterator
2496time_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
2497 char_type, const tm* __tm,
2498 char __fmt, char __mod) const
2499{
2500 char_type __nar[100];
2501 char_type* __nb = __nar;
2502 char_type* __ne = __nb + 100;
2503 __do_put(__nb, __ne, __tm, __fmt, __mod);
2504 return copy(__nb, __ne, __s);
2505}
2506
2507extern template class time_put<char>;
2508extern template class time_put<wchar_t>;
2509
2510template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
2511class time_put_byname
2512 : public time_put<_CharT, _OutputIterator>
2513{
2514public:
2515 _LIBCPP_ALWAYS_INLINE
2516 explicit time_put_byname(const char* __nm, size_t __refs = 0)
2517 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2518
2519 _LIBCPP_ALWAYS_INLINE
2520 explicit time_put_byname(const string& __nm, size_t __refs = 0)
2521 : time_put<_CharT, _OutputIterator>(__nm, __refs) {}
2522
2523protected:
2524 ~time_put_byname() {}
2525};
2526
2527extern template class time_put_byname<char>;
2528extern template class time_put_byname<wchar_t>;
2529
2530// money_base
2531
2532class money_base
2533{
2534public:
2535 enum part {none, space, symbol, sign, value};
2536 struct pattern {char field[4];};
2537
2538 _LIBCPP_ALWAYS_INLINE money_base() {}
2539};
2540
2541// moneypunct
2542
2543template <class _CharT, bool _International = false>
2544class moneypunct
2545 : public locale::facet,
2546 public money_base
2547{
2548public:
2549 typedef _CharT char_type;
2550 typedef basic_string<char_type> string_type;
2551
2552 explicit moneypunct(size_t __refs = 0)
2553 : locale::facet(__refs) {}
2554
2555 _LIBCPP_ALWAYS_INLINE char_type decimal_point() const {return do_decimal_point();}
2556 _LIBCPP_ALWAYS_INLINE char_type thousands_sep() const {return do_thousands_sep();}
2557 _LIBCPP_ALWAYS_INLINE string grouping() const {return do_grouping();}
2558 _LIBCPP_ALWAYS_INLINE string_type curr_symbol() const {return do_curr_symbol();}
2559 _LIBCPP_ALWAYS_INLINE string_type positive_sign() const {return do_positive_sign();}
2560 _LIBCPP_ALWAYS_INLINE string_type negative_sign() const {return do_negative_sign();}
2561 _LIBCPP_ALWAYS_INLINE int frac_digits() const {return do_frac_digits();}
2562 _LIBCPP_ALWAYS_INLINE pattern pos_format() const {return do_pos_format();}
2563 _LIBCPP_ALWAYS_INLINE pattern neg_format() const {return do_neg_format();}
2564
2565 static locale::id id;
2566 static const bool intl = _International;
2567
2568protected:
2569 ~moneypunct() {}
2570
2571 virtual char_type do_decimal_point() const {return numeric_limits<char_type>::max();}
2572 virtual char_type do_thousands_sep() const {return numeric_limits<char_type>::max();}
2573 virtual string do_grouping() const {return string();}
2574 virtual string_type do_curr_symbol() const {return string_type();}
2575 virtual string_type do_positive_sign() const {return string_type();}
2576 virtual string_type do_negative_sign() const {return string_type(1, '-');}
2577 virtual int do_frac_digits() const {return 0;}
2578 virtual pattern do_pos_format() const
2579 {pattern __p = {symbol, sign, none, value}; return __p;}
2580 virtual pattern do_neg_format() const
2581 {pattern __p = {symbol, sign, none, value}; return __p;}
2582};
2583
2584template <class _CharT, bool _International>
2585locale::id
2586moneypunct<_CharT, _International>::id;
2587
2588extern template class moneypunct<char, false>;
2589extern template class moneypunct<char, true>;
2590extern template class moneypunct<wchar_t, false>;
2591extern template class moneypunct<wchar_t, true>;
2592
2593// moneypunct_byname
2594
2595template <class _CharT, bool _International = false>
2596class moneypunct_byname : public moneypunct<_CharT, _International>
2597{
2598public:
2599 typedef money_base::pattern pattern;
2600 typedef _CharT char_type;
2601 typedef basic_string<char_type> string_type;
2602
2603 _LIBCPP_ALWAYS_INLINE
2604 explicit moneypunct_byname(const char* __nm, size_t __refs = 0)
2605 : moneypunct<_CharT, _International>(__refs) {init(__nm);}
2606
2607 _LIBCPP_ALWAYS_INLINE
2608 explicit moneypunct_byname(const string& __nm, size_t __refs = 0)
2609 : moneypunct<_CharT, _International>(__refs) {init(__nm.c_str());}
2610
2611protected:
2612 ~moneypunct_byname() {}
2613
2614 virtual char_type do_decimal_point() const {return __decimal_point_;}
2615 virtual char_type do_thousands_sep() const {return __thousands_sep_;}
2616 virtual string do_grouping() const {return __grouping_;}
2617 virtual string_type do_curr_symbol() const {return __curr_symbol_;}
2618 virtual string_type do_positive_sign() const {return __positive_sign_;}
2619 virtual string_type do_negative_sign() const {return __negative_sign_;}
2620 virtual int do_frac_digits() const {return __frac_digits_;}
2621 virtual pattern do_pos_format() const {return __pos_format_;}
2622 virtual pattern do_neg_format() const {return __neg_format_;}
2623
2624private:
2625 char_type __decimal_point_;
2626 char_type __thousands_sep_;
2627 string __grouping_;
2628 string_type __curr_symbol_;
2629 string_type __positive_sign_;
2630 string_type __negative_sign_;
2631 int __frac_digits_;
2632 pattern __pos_format_;
2633 pattern __neg_format_;
2634
2635 void init(const char*);
2636};
2637
2638template<> void moneypunct_byname<char, false>::init(const char*);
2639template<> void moneypunct_byname<char, true>::init(const char*);
2640template<> void moneypunct_byname<wchar_t, false>::init(const char*);
2641template<> void moneypunct_byname<wchar_t, true>::init(const char*);
2642
2643extern template class moneypunct_byname<char, false>;
2644extern template class moneypunct_byname<char, true>;
2645extern template class moneypunct_byname<wchar_t, false>;
2646extern template class moneypunct_byname<wchar_t, true>;
2647
2648// money_get
2649
2650template <class _CharT>
2651class __money_get
2652{
2653protected:
2654 typedef _CharT char_type;
2655 typedef basic_string<char_type> string_type;
2656
2657 _LIBCPP_ALWAYS_INLINE __money_get() {}
2658
2659 static void __gather_info(bool __intl, const locale& __loc,
2660 money_base::pattern& __pat, char_type& __dp,
2661 char_type& __ts, string& __grp,
2662 string_type& __sym, string_type& __psn,
2663 string_type& __nsn, int& __fd);
2664};
2665
2666template <class _CharT>
2667void
2668__money_get<_CharT>::__gather_info(bool __intl, const locale& __loc,
2669 money_base::pattern& __pat, char_type& __dp,
2670 char_type& __ts, string& __grp,
2671 string_type& __sym, string_type& __psn,
2672 string_type& __nsn, int& __fd)
2673{
2674 if (__intl)
2675 {
2676 const moneypunct<char_type, true>& __mp =
2677 use_facet<moneypunct<char_type, true> >(__loc);
2678 __pat = __mp.neg_format();
2679 __nsn = __mp.negative_sign();
2680 __psn = __mp.positive_sign();
2681 __dp = __mp.decimal_point();
2682 __ts = __mp.thousands_sep();
2683 __grp = __mp.grouping();
2684 __sym = __mp.curr_symbol();
2685 __fd = __mp.frac_digits();
2686 }
2687 else
2688 {
2689 const moneypunct<char_type, false>& __mp =
2690 use_facet<moneypunct<char_type, false> >(__loc);
2691 __pat = __mp.neg_format();
2692 __nsn = __mp.negative_sign();
2693 __psn = __mp.positive_sign();
2694 __dp = __mp.decimal_point();
2695 __ts = __mp.thousands_sep();
2696 __grp = __mp.grouping();
2697 __sym = __mp.curr_symbol();
2698 __fd = __mp.frac_digits();
2699 }
2700}
2701
2702extern template class __money_get<char>;
2703extern template class __money_get<wchar_t>;
2704
2705template <class _CharT, class _InputIterator = istreambuf_iterator<_CharT> >
2706class money_get
2707 : public locale::facet,
2708 private __money_get<_CharT>
2709{
2710public:
2711 typedef _CharT char_type;
2712 typedef _InputIterator iter_type;
2713 typedef basic_string<char_type> string_type;
2714
2715 _LIBCPP_ALWAYS_INLINE
2716 explicit money_get(size_t __refs = 0)
2717 : locale::facet(__refs) {}
2718
2719 _LIBCPP_ALWAYS_INLINE
2720 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2721 ios_base::iostate& __err, long double& __v) const
2722 {
2723 return do_get(__b, __e, __intl, __iob, __err, __v);
2724 }
2725
2726 _LIBCPP_ALWAYS_INLINE
2727 iter_type get(iter_type __b, iter_type __e, bool __intl, ios_base& __iob,
2728 ios_base::iostate& __err, string_type& __v) const
2729 {
2730 return do_get(__b, __e, __intl, __iob, __err, __v);
2731 }
2732
2733 static locale::id id;
2734
2735protected:
2736
2737 ~money_get() {}
2738
2739 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2740 ios_base& __iob, ios_base::iostate& __err,
2741 long double& __v) const;
2742 virtual iter_type do_get(iter_type __b, iter_type __e, bool __intl,
2743 ios_base& __iob, ios_base::iostate& __err,
2744 string_type& __v) const;
2745
2746private:
2747 static bool __do_get(iter_type& __b, iter_type __e,
2748 bool __intl, const locale& __loc,
2749 ios_base::fmtflags __flags, ios_base::iostate& __err,
2750 bool& __neg, const ctype<char_type>& __ct,
2751 unique_ptr<char_type, void(*)(void*)>& __wb,
2752 char_type*& __wn, char_type* __we);
2753};
2754
2755template <class _CharT, class _InputIterator>
2756locale::id
2757money_get<_CharT, _InputIterator>::id;
2758
2759void __do_nothing(void*);
2760
2761template <class _Tp>
2762_LIBCPP_HIDDEN
2763void
2764__double_or_nothing(unique_ptr<_Tp, void(*)(void*)>& __b, _Tp*& __n, _Tp*& __e)
2765{
2766 bool __owns = __b.get_deleter() != __do_nothing;
2767 size_t __cur_cap = (__e-__b.get()) * sizeof(_Tp);
2768 size_t __new_cap = __cur_cap < numeric_limits<size_t>::max() / 2 ?
2769 2 * __cur_cap : numeric_limits<size_t>::max();
2770 size_t __n_off = __n - __b.get();
2771 _Tp* __t = (_Tp*)realloc(__owns ? __b.get() : 0, __new_cap);
2772 if (__t == 0)
2773 __throw_bad_alloc();
2774 if (__owns)
2775 __b.release();
2776 __b = unique_ptr<_Tp, void(*)(void*)>(__t, free);
2777 __new_cap /= sizeof(_Tp);
2778 __n = __b.get() + __n_off;
2779 __e = __b.get() + __new_cap;
2780}
2781
2782// true == success
2783template <class _CharT, class _InputIterator>
2784bool
2785money_get<_CharT, _InputIterator>::__do_get(iter_type& __b, iter_type __e,
2786 bool __intl, const locale& __loc,
2787 ios_base::fmtflags __flags,
2788 ios_base::iostate& __err,
2789 bool& __neg,
2790 const ctype<char_type>& __ct,
2791 unique_ptr<char_type, void(*)(void*)>& __wb,
2792 char_type*& __wn, char_type* __we)
2793{
2794 const unsigned __bz = 100;
2795 unsigned __gbuf[__bz];
2796 unique_ptr<unsigned, void(*)(void*)> __gb(__gbuf, __do_nothing);
2797 unsigned* __gn = __gb.get();
2798 unsigned* __ge = __gn + __bz;
2799 money_base::pattern __pat;
2800 char_type __dp;
2801 char_type __ts;
2802 string __grp;
2803 string_type __sym;
2804 string_type __psn;
2805 string_type __nsn;
2806 int __fd;
2807 __money_get<_CharT>::__gather_info(__intl, __loc, __pat, __dp, __ts, __grp,
2808 __sym, __psn, __nsn, __fd);
2809 const string_type* __trailing_sign = 0;
2810 __wn = __wb.get();
2811 for (unsigned __p = 0; __p < 4 && __b != __e; ++__p)
2812 {
2813 switch (__pat.field[__p])
2814 {
2815 case money_base::space:
2816 if (__p != 3)
2817 {
2818 if (__ct.is(ctype_base::space, *__b))
2819 ++__b;
2820 else
2821 {
2822 __err |= ios_base::failbit;
2823 return false;
2824 }
2825 }
2826 // drop through
2827 case money_base::none:
2828 if (__p != 3)
2829 {
2830 while (__b != __e && __ct.is(ctype_base::space, *__b))
2831 ++__b;
2832 }
2833 break;
2834 case money_base::sign:
2835 if (__psn.size() + __nsn.size() > 0)
2836 {
2837 if (__psn.size() == 0 || __nsn.size() == 0)
2838 { // sign is optional
2839 if (__psn.size() > 0)
2840 { // __nsn.size() == 0
2841 if (*__b == __psn[0])
2842 {
2843 ++__b;
2844 if (__psn.size() > 1)
2845 __trailing_sign = &__psn;
2846 }
2847 else
2848 __neg = true;
2849 }
2850 else if (*__b == __nsn[0]) // __nsn.size() > 0 && __psn.size() == 0
2851 {
2852 ++__b;
2853 __neg = true;
2854 if (__nsn.size() > 1)
2855 __trailing_sign = &__nsn;
2856 }
2857 }
2858 else // sign is required
2859 {
2860 if (*__b == __psn[0])
2861 {
2862 ++__b;
2863 if (__psn.size() > 1)
2864 __trailing_sign = &__psn;
2865 }
2866 else if (*__b == __nsn[0])
2867 {
2868 ++__b;
2869 __neg = true;
2870 if (__nsn.size() > 1)
2871 __trailing_sign = &__nsn;
2872 }
2873 else
2874 {
2875 __err |= ios_base::failbit;
2876 return false;
2877 }
2878 }
2879 }
2880 break;
2881 case money_base::symbol:
2882 {
2883 bool __more_needed = __trailing_sign ||
2884 (__p < 2) ||
2885 (__p == 2 && __pat.field[3] != static_cast<char>(money_base::none));
2886 bool __sb = __flags & ios_base::showbase;
2887 if (__sb || __more_needed)
2888 {
2889 ios_base::iostate __et = ios_base::goodbit;
2890 string_type* __k = __scan_keyword(__b, __e, &__sym, &__sym+1,
2891 __ct, __et);
2892 if (__sb && __k != &__sym)
2893 {
2894 __err |= ios_base::failbit;
2895 return false;
2896 }
2897 }
2898 }
2899 break;
2900 case money_base::value:
2901 {
2902 unsigned __ng = 0;
2903 for (; __b != __e; ++__b)
2904 {
2905 char_type __c = *__b;
2906 if (__ct.is(ctype_base::digit, __c))
2907 {
2908 if (__wn == __we)
2909 __double_or_nothing(__wb, __wn, __we);
2910 *__wn++ = __c;
2911 ++__ng;
2912 }
2913 else if (__grp.size() > 0 && __ng > 0 && __c == __ts)
2914 {
2915 if (__gn == __ge)
2916 __double_or_nothing(__gb, __gn, __ge);
2917 *__gn++ = __ng;
2918 __ng = 0;
2919 }
2920 else
2921 break;
2922 }
2923 if (__gb.get() != __gn && __ng > 0)
2924 {
2925 if (__gn == __ge)
2926 __double_or_nothing(__gb, __gn, __ge);
2927 *__gn++ = __ng;
2928 }
2929 if (__fd > 0)
2930 {
2931 if (__b == __e || *__b != __dp)
2932 {
2933 __err |= ios_base::failbit;
2934 return false;
2935 }
2936 for (++__b; __fd > 0; --__fd, ++__b)
2937 {
2938 if (__b == __e || !__ct.is(ctype_base::digit, *__b))
2939 {
2940 __err |= ios_base::failbit;
2941 return false;
2942 }
2943 if (__wn == __we)
2944 __double_or_nothing(__wb, __wn, __we);
2945 *__wn++ = *__b;
2946 }
2947 }
2948 if (__wn == __wb.get())
2949 {
2950 __err |= ios_base::failbit;
2951 return false;
2952 }
2953 }
2954 break;
2955 }
2956 }
2957 if (__trailing_sign)
2958 {
2959 for (unsigned __i = 1; __i < __trailing_sign->size(); ++__i, ++__b)
2960 {
2961 if (__b == __e || *__b != (*__trailing_sign)[__i])
2962 {
2963 __err |= ios_base::failbit;
2964 return false;
2965 }
2966 }
2967 }
2968 if (__gb.get() != __gn)
2969 {
2970 ios_base::iostate __et = ios_base::goodbit;
2971 __check_grouping(__grp, __gb.get(), __gn, __et);
2972 if (__et)
2973 {
2974 __err |= ios_base::failbit;
2975 return false;
2976 }
2977 }
2978 return true;
2979}
2980
2981template <class _CharT, class _InputIterator>
2982_InputIterator
2983money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
2984 bool __intl, ios_base& __iob,
2985 ios_base::iostate& __err,
2986 long double& __v) const
2987{
2988 const unsigned __bz = 100;
2989 char_type __wbuf[__bz];
2990 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
2991 char_type* __wn;
2992 char_type* __we = __wbuf + __bz;
2993 locale __loc = __iob.getloc();
2994 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
2995 bool __neg = false;
2996 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
2997 __wb, __wn, __we))
2998 {
2999 const char __src[] = "0123456789";
3000 char_type __atoms[sizeof(__src)-1];
3001 __ct.widen(__src, __src + (sizeof(__src)-1), __atoms);
3002 char __nbuf[__bz];
3003 char* __nc = __nbuf;
3004 unique_ptr<char, void(*)(void*)> __h(0, free);
3005 if (__wn - __wb.get() > __bz-2)
3006 {
3007 __h.reset((char*)malloc(__wn - __wb.get() + 2));
3008 if (__h.get() == 0)
3009 __throw_bad_alloc();
3010 __nc = __h.get();
3011 }
3012 if (__neg)
3013 *__nc++ = '-';
3014 for (const char_type* __w = __wb.get(); __w < __wn; ++__w, ++__nc)
3015 *__nc = __src[find(__atoms, __atoms+sizeof(__atoms), *__w) - __atoms];
3016 *__nc = char();
3017 if (sscanf(__nbuf, "%Lf", &__v) != 1)
3018 __throw_runtime_error("money_get error");
3019 }
3020 if (__b == __e)
3021 __err |= ios_base::eofbit;
3022 return __b;
3023}
3024
3025template <class _CharT, class _InputIterator>
3026_InputIterator
3027money_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
3028 bool __intl, ios_base& __iob,
3029 ios_base::iostate& __err,
3030 string_type& __v) const
3031{
3032 const unsigned __bz = 100;
3033 char_type __wbuf[__bz];
3034 unique_ptr<char_type, void(*)(void*)> __wb(__wbuf, __do_nothing);
3035 char_type* __wn;
3036 char_type* __we = __wbuf + __bz;
3037 locale __loc = __iob.getloc();
3038 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3039 bool __neg = false;
3040 if (__do_get(__b, __e, __intl, __loc, __iob.flags(), __err, __neg, __ct,
3041 __wb, __wn, __we))
3042 {
3043 __v.clear();
3044 if (__neg)
3045 __v.push_back(__ct.widen('-'));
3046 char_type __z = __ct.widen('0');
3047 char_type* __w;
3048 for (__w = __wb.get(); __w < __wn-1; ++__w)
3049 if (*__w != __z)
3050 break;
3051 __v.append(__w, __wn);
3052 }
3053 if (__b == __e)
3054 __err |= ios_base::eofbit;
3055 return __b;
3056}
3057
3058extern template class money_get<char>;
3059extern template class money_get<wchar_t>;
3060
3061// money_put
3062
3063template <class _CharT>
3064class __money_put
3065{
3066protected:
3067 typedef _CharT char_type;
3068 typedef basic_string<char_type> string_type;
3069
3070 _LIBCPP_ALWAYS_INLINE __money_put() {}
3071
3072 static void __gather_info(bool __intl, bool __neg, const locale& __loc,
3073 money_base::pattern& __pat, char_type& __dp,
3074 char_type& __ts, string& __grp,
3075 string_type& __sym, string_type& __sn,
3076 int& __fd);
3077 static void __format(char_type* __mb, char_type*& __mi, char_type*& __me,
3078 ios_base::fmtflags __flags,
3079 const char_type* __db, const char_type* __de,
3080 const ctype<char_type>& __ct, bool __neg,
3081 const money_base::pattern& __pat, char_type __dp,
3082 char_type __ts, const string& __grp,
3083 const string_type& __sym, const string_type& __sn,
3084 int __fd);
3085};
3086
3087template <class _CharT>
3088void
3089__money_put<_CharT>::__gather_info(bool __intl, bool __neg, const locale& __loc,
3090 money_base::pattern& __pat, char_type& __dp,
3091 char_type& __ts, string& __grp,
3092 string_type& __sym, string_type& __sn,
3093 int& __fd)
3094{
3095 if (__intl)
3096 {
3097 const moneypunct<char_type, true>& __mp =
3098 use_facet<moneypunct<char_type, true> >(__loc);
3099 if (__neg)
3100 {
3101 __pat = __mp.neg_format();
3102 __sn = __mp.negative_sign();
3103 }
3104 else
3105 {
3106 __pat = __mp.pos_format();
3107 __sn = __mp.positive_sign();
3108 }
3109 __dp = __mp.decimal_point();
3110 __ts = __mp.thousands_sep();
3111 __grp = __mp.grouping();
3112 __sym = __mp.curr_symbol();
3113 __fd = __mp.frac_digits();
3114 }
3115 else
3116 {
3117 const moneypunct<char_type, false>& __mp =
3118 use_facet<moneypunct<char_type, false> >(__loc);
3119 if (__neg)
3120 {
3121 __pat = __mp.neg_format();
3122 __sn = __mp.negative_sign();
3123 }
3124 else
3125 {
3126 __pat = __mp.pos_format();
3127 __sn = __mp.positive_sign();
3128 }
3129 __dp = __mp.decimal_point();
3130 __ts = __mp.thousands_sep();
3131 __grp = __mp.grouping();
3132 __sym = __mp.curr_symbol();
3133 __fd = __mp.frac_digits();
3134 }
3135}
3136
3137template <class _CharT>
3138void
3139__money_put<_CharT>::__format(char_type* __mb, char_type*& __mi, char_type*& __me,
3140 ios_base::fmtflags __flags,
3141 const char_type* __db, const char_type* __de,
3142 const ctype<char_type>& __ct, bool __neg,
3143 const money_base::pattern& __pat, char_type __dp,
3144 char_type __ts, const string& __grp,
3145 const string_type& __sym, const string_type& __sn,
3146 int __fd)
3147{
3148 __me = __mb;
3149 for (unsigned __p = 0; __p < 4; ++__p)
3150 {
3151 switch (__pat.field[__p])
3152 {
3153 case money_base::none:
3154 __mi = __me;
3155 break;
3156 case money_base::space:
3157 __mi = __me;
3158 *__me++ = __ct.widen(' ');
3159 break;
3160 case money_base::sign:
3161 if (!__sn.empty())
3162 *__me++ = __sn[0];
3163 break;
3164 case money_base::symbol:
3165 if (!__sym.empty() && (__flags & ios_base::showbase))
3166 __me = copy(__sym.begin(), __sym.end(), __me);
3167 break;
3168 case money_base::value:
3169 {
3170 // remember start of value so we can reverse it
3171 char_type* __t = __me;
3172 // find beginning of digits
3173 if (__neg)
3174 ++__db;
3175 // find end of digits
3176 const char_type* __d;
3177 for (__d = __db; __d < __de; ++__d)
3178 if (!__ct.is(ctype_base::digit, *__d))
3179 break;
3180 // print fractional part
3181 if (__fd > 0)
3182 {
3183 int __f;
3184 for (__f = __fd; __d > __db && __f > 0; --__f)
3185 *__me++ = *--__d;
3186 char_type __z = __f > 0 ? __ct.widen('0') : char_type();
3187 for (; __f > 0; --__f)
3188 *__me++ = __z;
3189 *__me++ = __dp;
3190 }
3191 // print units part
3192 if (__d == __db)
3193 {
3194 *__me++ = __ct.widen('0');
3195 }
3196 else
3197 {
3198 unsigned __ng = 0;
3199 unsigned __ig = 0;
3200 unsigned __gl = __grp.empty() ? numeric_limits<unsigned>::max()
3201 : static_cast<unsigned>(__grp[__ig]);
3202 while (__d != __db)
3203 {
3204 if (__ng == __gl)
3205 {
3206 *__me++ = __ts;
3207 __ng = 0;
3208 if (++__ig < __grp.size())
3209 __gl = __grp[__ig] == numeric_limits<char>::max() ?
3210 numeric_limits<unsigned>::max() :
3211 static_cast<unsigned>(__grp[__ig]);
3212 }
3213 *__me++ = *--__d;
3214 ++__ng;
3215 }
3216 }
3217 // reverse it
3218 reverse(__t, __me);
3219 }
3220 break;
3221 }
3222 }
3223 // print rest of sign, if any
3224 if (__sn.size() > 1)
3225 __me = copy(__sn.begin()+1, __sn.end(), __me);
3226 // set alignment
3227 if ((__flags & ios_base::adjustfield) == ios_base::left)
3228 __mi = __me;
3229 else if ((__flags & ios_base::adjustfield) != ios_base::internal)
3230 __mi = __mb;
3231}
3232
3233extern template class __money_put<char>;
3234extern template class __money_put<wchar_t>;
3235
3236template <class _CharT, class _OutputIterator = ostreambuf_iterator<_CharT> >
3237class money_put
3238 : public locale::facet,
3239 private __money_put<_CharT>
3240{
3241public:
3242 typedef _CharT char_type;
3243 typedef _OutputIterator iter_type;
3244 typedef basic_string<char_type> string_type;
3245
3246 _LIBCPP_ALWAYS_INLINE
3247 explicit money_put(size_t __refs = 0)
3248 : locale::facet(__refs) {}
3249
3250 _LIBCPP_ALWAYS_INLINE
3251 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3252 long double __units) const
3253 {
3254 return do_put(__s, __intl, __iob, __fl, __units);
3255 }
3256
3257 _LIBCPP_ALWAYS_INLINE
3258 iter_type put(iter_type __s, bool __intl, ios_base& __iob, char_type __fl,
3259 const string_type& __digits) const
3260 {
3261 return do_put(__s, __intl, __iob, __fl, __digits);
3262 }
3263
3264 static locale::id id;
3265
3266protected:
3267 ~money_put() {}
3268
3269 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3270 char_type __fl, long double __units) const;
3271 virtual iter_type do_put(iter_type __s, bool __intl, ios_base& __iob,
3272 char_type __fl, const string_type& __digits) const;
3273};
3274
3275template <class _CharT, class _OutputIterator>
3276locale::id
3277money_put<_CharT, _OutputIterator>::id;
3278
3279template <class _CharT, class _OutputIterator>
3280_OutputIterator
3281money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3282 ios_base& __iob, char_type __fl,
3283 long double __units) const
3284{
3285 // convert to char
3286 const size_t __bs = 100;
3287 char __buf[__bs];
3288 char* __bb = __buf;
3289 char_type __digits[__bs];
3290 char_type* __db = __digits;
3291 size_t __n = snprintf(__bb, __bs, "%.0Lf", __units);
3292 unique_ptr<char, void(*)(void*)> __hn(0, free);
3293 unique_ptr<char_type, void(*)(void*)> __hd(0, free);
3294 // secure memory for digit storage
3295 if (__n > __bs-1)
3296 {
Howard Hinnantadff4892010-05-24 17:49:41 +00003297 __n = __nolocale_asprintf(&__bb, "%.0Lf", __units);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00003298 if (__bb == 0)
3299 __throw_bad_alloc();
3300 __hn.reset(__bb);
3301 __hd.reset((char_type*)malloc(__n * sizeof(char_type)));
3302 if (__hd == 0)
3303 __throw_bad_alloc();
3304 __db = __hd.get();
3305 }
3306 // gather info
3307 locale __loc = __iob.getloc();
3308 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3309 __ct.widen(__bb, __bb + __n, __db);
3310 bool __neg = __n > 0 && __bb[0] == '-';
3311 money_base::pattern __pat;
3312 char_type __dp;
3313 char_type __ts;
3314 string __grp;
3315 string_type __sym;
3316 string_type __sn;
3317 int __fd;
3318 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3319 // secure memory for formatting
3320 char_type __mbuf[__bs];
3321 char_type* __mb = __mbuf;
3322 unique_ptr<char_type, void(*)(void*)> __hw(0, free);
3323 size_t __exn = static_cast<int>(__n) > __fd ?
3324 (__n - __fd) * 2 + __sn.size() + __sym.size() + __fd + 1
3325 : __sn.size() + __sym.size() + __fd + 2;
3326 if (__exn > __bs)
3327 {
3328 __hw.reset((char_type*)malloc(__exn * sizeof(char_type)));
3329 __mb = __hw.get();
3330 if (__mb == 0)
3331 __throw_bad_alloc();
3332 }
3333 // format
3334 char_type* __mi;
3335 char_type* __me;
3336 this->__format(__mb, __mi, __me, __iob.flags(),
3337 __db, __db + __n, __ct,
3338 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3339 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3340}
3341
3342template <class _CharT, class _OutputIterator>
3343_OutputIterator
3344money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
3345 ios_base& __iob, char_type __fl,
3346 const string_type& __digits) const
3347{
3348 // gather info
3349 locale __loc = __iob.getloc();
3350 const ctype<char_type>& __ct = use_facet<ctype<char_type> >(__loc);
3351 bool __neg = __digits.size() > 0 && __digits[0] == __ct.widen('-');
3352 money_base::pattern __pat;
3353 char_type __dp;
3354 char_type __ts;
3355 string __grp;
3356 string_type __sym;
3357 string_type __sn;
3358 int __fd;
3359 this->__gather_info(__intl, __neg, __loc, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3360 // secure memory for formatting
3361 char_type __mbuf[100];
3362 char_type* __mb = __mbuf;
3363 unique_ptr<char_type, void(*)(void*)> __h(0, free);
3364 size_t __exn = __digits.size() > __fd ?
3365 (__digits.size() - __fd) * 2 + __sn.size() + __sym.size() + __fd + 1
3366 : __sn.size() + __sym.size() + __fd + 2;
3367 if (__exn > 100)
3368 {
3369 __h.reset((char_type*)malloc(__exn * sizeof(char_type)));
3370 __mb = __h.get();
3371 if (__mb == 0)
3372 __throw_bad_alloc();
3373 }
3374 // format
3375 char_type* __mi;
3376 char_type* __me;
3377 this->__format(__mb, __mi, __me, __iob.flags(),
3378 __digits.data(), __digits.data() + __digits.size(), __ct,
3379 __neg, __pat, __dp, __ts, __grp, __sym, __sn, __fd);
3380 return __pad_and_output(__s, __mb, __mi, __me, __iob, __fl);
3381}
3382
3383extern template class money_put<char>;
3384extern template class money_put<wchar_t>;
3385
3386// messages
3387
3388class messages_base
3389{
3390public:
3391 typedef nl_catd catalog;
3392
3393 _LIBCPP_ALWAYS_INLINE messages_base() {}
3394};
3395
3396template <class _CharT>
3397class messages
3398 : public locale::facet,
3399 public messages_base
3400{
3401public:
3402 typedef _CharT char_type;
3403 typedef basic_string<_CharT> string_type;
3404
3405 _LIBCPP_ALWAYS_INLINE
3406 explicit messages(size_t __refs = 0)
3407 : locale::facet(__refs) {}
3408
3409 _LIBCPP_ALWAYS_INLINE
3410 catalog open(const basic_string<char>& __nm, const locale& __loc) const
3411 {
3412 return do_open(__nm, __loc);
3413 }
3414
3415 _LIBCPP_ALWAYS_INLINE
3416 string_type get(catalog __c, int __set, int __msgid,
3417 const string_type& __dflt) const
3418 {
3419 return do_get(__c, __set, __msgid, __dflt);
3420 }
3421
3422 _LIBCPP_ALWAYS_INLINE
3423 void close(catalog __c) const
3424 {
3425 do_close(__c);
3426 }
3427
3428 static locale::id id;
3429
3430protected:
3431 ~messages() {}
3432
3433 virtual catalog do_open(const basic_string<char>&, const locale&) const;
3434 virtual string_type do_get(catalog, int __set, int __msgid,
3435 const string_type& __dflt) const;
3436 virtual void do_close(catalog) const;
3437};
3438
3439template <class _CharT>
3440locale::id
3441messages<_CharT>::id;
3442
3443template <class _CharT>
3444typename messages<_CharT>::catalog
3445messages<_CharT>::do_open(const basic_string<char>& __nm, const locale&) const
3446{
3447 return catopen(__nm.c_str(), NL_CAT_LOCALE);
3448}
3449
3450template <class _CharT>
3451typename messages<_CharT>::string_type
3452messages<_CharT>::do_get(catalog __c, int __set, int __msgid,
3453 const string_type& __dflt) const
3454{
3455 string __ndflt;
3456 __narrow_to_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__ndflt),
3457 __dflt.c_str(),
3458 __dflt.c_str() + __dflt.size());
3459 char* __n = catgets(__c, __set, __msgid, __ndflt.c_str());
3460 string_type __w;
3461 __widen_from_utf8<sizeof(char_type)*__CHAR_BIT__>()(back_inserter(__w),
3462 __n, __n + strlen(__n));
3463 return __w;
3464}
3465
3466template <class _CharT>
3467void
3468messages<_CharT>::do_close(catalog __c) const
3469{
3470 catclose(__c);
3471}
3472
3473extern template class messages<char>;
3474extern template class messages<wchar_t>;
3475
3476template <class _CharT>
3477class messages_byname
3478 : public messages<_CharT>
3479{
3480public:
3481 typedef messages_base::catalog catalog;
3482 typedef basic_string<_CharT> string_type;
3483
3484 _LIBCPP_ALWAYS_INLINE
3485 explicit messages_byname(const char*, size_t __refs = 0)
3486 : messages<_CharT>(__refs) {}
3487
3488 _LIBCPP_ALWAYS_INLINE
3489 explicit messages_byname(const string&, size_t __refs = 0)
3490 : messages<_CharT>(__refs) {}
3491
3492protected:
3493 ~messages_byname() {}
3494};
3495
3496extern template class messages_byname<char>;
3497extern template class messages_byname<wchar_t>;
3498
3499_LIBCPP_END_NAMESPACE_STD
3500
3501#endif // _LIBCPP_LOCALE