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