/contrib/libstdc++/include/bits/locale_facets.h

https://bitbucket.org/freebsd/freebsd-head/ · C++ Header · 4688 lines · 1690 code · 458 blank · 2540 comment · 53 complexity · 28a678fe0b8b6a609d3302064e0488ac MD5 · raw file

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