| 1 |
// Locale support -*- C++ -*- |
| 2 |
// gcc-3.4.6/libstdc++-v3/include/bits/locale_facets.h |
| 3 |
|
| 4 |
// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 |
| 5 |
// Free Software Foundation, Inc. |
| 6 |
// |
| 7 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 8 |
// software; you can redistribute it and/or modify it under the |
| 9 |
// terms of the GNU General Public License as published by the |
| 10 |
// Free Software Foundation; either version 2, or (at your option) |
| 11 |
// any later version. |
| 12 |
|
| 13 |
// This library is distributed in the hope that it will be useful, |
| 14 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
// GNU General Public License for more details. |
| 17 |
|
| 18 |
// You should have received a copy of the GNU General Public License along |
| 19 |
// with this library; see the file COPYING. If not, write to the Free |
| 20 |
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 21 |
// USA. |
| 22 |
|
| 23 |
// As a special exception, you may use this file as part of a free software |
| 24 |
// library without restriction. Specifically, if other files instantiate |
| 25 |
// templates or use macros or inline functions from this file, or you compile |
| 26 |
// this file and link it with other files to produce an executable, this |
| 27 |
// file does not by itself cause the resulting executable to be covered by |
| 28 |
// the GNU General Public License. This exception does not however |
| 29 |
// invalidate any other reasons why the executable file might be covered by |
| 30 |
// the GNU General Public License. |
| 31 |
|
| 32 |
// |
| 33 |
// ISO C++ 14882: 22.1 Locales |
| 34 |
// |
| 35 |
|
| 36 |
/** @file locale_facets.h |
| 37 |
* This is an internal header file, included by other library headers. |
| 38 |
* You should not attempt to use it directly. |
| 39 |
*/ |
| 40 |
|
| 41 |
#ifndef _LOCALE_FACETS_H |
| 42 |
#define _LOCALE_FACETS_H 1 |
| 43 |
|
| 44 |
#pragma GCC system_header |
| 45 |
|
| 46 |
#include <ctime> // For struct tm |
| 47 |
#include <cwctype> // For wctype_t |
| 48 |
#include <iosfwd> |
| 49 |
#include <bits/ios_base.h> // For ios_base, ios_base::iostate |
| 50 |
#include <streambuf> |
| 51 |
|
| 52 |
namespace std |
| 53 |
{ |
| 54 |
// NB: Don't instantiate required wchar_t facets if no wchar_t support. |
| 55 |
#ifdef _GLIBCXX_USE_WCHAR_T |
| 56 |
# define _GLIBCXX_NUM_FACETS 28 |
| 57 |
#else |
| 58 |
# define _GLIBCXX_NUM_FACETS 14 |
| 59 |
#endif |
| 60 |
|
| 61 |
// Convert string to numeric value of type _Tv and store results. |
| 62 |
// NB: This is specialized for all required types, there is no |
| 63 |
// generic definition. |
| 64 |
template<typename _Tv> |
| 65 |
void |
| 66 |
__convert_to_v(const char* __in, _Tv& __out, ios_base::iostate& __err, |
| 67 |
const __c_locale& __cloc); |
| 68 |
|
| 69 |
// Explicit specializations for required types. |
| 70 |
template<> |
| 71 |
void |
| 72 |
__convert_to_v(const char*, float&, ios_base::iostate&, |
| 73 |
const __c_locale&); |
| 74 |
|
| 75 |
template<> |
| 76 |
void |
| 77 |
__convert_to_v(const char*, double&, ios_base::iostate&, |
| 78 |
const __c_locale&); |
| 79 |
|
| 80 |
template<> |
| 81 |
void |
| 82 |
__convert_to_v(const char*, long double&, ios_base::iostate&, |
| 83 |
const __c_locale&); |
| 84 |
|
| 85 |
// NB: __pad is a struct, rather than a function, so it can be |
| 86 |
// partially-specialized. |
| 87 |
template<typename _CharT, typename _Traits> |
| 88 |
struct __pad |
| 89 |
{ |
| 90 |
static void |
| 91 |
_S_pad(ios_base& __io, _CharT __fill, _CharT* __news, |
| 92 |
const _CharT* __olds, const streamsize __newlen, |
| 93 |
const streamsize __oldlen, const bool __num); |
| 94 |
}; |
| 95 |
|
| 96 |
// Used by both numeric and monetary facets. |
| 97 |
// Inserts "group separator" characters into an array of characters. |
| 98 |
// It's recursive, one iteration per group. It moves the characters |
| 99 |
// in the buffer this way: "xxxx12345" -> "12,345xxx". Call this |
| 100 |
// only with __glen != 0. |
| 101 |
template<typename _CharT> |
| 102 |
_CharT* |
| 103 |
__add_grouping(_CharT* __s, _CharT __sep, |
| 104 |
const char* __gbeg, size_t __gsize, |
| 105 |
const _CharT* __first, const _CharT* __last); |
| 106 |
|
| 107 |
// This template permits specializing facet output code for |
| 108 |
// ostreambuf_iterator. For ostreambuf_iterator, sputn is |
| 109 |
// significantly more efficient than incrementing iterators. |
| 110 |
template<typename _CharT> |
| 111 |
inline |
| 112 |
ostreambuf_iterator<_CharT> |
| 113 |
__write(ostreambuf_iterator<_CharT> __s, const _CharT* __ws, int __len) |
| 114 |
{ |
| 115 |
__s._M_put(__ws, __len); |
| 116 |
return __s; |
| 117 |
} |
| 118 |
|
| 119 |
// This is the unspecialized form of the template. |
| 120 |
template<typename _CharT, typename _OutIter> |
| 121 |
inline |
| 122 |
_OutIter |
| 123 |
__write(_OutIter __s, const _CharT* __ws, int __len) |
| 124 |
{ |
| 125 |
for (int __j = 0; __j < __len; __j++, ++__s) |
| 126 |
*__s = __ws[__j]; |
| 127 |
return __s; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
// 22.2.1.1 Template class ctype |
| 132 |
// Include host and configuration specific ctype enums for ctype_base. |
| 133 |
#include <bits/ctype_base.h> |
| 134 |
|
| 135 |
// Common base for ctype<_CharT>. |
| 136 |
/** |
| 137 |
* @brief Common base for ctype facet |
| 138 |
* |
| 139 |
* This template class provides implementations of the public functions |
| 140 |
* that forward to the protected virtual functions. |
| 141 |
* |
| 142 |
* This template also provides abtract stubs for the protected virtual |
| 143 |
* functions. |
| 144 |
*/ |
| 145 |
template<typename _CharT> |
| 146 |
class __ctype_abstract_base : public locale::facet, public ctype_base |
| 147 |
; |
| 148 |
|
| 149 |
// NB: Generic, mostly useless implementation. |
| 150 |
/** |
| 151 |
* @brief Template ctype facet |
| 152 |
* |
| 153 |
* This template class defines classification and conversion functions for |
| 154 |
* character sets. It wraps <cctype> functionality. Ctype gets used by |
| 155 |
* streams for many I/O operations. |
| 156 |
* |
| 157 |
* This template provides the protected virtual functions the developer |
| 158 |
* will have to replace in a derived class or specialization to make a |
| 159 |
* working facet. The public functions that access them are defined in |
| 160 |
* __ctype_abstract_base, to allow for implementation flexibility. See |
| 161 |
* ctype<wchar_t> for an example. The functions are documented in |
| 162 |
* __ctype_abstract_base. |
| 163 |
* |
| 164 |
* Note: implementations are provided for all the protected virtual |
| 165 |
* functions, but will likely not be useful. |
| 166 |
*/ |
| 167 |
template<typename _CharT> |
| 168 |
class ctype : public __ctype_abstract_base<_CharT> |
| 169 |
; |
| 170 |
|
| 171 |
template<typename _CharT> |
| 172 |
locale::id ctype<_CharT>::id; |
| 173 |
|
| 174 |
// 22.2.1.3 ctype<char> specialization. |
| 175 |
/** |
| 176 |
* @brief The ctype<char> specialization. |
| 177 |
* |
| 178 |
* This class defines classification and conversion functions for |
| 179 |
* the char type. It gets used by char streams for many I/O |
| 180 |
* operations. The char specialization provides a number of |
| 181 |
* optimizations as well. |
| 182 |
*/ |
| 183 |
template<> |
| 184 |
class ctype<char> : public locale::facet, public ctype_base |
| 185 |
; |
| 186 |
|
| 187 |
template<> |
| 188 |
const ctype<char>& |
| 189 |
use_facet<ctype<char> >(const locale& __loc); |
| 190 |
|
| 191 |
#ifdef _GLIBCXX_USE_WCHAR_T |
| 192 |
// 22.2.1.3 ctype<wchar_t> specialization |
| 193 |
/** |
| 194 |
* @brief The ctype<wchar_t> specialization. |
| 195 |
* |
| 196 |
* This class defines classification and conversion functions for the |
| 197 |
* wchar_t type. It gets used by wchar_t streams for many I/O operations. |
| 198 |
* The wchar_t specialization provides a number of optimizations as well. |
| 199 |
* |
| 200 |
* ctype<wchar_t> inherits its public methods from |
| 201 |
* __ctype_abstract_base<wchar_t>. |
| 202 |
*/ |
| 203 |
template<> |
| 204 |
class ctype<wchar_t> : public __ctype_abstract_base<wchar_t> |
| 205 |
; |
| 206 |
|
| 207 |
template<> |
| 208 |
const ctype<wchar_t>& |
| 209 |
use_facet<ctype<wchar_t> >(const locale& __loc); |
| 210 |
#endif //_GLIBCXX_USE_WCHAR_T |
| 211 |
|
| 212 |
// Include host and configuration specific ctype inlines. |
| 213 |
#include <bits/ctype_inline.h> |
| 214 |
|
| 215 |
// 22.2.1.2 Template class ctype_byname |
| 216 |
template<typename _CharT> |
| 217 |
class ctype_byname : public ctype<_CharT> |
| 218 |
; |
| 219 |
|
| 220 |
// 22.2.1.4 Class ctype_byname specializations. |
| 221 |
template<> |
| 222 |
ctype_byname<char>::ctype_byname(const char*, size_t refs); |
| 223 |
|
| 224 |
template<> |
| 225 |
ctype_byname<wchar_t>::ctype_byname(const char*, size_t refs); |
| 226 |
|
| 227 |
// 22.2.1.5 Template class codecvt |
| 228 |
#include <bits/codecvt.h> |
| 229 |
|
| 230 |
// 22.2.2 The numeric category. |
| 231 |
class __num_base |
| 232 |
; |
| 233 |
|
| 234 |
template<typename _CharT> |
| 235 |
struct __numpunct_cache : public locale::facet |
| 236 |
; |
| 237 |
|
| 238 |
template<typename _CharT> |
| 239 |
__numpunct_cache<_CharT>::~__numpunct_cache() |
| 240 |
|
| 241 |
/** |
| 242 |
* @brief Numpunct facet. |
| 243 |
* |
| 244 |
* This facet stores several pieces of information related to printing and |
| 245 |
* scanning numbers, such as the decimal point character. It takes a |
| 246 |
* template parameter specifying the char type. The numpunct facet is |
| 247 |
* used by streams for many I/O operations involving numbers. |
| 248 |
* |
| 249 |
* The numpunct template uses protected virtual functions to provide the |
| 250 |
* actual results. The public accessors forward the call to the virtual |
| 251 |
* functions. These virtual functions are hooks for developers to |
| 252 |
* implement the behavior they require from a numpunct facet. |
| 253 |
*/ |
| 254 |
template<typename _CharT> |
| 255 |
class numpunct : public locale::facet |
| 256 |
; |
| 257 |
|
| 258 |
template<typename _CharT> |
| 259 |
locale::id numpunct<_CharT>::id; |
| 260 |
|
| 261 |
template<> |
| 262 |
numpunct<char>::~numpunct(); |
| 263 |
|
| 264 |
template<> |
| 265 |
void |
| 266 |
numpunct<char>::_M_initialize_numpunct(__c_locale __cloc); |
| 267 |
|
| 268 |
#ifdef _GLIBCXX_USE_WCHAR_T |
| 269 |
template<> |
| 270 |
numpunct<wchar_t>::~numpunct(); |
| 271 |
|
| 272 |
template<> |
| 273 |
void |
| 274 |
numpunct<wchar_t>::_M_initialize_numpunct(__c_locale __cloc); |
| 275 |
#endif |
| 276 |
|
| 277 |
template<typename _CharT> |
| 278 |
class numpunct_byname : public numpunct<_CharT> |
| 279 |
; |
| 280 |
|
| 281 |
/** |
| 282 |
* @brief Facet for parsing number strings. |
| 283 |
* |
| 284 |
* This facet encapsulates the code to parse and return a number |
| 285 |
* from a string. It is used by the istream numeric extraction |
| 286 |
* operators. |
| 287 |
* |
| 288 |
* The num_get template uses protected virtual functions to provide the |
| 289 |
* actual results. The public accessors forward the call to the virtual |
| 290 |
* functions. These virtual functions are hooks for developers to |
| 291 |
* implement the behavior they require from the num_get facet. |
| 292 |
*/ |
| 293 |
template<typename _CharT, typename _InIter> |
| 294 |
class num_get : public locale::facet |
| 295 |
; |
| 296 |
|
| 297 |
template<typename _CharT, typename _InIter> |
| 298 |
locale::id num_get<_CharT, _InIter>::id; |
| 299 |
|
| 300 |
|
| 301 |
/** |
| 302 |
* @brief Facet for converting numbers to strings. |
| 303 |
* |
| 304 |
* This facet encapsulates the code to convert a number to a string. It is |
| 305 |
* used by the ostream numeric insertion operators. |
| 306 |
* |
| 307 |
* The num_put template uses protected virtual functions to provide the |
| 308 |
* actual results. The public accessors forward the call to the virtual |
| 309 |
* functions. These virtual functions are hooks for developers to |
| 310 |
* implement the behavior they require from the num_put facet. |
| 311 |
*/ |
| 312 |
template<typename _CharT, typename _OutIter> |
| 313 |
class num_put : public locale::facet |
| 314 |
; |
| 315 |
|
| 316 |
template <typename _CharT, typename _OutIter> |
| 317 |
locale::id num_put<_CharT, _OutIter>::id; |
| 318 |
|
| 319 |
|
| 320 |
/** |
| 321 |
* @brief Facet for localized string comparison. |
| 322 |
* |
| 323 |
* This facet encapsulates the code to compare strings in a localized |
| 324 |
* manner. |
| 325 |
* |
| 326 |
* The collate template uses protected virtual functions to provide |
| 327 |
* the actual results. The public accessors forward the call to |
| 328 |
* the virtual functions. These virtual functions are hooks for |
| 329 |
* developers to implement the behavior they require from the |
| 330 |
* collate facet. |
| 331 |
*/ |
| 332 |
template<typename _CharT> |
| 333 |
class collate : public locale::facet |
| 334 |
; |
| 335 |
|
| 336 |
template<typename _CharT> |
| 337 |
locale::id collate<_CharT>::id; |
| 338 |
|
| 339 |
// Specializations. |
| 340 |
template<> |
| 341 |
int |
| 342 |
collate<char>::_M_compare(const char*, const char*) const; |
| 343 |
|
| 344 |
template<> |
| 345 |
size_t |
| 346 |
collate<char>::_M_transform(char*, const char*, size_t) const; |
| 347 |
|
| 348 |
#ifdef _GLIBCXX_USE_WCHAR_T |
| 349 |
template<> |
| 350 |
int |
| 351 |
collate<wchar_t>::_M_compare(const wchar_t*, const wchar_t*) const; |
| 352 |
|
| 353 |
template<> |
| 354 |
size_t |
| 355 |
collate<wchar_t>::_M_transform(wchar_t*, const wchar_t*, size_t) const; |
| 356 |
#endif |
| 357 |
|
| 358 |
template<typename _CharT> |
| 359 |
class collate_byname : public collate<_CharT> |
| 360 |
; |
| 361 |
|
| 362 |
|
| 363 |
/** |
| 364 |
* @brief Time format ordering data. |
| 365 |
* |
| 366 |
* This class provides an enum representing different orderings of day, |
| 367 |
* month, and year. |
| 368 |
*/ |
| 369 |
class time_base |
| 370 |
; |
| 371 |
|
| 372 |
template<typename _CharT> |
| 373 |
struct __timepunct_cache : public locale::facet |
| 374 |
; |
| 375 |
|
| 376 |
template<typename _CharT> |
| 377 |
__timepunct_cache<_CharT>::~__timepunct_cache() |
| 378 |
|
| 379 |
// Specializations. |
| 380 |
template<> |
| 381 |
const char* |
| 382 |
__timepunct_cache<char>::_S_timezones[14]; |
| 383 |
|
| 384 |
#ifdef _GLIBCXX_USE_WCHAR_T |
| 385 |
template<> |
| 386 |
const wchar_t* |
| 387 |
__timepunct_cache<wchar_t>::_S_timezones[14]; |
| 388 |
#endif |
| 389 |
|
| 390 |
// Generic. |
| 391 |
template<typename _CharT> |
| 392 |
const _CharT* __timepunct_cache<_CharT>::_S_timezones[14]; |
| 393 |
|
| 394 |
template<typename _CharT> |
| 395 |
class __timepunct : public locale::facet |
| 396 |
; |
| 397 |
|
| 398 |
template<typename _CharT> |
| 399 |
locale::id __timepunct<_CharT>::id; |
| 400 |
|
| 401 |
// Specializations. |
| 402 |
template<> |
| 403 |
void |
| 404 |
__timepunct<char>::_M_initialize_timepunct(__c_locale __cloc); |
| 405 |
|
| 406 |
template<> |
| 407 |
void |
| 408 |
__timepunct<char>::_M_put(char*, size_t, const char*, const tm*) const; |
| 409 |
|
| 410 |
#ifdef _GLIBCXX_USE_WCHAR_T |
| 411 |
template<> |
| 412 |
void |
| 413 |
__timepunct<wchar_t>::_M_initialize_timepunct(__c_locale __cloc); |
| 414 |
|
| 415 |
template<> |
| 416 |
void |
| 417 |
__timepunct<wchar_t>::_M_put(wchar_t*, size_t, const wchar_t*, |
| 418 |
const tm*) const; |
| 419 |
#endif |
| 420 |
|
| 421 |
// Include host and configuration specific timepunct functions. |
| 422 |
#include <bits/time_members.h> |
| 423 |
|
| 424 |
/** |
| 425 |
* @brief Facet for parsing dates and times. |
| 426 |
* |
| 427 |
* This facet encapsulates the code to parse and return a date or |
| 428 |
* time from a string. It is used by the istream numeric |
| 429 |
* extraction operators. |
| 430 |
* |
| 431 |
* The time_get template uses protected virtual functions to provide the |
| 432 |
* actual results. The public accessors forward the call to the virtual |
| 433 |
* functions. These virtual functions are hooks for developers to |
| 434 |
* implement the behavior they require from the time_get facet. |
| 435 |
*/ |
| 436 |
template<typename _CharT, typename _InIter> |
| 437 |
class time_get : public locale::facet, public time_base |
| 438 |
; |
| 439 |
|
| 440 |
template<typename _CharT, typename _InIter> |
| 441 |
locale::id time_get<_CharT, _InIter>::id; |
| 442 |
|
| 443 |
template<typename _CharT, typename _InIter> |
| 444 |
class time_get_byname : public time_get<_CharT, _InIter> |
| 445 |
{ |
| 446 |
public: |
| 447 |
// Types: |
| 448 |
typedef _CharT char_type; |
| 449 |
typedef _InIter iter_type; |
| 450 |
|
| 451 |
explicit |
| 452 |
time_get_byname(const char*, size_t __refs = 0) |
| 453 |
: time_get<_CharT, _InIter>(__refs) { } |
| 454 |
|
| 455 |
protected: |
| 456 |
virtual |
| 457 |
~time_get_byname() { } |
| 458 |
}; |
| 459 |
|
| 460 |
/** |
| 461 |
* @brief Facet for outputting dates and times. |
| 462 |
* |
| 463 |
* This facet encapsulates the code to format and output dates and times |
| 464 |
* according to formats used by strftime(). |
| 465 |
* |
| 466 |
* The time_put template uses protected virtual functions to provide the |
| 467 |
* actual results. The public accessors forward the call to the virtual |
| 468 |
* functions. These virtual functions are hooks for developers to |
| 469 |
* implement the behavior they require from the time_put facet. |
| 470 |
*/ |
| 471 |
template<typename _CharT, typename _OutIter> |
| 472 |
class time_put : public locale::facet |
| 473 |
; |
| 474 |
|
| 475 |
template<typename _CharT, typename _OutIter> |
| 476 |
locale::id time_put<_CharT, _OutIter>::id; |
| 477 |
|
| 478 |
template<typename _CharT, typename _OutIter> |
| 479 |
class time_put_byname : public time_put<_CharT, _OutIter> |
| 480 |
; |
| 481 |
|
| 482 |
|
| 483 |
/** |
| 484 |
* @brief Money format ordering data. |
| 485 |
* |
| 486 |
* This class contains an ordered array of 4 fields to represent the |
| 487 |
* pattern for formatting a money amount. Each field may contain one entry |
| 488 |
* from the part enum. symbol, sign, and value must be present and the |
| 489 |
* remaining field must contain either none or space. @see |
| 490 |
* moneypunct::pos_format() and moneypunct::neg_format() for details of how |
| 491 |
* these fields are interpreted. |
| 492 |
*/ |
| 493 |
class money_base |
| 494 |
; |
| 495 |
|
| 496 |
template<typename _CharT, bool _Intl> |
| 497 |
struct __moneypunct_cache : public locale::facet |
| 498 |
{ |
| 499 |
const char* _M_grouping; |
| 500 |
size_t _M_grouping_size; |
| 501 |
bool _M_use_grouping; |
| 502 |
_CharT _M_decimal_point; |
| 503 |
_CharT _M_thousands_sep; |
| 504 |
const _CharT* _M_curr_symbol; |
| 505 |
size_t _M_curr_symbol_size; |
| 506 |
const _CharT* _M_positive_sign; |
| 507 |
size_t _M_positive_sign_size; |
| 508 |
const _CharT* _M_negative_sign; |
| 509 |
size_t _M_negative_sign_size; |
| 510 |
int _M_frac_digits; |
| 511 |
money_base::pattern _M_pos_format; |
| 512 |
money_base::pattern _M_neg_format; |
| 513 |
|
| 514 |
// A list of valid numeric literals for input and output: in the standard |
| 515 |
// "C" locale, this is "-0123456789". This array contains the chars after |
| 516 |
// having been passed through the current locale's ctype<_CharT>.widen(). |
| 517 |
_CharT _M_atoms[money_base::_S_end]; |
| 518 |
|
| 519 |
bool _M_allocated; |
| 520 |
|
| 521 |
__moneypunct_cache(size_t __refs = 0) : facet(__refs), |
| 522 |
_M_grouping(NULL), _M_grouping_size(0), _M_use_grouping(false), |
| 523 |
_M_decimal_point(_CharT()), _M_thousands_sep(_CharT()), |
| 524 |
_M_curr_symbol(NULL), _M_curr_symbol_size(0), |
| 525 |
_M_positive_sign(NULL), _M_positive_sign_size(0), |
| 526 |
_M_negative_sign(NULL), _M_negative_sign_size(0), |
| 527 |
_M_frac_digits(0), |
| 528 |
_M_pos_format(money_base::pattern()), |
| 529 |
_M_neg_format(money_base::pattern()), _M_allocated(false) |
| 530 |
{ } |
| 531 |
|
| 532 |
~__moneypunct_cache(); |
| 533 |
|
| 534 |
void |
| 535 |
_M_cache(const locale& __loc); |
| 536 |
|
| 537 |
private: |
| 538 |
__moneypunct_cache& |
| 539 |
operator=(const __moneypunct_cache&); |
| 540 |
|
| 541 |
explicit |
| 542 |
__moneypunct_cache(const __moneypunct_cache&); |
| 543 |
}; |
| 544 |
|
| 545 |
template<typename _CharT, bool _Intl> |
| 546 |
__moneypunct_cache<_CharT, _Intl>::~__moneypunct_cache() |
| 547 |
{ |
| 548 |
if (_M_allocated) |
| 549 |
{ |
| 550 |
delete [] _M_grouping; |
| 551 |
delete [] _M_curr_symbol; |
| 552 |
delete [] _M_positive_sign; |
| 553 |
delete [] _M_negative_sign; |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
/** |
| 558 |
* @brief Facet for formatting data for money amounts. |
| 559 |
* |
| 560 |
* This facet encapsulates the punctuation, grouping and other formatting |
| 561 |
* features of money amount string representations. |
| 562 |
*/ |
| 563 |
template<typename _CharT, bool _Intl> |
| 564 |
class moneypunct : public locale::facet, public money_base |
| 565 |
{ |
| 566 |
public: |
| 567 |
// Types: |
| 568 |
//@{ |
| 569 |
/// Public typedefs |
| 570 |
typedef _CharT char_type; |
| 571 |
typedef basic_string<_CharT> string_type; |
| 572 |
//@} |
| 573 |
typedef __moneypunct_cache<_CharT, _Intl> __cache_type; |
| 574 |
|
| 575 |
private: |
| 576 |
__cache_type* _M_data; |
| 577 |
|
| 578 |
public: |
| 579 |
/// This value is provided by the standard, but no reason for its |
| 580 |
/// existence. |
| 581 |
static const bool intl = _Intl; |
| 582 |
/// Numpunct facet id. |
| 583 |
static locale::id id; |
| 584 |
|
| 585 |
/** |
| 586 |
* @brief Constructor performs initialization. |
| 587 |
* |
| 588 |
* This is the constructor provided by the standard. |
| 589 |
* |
| 590 |
* @param refs Passed to the base facet class. |
| 591 |
*/ |
| 592 |
explicit |
| 593 |
moneypunct(size_t __refs = 0) : facet(__refs), _M_data(NULL) |
| 594 |
{ _M_initialize_moneypunct(); } |
| 595 |
|
| 596 |
/** |
| 597 |
* @brief Constructor performs initialization. |
| 598 |
* |
| 599 |
* This is an internal constructor. |
| 600 |
* |
| 601 |
* @param cache Cache for optimization. |
| 602 |
* @param refs Passed to the base facet class. |
| 603 |
*/ |
| 604 |
explicit |
| 605 |
moneypunct(__cache_type* __cache, size_t __refs = 0) |
| 606 |
: facet(__refs), _M_data(__cache) |
| 607 |
{ _M_initialize_moneypunct(); } |
| 608 |
|
| 609 |
/** |
| 610 |
* @brief Internal constructor. Not for general use. |
| 611 |
* |
| 612 |
* This is a constructor for use by the library itself to set up new |
| 613 |
* locales. |
| 614 |
* |
| 615 |
* @param cloc The "C" locale. |
| 616 |
* @param s The name of a locale. |
| 617 |
* @param refs Passed to the base facet class. |
| 618 |
*/ |
| 619 |
explicit |
| 620 |
moneypunct(__c_locale __cloc, const char* __s, size_t __refs = 0) |
| 621 |
: facet(__refs), _M_data(NULL) |
| 622 |
{ _M_initialize_moneypunct(__cloc, __s); } |
| 623 |
|
| 624 |
/** |
| 625 |
* @brief Return decimal point character. |
| 626 |
* |
| 627 |
* This function returns a char_type to use as a decimal point. It |
| 628 |
* does so by returning returning |
| 629 |
* moneypunct<char_type>::do_decimal_point(). |
| 630 |
* |
| 631 |
* @return @a char_type representing a decimal point. |
| 632 |
*/ |
| 633 |
char_type |
| 634 |
decimal_point() const |
| 635 |
{ return this->do_decimal_point(); } |
| 636 |
|
| 637 |
/** |
| 638 |
* @brief Return thousands separator character. |
| 639 |
* |
| 640 |
* This function returns a char_type to use as a thousands |
| 641 |
* separator. It does so by returning returning |
| 642 |
* moneypunct<char_type>::do_thousands_sep(). |
| 643 |
* |
| 644 |
* @return char_type representing a thousands separator. |
| 645 |
*/ |
| 646 |
char_type |
| 647 |
thousands_sep() const |
| 648 |
{ return this->do_thousands_sep(); } |
| 649 |
|
| 650 |
/** |
| 651 |
* @brief Return grouping specification. |
| 652 |
* |
| 653 |
* This function returns a string representing groupings for the |
| 654 |
* integer part of an amount. Groupings indicate where thousands |
| 655 |
* separators should be inserted. |
| 656 |
* |
| 657 |
* Each char in the return string is interpret as an integer rather |
| 658 |
* than a character. These numbers represent the number of digits in a |
| 659 |
* group. The first char in the string represents the number of digits |
| 660 |
* in the least significant group. If a char is negative, it indicates |
| 661 |
* an unlimited number of digits for the group. If more chars from the |
| 662 |
* string are required to group a number, the last char is used |
| 663 |
* repeatedly. |
| 664 |
* |
| 665 |
* For example, if the grouping() returns "\003\002" and is applied to |
| 666 |
* the number 123456789, this corresponds to 12,34,56,789. Note that |
| 667 |
* if the string was "32", this would put more than 50 digits into the |
| 668 |
* least significant group if the character set is ASCII. |
| 669 |
* |
| 670 |
* The string is returned by calling |
| 671 |
* moneypunct<char_type>::do_grouping(). |
| 672 |
* |
| 673 |
* @return string representing grouping specification. |
| 674 |
*/ |
| 675 |
string |
| 676 |
grouping() const |
| 677 |
{ return this->do_grouping(); } |
| 678 |
|
| 679 |
/** |
| 680 |
* @brief Return currency symbol string. |
| 681 |
* |
| 682 |
* This function returns a string_type to use as a currency symbol. It |
| 683 |
* does so by returning returning |
| 684 |
* moneypunct<char_type>::do_curr_symbol(). |
| 685 |
* |
| 686 |
* @return @a string_type representing a currency symbol. |
| 687 |
*/ |
| 688 |
string_type |
| 689 |
curr_symbol() const |
| 690 |
{ return this->do_curr_symbol(); } |
| 691 |
|
| 692 |
/** |
| 693 |
* @brief Return positive sign string. |
| 694 |
* |
| 695 |
* This function returns a string_type to use as a sign for positive |
| 696 |
* amounts. It does so by returning returning |
| 697 |
* moneypunct<char_type>::do_positive_sign(). |
| 698 |
* |
| 699 |
* If the return value contains more than one character, the first |
| 700 |
* character appears in the position indicated by pos_format() and the |
| 701 |
* remainder appear at the end of the formatted string. |
| 702 |
* |
| 703 |
* @return @a string_type representing a positive sign. |
| 704 |
*/ |
| 705 |
string_type |
| 706 |
positive_sign() const |
| 707 |
{ return this->do_positive_sign(); } |
| 708 |
|
| 709 |
/** |
| 710 |
* @brief Return negative sign string. |
| 711 |
* |
| 712 |
* This function returns a string_type to use as a sign for negative |
| 713 |
* amounts. It does so by returning returning |
| 714 |
* moneypunct<char_type>::do_negative_sign(). |
| 715 |
* |
| 716 |
* If the return value contains more than one character, the first |
| 717 |
* character appears in the position indicated by neg_format() and the |
| 718 |
* remainder appear at the end of the formatted string. |
| 719 |
* |
| 720 |
* @return @a string_type representing a negative sign. |
| 721 |
*/ |
| 722 |
string_type |
| 723 |
negative_sign() const |
| 724 |
{ return this->do_negative_sign(); } |
| 725 |
|
| 726 |
/** |
| 727 |
* @brief Return number of digits in fraction. |
| 728 |
* |
| 729 |
* This function returns the exact number of digits that make up the |
| 730 |
* fractional part of a money amount. It does so by returning |
| 731 |
* returning moneypunct<char_type>::do_frac_digits(). |
| 732 |
* |
| 733 |
* The fractional part of a money amount is optional. But if it is |
| 734 |
* present, there must be frac_digits() digits. |
| 735 |
* |
| 736 |
* @return Number of digits in amount fraction. |
| 737 |
*/ |
| 738 |
int |
| 739 |
frac_digits() const |
| 740 |
{ return this->do_frac_digits(); } |
| 741 |
|
| 742 |
//@{ |
| 743 |
/** |
| 744 |
* @brief Return pattern for money values. |
| 745 |
* |
| 746 |
* This function returns a pattern describing the formatting of a |
| 747 |
* positive or negative valued money amount. It does so by returning |
| 748 |
* returning moneypunct<char_type>::do_pos_format() or |
| 749 |
* moneypunct<char_type>::do_neg_format(). |
| 750 |
* |
| 751 |
* The pattern has 4 fields describing the ordering of symbol, sign, |
| 752 |
* value, and none or space. There must be one of each in the pattern. |
| 753 |
* The none and space enums may not appear in the first field and space |
| 754 |
* may not appear in the final field. |
| 755 |
* |
| 756 |
* The parts of a money string must appear in the order indicated by |
| 757 |
* the fields of the pattern. The symbol field indicates that the |
| 758 |
* value of curr_symbol() may be present. The sign field indicates |
| 759 |
* that the value of positive_sign() or negative_sign() must be |
| 760 |
* present. The value field indicates that the absolute value of the |
| 761 |
* money amount is present. none indicates 0 or more whitespace |
| 762 |
* characters, except at the end, where it permits no whitespace. |
| 763 |
* space indicates that 1 or more whitespace characters must be |
| 764 |
* present. |
| 765 |
* |
| 766 |
* For example, for the US locale and pos_format() pattern |
| 767 |
* {symbol,sign,value,none}, curr_symbol() == '$' positive_sign() == |
| 768 |
* '+', and value 10.01, and options set to force the symbol, the |
| 769 |
* corresponding string is "$+10.01". |
| 770 |
* |
| 771 |
* @return Pattern for money values. |
| 772 |
*/ |
| 773 |
pattern |
| 774 |
pos_format() const |
| 775 |
{ return this->do_pos_format(); } |
| 776 |
|
| 777 |
pattern |
| 778 |
neg_format() const |
| 779 |
{ return this->do_neg_format(); } |
| 780 |
//@} |
| 781 |
|
| 782 |
protected: |
| 783 |
/// Destructor. |
| 784 |
virtual |
| 785 |
~moneypunct(); |
| 786 |
|
| 787 |
/** |
| 788 |
* @brief Return decimal point character. |
| 789 |
* |
| 790 |
* Returns a char_type to use as a decimal point. This function is a |
| 791 |
* hook for derived classes to change the value returned. |
| 792 |
* |
| 793 |
* @return @a char_type representing a decimal point. |
| 794 |
*/ |
| 795 |
virtual char_type |
| 796 |
do_decimal_point() const |
| 797 |
{ return _M_data->_M_decimal_point; } |
| 798 |
|
| 799 |
/** |
| 800 |
* @brief Return thousands separator character. |
| 801 |
* |
| 802 |
* Returns a char_type to use as a thousands separator. This function |
| 803 |
* is a hook for derived classes to change the value returned. |
| 804 |
* |
| 805 |
* @return @a char_type representing a thousands separator. |
| 806 |
*/ |
| 807 |
virtual char_type |
| 808 |
do_thousands_sep() const |
| 809 |
{ return _M_data->_M_thousands_sep; } |
| 810 |
|
| 811 |
/** |
| 812 |
* @brief Return grouping specification. |
| 813 |
* |
| 814 |
* Returns a string representing groupings for the integer part of a |
| 815 |
* number. This function is a hook for derived classes to change the |
| 816 |
* value returned. @see grouping() for details. |
| 817 |
* |
| 818 |
* @return String representing grouping specification. |
| 819 |
*/ |
| 820 |
virtual string |
| 821 |
do_grouping() const |
| 822 |
{ return _M_data->_M_grouping; } |
| 823 |
|
| 824 |
/** |
| 825 |
* @brief Return currency symbol string. |
| 826 |
* |
| 827 |
* This function returns a string_type to use as a currency symbol. |
| 828 |
* This function is a hook for derived classes to change the value |
| 829 |
* returned. @see curr_symbol() for details. |
| 830 |
* |
| 831 |
* @return @a string_type representing a currency symbol. |
| 832 |
*/ |
| 833 |
virtual string_type |
| 834 |
do_curr_symbol() const |
| 835 |
{ return _M_data->_M_curr_symbol; } |
| 836 |
|
| 837 |
/** |
| 838 |
* @brief Return positive sign string. |
| 839 |
* |
| 840 |
* This function returns a string_type to use as a sign for positive |
| 841 |
* amounts. This function is a hook for derived classes to change the |
| 842 |
* value returned. @see positive_sign() for details. |
| 843 |
* |
| 844 |
* @return @a string_type representing a positive sign. |
| 845 |
*/ |
| 846 |
virtual string_type |
| 847 |
do_positive_sign() const |
| 848 |
{ return _M_data->_M_positive_sign; } |
| 849 |
|
| 850 |
/** |
| 851 |
* @brief Return negative sign string. |
| 852 |
* |
| 853 |
* This function returns a string_type to use as a sign for negative |
| 854 |
* amounts. This function is a hook for derived classes to change the |
| 855 |
* value returned. @see negative_sign() for details. |
| 856 |
* |
| 857 |
* @return @a string_type representing a negative sign. |
| 858 |
*/ |
| 859 |
virtual string_type |
| 860 |
do_negative_sign() const |
| 861 |
{ return _M_data->_M_negative_sign; } |
| 862 |
|
| 863 |
/** |
| 864 |
* @brief Return number of digits in fraction. |
| 865 |
* |
| 866 |
* This function returns the exact number of digits that make up the |
| 867 |
* fractional part of a money amount. This function is a hook for |
| 868 |
* derived classes to change the value returned. @see frac_digits() |
| 869 |
* for details. |
| 870 |
* |
| 871 |
* @return Number of digits in amount fraction. |
| 872 |
*/ |
| 873 |
virtual int |
| 874 |
do_frac_digits() const |
| 875 |
{ return _M_data->_M_frac_digits; } |
| 876 |
|
| 877 |
/** |
| 878 |
* @brief Return pattern for money values. |
| 879 |
* |
| 880 |
* This function returns a pattern describing the formatting of a |
| 881 |
* positive valued money amount. This function is a hook for derived |
| 882 |
* classes to change the value returned. @see pos_format() for |
| 883 |
* details. |
| 884 |
* |
| 885 |
* @return Pattern for money values. |
| 886 |
*/ |
| 887 |
virtual pattern |
| 888 |
do_pos_format() const |
| 889 |
{ return _M_data->_M_pos_format; } |
| 890 |
|
| 891 |
/** |
| 892 |
* @brief Return pattern for money values. |
| 893 |
* |
| 894 |
* This function returns a pattern describing the formatting of a |
| 895 |
* negative valued money amount. This function is a hook for derived |
| 896 |
* classes to change the value returned. @see neg_format() for |
| 897 |
* details. |
| 898 |
* |
| 899 |
* @return Pattern for money values. |
| 900 |
*/ |
| 901 |
virtual pattern |
| 902 |
do_neg_format() const |
| 903 |
{ return _M_data->_M_neg_format; } |
| 904 |
|
| 905 |
// For use at construction time only. |
| 906 |
void |
| 907 |
_M_initialize_moneypunct(__c_locale __cloc = NULL, |
| 908 |
const char* __name = NULL); |
| 909 |
}; |
| 910 |
|
| 911 |
template<typename _CharT, bool _Intl> |
| 912 |
locale::id moneypunct<_CharT, _Intl>::id; |
| 913 |
|
| 914 |
template<typename _CharT, bool _Intl> |
| 915 |
const bool moneypunct<_CharT, _Intl>::intl; |
| 916 |
|
| 917 |
template<> |
| 918 |
moneypunct<char, true>::~moneypunct(); |
| 919 |
|
| 920 |
template<> |
| 921 |
moneypunct<char, false>::~moneypunct(); |
| 922 |
|
| 923 |
template<> |
| 924 |
void |
| 925 |
moneypunct<char, true>::_M_initialize_moneypunct(__c_locale, const char*); |
| 926 |
|
| 927 |
template<> |
| 928 |
void |
| 929 |
moneypunct<char, false>::_M_initialize_moneypunct(__c_locale, const char*); |
| 930 |
|
| 931 |
#ifdef _GLIBCXX_USE_WCHAR_T |
| 932 |
template<> |
| 933 |
moneypunct<wchar_t, true>::~moneypunct(); |
| 934 |
|
| 935 |
template<> |
| 936 |
moneypunct<wchar_t, false>::~moneypunct(); |
| 937 |
|
| 938 |
template<> |
| 939 |
void |
| 940 |
moneypunct<wchar_t, true>::_M_initialize_moneypunct(__c_locale, |
| 941 |
const char*); |
| 942 |
|
| 943 |
template<> |
| 944 |
void |
| 945 |
moneypunct<wchar_t, false>::_M_initialize_moneypunct(__c_locale, |
| 946 |
const char*); |
| 947 |
#endif |
| 948 |
|
| 949 |
template<typename _CharT, bool _Intl> |
| 950 |
class moneypunct_byname : public moneypunct<_CharT, _Intl> |
| 951 |
{ |
| 952 |
public: |
| 953 |
typedef _CharT char_type; |
| 954 |
typedef basic_string<_CharT> string_type; |
| 955 |
|
| 956 |
static const bool intl = _Intl; |
| 957 |
|
| 958 |
explicit |
| 959 |
moneypunct_byname(const char* __s, size_t __refs = 0) |
| 960 |
: moneypunct<_CharT, _Intl>(__refs) |
| 961 |
{ |
| 962 |
if (std::strcmp(__s, "C") != 0 && std::strcmp(__s, "POSIX") != 0) |
| 963 |
{ |
| 964 |
__c_locale __tmp; |
| 965 |
this->_S_create_c_locale(__tmp, __s); |
| 966 |
this->_M_initialize_moneypunct(__tmp); |
| 967 |
this->_S_destroy_c_locale(__tmp); |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
protected: |
| 972 |
virtual |
| 973 |
~moneypunct_byname() { } |
| 974 |
}; |
| 975 |
|
| 976 |
template<typename _CharT, bool _Intl> |
| 977 |
const bool moneypunct_byname<_CharT, _Intl>::intl; |
| 978 |
|
| 979 |
/** |
| 980 |
* @brief Facet for parsing monetary amounts. |
| 981 |
* |
| 982 |
* This facet encapsulates the code to parse and return a monetary |
| 983 |
* amount from a string. |
| 984 |
* |
| 985 |
* The money_get template uses protected virtual functions to |
| 986 |
* provide the actual results. The public accessors forward the |
| 987 |
* call to the virtual functions. These virtual functions are |
| 988 |
* hooks for developers to implement the behavior they require from |
| 989 |
* the money_get facet. |
| 990 |
*/ |
| 991 |
template<typename _CharT, typename _InIter> |
| 992 |
class money_get : public locale::facet |
| 993 |
{ |
| 994 |
public: |
| 995 |
// Types: |
| 996 |
//@{ |
| 997 |
/// Public typedefs |
| 998 |
typedef _CharT char_type; |
| 999 |
typedef _InIter iter_type; |
| 1000 |
typedef basic_string<_CharT> string_type; |
| 1001 |
//@} |
| 1002 |
|
| 1003 |
/// Numpunct facet id. |
| 1004 |
static locale::id id; |
| 1005 |
|
| 1006 |
/** |
| 1007 |
* @brief Constructor performs initialization. |
| 1008 |
* |
| 1009 |
* This is the constructor provided by the standard. |
| 1010 |
* |
| 1011 |
* @param refs Passed to the base facet class. |
| 1012 |
*/ |
| 1013 |
explicit |
| 1014 |
money_get(size_t __refs = 0) : facet(__refs) { } |
| 1015 |
|
| 1016 |
/** |
| 1017 |
* @brief Read and parse a monetary value. |
| 1018 |
* |
| 1019 |
* This function reads characters from @a s, interprets them as a |
| 1020 |
* monetary value according to moneypunct and ctype facets retrieved |
| 1021 |
* from io.getloc(), and returns the result in @a units as an integral |
| 1022 |
* value moneypunct::frac_digits() * the actual amount. For example, |
| 1023 |
* the string $10.01 in a US locale would store 1001 in @a units. |
| 1024 |
* |
| 1025 |
* Any characters not part of a valid money amount are not consumed. |
| 1026 |
* |
| 1027 |
* If a money value cannot be parsed from the input stream, sets |
| 1028 |
* err=(err|io.failbit). If the stream is consumed before finishing |
| 1029 |
* parsing, sets err=(err|io.failbit|io.eofbit). @a units is |
| 1030 |
* unchanged if parsing fails. |
| 1031 |
* |
| 1032 |
* This function works by returning the result of do_get(). |
| 1033 |
* |
| 1034 |
* @param s Start of characters to parse. |
| 1035 |
* @param end End of characters to parse. |
| 1036 |
* @param intl Parameter to use_facet<moneypunct<CharT,intl> >. |
| 1037 |
* @param io Source of facets and io state. |
| 1038 |
* @param err Error field to set if parsing fails. |
| 1039 |
* @param units Place to store result of parsing. |
| 1040 |
* @return Iterator referencing first character beyond valid money |
| 1041 |
* amount. |
| 1042 |
*/ |
| 1043 |
iter_type |
| 1044 |
get(iter_type __s, iter_type __end, bool __intl, ios_base& __io, |
| 1045 |
ios_base::iostate& __err, long double& __units) const |
| 1046 |
{ return this->do_get(__s, __end, __intl, __io, __err, __units); } |
| 1047 |
|
| 1048 |
/** |
| 1049 |
* @brief Read and parse a monetary value. |
| 1050 |
* |
| 1051 |
* This function reads characters from @a s, interprets them as a |
| 1052 |
* monetary value according to moneypunct and ctype facets retrieved |
| 1053 |
* from io.getloc(), and returns the result in @a digits. For example, |
| 1054 |
* the string $10.01 in a US locale would store "1001" in @a digits. |
| 1055 |
* |
| 1056 |
* Any characters not part of a valid money amount are not consumed. |
| 1057 |
* |
| 1058 |
* If a money value cannot be parsed from the input stream, sets |
| 1059 |
* err=(err|io.failbit). If the stream is consumed before finishing |
| 1060 |
* parsing, sets err=(err|io.failbit|io.eofbit). |
| 1061 |
* |
| 1062 |
* This function works by returning the result of do_get(). |
| 1063 |
* |
| 1064 |
* @param s Start of characters to parse. |
| 1065 |
* @param end End of characters to parse. |
| 1066 |
* @param intl Parameter to use_facet<moneypunct<CharT,intl> >. |
| 1067 |
* @param io Source of facets and io state. |
| 1068 |
* @param err Error field to set if parsing fails. |
| 1069 |
* @param digits Place to store result of parsing. |
| 1070 |
* @return Iterator referencing first character beyond valid money |
| 1071 |
* amount. |
| 1072 |
*/ |
| 1073 |
iter_type |
| 1074 |
get(iter_type __s, iter_type __end, bool __intl, ios_base& __io, |
| 1075 |
ios_base::iostate& __err, string_type& __digits) const |
| 1076 |
{ return this->do_get(__s, __end, __intl, __io, __err, __digits); } |
| 1077 |
|
| 1078 |
protected: |
| 1079 |
/// Destructor. |
| 1080 |
virtual |
| 1081 |
~money_get() { } |
| 1082 |
|
| 1083 |
/** |
| 1084 |
* @brief Read and parse a monetary value. |
| 1085 |
* |
| 1086 |
* This function reads and parses characters representing a monetary |
| 1087 |
* value. This function is a hook for derived classes to change the |
| 1088 |
* value returned. @see get() for details. |
| 1089 |
*/ |
| 1090 |
virtual iter_type |
| 1091 |
do_get(iter_type __s, iter_type __end, bool __intl, ios_base& __io, |
| 1092 |
ios_base::iostate& __err, long double& __units) const; |
| 1093 |
|
| 1094 |
/** |
| 1095 |
* @brief Read and parse a monetary value. |
| 1096 |
* |
| 1097 |
* This function reads and parses characters representing a monetary |
| 1098 |
* value. This function is a hook for derived classes to change the |
| 1099 |
* value returned. @see get() for details. |
| 1100 |
*/ |
| 1101 |
virtual iter_type |
| 1102 |
do_get(iter_type __s, iter_type __end, bool __intl, ios_base& __io, |
| 1103 |
ios_base::iostate& __err, string_type& __digits) const; |
| 1104 |
|
| 1105 |
template<bool _Intl> |
| 1106 |
iter_type |
| 1107 |
_M_extract(iter_type __s, iter_type __end, ios_base& __io, |
| 1108 |
ios_base::iostate& __err, string& __digits) const; |
| 1109 |
}; |
| 1110 |
|
| 1111 |
template<typename _CharT, typename _InIter> |
| 1112 |
locale::id money_get<_CharT, _InIter>::id; |
| 1113 |
|
| 1114 |
/** |
| 1115 |
* @brief Facet for outputting monetary amounts. |
| 1116 |
* |
| 1117 |
* This facet encapsulates the code to format and output a monetary |
| 1118 |
* amount. |
| 1119 |
* |
| 1120 |
* The money_put template uses protected virtual functions to |
| 1121 |
* provide the actual results. The public accessors forward the |
| 1122 |
* call to the virtual functions. These virtual functions are |
| 1123 |
* hooks for developers to implement the behavior they require from |
| 1124 |
* the money_put facet. |
| 1125 |
*/ |
| 1126 |
template<typename _CharT, typename _OutIter> |
| 1127 |
class money_put : public locale::facet |
| 1128 |
{ |
| 1129 |
public: |
| 1130 |
//@{ |
| 1131 |
/// Public typedefs |
| 1132 |
typedef _CharT char_type; |
| 1133 |
typedef _OutIter iter_type; |
| 1134 |
typedef basic_string<_CharT> string_type; |
| 1135 |
//@} |
| 1136 |
|
| 1137 |
/// Numpunct facet id. |
| 1138 |
static locale::id id; |
| 1139 |
|
| 1140 |
/** |
| 1141 |
* @brief Constructor performs initialization. |
| 1142 |
* |
| 1143 |
* This is the constructor provided by the standard. |
| 1144 |
* |
| 1145 |
* @param refs Passed to the base facet class. |
| 1146 |
*/ |
| 1147 |
explicit |
| 1148 |
money_put(size_t __refs = 0) : facet(__refs) { } |
| 1149 |
|
| 1150 |
/** |
| 1151 |
* @brief Format and output a monetary value. |
| 1152 |
* |
| 1153 |
* This function formats @a units as a monetary value according to |
| 1154 |
* moneypunct and ctype facets retrieved from io.getloc(), and writes |
| 1155 |
* the resulting characters to @a s. For example, the value 1001 in a |
| 1156 |
* US locale would write "$10.01" to @a s. |
| 1157 |
* |
| 1158 |
* This function works by returning the result of do_put(). |
| 1159 |
* |
| 1160 |
* @param s The stream to write to. |
| 1161 |
* @param intl Parameter to use_facet<moneypunct<CharT,intl> >. |
| 1162 |
* @param io Source of facets and io state. |
| 1163 |
* @param fill char_type to use for padding. |
| 1164 |
* @param units Place to store result of parsing. |
| 1165 |
* @return Iterator after writing. |
| 1166 |
*/ |
| 1167 |
iter_type |
| 1168 |
put(iter_type __s, bool __intl, ios_base& __io, |
| 1169 |
char_type __fill, long double __units) const |
| 1170 |
{ return this->do_put(__s, __intl, __io, __fill, __units); } |
| 1171 |
|
| 1172 |
/** |
| 1173 |
* @brief Format and output a monetary value. |
| 1174 |
* |
| 1175 |
* This function formats @a digits as a monetary value according to |
| 1176 |
* moneypunct and ctype facets retrieved from io.getloc(), and writes |
| 1177 |
* the resulting characters to @a s. For example, the string "1001" in |
| 1178 |
* a US locale would write "$10.01" to @a s. |
| 1179 |
* |
| 1180 |
* This function works by returning the result of do_put(). |
| 1181 |
* |
| 1182 |
* @param s The stream to write to. |
| 1183 |
* @param intl Parameter to use_facet<moneypunct<CharT,intl> >. |
| 1184 |
* @param io Source of facets and io state. |
| 1185 |
* @param fill char_type to use for padding. |
| 1186 |
* @param units Place to store result of parsing. |
| 1187 |
* @return Iterator after writing. |
| 1188 |
*/ |
| 1189 |
iter_type |
| 1190 |
put(iter_type __s, bool __intl, ios_base& __io, |
| 1191 |
char_type __fill, const string_type& __digits) const |
| 1192 |
{ return this->do_put(__s, __intl, __io, __fill, __digits); } |
| 1193 |
|
| 1194 |
protected: |
| 1195 |
/// Destructor. |
| 1196 |
virtual |
| 1197 |
~money_put() { } |
| 1198 |
|
| 1199 |
/** |
| 1200 |
* @brief Format and output a monetary value. |
| 1201 |
* |
| 1202 |
* This function formats @a units as a monetary value according to |
| 1203 |
* moneypunct and ctype facets retrieved from io.getloc(), and writes |
| 1204 |
* the resulting characters to @a s. For example, the value 1001 in a |
| 1205 |
* US locale would write "$10.01" to @a s. |
| 1206 |
* |
| 1207 |
* This function is a hook for derived classes to change the value |
| 1208 |
* returned. @see put(). |
| 1209 |
* |
| 1210 |
* @param s The stream to write to. |
| 1211 |
* @param intl Parameter to use_facet<moneypunct<CharT,intl> >. |
| 1212 |
* @param io Source of facets and io state. |
| 1213 |
* @param fill char_type to use for padding. |
| 1214 |
* @param units Place to store result of parsing. |
| 1215 |
* @return Iterator after writing. |
| 1216 |
*/ |
| 1217 |
virtual iter_type |
| 1218 |
do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill, |
| 1219 |
long double __units) const; |
| 1220 |
|
| 1221 |
/** |
| 1222 |
* @brief Format and output a monetary value. |
| 1223 |
* |
| 1224 |
* This function formats @a digits as a monetary value according to |
| 1225 |
* moneypunct and ctype facets retrieved from io.getloc(), and writes |
| 1226 |
* the resulting characters to @a s. For example, the string "1001" in |
| 1227 |
* a US locale would write "$10.01" to @a s. |
| 1228 |
* |
| 1229 |
* This function is a hook for derived classes to change the value |
| 1230 |
* returned. @see put(). |
| 1231 |
* |
| 1232 |
* @param s The stream to write to. |
| 1233 |
* @param intl Parameter to use_facet<moneypunct<CharT,intl> >. |
| 1234 |
* @param io Source of facets and io state. |
| 1235 |
* @param fill char_type to use for padding. |
| 1236 |
* @param units Place to store result of parsing. |
| 1237 |
* @return Iterator after writing. |
| 1238 |
*/ |
| 1239 |
virtual iter_type |
| 1240 |
do_put(iter_type __s, bool __intl, ios_base& __io, char_type __fill, |
| 1241 |
const string_type& __digits) const; |
| 1242 |
|
| 1243 |
template<bool _Intl> |
| 1244 |
iter_type |
| 1245 |
_M_insert(iter_type __s, ios_base& __io, char_type __fill, |
| 1246 |
const string_type& __digits) const; |
| 1247 |
}; |
| 1248 |
|
| 1249 |
template<typename _CharT, typename _OutIter> |
| 1250 |
locale::id money_put<_CharT, _OutIter>::id; |
| 1251 |
|
| 1252 |
/** |
| 1253 |
* @brief Messages facet base class providing catalog typedef. |
| 1254 |
*/ |
| 1255 |
struct messages_base |
| 1256 |
{ |
| 1257 |
typedef int catalog; |
| 1258 |
}; |
| 1259 |
|
| 1260 |
/** |
| 1261 |
* @brief Facet for handling message catalogs |
| 1262 |
* |
| 1263 |
* This facet encapsulates the code to retrieve messages from |
| 1264 |
* message catalogs. The only thing defined by the standard for this facet |
| 1265 |
* is the interface. All underlying functionality is |
| 1266 |
* implementation-defined. |
| 1267 |
* |
| 1268 |
* This library currently implements 3 versions of the message facet. The |
| 1269 |
* first version (gnu) is a wrapper around gettext, provided by libintl. |
| 1270 |
* The second version (ieee) is a wrapper around catgets. The final |
| 1271 |
* version (default) does no actual translation. These implementations are |
| 1272 |
* only provided for char and wchar_t instantiations. |
| 1273 |
* |
| 1274 |
* The messages template uses protected virtual functions to |
| 1275 |
* provide the actual results. The public accessors forward the |
| 1276 |
* call to the virtual functions. These virtual functions are |
| 1277 |
* hooks for developers to implement the behavior they require from |
| 1278 |
* the messages facet. |
| 1279 |
*/ |
| 1280 |
template<typename _CharT> |
| 1281 |
class messages : public locale::facet, public messages_base |
| 1282 |
{ |
| 1283 |
public: |
| 1284 |
// Types: |
| 1285 |
//@{ |
| 1286 |
/// Public typedefs |
| 1287 |
typedef _CharT char_type; |
| 1288 |
typedef basic_string<_CharT> string_type; |
| 1289 |
//@} |
| 1290 |
|
| 1291 |
protected: |
| 1292 |
// Underlying "C" library locale information saved from |
| 1293 |
// initialization, needed by messages_byname as well. |
| 1294 |
__c_locale _M_c_locale_messages; |
| 1295 |
const char* _M_name_messages; |
| 1296 |
|
| 1297 |
public: |
| 1298 |
/// Numpunct facet id. |
| 1299 |
static locale::id id; |
| 1300 |
|
| 1301 |
/** |
| 1302 |
* @brief Constructor performs initialization. |
| 1303 |
* |
| 1304 |
* This is the constructor provided by the standard. |
| 1305 |
* |
| 1306 |
* @param refs Passed to the base facet class. |
| 1307 |
*/ |
| 1308 |
explicit |
| 1309 |
messages(size_t __refs = 0); |
| 1310 |
|
| 1311 |
// Non-standard. |
| 1312 |
/** |
| 1313 |
* @brief Internal constructor. Not for general use. |
| 1314 |
* |
| 1315 |
* This is a constructor for use by the library itself to set up new |
| 1316 |
* locales. |
| 1317 |
* |
| 1318 |
* @param cloc The "C" locale. |
| 1319 |
* @param s The name of a locale. |
| 1320 |
* @param refs Refcount to pass to the base class. |
| 1321 |
*/ |
| 1322 |
explicit |
| 1323 |
messages(__c_locale __cloc, const char* __s, size_t __refs = 0); |
| 1324 |
|
| 1325 |
/* |
| 1326 |
* @brief Open a message catalog. |
| 1327 |
* |
| 1328 |
* This function opens and returns a handle to a message catalog by |
| 1329 |
* returning do_open(s, loc). |
| 1330 |
* |
| 1331 |
* @param s The catalog to open. |
| 1332 |
* @param loc Locale to use for character set conversions. |
| 1333 |
* @return Handle to the catalog or value < 0 if open fails. |
| 1334 |
*/ |
| 1335 |
catalog |
| 1336 |
open(const basic_string<char>& __s, const locale& __loc) const |
| 1337 |
{ return this->do_open(__s, __loc); } |
| 1338 |
|
| 1339 |
// Non-standard and unorthodox, yet effective. |
| 1340 |
/* |
| 1341 |
* @brief Open a message catalog. |
| 1342 |
* |
| 1343 |
* This non-standard function opens and returns a handle to a message |
| 1344 |
* catalog by returning do_open(s, loc). The third argument provides a |
| 1345 |
* message catalog root directory for gnu gettext and is ignored |
| 1346 |
* otherwise. |
| 1347 |
* |
| 1348 |
* @param s The catalog to open. |
| 1349 |
* @param loc Locale to use for character set conversions. |
| 1350 |
* @param dir Message catalog root directory. |
| 1351 |
* @return Handle to the catalog or value < 0 if open fails. |
| 1352 |
*/ |
| 1353 |
catalog |
| 1354 |
open(const basic_string<char>&, const locale&, const char*) const; |
| 1355 |
|
| 1356 |
/* |
| 1357 |
* @brief Look up a string in a message catalog. |
| 1358 |
* |
| 1359 |
* This function retrieves and returns a message from a catalog by |
| 1360 |
* returning do_get(c, set, msgid, s). |
| 1361 |
* |
| 1362 |
* For gnu, @a set and @a msgid are ignored. Returns gettext(s). |
| 1363 |
* For default, returns s. For ieee, returns catgets(c,set,msgid,s). |
| 1364 |
* |
| 1365 |
* @param c The catalog to access. |
| 1366 |
* @param set Implementation-defined. |
| 1367 |
* @param msgid Implementation-defined. |
| 1368 |
* @param s Default return value if retrieval fails. |
| 1369 |
* @return Retrieved message or @a s if get fails. |
| 1370 |
*/ |
| 1371 |
string_type |
| 1372 |
get(catalog __c, int __set, int __msgid, const string_type& __s) const |
| 1373 |
{ return this->do_get(__c, __set, __msgid, __s); } |
| 1374 |
|
| 1375 |
/* |
| 1376 |
* @brief Close a message catalog. |
| 1377 |
* |
| 1378 |
* Closes catalog @a c by calling do_close(c). |
| 1379 |
* |
| 1380 |
* @param c The catalog to close. |
| 1381 |
*/ |
| 1382 |
void |
| 1383 |
close(catalog __c) const |
| 1384 |
{ return this->do_close(__c); } |
| 1385 |
|
| 1386 |
protected: |
| 1387 |
/// Destructor. |
| 1388 |
virtual |
| 1389 |
~messages(); |
| 1390 |
|
| 1391 |
/* |
| 1392 |
* @brief Open a message catalog. |
| 1393 |
* |
| 1394 |
* This function opens and returns a handle to a message catalog in an |
| 1395 |
* implementation-defined manner. This function is a hook for derived |
| 1396 |
* classes to change the value returned. |
| 1397 |
* |
| 1398 |
* @param s The catalog to open. |
| 1399 |
* @param loc Locale to use for character set conversions. |
| 1400 |
* @return Handle to the opened catalog, value < 0 if open failed. |
| 1401 |
*/ |
| 1402 |
virtual catalog |
| 1403 |
do_open(const basic_string<char>&, const locale&) const; |
| 1404 |
|
| 1405 |
/* |
| 1406 |
* @brief Look up a string in a message catalog. |
| 1407 |
* |
| 1408 |
* This function retrieves and returns a message from a catalog in an |
| 1409 |
* implementation-defined manner. This function is a hook for derived |
| 1410 |
* classes to change the value returned. |
| 1411 |
* |
| 1412 |
* For gnu, @a set and @a msgid are ignored. Returns gettext(s). |
| 1413 |
* For default, returns s. For ieee, returns catgets(c,set,msgid,s). |
| 1414 |
* |
| 1415 |
* @param c The catalog to access. |
| 1416 |
* @param set Implementation-defined. |
| 1417 |
* @param msgid Implementation-defined. |
| 1418 |
* @param s Default return value if retrieval fails. |
| 1419 |
* @return Retrieved message or @a s if get fails. |
| 1420 |
*/ |
| 1421 |
virtual string_type |
| 1422 |
do_get(catalog, int, int, const string_type& __dfault) const; |
| 1423 |
|
| 1424 |
/* |
| 1425 |
* @brief Close a message catalog. |
| 1426 |
* |
| 1427 |
* @param c The catalog to close. |
| 1428 |
*/ |
| 1429 |
virtual void |
| 1430 |
do_close(catalog) const; |
| 1431 |
|
| 1432 |
// Returns a locale and codeset-converted string, given a char* message. |
| 1433 |
char* |
| 1434 |
_M_convert_to_char(const string_type& __msg) const |
| 1435 |
{ |
| 1436 |
// XXX |
| 1437 |
return reinterpret_cast<char*>(const_cast<_CharT*>(__msg.c_str())); |
| 1438 |
} |
| 1439 |
|
| 1440 |
// Returns a locale and codeset-converted string, given a char* message. |
| 1441 |
string_type |
| 1442 |
_M_convert_from_char(char*) const |
| 1443 |
{ |
| 1444 |
#if 0 |
| 1445 |
// Length of message string without terminating null. |
| 1446 |
size_t __len = char_traits<char>::length(__msg) - 1; |
| 1447 |
|
| 1448 |
// "everybody can easily convert the string using |
| 1449 |
// mbsrtowcs/wcsrtombs or with iconv()" |
| 1450 |
|
| 1451 |
// Convert char* to _CharT in locale used to open catalog. |
| 1452 |
// XXX need additional template parameter on messages class for this.. |
| 1453 |
// typedef typename codecvt<char, _CharT, _StateT> __codecvt_type; |
| 1454 |
typedef typename codecvt<char, _CharT, mbstate_t> __codecvt_type; |
| 1455 |
|
| 1456 |
__codecvt_type::state_type __state; |
| 1457 |
// XXX may need to initialize state. |
| 1458 |
//initialize_state(__state._M_init()); |
| 1459 |
|
| 1460 |
char* __from_next; |
| 1461 |
// XXX what size for this string? |
| 1462 |
_CharT* __to = static_cast<_CharT*>(__builtin_alloca(__len + 1)); |
| 1463 |
const __codecvt_type& __cvt = use_facet<__codecvt_type>(_M_locale_conv); |
| 1464 |
__cvt.out(__state, __msg, __msg + __len, __from_next, |
| 1465 |
__to, __to + __len + 1, __to_next); |
| 1466 |
return string_type(__to); |
| 1467 |
#endif |
| 1468 |
#if 0 |
| 1469 |
typedef ctype<_CharT> __ctype_type; |
| 1470 |
// const __ctype_type& __cvt = use_facet<__ctype_type>(_M_locale_msg); |
| 1471 |
const __ctype_type& __cvt = use_facet<__ctype_type>(locale()); |
| 1472 |
// XXX Again, proper length of converted string an issue here. |
| 1473 |
// For now, assume the converted length is not larger. |
| 1474 |
_CharT* __dest = static_cast<_CharT*>(__builtin_alloca(__len + 1)); |
| 1475 |
__cvt.widen(__msg, __msg + __len, __dest); |
| 1476 |
return basic_string<_CharT>(__dest); |
| 1477 |
#endif |
| 1478 |
return string_type(); |
| 1479 |
} |
| 1480 |
}; |
| 1481 |
|
| 1482 |
template<typename _CharT> |
| 1483 |
locale::id messages<_CharT>::id; |
| 1484 |
|
| 1485 |
// Specializations for required instantiations. |
| 1486 |
template<> |
| 1487 |
string |
| 1488 |
messages<char>::do_get(catalog, int, int, const string&) const; |
| 1489 |
|
| 1490 |
#ifdef _GLIBCXX_USE_WCHAR_T |
| 1491 |
template<> |
| 1492 |
wstring |
| 1493 |
messages<wchar_t>::do_get(catalog, int, int, const wstring&) const; |
| 1494 |
#endif |
| 1495 |
|
| 1496 |
template<typename _CharT> |
| 1497 |
class messages_byname : public messages<_CharT> |
| 1498 |
{ |
| 1499 |
public: |
| 1500 |
typedef _CharT char_type; |
| 1501 |
typedef basic_string<_CharT> string_type; |
| 1502 |
|
| 1503 |
explicit |
| 1504 |
messages_byname(const char* __s, size_t __refs = 0); |
| 1505 |
|
| 1506 |
protected: |
| 1507 |
virtual |
| 1508 |
~messages_byname() |
| 1509 |
{ } |
| 1510 |
}; |
| 1511 |
|
| 1512 |
// Include host and configuration specific messages functions. |
| 1513 |
#include <bits/messages_members.h> |
| 1514 |
|
| 1515 |
|
| 1516 |
// Subclause convenience interfaces, inlines. |
| 1517 |
// NB: These are inline because, when used in a loop, some compilers |
| 1518 |
// can hoist the body out of the loop; then it's just as fast as the |
| 1519 |
// C is*() function. |
| 1520 |
//@{ |
| 1521 |
/// Convenience interface to ctype.is(). |
| 1522 |
template<typename _CharT> |
| 1523 |
inline bool |
| 1524 |
isspace(_CharT __c, const locale& __loc) |
| 1525 |
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c); } |
| 1526 |
|
| 1527 |
template<typename _CharT> |
| 1528 |
inline bool |
| 1529 |
isprint(_CharT __c, const locale& __loc) |
| 1530 |
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c); } |
| 1531 |
|
| 1532 |
template<typename _CharT> |
| 1533 |
inline bool |
| 1534 |
iscntrl(_CharT __c, const locale& __loc) |
| 1535 |
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c); } |
| 1536 |
|
| 1537 |
template<typename _CharT> |
| 1538 |
inline bool |
| 1539 |
isupper(_CharT __c, const locale& __loc) |
| 1540 |
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c); } |
| 1541 |
|
| 1542 |
template<typename _CharT> |
| 1543 |
inline bool islower(_CharT __c, const locale& __loc) |
| 1544 |
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c); } |
| 1545 |
|
| 1546 |
template<typename _CharT> |
| 1547 |
inline bool |
| 1548 |
isalpha(_CharT __c, const locale& __loc) |
| 1549 |
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c); } |
| 1550 |
|
| 1551 |
template<typename _CharT> |
| 1552 |
inline bool |
| 1553 |
isdigit(_CharT __c, const locale& __loc) |
| 1554 |
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c); } |
| 1555 |
|
| 1556 |
template<typename _CharT> |
| 1557 |
inline bool |
| 1558 |
ispunct(_CharT __c, const locale& __loc) |
| 1559 |
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c); } |
| 1560 |
|
| 1561 |
template<typename _CharT> |
| 1562 |
inline bool |
| 1563 |
isxdigit(_CharT __c, const locale& __loc) |
| 1564 |
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c); } |
| 1565 |
|
| 1566 |
template<typename _CharT> |
| 1567 |
inline bool |
| 1568 |
isalnum(_CharT __c, const locale& __loc) |
| 1569 |
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c); } |
| 1570 |
|
| 1571 |
template<typename _CharT> |
| 1572 |
inline bool |
| 1573 |
isgraph(_CharT __c, const locale& __loc) |
| 1574 |
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c); } |
| 1575 |
|
| 1576 |
template<typename _CharT> |
| 1577 |
inline _CharT |
| 1578 |
toupper(_CharT __c, const locale& __loc) |
| 1579 |
{ return use_facet<ctype<_CharT> >(__loc).toupper(__c); } |
| 1580 |
|
| 1581 |
template<typename _CharT> |
| 1582 |
inline _CharT |
| 1583 |
tolower(_CharT __c, const locale& __loc) |
| 1584 |
{ return use_facet<ctype<_CharT> >(__loc).tolower(__c); } |
| 1585 |
//@} |
| 1586 |
} // namespace std |
| 1587 |
|
| 1588 |
#endif |