PR libstdc++/28080 (partial)
[gcc.git] / libstdc++-v3 / include / bits / locale_facets.h
1 // Locale support -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
11 // any later version.
12
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // USA.
22
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
31
32 /** @file locale_facets.h
33 * This is an internal header file, included by other library headers.
34 * You should not attempt to use it directly.
35 */
36
37 //
38 // ISO C++ 14882: 22.1 Locales
39 //
40
41 #ifndef _LOCALE_FACETS_H
42 #define _LOCALE_FACETS_H 1
43
44 #pragma GCC system_header
45
46 #include <ctime> // For struct tm
47 #include <cwctype> // For wctype_t
48 #include <bits/ctype_base.h>
49 #include <iosfwd>
50 #include <bits/ios_base.h> // For ios_base, ios_base::iostate
51 #include <streambuf>
52 #include <bits/cpp_type_traits.h>
53
54 _GLIBCXX_BEGIN_NAMESPACE(std)
55
56 // NB: Don't instantiate required wchar_t facets if no wchar_t support.
57 #ifdef _GLIBCXX_USE_WCHAR_T
58 # define _GLIBCXX_NUM_FACETS 28
59 #else
60 # define _GLIBCXX_NUM_FACETS 14
61 #endif
62
63 // Convert string to numeric value of type _Tv and store results.
64 // NB: This is specialized for all required types, there is no
65 // generic definition.
66 template<typename _Tv>
67 void
68 __convert_to_v(const char* __in, _Tv& __out, ios_base::iostate& __err,
69 const __c_locale& __cloc);
70
71 // Explicit specializations for required types.
72 template<>
73 void
74 __convert_to_v(const char*, float&, ios_base::iostate&,
75 const __c_locale&);
76
77 template<>
78 void
79 __convert_to_v(const char*, double&, ios_base::iostate&,
80 const __c_locale&);
81
82 template<>
83 void
84 __convert_to_v(const char*, long double&, ios_base::iostate&,
85 const __c_locale&);
86
87 // NB: __pad is a struct, rather than a function, so it can be
88 // partially-specialized.
89 template<typename _CharT, typename _Traits>
90 struct __pad
91 {
92 static void
93 _S_pad(ios_base& __io, _CharT __fill, _CharT* __news,
94 const _CharT* __olds, const streamsize __newlen,
95 const streamsize __oldlen, const bool __num);
96 };
97
98 // Used by both numeric and monetary facets.
99 // Inserts "group separator" characters into an array of characters.
100 // It's recursive, one iteration per group. It moves the characters
101 // in the buffer this way: "xxxx12345" -> "12,345xxx". Call this
102 // only with __glen != 0.
103 template<typename _CharT>
104 _CharT*
105 __add_grouping(_CharT* __s, _CharT __sep,
106 const char* __gbeg, size_t __gsize,
107 const _CharT* __first, const _CharT* __last);
108
109 // This template permits specializing facet output code for
110 // ostreambuf_iterator. For ostreambuf_iterator, sputn is
111 // significantly more efficient than incrementing iterators.
112 template<typename _CharT>
113 inline
114 ostreambuf_iterator<_CharT>
115 __write(ostreambuf_iterator<_CharT> __s, const _CharT* __ws, int __len)
116 {
117 __s._M_put(__ws, __len);
118 return __s;
119 }
120
121 // This is the unspecialized form of the template.
122 template<typename _CharT, typename _OutIter>
123 inline
124 _OutIter
125 __write(_OutIter __s, const _CharT* __ws, int __len)
126 {
127 for (int __j = 0; __j < __len; __j++, ++__s)
128 *__s = __ws[__j];
129 return __s;
130 }
131
132
133 // 22.2.1.1 Template class ctype
134 // Include host and configuration specific ctype enums for ctype_base.
135
136 // Common base for ctype<_CharT>.
137 /**
138 * @brief Common base for ctype facet
139 *
140 * This template class provides implementations of the public functions
141 * that forward to the protected virtual functions.
142 *
143 * This template also provides abtract stubs for the protected virtual
144 * functions.
145 */
146 template<typename _CharT>
147 class __ctype_abstract_base : public locale::facet, public ctype_base
148 {
149 public:
150 // Types:
151 /// Typedef for the template parameter
152 typedef _CharT char_type;
153
154 /**
155 * @brief Test char_type classification.
156 *
157 * This function finds a mask M for @a c and compares it to mask @a m.
158 * It does so by returning the value of ctype<char_type>::do_is().
159 *
160 * @param c The char_type to compare the mask of.
161 * @param m The mask to compare against.
162 * @return (M & m) != 0.
163 */
164 bool
165 is(mask __m, char_type __c) const
166 { return this->do_is(__m, __c); }
167
168 /**
169 * @brief Return a mask array.
170 *
171 * This function finds the mask for each char_type in the range [lo,hi)
172 * and successively writes it to vec. vec must have as many elements
173 * as the char array. It does so by returning the value of
174 * ctype<char_type>::do_is().
175 *
176 * @param lo Pointer to start of range.
177 * @param hi Pointer to end of range.
178 * @param vec Pointer to an array of mask storage.
179 * @return @a hi.
180 */
181 const char_type*
182 is(const char_type *__lo, const char_type *__hi, mask *__vec) const
183 { return this->do_is(__lo, __hi, __vec); }
184
185 /**
186 * @brief Find char_type matching a mask
187 *
188 * This function searches for and returns the first char_type c in
189 * [lo,hi) for which is(m,c) is true. It does so by returning
190 * ctype<char_type>::do_scan_is().
191 *
192 * @param m The mask to compare against.
193 * @param lo Pointer to start of range.
194 * @param hi Pointer to end of range.
195 * @return Pointer to matching char_type if found, else @a hi.
196 */
197 const char_type*
198 scan_is(mask __m, const char_type* __lo, const char_type* __hi) const
199 { return this->do_scan_is(__m, __lo, __hi); }
200
201 /**
202 * @brief Find char_type not matching a mask
203 *
204 * This function searches for and returns the first char_type c in
205 * [lo,hi) for which is(m,c) is false. It does so by returning
206 * ctype<char_type>::do_scan_not().
207 *
208 * @param m The mask to compare against.
209 * @param lo Pointer to first char in range.
210 * @param hi Pointer to end of range.
211 * @return Pointer to non-matching char if found, else @a hi.
212 */
213 const char_type*
214 scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
215 { return this->do_scan_not(__m, __lo, __hi); }
216
217 /**
218 * @brief Convert to uppercase.
219 *
220 * This function converts the argument to uppercase if possible.
221 * If not possible (for example, '2'), returns the argument. It does
222 * so by returning ctype<char_type>::do_toupper().
223 *
224 * @param c The char_type to convert.
225 * @return The uppercase char_type if convertible, else @a c.
226 */
227 char_type
228 toupper(char_type __c) const
229 { return this->do_toupper(__c); }
230
231 /**
232 * @brief Convert array to uppercase.
233 *
234 * This function converts each char_type in the range [lo,hi) to
235 * uppercase if possible. Other elements remain untouched. It does so
236 * by returning ctype<char_type>:: do_toupper(lo, hi).
237 *
238 * @param lo Pointer to start of range.
239 * @param hi Pointer to end of range.
240 * @return @a hi.
241 */
242 const char_type*
243 toupper(char_type *__lo, const char_type* __hi) const
244 { return this->do_toupper(__lo, __hi); }
245
246 /**
247 * @brief Convert to lowercase.
248 *
249 * This function converts the argument to lowercase if possible. If
250 * not possible (for example, '2'), returns the argument. It does so
251 * by returning ctype<char_type>::do_tolower(c).
252 *
253 * @param c The char_type to convert.
254 * @return The lowercase char_type if convertible, else @a c.
255 */
256 char_type
257 tolower(char_type __c) const
258 { return this->do_tolower(__c); }
259
260 /**
261 * @brief Convert array to lowercase.
262 *
263 * This function converts each char_type in the range [lo,hi) to
264 * lowercase if possible. Other elements remain untouched. It does so
265 * by returning ctype<char_type>:: do_tolower(lo, hi).
266 *
267 * @param lo Pointer to start of range.
268 * @param hi Pointer to end of range.
269 * @return @a hi.
270 */
271 const char_type*
272 tolower(char_type* __lo, const char_type* __hi) const
273 { return this->do_tolower(__lo, __hi); }
274
275 /**
276 * @brief Widen char to char_type
277 *
278 * This function converts the char argument to char_type using the
279 * simplest reasonable transformation. It does so by returning
280 * ctype<char_type>::do_widen(c).
281 *
282 * Note: this is not what you want for codepage conversions. See
283 * codecvt for that.
284 *
285 * @param c The char to convert.
286 * @return The converted char_type.
287 */
288 char_type
289 widen(char __c) const
290 { return this->do_widen(__c); }
291
292 /**
293 * @brief Widen array to char_type
294 *
295 * This function converts each char in the input to char_type using the
296 * simplest reasonable transformation. It does so by returning
297 * ctype<char_type>::do_widen(c).
298 *
299 * Note: this is not what you want for codepage conversions. See
300 * codecvt for that.
301 *
302 * @param lo Pointer to start of range.
303 * @param hi Pointer to end of range.
304 * @param to Pointer to the destination array.
305 * @return @a hi.
306 */
307 const char*
308 widen(const char* __lo, const char* __hi, char_type* __to) const
309 { return this->do_widen(__lo, __hi, __to); }
310
311 /**
312 * @brief Narrow char_type to char
313 *
314 * This function converts the char_type to char using the simplest
315 * reasonable transformation. If the conversion fails, dfault is
316 * returned instead. It does so by returning
317 * ctype<char_type>::do_narrow(c).
318 *
319 * Note: this is not what you want for codepage conversions. See
320 * codecvt for that.
321 *
322 * @param c The char_type to convert.
323 * @param dfault Char to return if conversion fails.
324 * @return The converted char.
325 */
326 char
327 narrow(char_type __c, char __dfault) const
328 { return this->do_narrow(__c, __dfault); }
329
330 /**
331 * @brief Narrow array to char array
332 *
333 * This function converts each char_type in the input to char using the
334 * simplest reasonable transformation and writes the results to the
335 * destination array. For any char_type in the input that cannot be
336 * converted, @a dfault is used instead. It does so by returning
337 * ctype<char_type>::do_narrow(lo, hi, dfault, to).
338 *
339 * Note: this is not what you want for codepage conversions. See
340 * codecvt for that.
341 *
342 * @param lo Pointer to start of range.
343 * @param hi Pointer to end of range.
344 * @param dfault Char to use if conversion fails.
345 * @param to Pointer to the destination array.
346 * @return @a hi.
347 */
348 const char_type*
349 narrow(const char_type* __lo, const char_type* __hi,
350 char __dfault, char *__to) const
351 { return this->do_narrow(__lo, __hi, __dfault, __to); }
352
353 protected:
354 explicit
355 __ctype_abstract_base(size_t __refs = 0): facet(__refs) { }
356
357 virtual
358 ~__ctype_abstract_base() { }
359
360 /**
361 * @brief Test char_type classification.
362 *
363 * This function finds a mask M for @a c and compares it to mask @a m.
364 *
365 * do_is() is a hook for a derived facet to change the behavior of
366 * classifying. do_is() must always return the same result for the
367 * same input.
368 *
369 * @param c The char_type to find the mask of.
370 * @param m The mask to compare against.
371 * @return (M & m) != 0.
372 */
373 virtual bool
374 do_is(mask __m, char_type __c) const = 0;
375
376 /**
377 * @brief Return a mask array.
378 *
379 * This function finds the mask for each char_type in the range [lo,hi)
380 * and successively writes it to vec. vec must have as many elements
381 * as the input.
382 *
383 * do_is() is a hook for a derived facet to change the behavior of
384 * classifying. do_is() must always return the same result for the
385 * same input.
386 *
387 * @param lo Pointer to start of range.
388 * @param hi Pointer to end of range.
389 * @param vec Pointer to an array of mask storage.
390 * @return @a hi.
391 */
392 virtual const char_type*
393 do_is(const char_type* __lo, const char_type* __hi,
394 mask* __vec) const = 0;
395
396 /**
397 * @brief Find char_type matching mask
398 *
399 * This function searches for and returns the first char_type c in
400 * [lo,hi) for which is(m,c) is true.
401 *
402 * do_scan_is() is a hook for a derived facet to change the behavior of
403 * match searching. do_is() must always return the same result for the
404 * same input.
405 *
406 * @param m The mask to compare against.
407 * @param lo Pointer to start of range.
408 * @param hi Pointer to end of range.
409 * @return Pointer to a matching char_type if found, else @a hi.
410 */
411 virtual const char_type*
412 do_scan_is(mask __m, const char_type* __lo,
413 const char_type* __hi) const = 0;
414
415 /**
416 * @brief Find char_type not matching mask
417 *
418 * This function searches for and returns a pointer to the first
419 * char_type c of [lo,hi) for which is(m,c) is false.
420 *
421 * do_scan_is() is a hook for a derived facet to change the behavior of
422 * match searching. do_is() must always return the same result for the
423 * same input.
424 *
425 * @param m The mask to compare against.
426 * @param lo Pointer to start of range.
427 * @param hi Pointer to end of range.
428 * @return Pointer to a non-matching char_type if found, else @a hi.
429 */
430 virtual const char_type*
431 do_scan_not(mask __m, const char_type* __lo,
432 const char_type* __hi) const = 0;
433
434 /**
435 * @brief Convert to uppercase.
436 *
437 * This virtual function converts the char_type argument to uppercase
438 * if possible. If not possible (for example, '2'), returns the
439 * argument.
440 *
441 * do_toupper() is a hook for a derived facet to change the behavior of
442 * uppercasing. do_toupper() must always return the same result for
443 * the same input.
444 *
445 * @param c The char_type to convert.
446 * @return The uppercase char_type if convertible, else @a c.
447 */
448 virtual char_type
449 do_toupper(char_type) const = 0;
450
451 /**
452 * @brief Convert array to uppercase.
453 *
454 * This virtual function converts each char_type in the range [lo,hi)
455 * to uppercase if possible. Other elements remain untouched.
456 *
457 * do_toupper() is a hook for a derived facet to change the behavior of
458 * uppercasing. do_toupper() must always return the same result for
459 * the same input.
460 *
461 * @param lo Pointer to start of range.
462 * @param hi Pointer to end of range.
463 * @return @a hi.
464 */
465 virtual const char_type*
466 do_toupper(char_type* __lo, const char_type* __hi) const = 0;
467
468 /**
469 * @brief Convert to lowercase.
470 *
471 * This virtual function converts the argument to lowercase if
472 * possible. If not possible (for example, '2'), returns the argument.
473 *
474 * do_tolower() is a hook for a derived facet to change the behavior of
475 * lowercasing. do_tolower() must always return the same result for
476 * the same input.
477 *
478 * @param c The char_type to convert.
479 * @return The lowercase char_type if convertible, else @a c.
480 */
481 virtual char_type
482 do_tolower(char_type) const = 0;
483
484 /**
485 * @brief Convert array to lowercase.
486 *
487 * This virtual function converts each char_type in the range [lo,hi)
488 * to lowercase if possible. Other elements remain untouched.
489 *
490 * do_tolower() is a hook for a derived facet to change the behavior of
491 * lowercasing. do_tolower() must always return the same result for
492 * the same input.
493 *
494 * @param lo Pointer to start of range.
495 * @param hi Pointer to end of range.
496 * @return @a hi.
497 */
498 virtual const char_type*
499 do_tolower(char_type* __lo, const char_type* __hi) const = 0;
500
501 /**
502 * @brief Widen char
503 *
504 * This virtual function converts the char to char_type using the
505 * simplest reasonable transformation.
506 *
507 * do_widen() is a hook for a derived facet to change the behavior of
508 * widening. do_widen() must always return the same result for the
509 * same input.
510 *
511 * Note: this is not what you want for codepage conversions. See
512 * codecvt for that.
513 *
514 * @param c The char to convert.
515 * @return The converted char_type
516 */
517 virtual char_type
518 do_widen(char) const = 0;
519
520 /**
521 * @brief Widen char array
522 *
523 * This function converts each char in the input to char_type using the
524 * simplest reasonable transformation.
525 *
526 * do_widen() is a hook for a derived facet to change the behavior of
527 * widening. do_widen() must always return the same result for the
528 * same input.
529 *
530 * Note: this is not what you want for codepage conversions. See
531 * codecvt for that.
532 *
533 * @param lo Pointer to start range.
534 * @param hi Pointer to end of range.
535 * @param to Pointer to the destination array.
536 * @return @a hi.
537 */
538 virtual const char*
539 do_widen(const char* __lo, const char* __hi,
540 char_type* __dest) const = 0;
541
542 /**
543 * @brief Narrow char_type to char
544 *
545 * This virtual function converts the argument to char using the
546 * simplest reasonable transformation. If the conversion fails, dfault
547 * is returned instead.
548 *
549 * do_narrow() is a hook for a derived facet to change the behavior of
550 * narrowing. do_narrow() must always return the same result for the
551 * same input.
552 *
553 * Note: this is not what you want for codepage conversions. See
554 * codecvt for that.
555 *
556 * @param c The char_type to convert.
557 * @param dfault Char to return if conversion fails.
558 * @return The converted char.
559 */
560 virtual char
561 do_narrow(char_type, char __dfault) const = 0;
562
563 /**
564 * @brief Narrow char_type array to char
565 *
566 * This virtual function converts each char_type in the range [lo,hi) to
567 * char using the simplest reasonable transformation and writes the
568 * results to the destination array. For any element in the input that
569 * cannot be converted, @a dfault is used instead.
570 *
571 * do_narrow() is a hook for a derived facet to change the behavior of
572 * narrowing. do_narrow() must always return the same result for the
573 * same input.
574 *
575 * Note: this is not what you want for codepage conversions. See
576 * codecvt for that.
577 *
578 * @param lo Pointer to start of range.
579 * @param hi Pointer to end of range.
580 * @param dfault Char to use if conversion fails.
581 * @param to Pointer to the destination array.
582 * @return @a hi.
583 */
584 virtual const char_type*
585 do_narrow(const char_type* __lo, const char_type* __hi,
586 char __dfault, char* __dest) const = 0;
587 };
588
589 // NB: Generic, mostly useless implementation.
590 /**
591 * @brief Template ctype facet
592 *
593 * This template class defines classification and conversion functions for
594 * character sets. It wraps <cctype> functionality. Ctype gets used by
595 * streams for many I/O operations.
596 *
597 * This template provides the protected virtual functions the developer
598 * will have to replace in a derived class or specialization to make a
599 * working facet. The public functions that access them are defined in
600 * __ctype_abstract_base, to allow for implementation flexibility. See
601 * ctype<wchar_t> for an example. The functions are documented in
602 * __ctype_abstract_base.
603 *
604 * Note: implementations are provided for all the protected virtual
605 * functions, but will likely not be useful.
606 */
607 template<typename _CharT>
608 class ctype : public __ctype_abstract_base<_CharT>
609 {
610 public:
611 // Types:
612 typedef _CharT char_type;
613 typedef typename __ctype_abstract_base<_CharT>::mask mask;
614
615 /// The facet id for ctype<char_type>
616 static locale::id id;
617
618 explicit
619 ctype(size_t __refs = 0) : __ctype_abstract_base<_CharT>(__refs) { }
620
621 protected:
622 virtual
623 ~ctype();
624
625 virtual bool
626 do_is(mask __m, char_type __c) const;
627
628 virtual const char_type*
629 do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
630
631 virtual const char_type*
632 do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
633
634 virtual const char_type*
635 do_scan_not(mask __m, const char_type* __lo,
636 const char_type* __hi) const;
637
638 virtual char_type
639 do_toupper(char_type __c) const;
640
641 virtual const char_type*
642 do_toupper(char_type* __lo, const char_type* __hi) const;
643
644 virtual char_type
645 do_tolower(char_type __c) const;
646
647 virtual const char_type*
648 do_tolower(char_type* __lo, const char_type* __hi) const;
649
650 virtual char_type
651 do_widen(char __c) const;
652
653 virtual const char*
654 do_widen(const char* __lo, const char* __hi, char_type* __dest) const;
655
656 virtual char
657 do_narrow(char_type, char __dfault) const;
658
659 virtual const char_type*
660 do_narrow(const char_type* __lo, const char_type* __hi,
661 char __dfault, char* __dest) const;
662 };
663
664 template<typename _CharT>
665 locale::id ctype<_CharT>::id;
666
667 // 22.2.1.3 ctype<char> specialization.
668 /**
669 * @brief The ctype<char> specialization.
670 *
671 * This class defines classification and conversion functions for
672 * the char type. It gets used by char streams for many I/O
673 * operations. The char specialization provides a number of
674 * optimizations as well.
675 */
676 template<>
677 class ctype<char> : public locale::facet, public ctype_base
678 {
679 public:
680 // Types:
681 /// Typedef for the template parameter char.
682 typedef char char_type;
683
684 protected:
685 // Data Members:
686 __c_locale _M_c_locale_ctype;
687 bool _M_del;
688 __to_type _M_toupper;
689 __to_type _M_tolower;
690 const mask* _M_table;
691 mutable char _M_widen_ok;
692 mutable char _M_widen[1 + static_cast<unsigned char>(-1)];
693 mutable char _M_narrow[1 + static_cast<unsigned char>(-1)];
694 mutable char _M_narrow_ok; // 0 uninitialized, 1 init,
695 // 2 memcpy can't be used
696
697 public:
698 /// The facet id for ctype<char>
699 static locale::id id;
700 /// The size of the mask table. It is SCHAR_MAX + 1.
701 static const size_t table_size = 1 + static_cast<unsigned char>(-1);
702
703 /**
704 * @brief Constructor performs initialization.
705 *
706 * This is the constructor provided by the standard.
707 *
708 * @param table If non-zero, table is used as the per-char mask.
709 * Else classic_table() is used.
710 * @param del If true, passes ownership of table to this facet.
711 * @param refs Passed to the base facet class.
712 */
713 explicit
714 ctype(const mask* __table = 0, bool __del = false, size_t __refs = 0);
715
716 /**
717 * @brief Constructor performs static initialization.
718 *
719 * This constructor is used to construct the initial C locale facet.
720 *
721 * @param cloc Handle to C locale data.
722 * @param table If non-zero, table is used as the per-char mask.
723 * @param del If true, passes ownership of table to this facet.
724 * @param refs Passed to the base facet class.
725 */
726 explicit
727 ctype(__c_locale __cloc, const mask* __table = 0, bool __del = false,
728 size_t __refs = 0);
729
730 /**
731 * @brief Test char classification.
732 *
733 * This function compares the mask table[c] to @a m.
734 *
735 * @param c The char to compare the mask of.
736 * @param m The mask to compare against.
737 * @return True if m & table[c] is true, false otherwise.
738 */
739 inline bool
740 is(mask __m, char __c) const;
741
742 /**
743 * @brief Return a mask array.
744 *
745 * This function finds the mask for each char in the range [lo, hi) and
746 * successively writes it to vec. vec must have as many elements as
747 * the char array.
748 *
749 * @param lo Pointer to start of range.
750 * @param hi Pointer to end of range.
751 * @param vec Pointer to an array of mask storage.
752 * @return @a hi.
753 */
754 inline const char*
755 is(const char* __lo, const char* __hi, mask* __vec) const;
756
757 /**
758 * @brief Find char matching a mask
759 *
760 * This function searches for and returns the first char in [lo,hi) for
761 * which is(m,char) is true.
762 *
763 * @param m The mask to compare against.
764 * @param lo Pointer to start of range.
765 * @param hi Pointer to end of range.
766 * @return Pointer to a matching char if found, else @a hi.
767 */
768 inline const char*
769 scan_is(mask __m, const char* __lo, const char* __hi) const;
770
771 /**
772 * @brief Find char not matching a mask
773 *
774 * This function searches for and returns a pointer to the first char
775 * in [lo,hi) for which is(m,char) is false.
776 *
777 * @param m The mask to compare against.
778 * @param lo Pointer to start of range.
779 * @param hi Pointer to end of range.
780 * @return Pointer to a non-matching char if found, else @a hi.
781 */
782 inline const char*
783 scan_not(mask __m, const char* __lo, const char* __hi) const;
784
785 /**
786 * @brief Convert to uppercase.
787 *
788 * This function converts the char argument to uppercase if possible.
789 * If not possible (for example, '2'), returns the argument.
790 *
791 * toupper() acts as if it returns ctype<char>::do_toupper(c).
792 * do_toupper() must always return the same result for the same input.
793 *
794 * @param c The char to convert.
795 * @return The uppercase char if convertible, else @a c.
796 */
797 char_type
798 toupper(char_type __c) const
799 { return this->do_toupper(__c); }
800
801 /**
802 * @brief Convert array to uppercase.
803 *
804 * This function converts each char in the range [lo,hi) to uppercase
805 * if possible. Other chars remain untouched.
806 *
807 * toupper() acts as if it returns ctype<char>:: do_toupper(lo, hi).
808 * do_toupper() must always return the same result for the same input.
809 *
810 * @param lo Pointer to first char in range.
811 * @param hi Pointer to end of range.
812 * @return @a hi.
813 */
814 const char_type*
815 toupper(char_type *__lo, const char_type* __hi) const
816 { return this->do_toupper(__lo, __hi); }
817
818 /**
819 * @brief Convert to lowercase.
820 *
821 * This function converts the char argument to lowercase if possible.
822 * If not possible (for example, '2'), returns the argument.
823 *
824 * tolower() acts as if it returns ctype<char>::do_tolower(c).
825 * do_tolower() must always return the same result for the same input.
826 *
827 * @param c The char to convert.
828 * @return The lowercase char if convertible, else @a c.
829 */
830 char_type
831 tolower(char_type __c) const
832 { return this->do_tolower(__c); }
833
834 /**
835 * @brief Convert array to lowercase.
836 *
837 * This function converts each char in the range [lo,hi) to lowercase
838 * if possible. Other chars remain untouched.
839 *
840 * tolower() acts as if it returns ctype<char>:: do_tolower(lo, hi).
841 * do_tolower() must always return the same result for the same input.
842 *
843 * @param lo Pointer to first char in range.
844 * @param hi Pointer to end of range.
845 * @return @a hi.
846 */
847 const char_type*
848 tolower(char_type* __lo, const char_type* __hi) const
849 { return this->do_tolower(__lo, __hi); }
850
851 /**
852 * @brief Widen char
853 *
854 * This function converts the char to char_type using the simplest
855 * reasonable transformation. For an underived ctype<char> facet, the
856 * argument will be returned unchanged.
857 *
858 * This function works as if it returns ctype<char>::do_widen(c).
859 * do_widen() must always return the same result for the same input.
860 *
861 * Note: this is not what you want for codepage conversions. See
862 * codecvt for that.
863 *
864 * @param c The char to convert.
865 * @return The converted character.
866 */
867 char_type
868 widen(char __c) const
869 {
870 if (_M_widen_ok)
871 return _M_widen[static_cast<unsigned char>(__c)];
872 this->_M_widen_init();
873 return this->do_widen(__c);
874 }
875
876 /**
877 * @brief Widen char array
878 *
879 * This function converts each char in the input to char using the
880 * simplest reasonable transformation. For an underived ctype<char>
881 * facet, the argument will be copied unchanged.
882 *
883 * This function works as if it returns ctype<char>::do_widen(c).
884 * do_widen() must always return the same result for the same input.
885 *
886 * Note: this is not what you want for codepage conversions. See
887 * codecvt for that.
888 *
889 * @param lo Pointer to first char in range.
890 * @param hi Pointer to end of range.
891 * @param to Pointer to the destination array.
892 * @return @a hi.
893 */
894 const char*
895 widen(const char* __lo, const char* __hi, char_type* __to) const
896 {
897 if (_M_widen_ok == 1)
898 {
899 memcpy(__to, __lo, __hi - __lo);
900 return __hi;
901 }
902 if (!_M_widen_ok)
903 _M_widen_init();
904 return this->do_widen(__lo, __hi, __to);
905 }
906
907 /**
908 * @brief Narrow char
909 *
910 * This function converts the char to char using the simplest
911 * reasonable transformation. If the conversion fails, dfault is
912 * returned instead. For an underived ctype<char> facet, @a c
913 * will be returned unchanged.
914 *
915 * This function works as if it returns ctype<char>::do_narrow(c).
916 * do_narrow() must always return the same result for the same input.
917 *
918 * Note: this is not what you want for codepage conversions. See
919 * codecvt for that.
920 *
921 * @param c The char to convert.
922 * @param dfault Char to return if conversion fails.
923 * @return The converted character.
924 */
925 char
926 narrow(char_type __c, char __dfault) const
927 {
928 if (_M_narrow[static_cast<unsigned char>(__c)])
929 return _M_narrow[static_cast<unsigned char>(__c)];
930 const char __t = do_narrow(__c, __dfault);
931 if (__t != __dfault)
932 _M_narrow[static_cast<unsigned char>(__c)] = __t;
933 return __t;
934 }
935
936 /**
937 * @brief Narrow char array
938 *
939 * This function converts each char in the input to char using the
940 * simplest reasonable transformation and writes the results to the
941 * destination array. For any char in the input that cannot be
942 * converted, @a dfault is used instead. For an underived ctype<char>
943 * facet, the argument will be copied unchanged.
944 *
945 * This function works as if it returns ctype<char>::do_narrow(lo, hi,
946 * dfault, to). do_narrow() must always return the same result for the
947 * same input.
948 *
949 * Note: this is not what you want for codepage conversions. See
950 * codecvt for that.
951 *
952 * @param lo Pointer to start of range.
953 * @param hi Pointer to end of range.
954 * @param dfault Char to use if conversion fails.
955 * @param to Pointer to the destination array.
956 * @return @a hi.
957 */
958 const char_type*
959 narrow(const char_type* __lo, const char_type* __hi,
960 char __dfault, char *__to) const
961 {
962 if (__builtin_expect(_M_narrow_ok == 1, true))
963 {
964 memcpy(__to, __lo, __hi - __lo);
965 return __hi;
966 }
967 if (!_M_narrow_ok)
968 _M_narrow_init();
969 return this->do_narrow(__lo, __hi, __dfault, __to);
970 }
971
972 protected:
973 /// Returns a pointer to the mask table provided to the constructor, or
974 /// the default from classic_table() if none was provided.
975 const mask*
976 table() const throw()
977 { return _M_table; }
978
979 /// Returns a pointer to the C locale mask table.
980 static const mask*
981 classic_table() throw();
982
983 /**
984 * @brief Destructor.
985 *
986 * This function deletes table() if @a del was true in the
987 * constructor.
988 */
989 virtual
990 ~ctype();
991
992 /**
993 * @brief Convert to uppercase.
994 *
995 * This virtual function converts the char argument to uppercase if
996 * possible. If not possible (for example, '2'), returns the argument.
997 *
998 * do_toupper() is a hook for a derived facet to change the behavior of
999 * uppercasing. do_toupper() must always return the same result for
1000 * the same input.
1001 *
1002 * @param c The char to convert.
1003 * @return The uppercase char if convertible, else @a c.
1004 */
1005 virtual char_type
1006 do_toupper(char_type) const;
1007
1008 /**
1009 * @brief Convert array to uppercase.
1010 *
1011 * This virtual function converts each char in the range [lo,hi) to
1012 * uppercase if possible. Other chars remain untouched.
1013 *
1014 * do_toupper() is a hook for a derived facet to change the behavior of
1015 * uppercasing. do_toupper() must always return the same result for
1016 * the same input.
1017 *
1018 * @param lo Pointer to start of range.
1019 * @param hi Pointer to end of range.
1020 * @return @a hi.
1021 */
1022 virtual const char_type*
1023 do_toupper(char_type* __lo, const char_type* __hi) const;
1024
1025 /**
1026 * @brief Convert to lowercase.
1027 *
1028 * This virtual function converts the char argument to lowercase if
1029 * possible. If not possible (for example, '2'), returns the argument.
1030 *
1031 * do_tolower() is a hook for a derived facet to change the behavior of
1032 * lowercasing. do_tolower() must always return the same result for
1033 * the same input.
1034 *
1035 * @param c The char to convert.
1036 * @return The lowercase char if convertible, else @a c.
1037 */
1038 virtual char_type
1039 do_tolower(char_type) const;
1040
1041 /**
1042 * @brief Convert array to lowercase.
1043 *
1044 * This virtual function converts each char in the range [lo,hi) to
1045 * lowercase if possible. Other chars remain untouched.
1046 *
1047 * do_tolower() is a hook for a derived facet to change the behavior of
1048 * lowercasing. do_tolower() must always return the same result for
1049 * the same input.
1050 *
1051 * @param lo Pointer to first char in range.
1052 * @param hi Pointer to end of range.
1053 * @return @a hi.
1054 */
1055 virtual const char_type*
1056 do_tolower(char_type* __lo, const char_type* __hi) const;
1057
1058 /**
1059 * @brief Widen char
1060 *
1061 * This virtual function converts the char to char using the simplest
1062 * reasonable transformation. For an underived ctype<char> facet, the
1063 * argument will be returned unchanged.
1064 *
1065 * do_widen() is a hook for a derived facet to change the behavior of
1066 * widening. do_widen() must always return the same result for the
1067 * same input.
1068 *
1069 * Note: this is not what you want for codepage conversions. See
1070 * codecvt for that.
1071 *
1072 * @param c The char to convert.
1073 * @return The converted character.
1074 */
1075 virtual char_type
1076 do_widen(char __c) const
1077 { return __c; }
1078
1079 /**
1080 * @brief Widen char array
1081 *
1082 * This function converts each char in the range [lo,hi) to char using
1083 * the simplest reasonable transformation. For an underived
1084 * ctype<char> facet, the argument will be copied unchanged.
1085 *
1086 * do_widen() is a hook for a derived facet to change the behavior of
1087 * widening. do_widen() must always return the same result for the
1088 * same input.
1089 *
1090 * Note: this is not what you want for codepage conversions. See
1091 * codecvt for that.
1092 *
1093 * @param lo Pointer to start of range.
1094 * @param hi Pointer to end of range.
1095 * @param to Pointer to the destination array.
1096 * @return @a hi.
1097 */
1098 virtual const char*
1099 do_widen(const char* __lo, const char* __hi, char_type* __dest) const
1100 {
1101 memcpy(__dest, __lo, __hi - __lo);
1102 return __hi;
1103 }
1104
1105 /**
1106 * @brief Narrow char
1107 *
1108 * This virtual function converts the char to char using the simplest
1109 * reasonable transformation. If the conversion fails, dfault is
1110 * returned instead. For an underived ctype<char> facet, @a c will be
1111 * returned unchanged.
1112 *
1113 * do_narrow() is a hook for a derived facet to change the behavior of
1114 * narrowing. do_narrow() must always return the same result for the
1115 * same input.
1116 *
1117 * Note: this is not what you want for codepage conversions. See
1118 * codecvt for that.
1119 *
1120 * @param c The char to convert.
1121 * @param dfault Char to return if conversion fails.
1122 * @return The converted char.
1123 */
1124 virtual char
1125 do_narrow(char_type __c, char) const
1126 { return __c; }
1127
1128 /**
1129 * @brief Narrow char array to char array
1130 *
1131 * This virtual function converts each char in the range [lo,hi) to
1132 * char using the simplest reasonable transformation and writes the
1133 * results to the destination array. For any char in the input that
1134 * cannot be converted, @a dfault is used instead. For an underived
1135 * ctype<char> facet, the argument will be copied unchanged.
1136 *
1137 * do_narrow() is a hook for a derived facet to change the behavior of
1138 * narrowing. do_narrow() must always return the same result for the
1139 * same input.
1140 *
1141 * Note: this is not what you want for codepage conversions. See
1142 * codecvt for that.
1143 *
1144 * @param lo Pointer to start of range.
1145 * @param hi Pointer to end of range.
1146 * @param dfault Char to use if conversion fails.
1147 * @param to Pointer to the destination array.
1148 * @return @a hi.
1149 */
1150 virtual const char_type*
1151 do_narrow(const char_type* __lo, const char_type* __hi,
1152 char, char* __dest) const
1153 {
1154 memcpy(__dest, __lo, __hi - __lo);
1155 return __hi;
1156 }
1157
1158 private:
1159
1160 void _M_widen_init() const
1161 {
1162 char __tmp[sizeof(_M_widen)];
1163 for (size_t __i = 0; __i < sizeof(_M_widen); ++__i)
1164 __tmp[__i] = __i;
1165 do_widen(__tmp, __tmp + sizeof(__tmp), _M_widen);
1166
1167 _M_widen_ok = 1;
1168 // Set _M_widen_ok to 2 if memcpy can't be used.
1169 if (memcmp(__tmp, _M_widen, sizeof(_M_widen)))
1170 _M_widen_ok = 2;
1171 }
1172
1173 // Fill in the narrowing cache and flag whether all values are
1174 // valid or not. _M_narrow_ok is set to 2 if memcpy can't
1175 // be used.
1176 void _M_narrow_init() const
1177 {
1178 char __tmp[sizeof(_M_narrow)];
1179 for (size_t __i = 0; __i < sizeof(_M_narrow); ++__i)
1180 __tmp[__i] = __i;
1181 do_narrow(__tmp, __tmp + sizeof(__tmp), 0, _M_narrow);
1182
1183 _M_narrow_ok = 1;
1184 if (memcmp(__tmp, _M_narrow, sizeof(_M_narrow)))
1185 _M_narrow_ok = 2;
1186 else
1187 {
1188 // Deal with the special case of zero: renarrow with a
1189 // different default and compare.
1190 char __c;
1191 do_narrow(__tmp, __tmp + 1, 1, &__c);
1192 if (__c == 1)
1193 _M_narrow_ok = 2;
1194 }
1195 }
1196 };
1197
1198 template<>
1199 const ctype<char>&
1200 use_facet<ctype<char> >(const locale& __loc);
1201
1202 #ifdef _GLIBCXX_USE_WCHAR_T
1203 // 22.2.1.3 ctype<wchar_t> specialization
1204 /**
1205 * @brief The ctype<wchar_t> specialization.
1206 *
1207 * This class defines classification and conversion functions for the
1208 * wchar_t type. It gets used by wchar_t streams for many I/O operations.
1209 * The wchar_t specialization provides a number of optimizations as well.
1210 *
1211 * ctype<wchar_t> inherits its public methods from
1212 * __ctype_abstract_base<wchar_t>.
1213 */
1214 template<>
1215 class ctype<wchar_t> : public __ctype_abstract_base<wchar_t>
1216 {
1217 public:
1218 // Types:
1219 /// Typedef for the template parameter wchar_t.
1220 typedef wchar_t char_type;
1221 typedef wctype_t __wmask_type;
1222
1223 protected:
1224 __c_locale _M_c_locale_ctype;
1225
1226 // Pre-computed narrowed and widened chars.
1227 bool _M_narrow_ok;
1228 char _M_narrow[128];
1229 wint_t _M_widen[1 + static_cast<unsigned char>(-1)];
1230
1231 // Pre-computed elements for do_is.
1232 mask _M_bit[16];
1233 __wmask_type _M_wmask[16];
1234
1235 public:
1236 // Data Members:
1237 /// The facet id for ctype<wchar_t>
1238 static locale::id id;
1239
1240 /**
1241 * @brief Constructor performs initialization.
1242 *
1243 * This is the constructor provided by the standard.
1244 *
1245 * @param refs Passed to the base facet class.
1246 */
1247 explicit
1248 ctype(size_t __refs = 0);
1249
1250 /**
1251 * @brief Constructor performs static initialization.
1252 *
1253 * This constructor is used to construct the initial C locale facet.
1254 *
1255 * @param cloc Handle to C locale data.
1256 * @param refs Passed to the base facet class.
1257 */
1258 explicit
1259 ctype(__c_locale __cloc, size_t __refs = 0);
1260
1261 protected:
1262 __wmask_type
1263 _M_convert_to_wmask(const mask __m) const;
1264
1265 /// Destructor
1266 virtual
1267 ~ctype();
1268
1269 /**
1270 * @brief Test wchar_t classification.
1271 *
1272 * This function finds a mask M for @a c and compares it to mask @a m.
1273 *
1274 * do_is() is a hook for a derived facet to change the behavior of
1275 * classifying. do_is() must always return the same result for the
1276 * same input.
1277 *
1278 * @param c The wchar_t to find the mask of.
1279 * @param m The mask to compare against.
1280 * @return (M & m) != 0.
1281 */
1282 virtual bool
1283 do_is(mask __m, char_type __c) const;
1284
1285 /**
1286 * @brief Return a mask array.
1287 *
1288 * This function finds the mask for each wchar_t in the range [lo,hi)
1289 * and successively writes it to vec. vec must have as many elements
1290 * as the input.
1291 *
1292 * do_is() is a hook for a derived facet to change the behavior of
1293 * classifying. do_is() must always return the same result for the
1294 * same input.
1295 *
1296 * @param lo Pointer to start of range.
1297 * @param hi Pointer to end of range.
1298 * @param vec Pointer to an array of mask storage.
1299 * @return @a hi.
1300 */
1301 virtual const char_type*
1302 do_is(const char_type* __lo, const char_type* __hi, mask* __vec) const;
1303
1304 /**
1305 * @brief Find wchar_t matching mask
1306 *
1307 * This function searches for and returns the first wchar_t c in
1308 * [lo,hi) for which is(m,c) is true.
1309 *
1310 * do_scan_is() is a hook for a derived facet to change the behavior of
1311 * match searching. do_is() must always return the same result for the
1312 * same input.
1313 *
1314 * @param m The mask to compare against.
1315 * @param lo Pointer to start of range.
1316 * @param hi Pointer to end of range.
1317 * @return Pointer to a matching wchar_t if found, else @a hi.
1318 */
1319 virtual const char_type*
1320 do_scan_is(mask __m, const char_type* __lo, const char_type* __hi) const;
1321
1322 /**
1323 * @brief Find wchar_t not matching mask
1324 *
1325 * This function searches for and returns a pointer to the first
1326 * wchar_t c of [lo,hi) for which is(m,c) is false.
1327 *
1328 * do_scan_is() is a hook for a derived facet to change the behavior of
1329 * match searching. do_is() must always return the same result for the
1330 * same input.
1331 *
1332 * @param m The mask to compare against.
1333 * @param lo Pointer to start of range.
1334 * @param hi Pointer to end of range.
1335 * @return Pointer to a non-matching wchar_t if found, else @a hi.
1336 */
1337 virtual const char_type*
1338 do_scan_not(mask __m, const char_type* __lo,
1339 const char_type* __hi) const;
1340
1341 /**
1342 * @brief Convert to uppercase.
1343 *
1344 * This virtual function converts the wchar_t argument to uppercase if
1345 * possible. If not possible (for example, '2'), returns the argument.
1346 *
1347 * do_toupper() is a hook for a derived facet to change the behavior of
1348 * uppercasing. do_toupper() must always return the same result for
1349 * the same input.
1350 *
1351 * @param c The wchar_t to convert.
1352 * @return The uppercase wchar_t if convertible, else @a c.
1353 */
1354 virtual char_type
1355 do_toupper(char_type) const;
1356
1357 /**
1358 * @brief Convert array to uppercase.
1359 *
1360 * This virtual function converts each wchar_t in the range [lo,hi) to
1361 * uppercase if possible. Other elements remain untouched.
1362 *
1363 * do_toupper() is a hook for a derived facet to change the behavior of
1364 * uppercasing. do_toupper() must always return the same result for
1365 * the same input.
1366 *
1367 * @param lo Pointer to start of range.
1368 * @param hi Pointer to end of range.
1369 * @return @a hi.
1370 */
1371 virtual const char_type*
1372 do_toupper(char_type* __lo, const char_type* __hi) const;
1373
1374 /**
1375 * @brief Convert to lowercase.
1376 *
1377 * This virtual function converts the argument to lowercase if
1378 * possible. If not possible (for example, '2'), returns the argument.
1379 *
1380 * do_tolower() is a hook for a derived facet to change the behavior of
1381 * lowercasing. do_tolower() must always return the same result for
1382 * the same input.
1383 *
1384 * @param c The wchar_t to convert.
1385 * @return The lowercase wchar_t if convertible, else @a c.
1386 */
1387 virtual char_type
1388 do_tolower(char_type) const;
1389
1390 /**
1391 * @brief Convert array to lowercase.
1392 *
1393 * This virtual function converts each wchar_t in the range [lo,hi) to
1394 * lowercase if possible. Other elements remain untouched.
1395 *
1396 * do_tolower() is a hook for a derived facet to change the behavior of
1397 * lowercasing. do_tolower() must always return the same result for
1398 * the same input.
1399 *
1400 * @param lo Pointer to start of range.
1401 * @param hi Pointer to end of range.
1402 * @return @a hi.
1403 */
1404 virtual const char_type*
1405 do_tolower(char_type* __lo, const char_type* __hi) const;
1406
1407 /**
1408 * @brief Widen char to wchar_t
1409 *
1410 * This virtual function converts the char to wchar_t using the
1411 * simplest reasonable transformation. For an underived ctype<wchar_t>
1412 * facet, the argument will be cast to wchar_t.
1413 *
1414 * do_widen() is a hook for a derived facet to change the behavior of
1415 * widening. do_widen() must always return the same result for the
1416 * same input.
1417 *
1418 * Note: this is not what you want for codepage conversions. See
1419 * codecvt for that.
1420 *
1421 * @param c The char to convert.
1422 * @return The converted wchar_t.
1423 */
1424 virtual char_type
1425 do_widen(char) const;
1426
1427 /**
1428 * @brief Widen char array to wchar_t array
1429 *
1430 * This function converts each char in the input to wchar_t using the
1431 * simplest reasonable transformation. For an underived ctype<wchar_t>
1432 * facet, the argument will be copied, casting each element to wchar_t.
1433 *
1434 * do_widen() is a hook for a derived facet to change the behavior of
1435 * widening. do_widen() must always return the same result for the
1436 * same input.
1437 *
1438 * Note: this is not what you want for codepage conversions. See
1439 * codecvt for that.
1440 *
1441 * @param lo Pointer to start range.
1442 * @param hi Pointer to end of range.
1443 * @param to Pointer to the destination array.
1444 * @return @a hi.
1445 */
1446 virtual const char*
1447 do_widen(const char* __lo, const char* __hi, char_type* __dest) const;
1448
1449 /**
1450 * @brief Narrow wchar_t to char
1451 *
1452 * This virtual function converts the argument to char using
1453 * the simplest reasonable transformation. If the conversion
1454 * fails, dfault is returned instead. For an underived
1455 * ctype<wchar_t> facet, @a c will be cast to char and
1456 * returned.
1457 *
1458 * do_narrow() is a hook for a derived facet to change the
1459 * behavior of narrowing. do_narrow() must always return the
1460 * same result for the same input.
1461 *
1462 * Note: this is not what you want for codepage conversions. See
1463 * codecvt for that.
1464 *
1465 * @param c The wchar_t to convert.
1466 * @param dfault Char to return if conversion fails.
1467 * @return The converted char.
1468 */
1469 virtual char
1470 do_narrow(char_type, char __dfault) const;
1471
1472 /**
1473 * @brief Narrow wchar_t array to char array
1474 *
1475 * This virtual function converts each wchar_t in the range [lo,hi) to
1476 * char using the simplest reasonable transformation and writes the
1477 * results to the destination array. For any wchar_t in the input that
1478 * cannot be converted, @a dfault is used instead. For an underived
1479 * ctype<wchar_t> facet, the argument will be copied, casting each
1480 * element to char.
1481 *
1482 * do_narrow() is a hook for a derived facet to change the behavior of
1483 * narrowing. do_narrow() must always return the same result for the
1484 * same input.
1485 *
1486 * Note: this is not what you want for codepage conversions. See
1487 * codecvt for that.
1488 *
1489 * @param lo Pointer to start of range.
1490 * @param hi Pointer to end of range.
1491 * @param dfault Char to use if conversion fails.
1492 * @param to Pointer to the destination array.
1493 * @return @a hi.
1494 */
1495 virtual const char_type*
1496 do_narrow(const char_type* __lo, const char_type* __hi,
1497 char __dfault, char* __dest) const;
1498
1499 // For use at construction time only.
1500 void
1501 _M_initialize_ctype();
1502 };
1503
1504 template<>
1505 const ctype<wchar_t>&
1506 use_facet<ctype<wchar_t> >(const locale& __loc);
1507 #endif //_GLIBCXX_USE_WCHAR_T
1508
1509 /// @brief class ctype_byname [22.2.1.2].
1510 template<typename _CharT>
1511 class ctype_byname : public ctype<_CharT>
1512 {
1513 public:
1514 typedef typename ctype<_CharT>::mask mask;
1515
1516 explicit
1517 ctype_byname(const char* __s, size_t __refs = 0);
1518
1519 protected:
1520 virtual
1521 ~ctype_byname() { };
1522 };
1523
1524 /// 22.2.1.4 Class ctype_byname specializations.
1525 template<>
1526 class ctype_byname<char> : public ctype<char>
1527 {
1528 public:
1529 explicit
1530 ctype_byname(const char* __s, size_t __refs = 0);
1531
1532 protected:
1533 virtual
1534 ~ctype_byname();
1535 };
1536
1537 #ifdef _GLIBCXX_USE_WCHAR_T
1538 template<>
1539 class ctype_byname<wchar_t> : public ctype<wchar_t>
1540 {
1541 public:
1542 explicit
1543 ctype_byname(const char* __s, size_t __refs = 0);
1544
1545 protected:
1546 virtual
1547 ~ctype_byname();
1548 };
1549 #endif
1550
1551 _GLIBCXX_END_NAMESPACE
1552
1553 // Include host and configuration specific ctype inlines.
1554 #include <bits/ctype_inline.h>
1555
1556 // 22.2.1.5 Template class codecvt
1557 #include <bits/codecvt.h>
1558
1559 _GLIBCXX_BEGIN_NAMESPACE(std)
1560
1561 // 22.2.2 The numeric category.
1562 class __num_base
1563 {
1564 public:
1565 // NB: Code depends on the order of _S_atoms_out elements.
1566 // Below are the indices into _S_atoms_out.
1567 enum
1568 {
1569 _S_ominus,
1570 _S_oplus,
1571 _S_ox,
1572 _S_oX,
1573 _S_odigits,
1574 _S_odigits_end = _S_odigits + 16,
1575 _S_oudigits = _S_odigits_end,
1576 _S_oudigits_end = _S_oudigits + 16,
1577 _S_oe = _S_odigits + 14, // For scientific notation, 'e'
1578 _S_oE = _S_oudigits + 14, // For scientific notation, 'E'
1579 _S_oend = _S_oudigits_end
1580 };
1581
1582 // A list of valid numeric literals for output. This array
1583 // contains chars that will be passed through the current locale's
1584 // ctype<_CharT>.widen() and then used to render numbers.
1585 // For the standard "C" locale, this is
1586 // "-+xX0123456789abcdef0123456789ABCDEF".
1587 static const char* _S_atoms_out;
1588
1589 // String literal of acceptable (narrow) input, for num_get.
1590 // "-+xX0123456789abcdefABCDEF"
1591 static const char* _S_atoms_in;
1592
1593 enum
1594 {
1595 _S_iminus,
1596 _S_iplus,
1597 _S_ix,
1598 _S_iX,
1599 _S_izero,
1600 _S_ie = _S_izero + 14,
1601 _S_iE = _S_izero + 20,
1602 _S_iend = 26
1603 };
1604
1605 // num_put
1606 // Construct and return valid scanf format for floating point types.
1607 static void
1608 _S_format_float(const ios_base& __io, char* __fptr, char __mod);
1609 };
1610
1611 template<typename _CharT>
1612 struct __numpunct_cache : public locale::facet
1613 {
1614 const char* _M_grouping;
1615 size_t _M_grouping_size;
1616 bool _M_use_grouping;
1617 const _CharT* _M_truename;
1618 size_t _M_truename_size;
1619 const _CharT* _M_falsename;
1620 size_t _M_falsename_size;
1621 _CharT _M_decimal_point;
1622 _CharT _M_thousands_sep;
1623
1624 // A list of valid numeric literals for output: in the standard
1625 // "C" locale, this is "-+xX0123456789abcdef0123456789ABCDEF".
1626 // This array contains the chars after having been passed
1627 // through the current locale's ctype<_CharT>.widen().
1628 _CharT _M_atoms_out[__num_base::_S_oend];
1629
1630 // A list of valid numeric literals for input: in the standard
1631 // "C" locale, this is "-+xX0123456789abcdefABCDEF"
1632 // This array contains the chars after having been passed
1633 // through the current locale's ctype<_CharT>.widen().
1634 _CharT _M_atoms_in[__num_base::_S_iend];
1635
1636 bool _M_allocated;
1637
1638 __numpunct_cache(size_t __refs = 0) : facet(__refs),
1639 _M_grouping(NULL), _M_grouping_size(0), _M_use_grouping(false),
1640 _M_truename(NULL), _M_truename_size(0), _M_falsename(NULL),
1641 _M_falsename_size(0), _M_decimal_point(_CharT()),
1642 _M_thousands_sep(_CharT()), _M_allocated(false)
1643 { }
1644
1645 ~__numpunct_cache();
1646
1647 void
1648 _M_cache(const locale& __loc);
1649
1650 private:
1651 __numpunct_cache&
1652 operator=(const __numpunct_cache&);
1653
1654 explicit
1655 __numpunct_cache(const __numpunct_cache&);
1656 };
1657
1658 template<typename _CharT>
1659 __numpunct_cache<_CharT>::~__numpunct_cache()
1660 {
1661 if (_M_allocated)
1662 {
1663 delete [] _M_grouping;
1664 delete [] _M_truename;
1665 delete [] _M_falsename;
1666 }
1667 }
1668
1669 /**
1670 * @brief Numpunct facet.
1671 *
1672 * This facet stores several pieces of information related to printing and
1673 * scanning numbers, such as the decimal point character. It takes a
1674 * template parameter specifying the char type. The numpunct facet is
1675 * used by streams for many I/O operations involving numbers.
1676 *
1677 * The numpunct template uses protected virtual functions to provide the
1678 * actual results. The public accessors forward the call to the virtual
1679 * functions. These virtual functions are hooks for developers to
1680 * implement the behavior they require from a numpunct facet.
1681 */
1682 template<typename _CharT>
1683 class numpunct : public locale::facet
1684 {
1685 public:
1686 // Types:
1687 //@{
1688 /// Public typedefs
1689 typedef _CharT char_type;
1690 typedef basic_string<_CharT> string_type;
1691 //@}
1692 typedef __numpunct_cache<_CharT> __cache_type;
1693
1694 protected:
1695 __cache_type* _M_data;
1696
1697 public:
1698 /// Numpunct facet id.
1699 static locale::id id;
1700
1701 /**
1702 * @brief Numpunct constructor.
1703 *
1704 * @param refs Refcount to pass to the base class.
1705 */
1706 explicit
1707 numpunct(size_t __refs = 0) : facet(__refs), _M_data(NULL)
1708 { _M_initialize_numpunct(); }
1709
1710 /**
1711 * @brief Internal constructor. Not for general use.
1712 *
1713 * This is a constructor for use by the library itself to set up the
1714 * predefined locale facets.
1715 *
1716 * @param cache __numpunct_cache object.
1717 * @param refs Refcount to pass to the base class.
1718 */
1719 explicit
1720 numpunct(__cache_type* __cache, size_t __refs = 0)
1721 : facet(__refs), _M_data(__cache)
1722 { _M_initialize_numpunct(); }
1723
1724 /**
1725 * @brief Internal constructor. Not for general use.
1726 *
1727 * This is a constructor for use by the library itself to set up new
1728 * locales.
1729 *
1730 * @param cloc The "C" locale.
1731 * @param refs Refcount to pass to the base class.
1732 */
1733 explicit
1734 numpunct(__c_locale __cloc, size_t __refs = 0)
1735 : facet(__refs), _M_data(NULL)
1736 { _M_initialize_numpunct(__cloc); }
1737
1738 /**
1739 * @brief Return decimal point character.
1740 *
1741 * This function returns a char_type to use as a decimal point. It
1742 * does so by returning returning
1743 * numpunct<char_type>::do_decimal_point().
1744 *
1745 * @return @a char_type representing a decimal point.
1746 */
1747 char_type
1748 decimal_point() const
1749 { return this->do_decimal_point(); }
1750
1751 /**
1752 * @brief Return thousands separator character.
1753 *
1754 * This function returns a char_type to use as a thousands
1755 * separator. It does so by returning returning
1756 * numpunct<char_type>::do_thousands_sep().
1757 *
1758 * @return char_type representing a thousands separator.
1759 */
1760 char_type
1761 thousands_sep() const
1762 { return this->do_thousands_sep(); }
1763
1764 /**
1765 * @brief Return grouping specification.
1766 *
1767 * This function returns a string representing groupings for the
1768 * integer part of a number. Groupings indicate where thousands
1769 * separators should be inserted in the integer part of a number.
1770 *
1771 * Each char in the return string is interpret as an integer
1772 * rather than a character. These numbers represent the number
1773 * of digits in a group. The first char in the string
1774 * represents the number of digits in the least significant
1775 * group. If a char is negative, it indicates an unlimited
1776 * number of digits for the group. If more chars from the
1777 * string are required to group a number, the last char is used
1778 * repeatedly.
1779 *
1780 * For example, if the grouping() returns "\003\002" and is
1781 * applied to the number 123456789, this corresponds to
1782 * 12,34,56,789. Note that if the string was "32", this would
1783 * put more than 50 digits into the least significant group if
1784 * the character set is ASCII.
1785 *
1786 * The string is returned by calling
1787 * numpunct<char_type>::do_grouping().
1788 *
1789 * @return string representing grouping specification.
1790 */
1791 string
1792 grouping() const
1793 { return this->do_grouping(); }
1794
1795 /**
1796 * @brief Return string representation of bool true.
1797 *
1798 * This function returns a string_type containing the text
1799 * representation for true bool variables. It does so by calling
1800 * numpunct<char_type>::do_truename().
1801 *
1802 * @return string_type representing printed form of true.
1803 */
1804 string_type
1805 truename() const
1806 { return this->do_truename(); }
1807
1808 /**
1809 * @brief Return string representation of bool false.
1810 *
1811 * This function returns a string_type containing the text
1812 * representation for false bool variables. It does so by calling
1813 * numpunct<char_type>::do_falsename().
1814 *
1815 * @return string_type representing printed form of false.
1816 */
1817 string_type
1818 falsename() const
1819 { return this->do_falsename(); }
1820
1821 protected:
1822 /// Destructor.
1823 virtual
1824 ~numpunct();
1825
1826 /**
1827 * @brief Return decimal point character.
1828 *
1829 * Returns a char_type to use as a decimal point. This function is a
1830 * hook for derived classes to change the value returned.
1831 *
1832 * @return @a char_type representing a decimal point.
1833 */
1834 virtual char_type
1835 do_decimal_point() const
1836 { return _M_data->_M_decimal_point; }
1837
1838 /**
1839 * @brief Return thousands separator character.
1840 *
1841 * Returns a char_type to use as a thousands separator. This function
1842 * is a hook for derived classes to change the value returned.
1843 *
1844 * @return @a char_type representing a thousands separator.
1845 */
1846 virtual char_type
1847 do_thousands_sep() const
1848 { return _M_data->_M_thousands_sep; }
1849
1850 /**
1851 * @brief Return grouping specification.
1852 *
1853 * Returns a string representing groupings for the integer part of a
1854 * number. This function is a hook for derived classes to change the
1855 * value returned. @see grouping() for details.
1856 *
1857 * @return String representing grouping specification.
1858 */
1859 virtual string
1860 do_grouping() const
1861 { return _M_data->_M_grouping; }
1862
1863 /**
1864 * @brief Return string representation of bool true.
1865 *
1866 * Returns a string_type containing the text representation for true
1867 * bool variables. This function is a hook for derived classes to
1868 * change the value returned.
1869 *
1870 * @return string_type representing printed form of true.
1871 */
1872 virtual string_type
1873 do_truename() const
1874 { return _M_data->_M_truename; }
1875
1876 /**
1877 * @brief Return string representation of bool false.
1878 *
1879 * Returns a string_type containing the text representation for false
1880 * bool variables. This function is a hook for derived classes to
1881 * change the value returned.
1882 *
1883 * @return string_type representing printed form of false.
1884 */
1885 virtual string_type
1886 do_falsename() const
1887 { return _M_data->_M_falsename; }
1888
1889 // For use at construction time only.
1890 void
1891 _M_initialize_numpunct(__c_locale __cloc = NULL);
1892 };
1893
1894 template<typename _CharT>
1895 locale::id numpunct<_CharT>::id;
1896
1897 template<>
1898 numpunct<char>::~numpunct();
1899
1900 template<>
1901 void
1902 numpunct<char>::_M_initialize_numpunct(__c_locale __cloc);
1903
1904 #ifdef _GLIBCXX_USE_WCHAR_T
1905 template<>
1906 numpunct<wchar_t>::~numpunct();
1907
1908 template<>
1909 void
1910 numpunct<wchar_t>::_M_initialize_numpunct(__c_locale __cloc);
1911 #endif
1912
1913 /// @brief class numpunct_byname [22.2.3.2].
1914 template<typename _CharT>
1915 class numpunct_byname : public numpunct<_CharT>
1916 {
1917 public:
1918 typedef _CharT char_type;
1919 typedef basic_string<_CharT> string_type;
1920
1921 explicit
1922 numpunct_byname(const char* __s, size_t __refs = 0)
1923 : numpunct<_CharT>(__refs)
1924 {
1925 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
1926 {
1927 __c_locale __tmp;
1928 this->_S_create_c_locale(__tmp, __s);
1929 this->_M_initialize_numpunct(__tmp);
1930 this->_S_destroy_c_locale(__tmp);
1931 }
1932 }
1933
1934 protected:
1935 virtual
1936 ~numpunct_byname() { }
1937 };
1938
1939 _GLIBCXX_BEGIN_LDBL_NAMESPACE
1940 /**
1941 * @brief Facet for parsing number strings.
1942 *
1943 * This facet encapsulates the code to parse and return a number
1944 * from a string. It is used by the istream numeric extraction
1945 * operators.
1946 *
1947 * The num_get template uses protected virtual functions to provide the
1948 * actual results. The public accessors forward the call to the virtual
1949 * functions. These virtual functions are hooks for developers to
1950 * implement the behavior they require from the num_get facet.
1951 */
1952 template<typename _CharT, typename _InIter>
1953 class num_get : public locale::facet
1954 {
1955 public:
1956 // Types:
1957 //@{
1958 /// Public typedefs
1959 typedef _CharT char_type;
1960 typedef _InIter iter_type;
1961 //@}
1962
1963 /// Numpunct facet id.
1964 static locale::id id;
1965
1966 /**
1967 * @brief Constructor performs initialization.
1968 *
1969 * This is the constructor provided by the standard.
1970 *
1971 * @param refs Passed to the base facet class.
1972 */
1973 explicit
1974 num_get(size_t __refs = 0) : facet(__refs) { }
1975
1976 /**
1977 * @brief Numeric parsing.
1978 *
1979 * Parses the input stream into the bool @a v. It does so by calling
1980 * num_get::do_get().
1981 *
1982 * If ios_base::boolalpha is set, attempts to read
1983 * ctype<CharT>::truename() or ctype<CharT>::falsename(). Sets
1984 * @a v to true or false if successful. Sets err to
1985 * ios_base::failbit if reading the string fails. Sets err to
1986 * ios_base::eofbit if the stream is emptied.
1987 *
1988 * If ios_base::boolalpha is not set, proceeds as with reading a long,
1989 * except if the value is 1, sets @a v to true, if the value is 0, sets
1990 * @a v to false, and otherwise set err to ios_base::failbit.
1991 *
1992 * @param in Start of input stream.
1993 * @param end End of input stream.
1994 * @param io Source of locale and flags.
1995 * @param err Error flags to set.
1996 * @param v Value to format and insert.
1997 * @return Iterator after reading.
1998 */
1999 iter_type
2000 get(iter_type __in, iter_type __end, ios_base& __io,
2001 ios_base::iostate& __err, bool& __v) const
2002 { return this->do_get(__in, __end, __io, __err, __v); }
2003
2004 //@{
2005 /**
2006 * @brief Numeric parsing.
2007 *
2008 * Parses the input stream into the integral variable @a v. It does so
2009 * by calling num_get::do_get().
2010 *
2011 * Parsing is affected by the flag settings in @a io.
2012 *
2013 * The basic parse is affected by the value of io.flags() &
2014 * ios_base::basefield. If equal to ios_base::oct, parses like the
2015 * scanf %o specifier. Else if equal to ios_base::hex, parses like %X
2016 * specifier. Else if basefield equal to 0, parses like the %i
2017 * specifier. Otherwise, parses like %d for signed and %u for unsigned
2018 * types. The matching type length modifier is also used.
2019 *
2020 * Digit grouping is intrepreted according to numpunct::grouping() and
2021 * numpunct::thousands_sep(). If the pattern of digit groups isn't
2022 * consistent, sets err to ios_base::failbit.
2023 *
2024 * If parsing the string yields a valid value for @a v, @a v is set.
2025 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
2026 * Sets err to ios_base::eofbit if the stream is emptied.
2027 *
2028 * @param in Start of input stream.
2029 * @param end End of input stream.
2030 * @param io Source of locale and flags.
2031 * @param err Error flags to set.
2032 * @param v Value to format and insert.
2033 * @return Iterator after reading.
2034 */
2035 iter_type
2036 get(iter_type __in, iter_type __end, ios_base& __io,
2037 ios_base::iostate& __err, long& __v) const
2038 { return this->do_get(__in, __end, __io, __err, __v); }
2039
2040 iter_type
2041 get(iter_type __in, iter_type __end, ios_base& __io,
2042 ios_base::iostate& __err, unsigned short& __v) const
2043 { return this->do_get(__in, __end, __io, __err, __v); }
2044
2045 iter_type
2046 get(iter_type __in, iter_type __end, ios_base& __io,
2047 ios_base::iostate& __err, unsigned int& __v) const
2048 { return this->do_get(__in, __end, __io, __err, __v); }
2049
2050 iter_type
2051 get(iter_type __in, iter_type __end, ios_base& __io,
2052 ios_base::iostate& __err, unsigned long& __v) const
2053 { return this->do_get(__in, __end, __io, __err, __v); }
2054
2055 #ifdef _GLIBCXX_USE_LONG_LONG
2056 iter_type
2057 get(iter_type __in, iter_type __end, ios_base& __io,
2058 ios_base::iostate& __err, long long& __v) const
2059 { return this->do_get(__in, __end, __io, __err, __v); }
2060
2061 iter_type
2062 get(iter_type __in, iter_type __end, ios_base& __io,
2063 ios_base::iostate& __err, unsigned long long& __v) const
2064 { return this->do_get(__in, __end, __io, __err, __v); }
2065 #endif
2066 //@}
2067
2068 //@{
2069 /**
2070 * @brief Numeric parsing.
2071 *
2072 * Parses the input stream into the integral variable @a v. It does so
2073 * by calling num_get::do_get().
2074 *
2075 * The input characters are parsed like the scanf %g specifier. The
2076 * matching type length modifier is also used.
2077 *
2078 * The decimal point character used is numpunct::decimal_point().
2079 * Digit grouping is intrepreted according to numpunct::grouping() and
2080 * numpunct::thousands_sep(). If the pattern of digit groups isn't
2081 * consistent, sets err to ios_base::failbit.
2082 *
2083 * If parsing the string yields a valid value for @a v, @a v is set.
2084 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
2085 * Sets err to ios_base::eofbit if the stream is emptied.
2086 *
2087 * @param in Start of input stream.
2088 * @param end End of input stream.
2089 * @param io Source of locale and flags.
2090 * @param err Error flags to set.
2091 * @param v Value to format and insert.
2092 * @return Iterator after reading.
2093 */
2094 iter_type
2095 get(iter_type __in, iter_type __end, ios_base& __io,
2096 ios_base::iostate& __err, float& __v) const
2097 { return this->do_get(__in, __end, __io, __err, __v); }
2098
2099 iter_type
2100 get(iter_type __in, iter_type __end, ios_base& __io,
2101 ios_base::iostate& __err, double& __v) const
2102 { return this->do_get(__in, __end, __io, __err, __v); }
2103
2104 iter_type
2105 get(iter_type __in, iter_type __end, ios_base& __io,
2106 ios_base::iostate& __err, long double& __v) const
2107 { return this->do_get(__in, __end, __io, __err, __v); }
2108 //@}
2109
2110 /**
2111 * @brief Numeric parsing.
2112 *
2113 * Parses the input stream into the pointer variable @a v. It does so
2114 * by calling num_get::do_get().
2115 *
2116 * The input characters are parsed like the scanf %p specifier.
2117 *
2118 * Digit grouping is intrepreted according to numpunct::grouping() and
2119 * numpunct::thousands_sep(). If the pattern of digit groups isn't
2120 * consistent, sets err to ios_base::failbit.
2121 *
2122 * Note that the digit grouping effect for pointers is a bit ambiguous
2123 * in the standard and shouldn't be relied on. See DR 344.
2124 *
2125 * If parsing the string yields a valid value for @a v, @a v is set.
2126 * Otherwise, sets err to ios_base::failbit and leaves @a v unaltered.
2127 * Sets err to ios_base::eofbit if the stream is emptied.
2128 *
2129 * @param in Start of input stream.
2130 * @param end End of input stream.
2131 * @param io Source of locale and flags.
2132 * @param err Error flags to set.
2133 * @param v Value to format and insert.
2134 * @return Iterator after reading.
2135 */
2136 iter_type
2137 get(iter_type __in, iter_type __end, ios_base& __io,
2138 ios_base::iostate& __err, void*& __v) const
2139 { return this->do_get(__in, __end, __io, __err, __v); }
2140
2141 protected:
2142 /// Destructor.
2143 virtual ~num_get() { }
2144
2145 iter_type
2146 _M_extract_float(iter_type, iter_type, ios_base&, ios_base::iostate&,
2147 string& __xtrc) const;
2148
2149 template<typename _ValueT>
2150 iter_type
2151 _M_extract_int(iter_type, iter_type, ios_base&, ios_base::iostate&,
2152 _ValueT& __v) const;
2153
2154 template<typename _CharT2>
2155 typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, int>::__type
2156 _M_find(const _CharT2*, size_t __len, _CharT2 __c) const
2157 {
2158 int __ret = -1;
2159 if (__len <= 10)
2160 {
2161 if (__c >= _CharT2('0') && __c < _CharT2(_CharT2('0') + __len))
2162 __ret = __c - _CharT2('0');
2163 }
2164 else
2165 {
2166 if (__c >= _CharT2('0') && __c <= _CharT2('9'))
2167 __ret = __c - _CharT2('0');
2168 else if (__c >= _CharT2('a') && __c <= _CharT2('f'))
2169 __ret = 10 + (__c - _CharT2('a'));
2170 else if (__c >= _CharT2('A') && __c <= _CharT2('F'))
2171 __ret = 10 + (__c - _CharT2('A'));
2172 }
2173 return __ret;
2174 }
2175
2176 template<typename _CharT2>
2177 typename __gnu_cxx::__enable_if<!__is_char<_CharT2>::__value,
2178 int>::__type
2179 _M_find(const _CharT2* __zero, size_t __len, _CharT2 __c) const
2180 {
2181 int __ret = -1;
2182 const char_type* __q = char_traits<_CharT2>::find(__zero, __len, __c);
2183 if (__q)
2184 {
2185 __ret = __q - __zero;
2186 if (__ret > 15)
2187 __ret -= 6;
2188 }
2189 return __ret;
2190 }
2191
2192 //@{
2193 /**
2194 * @brief Numeric parsing.
2195 *
2196 * Parses the input stream into the variable @a v. This function is a
2197 * hook for derived classes to change the value returned. @see get()
2198 * for more details.
2199 *
2200 * @param in Start of input stream.
2201 * @param end End of input stream.
2202 * @param io Source of locale and flags.
2203 * @param err Error flags to set.
2204 * @param v Value to format and insert.
2205 * @return Iterator after reading.
2206 */
2207 virtual iter_type
2208 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, bool&) const;
2209
2210
2211 virtual iter_type
2212 do_get(iter_type, iter_type, ios_base&, ios_base::iostate&, long&) const;
2213
2214 virtual iter_type
2215 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2216 unsigned short&) const;
2217
2218 virtual iter_type
2219 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2220 unsigned int&) const;
2221
2222 virtual iter_type
2223 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2224 unsigned long&) const;
2225
2226 #ifdef _GLIBCXX_USE_LONG_LONG
2227 virtual iter_type
2228 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2229 long long&) const;
2230
2231 virtual iter_type
2232 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2233 unsigned long long&) const;
2234 #endif
2235
2236 virtual iter_type
2237 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2238 float&) const;
2239
2240 virtual iter_type
2241 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2242 double&) const;
2243
2244 // XXX GLIBCXX_ABI Deprecated
2245 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2246 virtual iter_type
2247 __do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2248 double&) const;
2249 #else
2250 virtual iter_type
2251 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2252 long double&) const;
2253 #endif
2254
2255 virtual iter_type
2256 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2257 void*&) const;
2258
2259 // XXX GLIBCXX_ABI Deprecated
2260 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2261 virtual iter_type
2262 do_get(iter_type, iter_type, ios_base&, ios_base::iostate& __err,
2263 long double&) const;
2264 #endif
2265 //@}
2266 };
2267
2268 template<typename _CharT, typename _InIter>
2269 locale::id num_get<_CharT, _InIter>::id;
2270
2271
2272 /**
2273 * @brief Facet for converting numbers to strings.
2274 *
2275 * This facet encapsulates the code to convert a number to a string. It is
2276 * used by the ostream numeric insertion operators.
2277 *
2278 * The num_put template uses protected virtual functions to provide the
2279 * actual results. The public accessors forward the call to the virtual
2280 * functions. These virtual functions are hooks for developers to
2281 * implement the behavior they require from the num_put facet.
2282 */
2283 template<typename _CharT, typename _OutIter>
2284 class num_put : public locale::facet
2285 {
2286 public:
2287 // Types:
2288 //@{
2289 /// Public typedefs
2290 typedef _CharT char_type;
2291 typedef _OutIter iter_type;
2292 //@}
2293
2294 /// Numpunct facet id.
2295 static locale::id id;
2296
2297 /**
2298 * @brief Constructor performs initialization.
2299 *
2300 * This is the constructor provided by the standard.
2301 *
2302 * @param refs Passed to the base facet class.
2303 */
2304 explicit
2305 num_put(size_t __refs = 0) : facet(__refs) { }
2306
2307 /**
2308 * @brief Numeric formatting.
2309 *
2310 * Formats the boolean @a v and inserts it into a stream. It does so
2311 * by calling num_put::do_put().
2312 *
2313 * If ios_base::boolalpha is set, writes ctype<CharT>::truename() or
2314 * ctype<CharT>::falsename(). Otherwise formats @a v as an int.
2315 *
2316 * @param s Stream to write to.
2317 * @param io Source of locale and flags.
2318 * @param fill Char_type to use for filling.
2319 * @param v Value to format and insert.
2320 * @return Iterator after writing.
2321 */
2322 iter_type
2323 put(iter_type __s, ios_base& __f, char_type __fill, bool __v) const
2324 { return this->do_put(__s, __f, __fill, __v); }
2325
2326 //@{
2327 /**
2328 * @brief Numeric formatting.
2329 *
2330 * Formats the integral value @a v and inserts it into a
2331 * stream. It does so by calling num_put::do_put().
2332 *
2333 * Formatting is affected by the flag settings in @a io.
2334 *
2335 * The basic format is affected by the value of io.flags() &
2336 * ios_base::basefield. If equal to ios_base::oct, formats like the
2337 * printf %o specifier. Else if equal to ios_base::hex, formats like
2338 * %x or %X with ios_base::uppercase unset or set respectively.
2339 * Otherwise, formats like %d, %ld, %lld for signed and %u, %lu, %llu
2340 * for unsigned values. Note that if both oct and hex are set, neither
2341 * will take effect.
2342 *
2343 * If ios_base::showpos is set, '+' is output before positive values.
2344 * If ios_base::showbase is set, '0' precedes octal values (except 0)
2345 * and '0[xX]' precedes hex values.
2346 *
2347 * Thousands separators are inserted according to numpunct::grouping()
2348 * and numpunct::thousands_sep(). The decimal point character used is
2349 * numpunct::decimal_point().
2350 *
2351 * If io.width() is non-zero, enough @a fill characters are inserted to
2352 * make the result at least that wide. If
2353 * (io.flags() & ios_base::adjustfield) == ios_base::left, result is
2354 * padded at the end. If ios_base::internal, then padding occurs
2355 * immediately after either a '+' or '-' or after '0x' or '0X'.
2356 * Otherwise, padding occurs at the beginning.
2357 *
2358 * @param s Stream to write to.
2359 * @param io Source of locale and flags.
2360 * @param fill Char_type to use for filling.
2361 * @param v Value to format and insert.
2362 * @return Iterator after writing.
2363 */
2364 iter_type
2365 put(iter_type __s, ios_base& __f, char_type __fill, long __v) const
2366 { return this->do_put(__s, __f, __fill, __v); }
2367
2368 iter_type
2369 put(iter_type __s, ios_base& __f, char_type __fill,
2370 unsigned long __v) const
2371 { return this->do_put(__s, __f, __fill, __v); }
2372
2373 #ifdef _GLIBCXX_USE_LONG_LONG
2374 iter_type
2375 put(iter_type __s, ios_base& __f, char_type __fill, long long __v) const
2376 { return this->do_put(__s, __f, __fill, __v); }
2377
2378 iter_type
2379 put(iter_type __s, ios_base& __f, char_type __fill,
2380 unsigned long long __v) const
2381 { return this->do_put(__s, __f, __fill, __v); }
2382 #endif
2383 //@}
2384
2385 //@{
2386 /**
2387 * @brief Numeric formatting.
2388 *
2389 * Formats the floating point value @a v and inserts it into a stream.
2390 * It does so by calling num_put::do_put().
2391 *
2392 * Formatting is affected by the flag settings in @a io.
2393 *
2394 * The basic format is affected by the value of io.flags() &
2395 * ios_base::floatfield. If equal to ios_base::fixed, formats like the
2396 * printf %f specifier. Else if equal to ios_base::scientific, formats
2397 * like %e or %E with ios_base::uppercase unset or set respectively.
2398 * Otherwise, formats like %g or %G depending on uppercase. Note that
2399 * if both fixed and scientific are set, the effect will also be like
2400 * %g or %G.
2401 *
2402 * The output precision is given by io.precision(). This precision is
2403 * capped at numeric_limits::digits10 + 2 (different for double and
2404 * long double). The default precision is 6.
2405 *
2406 * If ios_base::showpos is set, '+' is output before positive values.
2407 * If ios_base::showpoint is set, a decimal point will always be
2408 * output.
2409 *
2410 * Thousands separators are inserted according to numpunct::grouping()
2411 * and numpunct::thousands_sep(). The decimal point character used is
2412 * numpunct::decimal_point().
2413 *
2414 * If io.width() is non-zero, enough @a fill characters are inserted to
2415 * make the result at least that wide. If
2416 * (io.flags() & ios_base::adjustfield) == ios_base::left, result is
2417 * padded at the end. If ios_base::internal, then padding occurs
2418 * immediately after either a '+' or '-' or after '0x' or '0X'.
2419 * Otherwise, padding occurs at the beginning.
2420 *
2421 * @param s Stream to write to.
2422 * @param io Source of locale and flags.
2423 * @param fill Char_type to use for filling.
2424 * @param v Value to format and insert.
2425 * @return Iterator after writing.
2426 */
2427 iter_type
2428 put(iter_type __s, ios_base& __f, char_type __fill, double __v) const
2429 { return this->do_put(__s, __f, __fill, __v); }
2430
2431 iter_type
2432 put(iter_type __s, ios_base& __f, char_type __fill,
2433 long double __v) const
2434 { return this->do_put(__s, __f, __fill, __v); }
2435 //@}
2436
2437 /**
2438 * @brief Numeric formatting.
2439 *
2440 * Formats the pointer value @a v and inserts it into a stream. It
2441 * does so by calling num_put::do_put().
2442 *
2443 * This function formats @a v as an unsigned long with ios_base::hex
2444 * and ios_base::showbase set.
2445 *
2446 * @param s Stream to write to.
2447 * @param io Source of locale and flags.
2448 * @param fill Char_type to use for filling.
2449 * @param v Value to format and insert.
2450 * @return Iterator after writing.
2451 */
2452 iter_type
2453 put(iter_type __s, ios_base& __f, char_type __fill,
2454 const void* __v) const
2455 { return this->do_put(__s, __f, __fill, __v); }
2456
2457 protected:
2458 template<typename _ValueT>
2459 iter_type
2460 _M_insert_float(iter_type, ios_base& __io, char_type __fill,
2461 char __mod, _ValueT __v) const;
2462
2463 void
2464 _M_group_float(const char* __grouping, size_t __grouping_size,
2465 char_type __sep, const char_type* __p, char_type* __new,
2466 char_type* __cs, int& __len) const;
2467
2468 template<typename _ValueT>
2469 iter_type
2470 _M_insert_int(iter_type, ios_base& __io, char_type __fill,
2471 _ValueT __v) const;
2472
2473 void
2474 _M_group_int(const char* __grouping, size_t __grouping_size,
2475 char_type __sep, ios_base& __io, char_type* __new,
2476 char_type* __cs, int& __len) const;
2477
2478 void
2479 _M_pad(char_type __fill, streamsize __w, ios_base& __io,
2480 char_type* __new, const char_type* __cs, int& __len) const;
2481
2482 /// Destructor.
2483 virtual
2484 ~num_put() { };
2485
2486 //@{
2487 /**
2488 * @brief Numeric formatting.
2489 *
2490 * These functions do the work of formatting numeric values and
2491 * inserting them into a stream. This function is a hook for derived
2492 * classes to change the value returned.
2493 *
2494 * @param s Stream to write to.
2495 * @param io Source of locale and flags.
2496 * @param fill Char_type to use for filling.
2497 * @param v Value to format and insert.
2498 * @return Iterator after writing.
2499 */
2500 virtual iter_type
2501 do_put(iter_type, ios_base&, char_type __fill, bool __v) const;
2502
2503 virtual iter_type
2504 do_put(iter_type, ios_base&, char_type __fill, long __v) const;
2505
2506 virtual iter_type
2507 do_put(iter_type, ios_base&, char_type __fill, unsigned long) const;
2508
2509 #ifdef _GLIBCXX_USE_LONG_LONG
2510 virtual iter_type
2511 do_put(iter_type, ios_base&, char_type __fill, long long __v) const;
2512
2513 virtual iter_type
2514 do_put(iter_type, ios_base&, char_type __fill, unsigned long long) const;
2515 #endif
2516
2517 virtual iter_type
2518 do_put(iter_type, ios_base&, char_type __fill, double __v) const;
2519
2520 // XXX GLIBCXX_ABI Deprecated
2521 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2522 virtual iter_type
2523 __do_put(iter_type, ios_base&, char_type __fill, double __v) const;
2524 #else
2525 virtual iter_type
2526 do_put(iter_type, ios_base&, char_type __fill, long double __v) const;
2527 #endif
2528
2529 virtual iter_type
2530 do_put(iter_type, ios_base&, char_type __fill, const void* __v) const;
2531
2532 // XXX GLIBCXX_ABI Deprecated
2533 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
2534 virtual iter_type
2535 do_put(iter_type, ios_base&, char_type __fill, long double __v) const;
2536 #endif
2537 //@}
2538 };
2539
2540 template <typename _CharT, typename _OutIter>
2541 locale::id num_put<_CharT, _OutIter>::id;
2542
2543 _GLIBCXX_END_LDBL_NAMESPACE
2544
2545 /**
2546 * @brief Facet for localized string comparison.
2547 *
2548 * This facet encapsulates the code to compare strings in a localized
2549 * manner.
2550 *
2551 * The collate template uses protected virtual functions to provide
2552 * the actual results. The public accessors forward the call to
2553 * the virtual functions. These virtual functions are hooks for
2554 * developers to implement the behavior they require from the
2555 * collate facet.
2556 */
2557 template<typename _CharT>
2558 class collate : public locale::facet
2559 {
2560 public:
2561 // Types:
2562 //@{
2563 /// Public typedefs
2564 typedef _CharT char_type;
2565 typedef basic_string<_CharT> string_type;
2566 //@}
2567
2568 protected:
2569 // Underlying "C" library locale information saved from
2570 // initialization, needed by collate_byname as well.
2571 __c_locale _M_c_locale_collate;
2572
2573 public:
2574 /// Numpunct facet id.
2575 static locale::id id;
2576
2577 /**
2578 * @brief Constructor performs initialization.
2579 *
2580 * This is the constructor provided by the standard.
2581 *
2582 * @param refs Passed to the base facet class.
2583 */
2584 explicit
2585 collate(size_t __refs = 0)
2586 : facet(__refs), _M_c_locale_collate(_S_get_c_locale())
2587 { }
2588
2589 /**
2590 * @brief Internal constructor. Not for general use.
2591 *
2592 * This is a constructor for use by the library itself to set up new
2593 * locales.
2594 *
2595 * @param cloc The "C" locale.
2596 * @param refs Passed to the base facet class.
2597 */
2598 explicit
2599 collate(__c_locale __cloc, size_t __refs = 0)
2600 : facet(__refs), _M_c_locale_collate(_S_clone_c_locale(__cloc))
2601 { }
2602
2603 /**
2604 * @brief Compare two strings.
2605 *
2606 * This function compares two strings and returns the result by calling
2607 * collate::do_compare().
2608 *
2609 * @param lo1 Start of string 1.
2610 * @param hi1 End of string 1.
2611 * @param lo2 Start of string 2.
2612 * @param hi2 End of string 2.
2613 * @return 1 if string1 > string2, -1 if string1 < string2, else 0.
2614 */
2615 int
2616 compare(const _CharT* __lo1, const _CharT* __hi1,
2617 const _CharT* __lo2, const _CharT* __hi2) const
2618 { return this->do_compare(__lo1, __hi1, __lo2, __hi2); }
2619
2620 /**
2621 * @brief Transform string to comparable form.
2622 *
2623 * This function is a wrapper for strxfrm functionality. It takes the
2624 * input string and returns a modified string that can be directly
2625 * compared to other transformed strings. In the "C" locale, this
2626 * function just returns a copy of the input string. In some other
2627 * locales, it may replace two chars with one, change a char for
2628 * another, etc. It does so by returning collate::do_transform().
2629 *
2630 * @param lo Start of string.
2631 * @param hi End of string.
2632 * @return Transformed string_type.
2633 */
2634 string_type
2635 transform(const _CharT* __lo, const _CharT* __hi) const
2636 { return this->do_transform(__lo, __hi); }
2637
2638 /**
2639 * @brief Return hash of a string.
2640 *
2641 * This function computes and returns a hash on the input string. It
2642 * does so by returning collate::do_hash().
2643 *
2644 * @param lo Start of string.
2645 * @param hi End of string.
2646 * @return Hash value.
2647 */
2648 long
2649 hash(const _CharT* __lo, const _CharT* __hi) const
2650 { return this->do_hash(__lo, __hi); }
2651
2652 // Used to abstract out _CharT bits in virtual member functions, below.
2653 int
2654 _M_compare(const _CharT*, const _CharT*) const;
2655
2656 size_t
2657 _M_transform(_CharT*, const _CharT*, size_t) const;
2658
2659 protected:
2660 /// Destructor.
2661 virtual
2662 ~collate()
2663 { _S_destroy_c_locale(_M_c_locale_collate); }
2664
2665 /**
2666 * @brief Compare two strings.
2667 *
2668 * This function is a hook for derived classes to change the value
2669 * returned. @see compare().
2670 *
2671 * @param lo1 Start of string 1.
2672 * @param hi1 End of string 1.
2673 * @param lo2 Start of string 2.
2674 * @param hi2 End of string 2.
2675 * @return 1 if string1 > string2, -1 if string1 < string2, else 0.
2676 */
2677 virtual int
2678 do_compare(const _CharT* __lo1, const _CharT* __hi1,
2679 const _CharT* __lo2, const _CharT* __hi2) const;
2680
2681 /**
2682 * @brief Transform string to comparable form.
2683 *
2684 * This function is a hook for derived classes to change the value
2685 * returned.
2686 *
2687 * @param lo1 Start of string 1.
2688 * @param hi1 End of string 1.
2689 * @param lo2 Start of string 2.
2690 * @param hi2 End of string 2.
2691 * @return 1 if string1 > string2, -1 if string1 < string2, else 0.
2692 */
2693 virtual string_type
2694 do_transform(const _CharT* __lo, const _CharT* __hi) const;
2695
2696 /**
2697 * @brief Return hash of a string.
2698 *
2699 * This function computes and returns a hash on the input string. This
2700 * function is a hook for derived classes to change the value returned.
2701 *
2702 * @param lo Start of string.
2703 * @param hi End of string.
2704 * @return Hash value.
2705 */
2706 virtual long
2707 do_hash(const _CharT* __lo, const _CharT* __hi) const;
2708 };
2709
2710 template<typename _CharT>
2711 locale::id collate<_CharT>::id;
2712
2713 // Specializations.
2714 template<>
2715 int
2716 collate<char>::_M_compare(const char*, const char*) const;
2717
2718 template<>
2719 size_t
2720 collate<char>::_M_transform(char*, const char*, size_t) const;
2721
2722 #ifdef _GLIBCXX_USE_WCHAR_T
2723 template<>
2724 int
2725 collate<wchar_t>::_M_compare(const wchar_t*, const wchar_t*) const;
2726
2727 template<>
2728 size_t
2729 collate<wchar_t>::_M_transform(wchar_t*, const wchar_t*, size_t) const;
2730 #endif
2731
2732 /// @brief class collate_byname [22.2.4.2].
2733 template<typename _CharT>
2734 class collate_byname : public collate<_CharT>
2735 {
2736 public:
2737 //@{
2738 /// Public typedefs
2739 typedef _CharT char_type;
2740 typedef basic_string<_CharT> string_type;
2741 //@}
2742
2743 explicit
2744 collate_byname(const char* __s, size_t __refs = 0)
2745 : collate<_CharT>(__refs)
2746 {
2747 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
2748 {
2749 this->_S_destroy_c_locale(this->_M_c_locale_collate);
2750 this->_S_create_c_locale(this->_M_c_locale_collate, __s);
2751 }
2752 }
2753
2754 protected:
2755 virtual
2756 ~collate_byname() { }
2757 };
2758
2759
2760 /**
2761 * @brief Time format ordering data.
2762 *
2763 * This class provides an enum representing different orderings of day,
2764 * month, and year.
2765 */
2766 class time_base
2767 {
2768 public:
2769 enum dateorder { no_order, dmy, mdy, ymd, ydm };
2770 };
2771
2772 template<typename _CharT>
2773 struct __timepunct_cache : public locale::facet
2774 {
2775 // List of all known timezones, with GMT first.
2776 static const _CharT* _S_timezones[14];
2777
2778 const _CharT* _M_date_format;
2779 const _CharT* _M_date_era_format;
2780 const _CharT* _M_time_format;
2781 const _CharT* _M_time_era_format;
2782 const _CharT* _M_date_time_format;
2783 const _CharT* _M_date_time_era_format;
2784 const _CharT* _M_am;
2785 const _CharT* _M_pm;
2786 const _CharT* _M_am_pm_format;
2787
2788 // Day names, starting with "C"'s Sunday.
2789 const _CharT* _M_day1;
2790 const _CharT* _M_day2;
2791 const _CharT* _M_day3;
2792 const _CharT* _M_day4;
2793 const _CharT* _M_day5;
2794 const _CharT* _M_day6;
2795 const _CharT* _M_day7;
2796
2797 // Abbreviated day names, starting with "C"'s Sun.
2798 const _CharT* _M_aday1;
2799 const _CharT* _M_aday2;
2800 const _CharT* _M_aday3;
2801 const _CharT* _M_aday4;
2802 const _CharT* _M_aday5;
2803 const _CharT* _M_aday6;
2804 const _CharT* _M_aday7;
2805
2806 // Month names, starting with "C"'s January.
2807 const _CharT* _M_month01;
2808 const _CharT* _M_month02;
2809 const _CharT* _M_month03;
2810 const _CharT* _M_month04;
2811 const _CharT* _M_month05;
2812 const _CharT* _M_month06;
2813 const _CharT* _M_month07;
2814 const _CharT* _M_month08;
2815 const _CharT* _M_month09;
2816 const _CharT* _M_month10;
2817 const _CharT* _M_month11;
2818 const _CharT* _M_month12;
2819
2820 // Abbreviated month names, starting with "C"'s Jan.
2821 const _CharT* _M_amonth01;
2822 const _CharT* _M_amonth02;
2823 const _CharT* _M_amonth03;
2824 const _CharT* _M_amonth04;
2825 const _CharT* _M_amonth05;
2826 const _CharT* _M_amonth06;
2827 const _CharT* _M_amonth07;
2828 const _CharT* _M_amonth08;
2829 const _CharT* _M_amonth09;
2830 const _CharT* _M_amonth10;
2831 const _CharT* _M_amonth11;
2832 const _CharT* _M_amonth12;
2833
2834 bool _M_allocated;
2835
2836 __timepunct_cache(size_t __refs = 0) : facet(__refs),
2837 _M_date_format(NULL), _M_date_era_format(NULL), _M_time_format(NULL),
2838 _M_time_era_format(NULL), _M_date_time_format(NULL),
2839 _M_date_time_era_format(NULL), _M_am(NULL), _M_pm(NULL),
2840 _M_am_pm_format(NULL), _M_day1(NULL), _M_day2(NULL), _M_day3(NULL),
2841 _M_day4(NULL), _M_day5(NULL), _M_day6(NULL), _M_day7(NULL),
2842 _M_aday1(NULL), _M_aday2(NULL), _M_aday3(NULL), _M_aday4(NULL),
2843 _M_aday5(NULL), _M_aday6(NULL), _M_aday7(NULL), _M_month01(NULL),
2844 _M_month02(NULL), _M_month03(NULL), _M_month04(NULL), _M_month05(NULL),
2845 _M_month06(NULL), _M_month07(NULL), _M_month08(NULL), _M_month09(NULL),
2846 _M_month10(NULL), _M_month11(NULL), _M_month12(NULL), _M_amonth01(NULL),
2847 _M_amonth02(NULL), _M_amonth03(NULL), _M_amonth04(NULL),
2848 _M_amonth05(NULL), _M_amonth06(NULL), _M_amonth07(NULL),
2849 _M_amonth08(NULL), _M_amonth09(NULL), _M_amonth10(NULL),
2850 _M_amonth11(NULL), _M_amonth12(NULL), _M_allocated(false)
2851 { }
2852
2853 ~__timepunct_cache();
2854
2855 void
2856 _M_cache(const locale& __loc);
2857
2858 private:
2859 __timepunct_cache&
2860 operator=(const __timepunct_cache&);
2861
2862 explicit
2863 __timepunct_cache(const __timepunct_cache&);
2864 };
2865
2866 template<typename _CharT>
2867 __timepunct_cache<_CharT>::~__timepunct_cache()
2868 {
2869 if (_M_allocated)
2870 {
2871 // Unused.
2872 }
2873 }
2874
2875 // Specializations.
2876 template<>
2877 const char*
2878 __timepunct_cache<char>::_S_timezones[14];
2879
2880 #ifdef _GLIBCXX_USE_WCHAR_T
2881 template<>
2882 const wchar_t*
2883 __timepunct_cache<wchar_t>::_S_timezones[14];
2884 #endif
2885
2886 // Generic.
2887 template<typename _CharT>
2888 const _CharT* __timepunct_cache<_CharT>::_S_timezones[14];
2889
2890 template<typename _CharT>
2891 class __timepunct : public locale::facet
2892 {
2893 public:
2894 // Types:
2895 typedef _CharT __char_type;
2896 typedef basic_string<_CharT> __string_type;
2897 typedef __timepunct_cache<_CharT> __cache_type;
2898
2899 protected:
2900 __cache_type* _M_data;
2901 __c_locale _M_c_locale_timepunct;
2902 const char* _M_name_timepunct;
2903
2904 public:
2905 /// Numpunct facet id.
2906 static locale::id id;
2907
2908 explicit
2909 __timepunct(size_t __refs = 0);
2910
2911 explicit
2912 __timepunct(__cache_type* __cache, size_t __refs = 0);
2913
2914 /**
2915 * @brief Internal constructor. Not for general use.
2916 *
2917 * This is a constructor for use by the library itself to set up new
2918 * locales.
2919 *
2920 * @param cloc The "C" locale.
2921 * @param s The name of a locale.
2922 * @param refs Passed to the base facet class.
2923 */
2924 explicit
2925 __timepunct(__c_locale __cloc, const char* __s, size_t __refs = 0);
2926
2927 // FIXME: for error checking purposes _M_put should return the return
2928 // value of strftime/wcsftime.
2929 void
2930 _M_put(_CharT* __s, size_t __maxlen, const _CharT* __format,
2931 const tm* __tm) const;
2932
2933 void
2934 _M_date_formats(const _CharT** __date) const
2935 {
2936 // Always have default first.
2937 __date[0] = _M_data->_M_date_format;
2938 __date[1] = _M_data->_M_date_era_format;
2939 }
2940
2941 void
2942 _M_time_formats(const _CharT** __time) const
2943 {
2944 // Always have default first.
2945 __time[0] = _M_data->_M_time_format;
2946 __time[1] = _M_data->_M_time_era_format;
2947 }
2948
2949 void
2950 _M_date_time_formats(const _CharT** __dt) const
2951 {
2952 // Always have default first.
2953 __dt[0] = _M_data->_M_date_time_format;
2954 __dt[1] = _M_data->_M_date_time_era_format;
2955 }
2956
2957 void
2958 _M_am_pm_format(const _CharT* __ampm) const
2959 { __ampm = _M_data->_M_am_pm_format; }
2960
2961 void
2962 _M_am_pm(const _CharT** __ampm) const
2963 {
2964 __ampm[0] = _M_data->_M_am;
2965 __ampm[1] = _M_data->_M_pm;
2966 }
2967
2968 void
2969 _M_days(const _CharT** __days) const
2970 {
2971 __days[0] = _M_data->_M_day1;
2972 __days[1] = _M_data->_M_day2;
2973 __days[2] = _M_data->_M_day3;
2974 __days[3] = _M_data->_M_day4;
2975 __days[4] = _M_data->_M_day5;
2976 __days[5] = _M_data->_M_day6;
2977 __days[6] = _M_data->_M_day7;
2978 }
2979
2980 void
2981 _M_days_abbreviated(const _CharT** __days) const
2982 {
2983 __days[0] = _M_data->_M_aday1;
2984 __days[1] = _M_data->_M_aday2;
2985 __days[2] = _M_data->_M_aday3;
2986 __days[3] = _M_data->_M_aday4;
2987 __days[4] = _M_data->_M_aday5;
2988 __days[5] = _M_data->_M_aday6;
2989 __days[6] = _M_data->_M_aday7;
2990 }
2991
2992 void
2993 _M_months(const _CharT** __months) const
2994 {
2995 __months[0] = _M_data->_M_month01;
2996 __months[1] = _M_data->_M_month02;
2997 __months[2] = _M_data->_M_month03;
2998 __months[3] = _M_data->_M_month04;
2999 __months[4] = _M_data->_M_month05;
3000 __months[5] = _M_data->_M_month06;
3001 __months[6] = _M_data->_M_month07;
3002 __months[7] = _M_data->_M_month08;
3003 __months[8] = _M_data->_M_month09;
3004 __months[9] = _M_data->_M_month10;
3005 __months[10] = _M_data->_M_month11;
3006 __months[11] = _M_data->_M_month12;
3007 }
3008
3009 void
3010 _M_months_abbreviated(const _CharT** __months) const
3011 {
3012 __months[0] = _M_data->_M_amonth01;
3013 __months[1] = _M_data->_M_amonth02;
3014 __months[2] = _M_data->_M_amonth03;
3015 __months[3] = _M_data->_M_amonth04;
3016 __months[4] = _M_data->_M_amonth05;
3017 __months[5] = _M_data->_M_amonth06;
3018 __months[6] = _M_data->_M_amonth07;
3019 __months[7] = _M_data->_M_amonth08;
3020 __months[8] = _M_data->_M_amonth09;
3021 __months[9] = _M_data->_M_amonth10;
3022 __months[10] = _M_data->_M_amonth11;
3023 __months[11] = _M_data->_M_amonth12;
3024 }
3025
3026 protected:
3027 virtual
3028 ~__timepunct();
3029
3030 // For use at construction time only.
3031 void
3032 _M_initialize_timepunct(__c_locale __cloc = NULL);
3033 };
3034
3035 template<typename _CharT>
3036 locale::id __timepunct<_CharT>::id;
3037
3038 // Specializations.
3039 template<>
3040 void
3041 __timepunct<char>::_M_initialize_timepunct(__c_locale __cloc);
3042
3043 template<>
3044 void
3045 __timepunct<char>::_M_put(char*, size_t, const char*, const tm*) const;
3046
3047 #ifdef _GLIBCXX_USE_WCHAR_T
3048 template<>
3049 void
3050 __timepunct<wchar_t>::_M_initialize_timepunct(__c_locale __cloc);
3051
3052 template<>
3053 void
3054 __timepunct<wchar_t>::_M_put(wchar_t*, size_t, const wchar_t*,
3055 const tm*) const;
3056 #endif
3057
3058 _GLIBCXX_END_NAMESPACE
3059
3060 // Include host and configuration specific timepunct functions.
3061 #include <bits/time_members.h>
3062
3063 _GLIBCXX_BEGIN_NAMESPACE(std)
3064
3065 /**
3066 * @brief Facet for parsing dates and times.
3067 *
3068 * This facet encapsulates the code to parse and return a date or
3069 * time from a string. It is used by the istream numeric
3070 * extraction operators.
3071 *
3072 * The time_get template uses protected virtual functions to provide the
3073 * actual results. The public accessors forward the call to the virtual
3074 * functions. These virtual functions are hooks for developers to
3075 * implement the behavior they require from the time_get facet.
3076 */
3077 template<typename _CharT, typename _InIter>
3078 class time_get : public locale::facet, public time_base
3079 {
3080 public:
3081 // Types:
3082 //@{
3083 /// Public typedefs
3084 typedef _CharT char_type;
3085 typedef _InIter iter_type;
3086 //@}
3087 typedef basic_string<_CharT> __string_type;
3088
3089 /// Numpunct facet id.
3090 static locale::id id;
3091
3092 /**
3093 * @brief Constructor performs initialization.
3094 *
3095 * This is the constructor provided by the standard.
3096 *
3097 * @param refs Passed to the base facet class.
3098 */
3099 explicit
3100 time_get(size_t __refs = 0)
3101 : facet (__refs) { }
3102
3103 /**
3104 * @brief Return preferred order of month, day, and year.
3105 *
3106 * This function returns an enum from timebase::dateorder giving the
3107 * preferred ordering if the format "x" given to time_put::put() only
3108 * uses month, day, and year. If the format "x" for the associated
3109 * locale uses other fields, this function returns
3110 * timebase::dateorder::noorder.
3111 *
3112 * NOTE: The library always returns noorder at the moment.
3113 *
3114 * @return A member of timebase::dateorder.
3115 */
3116 dateorder
3117 date_order() const
3118 { return this->do_date_order(); }
3119
3120 /**
3121 * @brief Parse input time string.
3122 *
3123 * This function parses a time according to the format "x" and puts the
3124 * results into a user-supplied struct tm. The result is returned by
3125 * calling time_get::do_get_time().
3126 *
3127 * If there is a valid time string according to format "x", @a tm will
3128 * be filled in accordingly and the returned iterator will point to the
3129 * first character beyond the time string. If an error occurs before
3130 * the end, err |= ios_base::failbit. If parsing reads all the
3131 * characters, err |= ios_base::eofbit.
3132 *
3133 * @param beg Start of string to parse.
3134 * @param end End of string to parse.
3135 * @param io Source of the locale.
3136 * @param err Error flags to set.
3137 * @param tm Pointer to struct tm to fill in.
3138 * @return Iterator to first char beyond time string.
3139 */
3140 iter_type
3141 get_time(iter_type __beg, iter_type __end, ios_base& __io,
3142 ios_base::iostate& __err, tm* __tm) const
3143 { return this->do_get_time(__beg, __end, __io, __err, __tm); }
3144
3145 /**
3146 * @brief Parse input date string.
3147 *
3148 * This function parses a date according to the format "X" and puts the
3149 * results into a user-supplied struct tm. The result is returned by
3150 * calling time_get::do_get_date().
3151 *
3152 * If there is a valid date string according to format "X", @a tm will
3153 * be filled in accordingly and the returned iterator will point to the
3154 * first character beyond the date string. If an error occurs before
3155 * the end, err |= ios_base::failbit. If parsing reads all the
3156 * characters, err |= ios_base::eofbit.
3157 *
3158 * @param beg Start of string to parse.
3159 * @param end End of string to parse.
3160 * @param io Source of the locale.
3161 * @param err Error flags to set.
3162 * @param tm Pointer to struct tm to fill in.
3163 * @return Iterator to first char beyond date string.
3164 */
3165 iter_type
3166 get_date(iter_type __beg, iter_type __end, ios_base& __io,
3167 ios_base::iostate& __err, tm* __tm) const
3168 { return this->do_get_date(__beg, __end, __io, __err, __tm); }
3169
3170 /**
3171 * @brief Parse input weekday string.
3172 *
3173 * This function parses a weekday name and puts the results into a
3174 * user-supplied struct tm. The result is returned by calling
3175 * time_get::do_get_weekday().
3176 *
3177 * Parsing starts by parsing an abbreviated weekday name. If a valid
3178 * abbreviation is followed by a character that would lead to the full
3179 * weekday name, parsing continues until the full name is found or an
3180 * error occurs. Otherwise parsing finishes at the end of the
3181 * abbreviated name.
3182 *
3183 * If an error occurs before the end, err |= ios_base::failbit. If
3184 * parsing reads all the characters, err |= ios_base::eofbit.
3185 *
3186 * @param beg Start of string to parse.
3187 * @param end End of string to parse.
3188 * @param io Source of the locale.
3189 * @param err Error flags to set.
3190 * @param tm Pointer to struct tm to fill in.
3191 * @return Iterator to first char beyond weekday name.
3192 */
3193 iter_type
3194 get_weekday(iter_type __beg, iter_type __end, ios_base& __io,
3195 ios_base::iostate& __err, tm* __tm) const
3196 { return this->do_get_weekday(__beg, __end, __io, __err, __tm); }
3197
3198 /**
3199 * @brief Parse input month string.
3200 *
3201 * This function parses a month name and puts the results into a
3202 * user-supplied struct tm. The result is returned by calling
3203 * time_get::do_get_monthname().
3204 *
3205 * Parsing starts by parsing an abbreviated month name. If a valid
3206 * abbreviation is followed by a character that would lead to the full
3207 * month name, parsing continues until the full name is found or an
3208 * error occurs. Otherwise parsing finishes at the end of the
3209 * abbreviated name.
3210 *
3211 * If an error occurs before the end, err |= ios_base::failbit. If
3212 * parsing reads all the characters, err |=
3213 * ios_base::eofbit.
3214 *
3215 * @param beg Start of string to parse.
3216 * @param end End of string to parse.
3217 * @param io Source of the locale.
3218 * @param err Error flags to set.
3219 * @param tm Pointer to struct tm to fill in.
3220 * @return Iterator to first char beyond month name.
3221 */
3222 iter_type
3223 get_monthname(iter_type __beg, iter_type __end, ios_base& __io,
3224 ios_base::iostate& __err, tm* __tm) const
3225 { return this->do_get_monthname(__beg, __end, __io, __err, __tm); }
3226
3227 /**
3228 * @brief Parse input year string.
3229 *
3230 * This function reads up to 4 characters to parse a year string and
3231 * puts the results into a user-supplied struct tm. The result is
3232 * returned by calling time_get::do_get_year().
3233 *
3234 * 4 consecutive digits are interpreted as a full year. If there are
3235 * exactly 2 consecutive digits, the library interprets this as the
3236 * number of years since 1900.
3237 *
3238 * If an error occurs before the end, err |= ios_base::failbit. If
3239 * parsing reads all the characters, err |= ios_base::eofbit.
3240 *
3241 * @param beg Start of string to parse.
3242 * @param end End of string to parse.
3243 * @param io Source of the locale.
3244 * @param err Error flags to set.
3245 * @param tm Pointer to struct tm to fill in.
3246 * @return Iterator to first char beyond year.
3247 */
3248 iter_type
3249 get_year(iter_type __beg, iter_type __end, ios_base& __io,
3250 ios_base::iostate& __err, tm* __tm) const
3251 { return this->do_get_year(__beg, __end, __io, __err, __tm); }
3252
3253 protected:
3254 /// Destructor.
3255 virtual
3256 ~time_get() { }
3257
3258 /**
3259 * @brief Return preferred order of month, day, and year.
3260 *
3261 * This function returns an enum from timebase::dateorder giving the
3262 * preferred ordering if the format "x" given to time_put::put() only
3263 * uses month, day, and year. This function is a hook for derived
3264 * classes to change the value returned.
3265 *
3266 * @return A member of timebase::dateorder.
3267 */
3268 virtual dateorder
3269 do_date_order() const;
3270
3271 /**
3272 * @brief Parse input time string.
3273 *
3274 * This function parses a time according to the format "x" and puts the
3275 * results into a user-supplied struct tm. This function is a hook for
3276 * derived classes to change the value returned. @see get_time() for
3277 * details.
3278 *
3279 * @param beg Start of string to parse.
3280 * @param end End of string to parse.
3281 * @param io Source of the locale.
3282 * @param err Error flags to set.
3283 * @param tm Pointer to struct tm to fill in.
3284 * @return Iterator to first char beyond time string.
3285 */
3286 virtual iter_type
3287 do_get_time(iter_type __beg, iter_type __end, ios_base& __io,
3288 ios_base::iostate& __err, tm* __tm) const;
3289
3290 /**
3291 * @brief Parse input date string.
3292 *
3293 * This function parses a date according to the format "X" and puts the
3294 * results into a user-supplied struct tm. This function is a hook for
3295 * derived classes to change the value returned. @see get_date() for
3296 * details.
3297 *
3298 * @param beg Start of string to parse.
3299 * @param end End of string to parse.
3300 * @param io Source of the locale.
3301 * @param err Error flags to set.
3302 * @param tm Pointer to struct tm to fill in.
3303 * @return Iterator to first char beyond date string.
3304 */
3305 virtual iter_type
3306 do_get_date(iter_type __beg, iter_type __end, ios_base& __io,
3307 ios_base::iostate& __err, tm* __tm) const;
3308
3309 /**
3310 * @brief Parse input weekday string.
3311 *
3312 * This function parses a weekday name and puts the results into a
3313 * user-supplied struct tm. This function is a hook for derived
3314 * classes to change the value returned. @see get_weekday() for
3315 * details.
3316 *
3317 * @param beg Start of string to parse.
3318 * @param end End of string to parse.
3319 * @param io Source of the locale.
3320 * @param err Error flags to set.
3321 * @param tm Pointer to struct tm to fill in.
3322 * @return Iterator to first char beyond weekday name.
3323 */
3324 virtual iter_type
3325 do_get_weekday(iter_type __beg, iter_type __end, ios_base&,
3326 ios_base::iostate& __err, tm* __tm) const;
3327
3328 /**
3329 * @brief Parse input month string.
3330 *
3331 * This function parses a month name and puts the results into a
3332 * user-supplied struct tm. This function is a hook for derived
3333 * classes to change the value returned. @see get_monthname() for
3334 * details.
3335 *
3336 * @param beg Start of string to parse.
3337 * @param end End of string to parse.
3338 * @param io Source of the locale.
3339 * @param err Error flags to set.
3340 * @param tm Pointer to struct tm to fill in.
3341 * @return Iterator to first char beyond month name.
3342 */
3343 virtual iter_type
3344 do_get_monthname(iter_type __beg, iter_type __end, ios_base&,
3345 ios_base::iostate& __err, tm* __tm) const;
3346
3347 /**
3348 * @brief Parse input year string.
3349 *
3350 * This function reads up to 4 characters to parse a year string and
3351 * puts the results into a user-supplied struct tm. This function is a
3352 * hook for derived classes to change the value returned. @see
3353 * get_year() for details.
3354 *
3355 * @param beg Start of string to parse.
3356 * @param end End of string to parse.
3357 * @param io Source of the locale.
3358 * @param err Error flags to set.
3359 * @param tm Pointer to struct tm to fill in.
3360 * @return Iterator to first char beyond year.
3361 */
3362 virtual iter_type
3363 do_get_year(iter_type __beg, iter_type __end, ios_base& __io,
3364 ios_base::iostate& __err, tm* __tm) const;
3365
3366 // Extract numeric component of length __len.
3367 iter_type
3368 _M_extract_num(iter_type __beg, iter_type __end, int& __member,
3369 int __min, int __max, size_t __len,
3370 ios_base& __io, ios_base::iostate& __err) const;
3371
3372 // Extract day or month name, or any unique array of string
3373 // literals in a const _CharT* array.
3374 iter_type
3375 _M_extract_name(iter_type __beg, iter_type __end, int& __member,
3376 const _CharT** __names, size_t __indexlen,
3377 ios_base& __io, ios_base::iostate& __err) const;
3378
3379 // Extract on a component-by-component basis, via __format argument.
3380 iter_type
3381 _M_extract_via_format(iter_type __beg, iter_type __end, ios_base& __io,
3382 ios_base::iostate& __err, tm* __tm,
3383 const _CharT* __format) const;
3384 };
3385
3386 template<typename _CharT, typename _InIter>
3387 locale::id time_get<_CharT, _InIter>::id;
3388
3389 /// @brief class time_get_byname [22.2.5.2].
3390 template<typename _CharT, typename _InIter>
3391 class time_get_byname : public time_get<_CharT, _InIter>
3392 {
3393 public:
3394 // Types:
3395 typedef _CharT char_type;
3396 typedef _InIter iter_type;
3397
3398 explicit
3399 time_get_byname(const char*, size_t __refs = 0)
3400 : time_get<_CharT, _InIter>(__refs) { }
3401
3402 protected:
3403 virtual
3404 ~time_get_byname() { }
3405 };
3406
3407 /**
3408 * @brief Facet for outputting dates and times.
3409 *
3410 * This facet encapsulates the code to format and output dates and times
3411 * according to formats used by strftime().
3412 *
3413 * The time_put template uses protected virtual functions to provide the
3414 * actual results. The public accessors forward the call to the virtual
3415 * functions. These virtual functions are hooks for developers to
3416 * implement the behavior they require from the time_put facet.
3417 */
3418 template<typename _CharT, typename _OutIter>
3419 class time_put : public locale::facet
3420 {
3421 public:
3422 // Types:
3423 //@{
3424 /// Public typedefs
3425 typedef _CharT char_type;
3426 typedef _OutIter iter_type;
3427 //@}
3428
3429 /// Numpunct facet id.
3430 static locale::id id;
3431
3432 /**
3433 * @brief Constructor performs initialization.
3434 *
3435 * This is the constructor provided by the standard.
3436 *
3437 * @param refs Passed to the base facet class.
3438 */
3439 explicit
3440 time_put(size_t __refs = 0)
3441 : facet(__refs) { }
3442
3443 /**
3444 * @brief Format and output a time or date.
3445 *
3446 * This function formats the data in struct tm according to the
3447 * provided format string. The format string is interpreted as by
3448 * strftime().
3449 *
3450 * @param s The stream to write to.
3451 * @param io Source of locale.
3452 * @param fill char_type to use for padding.
3453 * @param tm Struct tm with date and time info to format.
3454 * @param beg Start of format string.
3455 * @param end End of format string.
3456 * @return Iterator after writing.
3457 */
3458 iter_type
3459 put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm,
3460 const _CharT* __beg, const _CharT* __end) const;
3461
3462 /**
3463 * @brief Format and output a time or date.
3464 *
3465 * This function formats the data in struct tm according to the
3466 * provided format char and optional modifier. The format and modifier
3467 * are interpreted as by strftime(). It does so by returning
3468 * time_put::do_put().
3469 *
3470 * @param s The stream to write to.
3471 * @param io Source of locale.
3472 * @param fill char_type to use for padding.
3473 * @param tm Struct tm with date and time info to format.
3474 * @param format Format char.
3475 * @param mod Optional modifier char.
3476 * @return Iterator after writing.
3477 */
3478 iter_type
3479 put(iter_type __s, ios_base& __io, char_type __fill,
3480 const tm* __tm, char __format, char __mod = 0) const
3481 { return this->do_put(__s, __io, __fill, __tm, __format, __mod); }
3482
3483 protected:
3484 /// Destructor.
3485 virtual
3486 ~time_put()
3487 { }
3488
3489 /**
3490 * @brief Format and output a time or date.
3491 *
3492 * This function formats the data in struct tm according to the
3493 * provided format char and optional modifier. This function is a hook
3494 * for derived classes to change the value returned. @see put() for
3495 * more details.
3496 *
3497 * @param s The stream to write to.
3498 * @param io Source of locale.
3499 * @param fill char_type to use for padding.
3500 * @param tm Struct tm with date and time info to format.
3501 * @param format Format char.
3502 * @param mod Optional modifier char.
3503 * @return Iterator after writing.
3504 */
3505 virtual iter_type
3506 do_put(iter_type __s, ios_base& __io, char_type __fill, const tm* __tm,
3507 char __format, char __mod) const;
3508 };
3509
3510 template<typename _CharT, typename _OutIter>
3511 locale::id time_put<_CharT, _OutIter>::id;
3512
3513 /// @brief class time_put_byname [22.2.5.4].
3514 template<typename _CharT, typename _OutIter>
3515 class time_put_byname : public time_put<_CharT, _OutIter>
3516 {
3517 public:
3518 // Types:
3519 typedef _CharT char_type;
3520 typedef _OutIter iter_type;
3521
3522 explicit
3523 time_put_byname(const char*, size_t __refs = 0)
3524 : time_put<_CharT, _OutIter>(__refs)
3525 { };
3526
3527 protected:
3528 virtual
3529 ~time_put_byname() { }
3530 };
3531
3532
3533 /**
3534 * @brief Money format ordering data.
3535 *
3536 * This class contains an ordered array of 4 fields to represent the
3537 * pattern for formatting a money amount. Each field may contain one entry
3538 * from the part enum. symbol, sign, and value must be present and the
3539 * remaining field must contain either none or space. @see
3540 * moneypunct::pos_format() and moneypunct::neg_format() for details of how
3541 * these fields are interpreted.
3542 */
3543 class money_base
3544 {
3545 public:
3546 enum part { none, space, symbol, sign, value };
3547 struct pattern { char field[4]; };
3548
3549 static const pattern _S_default_pattern;
3550
3551 enum
3552 {
3553 _S_minus,
3554 _S_zero,
3555 _S_end = 11
3556 };
3557
3558 // String literal of acceptable (narrow) input/output, for
3559 // money_get/money_put. "-0123456789"
3560 static const char* _S_atoms;
3561
3562 // Construct and return valid pattern consisting of some combination of:
3563 // space none symbol sign value
3564 static pattern
3565 _S_construct_pattern(char __precedes, char __space, char __posn);
3566 };
3567
3568 template<typename _CharT, bool _Intl>
3569 struct __moneypunct_cache : public locale::facet
3570 {
3571 const char* _M_grouping;
3572 size_t _M_grouping_size;
3573 bool _M_use_grouping;
3574 _CharT _M_decimal_point;
3575 _CharT _M_thousands_sep;
3576 const _CharT* _M_curr_symbol;
3577 size_t _M_curr_symbol_size;
3578 const _CharT* _M_positive_sign;
3579 size_t _M_positive_sign_size;
3580 const _CharT* _M_negative_sign;
3581 size_t _M_negative_sign_size;
3582 int _M_frac_digits;
3583 money_base::pattern _M_pos_format;
3584 money_base::pattern _M_neg_format;
3585
3586 // A list of valid numeric literals for input and output: in the standard
3587 // "C" locale, this is "-0123456789". This array contains the chars after
3588 // having been passed through the current locale's ctype<_CharT>.widen().
3589 _CharT _M_atoms[money_base::_S_end];
3590
3591 bool _M_allocated;
3592
3593 __moneypunct_cache(size_t __refs = 0) : facet(__refs),
3594 _M_grouping(NULL), _M_grouping_size(0), _M_use_grouping(false),
3595 _M_decimal_point(_CharT()), _M_thousands_sep(_CharT()),
3596 _M_curr_symbol(NULL), _M_curr_symbol_size(0),
3597 _M_positive_sign(NULL), _M_positive_sign_size(0),
3598 _M_negative_sign(NULL), _M_negative_sign_size(0),
3599 _M_frac_digits(0),
3600 _M_pos_format(money_base::pattern()),
3601 _M_neg_format(money_base::pattern()), _M_allocated(false)
3602 { }
3603
3604 ~__moneypunct_cache();
3605
3606 void
3607 _M_cache(const locale& __loc);
3608
3609 private:
3610 __moneypunct_cache&
3611 operator=(const __moneypunct_cache&);
3612
3613 explicit
3614 __moneypunct_cache(const __moneypunct_cache&);
3615 };
3616
3617 template<typename _CharT, bool _Intl>
3618 __moneypunct_cache<_CharT, _Intl>::~__moneypunct_cache()
3619 {
3620 if (_M_allocated)
3621 {
3622 delete [] _M_grouping;
3623 delete [] _M_curr_symbol;
3624 delete [] _M_positive_sign;
3625 delete [] _M_negative_sign;
3626 }
3627 }
3628
3629 /**
3630 * @brief Facet for formatting data for money amounts.
3631 *
3632 * This facet encapsulates the punctuation, grouping and other formatting
3633 * features of money amount string representations.
3634 */
3635 template<typename _CharT, bool _Intl>
3636 class moneypunct : public locale::facet, public money_base
3637 {
3638 public:
3639 // Types:
3640 //@{
3641 /// Public typedefs
3642 typedef _CharT char_type;
3643 typedef basic_string<_CharT> string_type;
3644 //@}
3645 typedef __moneypunct_cache<_CharT, _Intl> __cache_type;
3646
3647 private:
3648 __cache_type* _M_data;
3649
3650 public:
3651 /// This value is provided by the standard, but no reason for its
3652 /// existence.
3653 static const bool intl = _Intl;
3654 /// Numpunct facet id.
3655 static locale::id id;
3656
3657 /**
3658 * @brief Constructor performs initialization.
3659 *
3660 * This is the constructor provided by the standard.
3661 *
3662 * @param refs Passed to the base facet class.
3663 */
3664 explicit
3665 moneypunct(size_t __refs = 0) : facet(__refs), _M_data(NULL)
3666 { _M_initialize_moneypunct(); }
3667
3668 /**
3669 * @brief Constructor performs initialization.
3670 *
3671 * This is an internal constructor.
3672 *
3673 * @param cache Cache for optimization.
3674 * @param refs Passed to the base facet class.
3675 */
3676 explicit
3677 moneypunct(__cache_type* __cache, size_t __refs = 0)
3678 : facet(__refs), _M_data(__cache)
3679 { _M_initialize_moneypunct(); }
3680
3681 /**
3682 * @brief Internal constructor. Not for general use.
3683 *
3684 * This is a constructor for use by the library itself to set up new
3685 * locales.
3686 *
3687 * @param cloc The "C" locale.
3688 * @param s The name of a locale.
3689 * @param refs Passed to the base facet class.
3690 */
3691 explicit
3692 moneypunct(__c_locale __cloc, const char* __s, size_t __refs = 0)
3693 : facet(__refs), _M_data(NULL)
3694 { _M_initialize_moneypunct(__cloc, __s); }
3695
3696 /**
3697 * @brief Return decimal point character.
3698 *
3699 * This function returns a char_type to use as a decimal point. It
3700 * does so by returning returning
3701 * moneypunct<char_type>::do_decimal_point().
3702 *
3703 * @return @a char_type representing a decimal point.
3704 */
3705 char_type
3706 decimal_point() const
3707 { return this->do_decimal_point(); }
3708
3709 /**
3710 * @brief Return thousands separator character.
3711 *
3712 * This function returns a char_type to use as a thousands
3713 * separator. It does so by returning returning
3714 * moneypunct<char_type>::do_thousands_sep().
3715 *
3716 * @return char_type representing a thousands separator.
3717 */
3718 char_type
3719 thousands_sep() const
3720 { return this->do_thousands_sep(); }
3721
3722 /**
3723 * @brief Return grouping specification.
3724 *
3725 * This function returns a string representing groupings for the
3726 * integer part of an amount. Groupings indicate where thousands
3727 * separators should be inserted.
3728 *
3729 * Each char in the return string is interpret as an integer rather
3730 * than a character. These numbers represent the number of digits in a
3731 * group. The first char in the string represents the number of digits
3732 * in the least significant group. If a char is negative, it indicates
3733 * an unlimited number of digits for the group. If more chars from the
3734 * string are required to group a number, the last char is used
3735 * repeatedly.
3736 *
3737 * For example, if the grouping() returns "\003\002" and is applied to
3738 * the number 123456789, this corresponds to 12,34,56,789. Note that
3739 * if the string was "32", this would put more than 50 digits into the
3740 * least significant group if the character set is ASCII.
3741 *
3742 * The string is returned by calling
3743 * moneypunct<char_type>::do_grouping().
3744 *
3745 * @return string representing grouping specification.
3746 */
3747 string
3748 grouping() const
3749 { return this->do_grouping(); }
3750
3751 /**
3752 * @brief Return currency symbol string.
3753 *
3754 * This function returns a string_type to use as a currency symbol. It
3755 * does so by returning returning
3756 * moneypunct<char_type>::do_curr_symbol().
3757 *
3758 * @return @a string_type representing a currency symbol.
3759 */
3760 string_type
3761 curr_symbol() const
3762 { return this->do_curr_symbol(); }
3763
3764 /**
3765 * @brief Return positive sign string.
3766 *
3767 * This function returns a string_type to use as a sign for positive
3768 * amounts. It does so by returning returning
3769 * moneypunct<char_type>::do_positive_sign().
3770 *
3771 * If the return value contains more than one character, the first
3772 * character appears in the position indicated by pos_format() and the
3773 * remainder appear at the end of the formatted string.
3774 *
3775 * @return @a string_type representing a positive sign.
3776 */
3777 string_type
3778 positive_sign() const
3779 { return this->do_positive_sign(); }
3780
3781 /**
3782 * @brief Return negative sign string.
3783 *
3784 * This function returns a string_type to use as a sign for negative
3785 * amounts. It does so by returning returning
3786 * moneypunct<char_type>::do_negative_sign().
3787 *
3788 * If the return value contains more than one character, the first
3789 * character appears in the position indicated by neg_format() and the
3790 * remainder appear at the end of the formatted string.
3791 *
3792 * @return @a string_type representing a negative sign.
3793 */
3794 string_type
3795 negative_sign() const
3796 { return this->do_negative_sign(); }
3797
3798 /**
3799 * @brief Return number of digits in fraction.
3800 *
3801 * This function returns the exact number of digits that make up the
3802 * fractional part of a money amount. It does so by returning
3803 * returning moneypunct<char_type>::do_frac_digits().
3804 *
3805 * The fractional part of a money amount is optional. But if it is
3806 * present, there must be frac_digits() digits.
3807 *
3808 * @return Number of digits in amount fraction.
3809 */
3810 int
3811 frac_digits() const
3812 { return this->do_frac_digits(); }
3813
3814 //@{
3815 /**
3816 * @brief Return pattern for money values.
3817 *
3818 * This function returns a pattern describing the formatting of a
3819 * positive or negative valued money amount. It does so by returning
3820 * returning moneypunct<char_type>::do_pos_format() or
3821 * moneypunct<char_type>::do_neg_format().
3822 *
3823 * The pattern has 4 fields describing the ordering of symbol, sign,
3824 * value, and none or space. There must be one of each in the pattern.
3825 * The none and space enums may not appear in the first field and space
3826 * may not appear in the final field.
3827 *
3828 * The parts of a money string must appear in the order indicated by
3829 * the fields of the pattern. The symbol field indicates that the
3830 * value of curr_symbol() may be present. The sign field indicates
3831 * that the value of positive_sign() or negative_sign() must be
3832 * present. The value field indicates that the absolute value of the
3833 * money amount is present. none indicates 0 or more whitespace
3834 * characters, except at the end, where it permits no whitespace.
3835 * space indicates that 1 or more whitespace characters must be
3836 * present.
3837 *
3838 * For example, for the US locale and pos_format() pattern
3839 * {symbol,sign,value,none}, curr_symbol() == '$' positive_sign() ==
3840 * '+', and value 10.01, and options set to force the symbol, the
3841 * corresponding string is "$+10.01".
3842 *
3843 * @return Pattern for money values.
3844 */
3845 pattern
3846 pos_format() const
3847 { return this->do_pos_format(); }
3848
3849 pattern
3850 neg_format() const
3851 { return this->do_neg_format(); }
3852 //@}
3853
3854 protected:
3855 /// Destructor.
3856 virtual
3857 ~moneypunct();
3858
3859 /**
3860 * @brief Return decimal point character.
3861 *
3862 * Returns a char_type to use as a decimal point. This function is a
3863 * hook for derived classes to change the value returned.
3864 *
3865 * @return @a char_type representing a decimal point.
3866 */
3867 virtual char_type
3868 do_decimal_point() const
3869 { return _M_data->_M_decimal_point; }
3870
3871 /**
3872 * @brief Return thousands separator character.
3873 *
3874 * Returns a char_type to use as a thousands separator. This function
3875 * is a hook for derived classes to change the value returned.
3876 *
3877 * @return @a char_type representing a thousands separator.
3878 */
3879 virtual char_type
3880 do_thousands_sep() const
3881 { return _M_data->_M_thousands_sep; }
3882
3883 /**
3884 * @brief Return grouping specification.
3885 *
3886 * Returns a string representing groupings for the integer part of a
3887 * number. This function is a hook for derived classes to change the
3888 * value returned. @see grouping() for details.
3889 *
3890 * @return String representing grouping specification.
3891 */
3892 virtual string
3893 do_grouping() const
3894 { return _M_data->_M_grouping; }
3895
3896 /**
3897 * @brief Return currency symbol string.
3898 *
3899 * This function returns a string_type to use as a currency symbol.
3900 * This function is a hook for derived classes to change the value
3901 * returned. @see curr_symbol() for details.
3902 *
3903 * @return @a string_type representing a currency symbol.
3904 */
3905 virtual string_type
3906 do_curr_symbol() const
3907 { return _M_data->_M_curr_symbol; }
3908
3909 /**
3910 * @brief Return positive sign string.
3911 *
3912 * This function returns a string_type to use as a sign for positive
3913 * amounts. This function is a hook for derived classes to change the
3914 * value returned. @see positive_sign() for details.
3915 *
3916 * @return @a string_type representing a positive sign.
3917 */
3918 virtual string_type
3919 do_positive_sign() const
3920 { return _M_data->_M_positive_sign; }
3921
3922 /**
3923 * @brief Return negative sign string.
3924 *
3925 * This function returns a string_type to use as a sign for negative
3926 * amounts. This function is a hook for derived classes to change the
3927 * value returned. @see negative_sign() for details.
3928 *
3929 * @return @a string_type representing a negative sign.
3930 */
3931 virtual string_type
3932 do_negative_sign() const
3933 { return _M_data->_M_negative_sign; }
3934
3935 /**
3936 * @brief Return number of digits in fraction.
3937 *
3938 * This function returns the exact number of digits that make up the
3939 * fractional part of a money amount. This function is a hook for
3940 * derived classes to change the value returned. @see frac_digits()
3941 * for details.
3942 *
3943 * @return Number of digits in amount fraction.
3944 */
3945 virtual int
3946 do_frac_digits() const
3947 { return _M_data->_M_frac_digits; }
3948
3949 /**
3950 * @brief Return pattern for money values.
3951 *
3952 * This function returns a pattern describing the formatting of a
3953 * positive valued money amount. This function is a hook for derived
3954 * classes to change the value returned. @see pos_format() for
3955 * details.
3956 *
3957 * @return Pattern for money values.
3958 */
3959 virtual pattern
3960 do_pos_format() const
3961 { return _M_data->_M_pos_format; }
3962
3963 /**
3964 * @brief Return pattern for money values.
3965 *
3966 * This function returns a pattern describing the formatting of a
3967 * negative valued money amount. This function is a hook for derived
3968 * classes to change the value returned. @see neg_format() for
3969 * details.
3970 *
3971 * @return Pattern for money values.
3972 */
3973 virtual pattern
3974 do_neg_format() const
3975 { return _M_data->_M_neg_format; }
3976
3977 // For use at construction time only.
3978 void
3979 _M_initialize_moneypunct(__c_locale __cloc = NULL,
3980 const char* __name = NULL);
3981 };
3982
3983 template<typename _CharT, bool _Intl>
3984 locale::id moneypunct<_CharT, _Intl>::id;
3985
3986 template<typename _CharT, bool _Intl>
3987 const bool moneypunct<_CharT, _Intl>::intl;
3988
3989 template<>
3990 moneypunct<char, true>::~moneypunct();
3991
3992 template<>
3993 moneypunct<char, false>::~moneypunct();
3994
3995 template<>
3996 void
3997 moneypunct<char, true>::_M_initialize_moneypunct(__c_locale, const char*);
3998
3999 template<>
4000 void
4001 moneypunct<char, false>::_M_initialize_moneypunct(__c_locale, const char*);
4002
4003 #ifdef _GLIBCXX_USE_WCHAR_T
4004 template<>
4005 moneypunct<wchar_t, true>::~moneypunct();
4006
4007 template<>
4008 moneypunct<wchar_t, false>::~moneypunct();
4009
4010 template<>
4011 void
4012 moneypunct<wchar_t, true>::_M_initialize_moneypunct(__c_locale,
4013 const char*);
4014
4015 template<>
4016 void
4017 moneypunct<wchar_t, false>::_M_initialize_moneypunct(__c_locale,
4018 const char*);
4019 #endif
4020
4021 /// @brief class moneypunct_byname [22.2.6.4].
4022 template<typename _CharT, bool _Intl>
4023 class moneypunct_byname : public moneypunct<_CharT, _Intl>
4024 {
4025 public:
4026 typedef _CharT char_type;
4027 typedef basic_string<_CharT> string_type;
4028
4029 static const bool intl = _Intl;
4030
4031 explicit
4032 moneypunct_byname(const char* __s, size_t __refs = 0)
4033 : moneypunct<_CharT, _Intl>(__refs)
4034 {
4035 if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0)
4036 {
4037 __c_locale __tmp;
4038 this->_S_create_c_locale(__tmp, __s);
4039 this->_M_initialize_moneypunct(__tmp);
4040 this->_S_destroy_c_locale(__tmp);
4041 }
4042 }
4043
4044 protected:
4045 virtual
4046 ~moneypunct_byname() { }
4047 };
4048
4049 template<typename _CharT, bool _Intl>
4050 const bool moneypunct_byname<_CharT, _Intl>::intl;
4051
4052 _GLIBCXX_BEGIN_LDBL_NAMESPACE
4053 /**
4054 * @brief Facet for parsing monetary amounts.
4055 *
4056 * This facet encapsulates the code to parse and return a monetary
4057 * amount from a string.
4058 *
4059 * The money_get template uses protected virtual functions to
4060 * provide the actual results. The public accessors forward the
4061 * call to the virtual functions. These virtual functions are
4062 * hooks for developers to implement the behavior they require from
4063 * the money_get facet.
4064 */
4065 template<typename _CharT, typename _InIter>
4066 class money_get : public locale::facet
4067 {
4068 public:
4069 // Types:
4070 //@{
4071 /// Public typedefs
4072 typedef _CharT char_type;
4073 typedef _InIter iter_type;
4074 typedef basic_string<_CharT> string_type;
4075 //@}
4076
4077 /// Numpunct facet id.
4078 static locale::id id;
4079
4080 /**
4081 * @brief Constructor performs initialization.
4082 *
4083 * This is the constructor provided by the standard.
4084 *
4085 * @param refs Passed to the base facet class.
4086 */
4087 explicit
4088 money_get(size_t __refs = 0) : facet(__refs) { }
4089
4090 /**
4091 * @brief Read and parse a monetary value.
4092 *
4093 * This function reads characters from @a s, interprets them as a
4094 * monetary value according to moneypunct and ctype facets retrieved
4095 * from io.getloc(), and returns the result in @a units as an integral
4096 * value moneypunct::frac_digits() * the actual amount. For example,
4097 * the string $10.01 in a US locale would store 1001 in @a units.
4098 *
4099 * Any characters not part of a valid money amount are not consumed.
4100 *
4101 * If a money value cannot be parsed from the input stream, sets
4102 * err=(err|io.failbit). If the stream is consumed before finishing
4103 * parsing, sets err=(err|io.failbit|io.eofbit). @a units is
4104 * unchanged if parsing fails.
4105 *
4106 * This function works by returning the result of do_get().
4107 *
4108 * @param s Start of characters to parse.
4109 * @param end End of characters to parse.
4110 * @param intl Parameter to use_facet<moneypunct<CharT,intl> >.
4111 * @param io Source of facets and io state.
4112 * @param err Error field to set if parsing fails.
4113 * @param units Place to store result of parsing.
4114 * @return Iterator referencing first character beyond valid money
4115 * amount.
4116 */
4117 iter_type
4118 get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4119 ios_base::iostate& __err, long double& __units) const
4120 { return this->do_get(__s, __end, __intl, __io, __err, __units); }
4121
4122 /**
4123 * @brief Read and parse a monetary value.
4124 *
4125 * This function reads characters from @a s, interprets them as a
4126 * monetary value according to moneypunct and ctype facets retrieved
4127 * from io.getloc(), and returns the result in @a digits. For example,
4128 * the string $10.01 in a US locale would store "1001" in @a digits.
4129 *
4130 * Any characters not part of a valid money amount are not consumed.
4131 *
4132 * If a money value cannot be parsed from the input stream, sets
4133 * err=(err|io.failbit). If the stream is consumed before finishing
4134 * parsing, sets err=(err|io.failbit|io.eofbit).
4135 *
4136 * This function works by returning the result of do_get().
4137 *
4138 * @param s Start of characters to parse.
4139 * @param end End of characters to parse.
4140 * @param intl Parameter to use_facet<moneypunct<CharT,intl> >.
4141 * @param io Source of facets and io state.
4142 * @param err Error field to set if parsing fails.
4143 * @param digits Place to store result of parsing.
4144 * @return Iterator referencing first character beyond valid money
4145 * amount.
4146 */
4147 iter_type
4148 get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4149 ios_base::iostate& __err, string_type& __digits) const
4150 { return this->do_get(__s, __end, __intl, __io, __err, __digits); }
4151
4152 protected:
4153 /// Destructor.
4154 virtual
4155 ~money_get() { }
4156
4157 /**
4158 * @brief Read and parse a monetary value.
4159 *
4160 * This function reads and parses characters representing a monetary
4161 * value. This function is a hook for derived classes to change the
4162 * value returned. @see get() for details.
4163 */
4164 // XXX GLIBCXX_ABI Deprecated
4165 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
4166 virtual iter_type
4167 __do_get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4168 ios_base::iostate& __err, double& __units) const;
4169 #else
4170 virtual iter_type
4171 do_get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4172 ios_base::iostate& __err, long double& __units) const;
4173 #endif
4174
4175 /**
4176 * @brief Read and parse a monetary value.
4177 *
4178 * This function reads and parses characters representing a monetary
4179 * value. This function is a hook for derived classes to change the
4180 * value returned. @see get() for details.
4181 */
4182 virtual iter_type
4183 do_get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4184 ios_base::iostate& __err, string_type& __digits) const;
4185
4186 // XXX GLIBCXX_ABI Deprecated
4187 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
4188 virtual iter_type
4189 do_get(iter_type __s, iter_type __end, bool __intl, ios_base& __io,
4190 ios_base::iostate& __err, long double& __units) const;
4191 #endif
4192
4193 template<bool _Intl>
4194 iter_type
4195 _M_extract(iter_type __s, iter_type __end, ios_base& __io,
4196 ios_base::iostate& __err, string& __digits) const;
4197 };
4198
4199 template<typename _CharT, typename _InIter>
4200 locale::id money_get<_CharT, _InIter>::id;
4201
4202 /**
4203 * @brief Facet for outputting monetary amounts.
4204 *
4205 * This facet encapsulates the code to format and output a monetary
4206 * amount.
4207 *
4208 * The money_put template uses protected virtual functions to
4209 * provide the actual results. The public accessors forward the
4210 * call to the virtual functions. These virtual functions are
4211 * hooks for developers to implement the behavior they require from
4212 * the money_put facet.
4213 */
4214 template<typename _CharT, typename _OutIter>
4215 class money_put : public locale::facet
4216 {
4217 public:
4218 //@{
4219 /// Public typedefs
4220 typedef _CharT char_type;
4221 typedef _OutIter iter_type;
4222 typedef basic_string<_CharT> string_type;
4223 //@}
4224
4225 /// Numpunct facet id.
4226 static locale::id id;
4227
4228 /**
4229 * @brief Constructor performs initialization.
4230 *
4231 * This is the constructor provided by the standard.
4232 *
4233 * @param refs Passed to the base facet class.
4234 */
4235 explicit
4236 money_put(size_t __refs = 0) : facet(__refs) { }
4237
4238 /**
4239 * @brief Format and output a monetary value.
4240 *
4241 * This function formats @a units as a monetary value according to
4242 * moneypunct and ctype facets retrieved from io.getloc(), and writes
4243 * the resulting characters to @a s. For example, the value 1001 in a
4244 * US locale would write "$10.01" to @a s.
4245 *
4246 * This function works by returning the result of do_put().
4247 *
4248 * @param s The stream to write to.
4249 * @param intl Parameter to use_facet<moneypunct<CharT,intl> >.
4250 * @param io Source of facets and io state.
4251 * @param fill char_type to use for padding.
4252 * @param units Place to store result of parsing.
4253 * @return Iterator after writing.
4254 */
4255 iter_type
4256 put(iter_type __s, bool __intl, ios_base& __io,
4257 char_type __fill, long double __units) const
4258 { return this->do_put(__s, __intl, __io, __fill, __units); }
4259
4260 /**
4261 * @brief Format and output a monetary value.
4262 *
4263 * This function formats @a digits as a monetary value according to
4264 * moneypunct and ctype facets retrieved from io.getloc(), and writes
4265 * the resulting characters to @a s. For example, the string "1001" in
4266 * a US locale would write "$10.01" to @a s.
4267 *
4268 * This function works by returning the result of do_put().
4269 *
4270 * @param s The stream to write to.
4271 * @param intl Parameter to use_facet<moneypunct<CharT,intl> >.
4272 * @param io Source of facets and io state.
4273 * @param fill char_type to use for padding.
4274 * @param units Place to store result of parsing.
4275 * @return Iterator after writing.
4276 */
4277 iter_type
4278 put(iter_type __s, bool __intl, ios_base& __io,
4279 char_type __fill, const string_type& __digits) const
4280 { return this->do_put(__s, __intl, __io, __fill, __digits); }
4281
4282 protected:
4283 /// Destructor.
4284 virtual
4285 ~money_put() { }
4286
4287 /**
4288 * @brief Format and output a monetary value.
4289 *
4290 * This function formats @a units as a monetary value according to
4291 * moneypunct and ctype facets retrieved from io.getloc(), and writes
4292 * the resulting characters to @a s. For example, the value 1001 in a
4293 * US locale would write "$10.01" to @a s.
4294 *
4295 * This function is a hook for derived classes to change the value
4296 * returned. @see put().
4297 *
4298 * @param s The stream to write to.
4299 * @param intl Parameter to use_facet<moneypunct<CharT,intl> >.
4300 * @param io Source of facets and io state.
4301 * @param fill char_type to use for padding.
4302 * @param units Place to store result of parsing.
4303 * @return Iterator after writing.
4304 */
4305 // XXX GLIBCXX_ABI Deprecated
4306 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
4307 virtual iter_type
4308 __do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
4309 double __units) const;
4310 #else
4311 virtual iter_type
4312 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
4313 long double __units) const;
4314 #endif
4315
4316 /**
4317 * @brief Format and output a monetary value.
4318 *
4319 * This function formats @a digits as a monetary value according to
4320 * moneypunct and ctype facets retrieved from io.getloc(), and writes
4321 * the resulting characters to @a s. For example, the string "1001" in
4322 * a US locale would write "$10.01" to @a s.
4323 *
4324 * This function is a hook for derived classes to change the value
4325 * returned. @see put().
4326 *
4327 * @param s The stream to write to.
4328 * @param intl Parameter to use_facet<moneypunct<CharT,intl> >.
4329 * @param io Source of facets and io state.
4330 * @param fill char_type to use for padding.
4331 * @param units Place to store result of parsing.
4332 * @return Iterator after writing.
4333 */
4334 virtual iter_type
4335 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
4336 const string_type& __digits) const;
4337
4338 // XXX GLIBCXX_ABI Deprecated
4339 #if defined _GLIBCXX_LONG_DOUBLE_COMPAT && defined __LONG_DOUBLE_128__
4340 virtual iter_type
4341 do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill,
4342 long double __units) const;
4343 #endif
4344
4345 template<bool _Intl>
4346 iter_type
4347 _M_insert(iter_type __s, ios_base& __io, char_type __fill,
4348 const string_type& __digits) const;
4349 };
4350
4351 template<typename _CharT, typename _OutIter>
4352 locale::id money_put<_CharT, _OutIter>::id;
4353
4354 _GLIBCXX_END_LDBL_NAMESPACE
4355
4356 /**
4357 * @brief Messages facet base class providing catalog typedef.
4358 */
4359 struct messages_base
4360 {
4361 typedef int catalog;
4362 };
4363
4364 /**
4365 * @brief Facet for handling message catalogs
4366 *
4367 * This facet encapsulates the code to retrieve messages from
4368 * message catalogs. The only thing defined by the standard for this facet
4369 * is the interface. All underlying functionality is
4370 * implementation-defined.
4371 *
4372 * This library currently implements 3 versions of the message facet. The
4373 * first version (gnu) is a wrapper around gettext, provided by libintl.
4374 * The second version (ieee) is a wrapper around catgets. The final
4375 * version (default) does no actual translation. These implementations are
4376 * only provided for char and wchar_t instantiations.
4377 *
4378 * The messages template uses protected virtual functions to
4379 * provide the actual results. The public accessors forward the
4380 * call to the virtual functions. These virtual functions are
4381 * hooks for developers to implement the behavior they require from
4382 * the messages facet.
4383 */
4384 template<typename _CharT>
4385 class messages : public locale::facet, public messages_base
4386 {
4387 public:
4388 // Types:
4389 //@{
4390 /// Public typedefs
4391 typedef _CharT char_type;
4392 typedef basic_string<_CharT> string_type;
4393 //@}
4394
4395 protected:
4396 // Underlying "C" library locale information saved from
4397 // initialization, needed by messages_byname as well.
4398 __c_locale _M_c_locale_messages;
4399 const char* _M_name_messages;
4400
4401 public:
4402 /// Numpunct facet id.
4403 static locale::id id;
4404
4405 /**
4406 * @brief Constructor performs initialization.
4407 *
4408 * This is the constructor provided by the standard.
4409 *
4410 * @param refs Passed to the base facet class.
4411 */
4412 explicit
4413 messages(size_t __refs = 0);
4414
4415 // Non-standard.
4416 /**
4417 * @brief Internal constructor. Not for general use.
4418 *
4419 * This is a constructor for use by the library itself to set up new
4420 * locales.
4421 *
4422 * @param cloc The "C" locale.
4423 * @param s The name of a locale.
4424 * @param refs Refcount to pass to the base class.
4425 */
4426 explicit
4427 messages(__c_locale __cloc, const char* __s, size_t __refs = 0);
4428
4429 /*
4430 * @brief Open a message catalog.
4431 *
4432 * This function opens and returns a handle to a message catalog by
4433 * returning do_open(s, loc).
4434 *
4435 * @param s The catalog to open.
4436 * @param loc Locale to use for character set conversions.
4437 * @return Handle to the catalog or value < 0 if open fails.
4438 */
4439 catalog
4440 open(const basic_string<char>& __s, const locale& __loc) const
4441 { return this->do_open(__s, __loc); }
4442
4443 // Non-standard and unorthodox, yet effective.
4444 /*
4445 * @brief Open a message catalog.
4446 *
4447 * This non-standard function opens and returns a handle to a message
4448 * catalog by returning do_open(s, loc). The third argument provides a
4449 * message catalog root directory for gnu gettext and is ignored
4450 * otherwise.
4451 *
4452 * @param s The catalog to open.
4453 * @param loc Locale to use for character set conversions.
4454 * @param dir Message catalog root directory.
4455 * @return Handle to the catalog or value < 0 if open fails.
4456 */
4457 catalog
4458 open(const basic_string<char>&, const locale&, const char*) const;
4459
4460 /*
4461 * @brief Look up a string in a message catalog.
4462 *
4463 * This function retrieves and returns a message from a catalog by
4464 * returning do_get(c, set, msgid, s).
4465 *
4466 * For gnu, @a set and @a msgid are ignored. Returns gettext(s).
4467 * For default, returns s. For ieee, returns catgets(c,set,msgid,s).
4468 *
4469 * @param c The catalog to access.
4470 * @param set Implementation-defined.
4471 * @param msgid Implementation-defined.
4472 * @param s Default return value if retrieval fails.
4473 * @return Retrieved message or @a s if get fails.
4474 */
4475 string_type
4476 get(catalog __c, int __set, int __msgid, const string_type& __s) const
4477 { return this->do_get(__c, __set, __msgid, __s); }
4478
4479 /*
4480 * @brief Close a message catalog.
4481 *
4482 * Closes catalog @a c by calling do_close(c).
4483 *
4484 * @param c The catalog to close.
4485 */
4486 void
4487 close(catalog __c) const
4488 { return this->do_close(__c); }
4489
4490 protected:
4491 /// Destructor.
4492 virtual
4493 ~messages();
4494
4495 /*
4496 * @brief Open a message catalog.
4497 *
4498 * This function opens and returns a handle to a message catalog in an
4499 * implementation-defined manner. This function is a hook for derived
4500 * classes to change the value returned.
4501 *
4502 * @param s The catalog to open.
4503 * @param loc Locale to use for character set conversions.
4504 * @return Handle to the opened catalog, value < 0 if open failed.
4505 */
4506 virtual catalog
4507 do_open(const basic_string<char>&, const locale&) const;
4508
4509 /*
4510 * @brief Look up a string in a message catalog.
4511 *
4512 * This function retrieves and returns a message from a catalog in an
4513 * implementation-defined manner. This function is a hook for derived
4514 * classes to change the value returned.
4515 *
4516 * For gnu, @a set and @a msgid are ignored. Returns gettext(s).
4517 * For default, returns s. For ieee, returns catgets(c,set,msgid,s).
4518 *
4519 * @param c The catalog to access.
4520 * @param set Implementation-defined.
4521 * @param msgid Implementation-defined.
4522 * @param s Default return value if retrieval fails.
4523 * @return Retrieved message or @a s if get fails.
4524 */
4525 virtual string_type
4526 do_get(catalog, int, int, const string_type& __dfault) const;
4527
4528 /*
4529 * @brief Close a message catalog.
4530 *
4531 * @param c The catalog to close.
4532 */
4533 virtual void
4534 do_close(catalog) const;
4535
4536 // Returns a locale and codeset-converted string, given a char* message.
4537 char*
4538 _M_convert_to_char(const string_type& __msg) const
4539 {
4540 // XXX
4541 return reinterpret_cast<char*>(const_cast<_CharT*>(__msg.c_str()));
4542 }
4543
4544 // Returns a locale and codeset-converted string, given a char* message.
4545 string_type
4546 _M_convert_from_char(char*) const
4547 {
4548 #if 0
4549 // Length of message string without terminating null.
4550 size_t __len = char_traits<char>::length(__msg) - 1;
4551
4552 // "everybody can easily convert the string using
4553 // mbsrtowcs/wcsrtombs or with iconv()"
4554
4555 // Convert char* to _CharT in locale used to open catalog.
4556 // XXX need additional template parameter on messages class for this..
4557 // typedef typename codecvt<char, _CharT, _StateT> __codecvt_type;
4558 typedef typename codecvt<char, _CharT, mbstate_t> __codecvt_type;
4559
4560 __codecvt_type::state_type __state;
4561 // XXX may need to initialize state.
4562 //initialize_state(__state._M_init());
4563
4564 char* __from_next;
4565 // XXX what size for this string?
4566 _CharT* __to = static_cast<_CharT*>(__builtin_alloca(__len + 1));
4567 const __codecvt_type& __cvt = use_facet<__codecvt_type>(_M_locale_conv);
4568 __cvt.out(__state, __msg, __msg + __len, __from_next,
4569 __to, __to + __len + 1, __to_next);
4570 return string_type(__to);
4571 #endif
4572 #if 0
4573 typedef ctype<_CharT> __ctype_type;
4574 // const __ctype_type& __cvt = use_facet<__ctype_type>(_M_locale_msg);
4575 const __ctype_type& __cvt = use_facet<__ctype_type>(locale());
4576 // XXX Again, proper length of converted string an issue here.
4577 // For now, assume the converted length is not larger.
4578 _CharT* __dest = static_cast<_CharT*>(__builtin_alloca(__len + 1));
4579 __cvt.widen(__msg, __msg + __len, __dest);
4580 return basic_string<_CharT>(__dest);
4581 #endif
4582 return string_type();
4583 }
4584 };
4585
4586 template<typename _CharT>
4587 locale::id messages<_CharT>::id;
4588
4589 // Specializations for required instantiations.
4590 template<>
4591 string
4592 messages<char>::do_get(catalog, int, int, const string&) const;
4593
4594 #ifdef _GLIBCXX_USE_WCHAR_T
4595 template<>
4596 wstring
4597 messages<wchar_t>::do_get(catalog, int, int, const wstring&) const;
4598 #endif
4599
4600 /// @brief class messages_byname [22.2.7.2].
4601 template<typename _CharT>
4602 class messages_byname : public messages<_CharT>
4603 {
4604 public:
4605 typedef _CharT char_type;
4606 typedef basic_string<_CharT> string_type;
4607
4608 explicit
4609 messages_byname(const char* __s, size_t __refs = 0);
4610
4611 protected:
4612 virtual
4613 ~messages_byname()
4614 { }
4615 };
4616
4617 _GLIBCXX_END_NAMESPACE
4618
4619 // Include host and configuration specific messages functions.
4620 #include <bits/messages_members.h>
4621
4622 _GLIBCXX_BEGIN_NAMESPACE(std)
4623
4624 // Subclause convenience interfaces, inlines.
4625 // NB: These are inline because, when used in a loop, some compilers
4626 // can hoist the body out of the loop; then it's just as fast as the
4627 // C is*() function.
4628
4629 /// Convenience interface to ctype.is(ctype_base::space, __c).
4630 template<typename _CharT>
4631 inline bool
4632 isspace(_CharT __c, const locale& __loc)
4633 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c); }
4634
4635 /// Convenience interface to ctype.is(ctype_base::print, __c).
4636 template<typename _CharT>
4637 inline bool
4638 isprint(_CharT __c, const locale& __loc)
4639 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c); }
4640
4641 /// Convenience interface to ctype.is(ctype_base::cntrl, __c).
4642 template<typename _CharT>
4643 inline bool
4644 iscntrl(_CharT __c, const locale& __loc)
4645 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c); }
4646
4647 /// Convenience interface to ctype.is(ctype_base::upper, __c).
4648 template<typename _CharT>
4649 inline bool
4650 isupper(_CharT __c, const locale& __loc)
4651 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c); }
4652
4653 /// Convenience interface to ctype.is(ctype_base::lower, __c).
4654 template<typename _CharT>
4655 inline bool
4656 islower(_CharT __c, const locale& __loc)
4657 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c); }
4658
4659 /// Convenience interface to ctype.is(ctype_base::alpha, __c).
4660 template<typename _CharT>
4661 inline bool
4662 isalpha(_CharT __c, const locale& __loc)
4663 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c); }
4664
4665 /// Convenience interface to ctype.is(ctype_base::digit, __c).
4666 template<typename _CharT>
4667 inline bool
4668 isdigit(_CharT __c, const locale& __loc)
4669 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c); }
4670
4671 /// Convenience interface to ctype.is(ctype_base::punct, __c).
4672 template<typename _CharT>
4673 inline bool
4674 ispunct(_CharT __c, const locale& __loc)
4675 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c); }
4676
4677 /// Convenience interface to ctype.is(ctype_base::xdigit, __c).
4678 template<typename _CharT>
4679 inline bool
4680 isxdigit(_CharT __c, const locale& __loc)
4681 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c); }
4682
4683 /// Convenience interface to ctype.is(ctype_base::alnum, __c).
4684 template<typename _CharT>
4685 inline bool
4686 isalnum(_CharT __c, const locale& __loc)
4687 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c); }
4688
4689 /// Convenience interface to ctype.is(ctype_base::graph, __c).
4690 template<typename _CharT>
4691 inline bool
4692 isgraph(_CharT __c, const locale& __loc)
4693 { return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c); }
4694
4695 /// Convenience interface to ctype.toupper(__c).
4696 template<typename _CharT>
4697 inline _CharT
4698 toupper(_CharT __c, const locale& __loc)
4699 { return use_facet<ctype<_CharT> >(__loc).toupper(__c); }
4700
4701 /// Convenience interface to ctype.tolower(__c).
4702 template<typename _CharT>
4703 inline _CharT
4704 tolower(_CharT __c, const locale& __loc)
4705 { return use_facet<ctype<_CharT> >(__loc).tolower(__c); }
4706
4707 _GLIBCXX_END_NAMESPACE
4708
4709 #endif