2 * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
5 * Silicon Graphics Computer Systems, Inc.
10 * This material is provided "as is", with absolutely no warranty expressed
11 * or implied. Any use is at your own risk.
13 * Permission to use or copy this software for any purpose is hereby granted
14 * without fee, provided the above notices are retained on all copies.
15 * Permission to modify the code and to distribute modified code is granted,
16 * provided the above notices are retained, and a notice that the code was
17 * modified is included with the above copyright notice.
20 #ifndef _STLP_MONETARY_C
21 #define _STLP_MONETARY_C
23 # ifndef _STLP_INTERNAL_MONETARY_H
24 # include <stl/_monetary.h>
27 #ifndef _STLP_INTERNAL_IOS_H
28 # include <stl/_ios.h>
31 #ifndef _STLP_INTERNAL_NUM_PUT_H
32 # include <stl/_num_put.h>
35 #ifndef _STLP_INTERNAL_NUM_GET_H
36 # include <stl/_num_get.h>
41 #if (_STLP_STATIC_TEMPLATE_DATA > 0)
42 #if !defined(__SYMBIAN32__WSD__)
44 # if !defined (__BORLANDC__) && (defined (__SYMBIAN32__) && defined(_STLP_DESIGNATED_DLL))
45 template <class _CharT, class _InputIterator>
46 locale::id money_get<_CharT, _InputIterator>::id;
48 template <class _CharT, class _OutputIterator>
49 locale::id money_put<_CharT, _OutputIterator>::id;
52 # if ((defined (__CYGWIN__) || defined (__MINGW32__)) && \
53 defined (_STLP_USE_DYNAMIC_LIB) && !defined (__BUILDING_STLPORT)) || (defined (__SYMBIAN32__) && defined(_STLP_DESIGNATED_DLL))
55 * Under cygwin, when STLport is used as a shared library, the id needs
56 * to be specified as imported otherwise they will be duplicated in the
59 #if defined (__SYMBIAN32__)
61 locale::id money_get<char, istreambuf_iterator<char, char_traits<char> > >::id; //_STLP_DECLSPEC removed; data should not be exported in symbian.
64 _STLP_DECLSPEC locale::id money_get<char, const char*>::id;
68 locale::id money_put<char, ostreambuf_iterator<char, char_traits<char> > >::id;
70 locale::id money_put<char, char*>::id;
72 # if !defined (_STLP_NO_WCHAR_T)
74 locale::id money_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id;
76 locale::id money_get<wchar_t, const wchar_t*>::id;
79 locale::id money_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id;
81 locale::id money_put<wchar_t, wchar_t*>::id;
82 # endif /* _STLP_NO_WCHAR_T */
86 _STLP_DECLSPEC locale::id money_get<char, istreambuf_iterator<char, char_traits<char> > >::id;
89 _STLP_DECLSPEC locale::id money_get<char, const char*>::id;
93 _STLP_DECLSPEC locale::id money_put<char, ostreambuf_iterator<char, char_traits<char> > >::id;
95 _STLP_DECLSPEC locale::id money_put<char, char*>::id;
97 # if !defined (_STLP_NO_WCHAR_T)
99 _STLP_DECLSPEC locale::id money_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id;
101 _STLP_DECLSPEC locale::id money_get<wchar_t, const wchar_t*>::id;
104 _STLP_DECLSPEC locale::id money_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id;
106 _STLP_DECLSPEC locale::id money_put<wchar_t, wchar_t*>::id;
107 # endif /* _STLP_NO_WCHAR_T */
111 #endif /* (defined (__CYGWIN__) || defined (__MINGW32__)) */
112 #endif /* __SYMBIAN32__WSD__ */
114 #else /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
116 //typedef money_get<char, const char*> money_get_char;
117 //typedef money_put<char, char*> money_put_char;
118 typedef money_get<char, istreambuf_iterator<char, char_traits<char> > > money_get_char_2;
119 typedef money_put<char, ostreambuf_iterator<char, char_traits<char> > > money_put_char_2;
121 //__DECLARE_INSTANCE(locale::id, money_get_char::id, );
122 //__DECLARE_INSTANCE(locale::id, money_put_char::id, );
123 __DECLARE_INSTANCE(locale::id, money_get_char_2::id, );
124 __DECLARE_INSTANCE(locale::id, money_put_char_2::id, );
126 # ifndef _STLP_NO_WCHAR_T
128 //typedef money_get<wchar_t, const wchar_t*> money_get_wchar_t;
129 //typedef money_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > > money_get_wchar_t_2;
130 typedef money_put<wchar_t, wchar_t*> money_put_wchar_t;
131 typedef money_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > > money_put_wchar_t_2;
133 //__DECLARE_INSTANCE(locale::id, money_get_wchar_t::id, );
134 //__DECLARE_INSTANCE(locale::id, money_put_wchar_t::id, );
135 __DECLARE_INSTANCE(locale::id, money_get_wchar_t_2::id, );
136 __DECLARE_INSTANCE(locale::id, money_put_wchar_t_2::id, );
138 # endif /* _STLP_NO_WCHAR_T */
139 #endif /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
143 _STLP_MOVE_TO_PRIV_NAMESPACE
145 // helper functions for do_get
146 template <class _InIt1, class _InIt2>
147 pair<_InIt1, bool> __get_string( _InIt1 __first, _InIt1 __last,
148 _InIt2 __str_first, _InIt2 __str_last) {
149 while ( __first != __last && __str_first != __str_last && *__first == *__str_first ) {
153 return make_pair(__first, __str_first == __str_last);
156 template <class _InIt, class _OuIt, class _CharT>
158 __get_monetary_value(_InIt& __first, _InIt __last, _OuIt __out_ite,
159 const ctype<_CharT>& _c_type,
160 _CharT __point, int __frac_digits, _CharT __sep,
161 const string& __grouping, bool &__syntax_ok) {
162 if (__first == __last || !_c_type.is(ctype_base::digit, *__first))
165 char __group_sizes[128];
166 char* __group_sizes_end = __grouping.empty()? 0 : __group_sizes;
167 char __current_group_size = 0;
169 while (__first != __last) {
170 if (_c_type.is(ctype_base::digit, *__first)) {
171 ++__current_group_size;
172 *__out_ite++ = *__first++;
174 else if (__group_sizes_end) {
175 if (*__first == __sep) {
176 *__group_sizes_end++ = __current_group_size;
177 __current_group_size = 0;
186 if (__grouping.empty())
189 if (__group_sizes_end != __group_sizes)
190 *__group_sizes_end++ = __current_group_size;
192 __syntax_ok = __valid_grouping(__group_sizes, __group_sizes_end,
193 __grouping.data(), __grouping.data()+ __grouping.size());
195 if (__first == __last || *__first != __point) {
196 for (int __digits = 0; __digits != __frac_digits; ++__digits)
197 *__out_ite++ = _CharT('0');
198 return true; // OK not to have decimal point
206 while (__first != __last && _c_type.is(ctype_base::digit, *__first)) {
207 *__out_ite++ = *__first++;
211 __syntax_ok = __syntax_ok && (__digits == __frac_digits);
217 template <class _CharT, class _InputIter, class _StrType>
218 _InputIter __money_do_get(_InputIter __s, _InputIter __end, bool __intl,
219 ios_base& __str, ios_base::iostate& __err,
220 _StrType& __digits, bool &__is_positive, _CharT* /*__dummy*/) {
222 __err |= ios_base::eofbit;
226 typedef _CharT char_type;
227 typedef _StrType string_type;
228 typedef _InputIter iter_type;
229 typedef moneypunct<char_type, false> _Punct;
230 typedef moneypunct<char_type, true> _Punct_intl;
231 typedef ctype<char_type> _Ctype;
233 locale __loc = __str.getloc();
234 const _Punct& __punct = use_facet<_Punct>(__loc) ;
235 const _Punct_intl& __punct_intl = use_facet<_Punct_intl>(__loc) ;
236 const _Ctype& __c_type = use_facet<_Ctype>(__loc) ;
238 money_base::pattern __format = __intl ? __punct_intl.neg_format()
239 : __punct.neg_format();
240 string_type __ns = __intl ? __punct_intl.negative_sign()
241 : __punct.negative_sign();
242 string_type __ps = __intl ? __punct_intl.positive_sign()
243 : __punct.positive_sign();
245 bool __symbol_required = (__str.flags() & ios_base::showbase) != 0;
247 back_insert_iterator<string_type> __out_ite(__buf);
249 for (__i = 0; __i < 4; ++__i) {
250 switch (__format.field[__i]) {
251 case money_base::none:
253 if (__c_type.is(ctype_base::space, *__s)) {
254 __err = ios_base::failbit;
259 while (__s != __end && __c_type.is(ctype_base::space, *__s))
262 case money_base::space:
263 if (!__c_type.is(ctype_base::space, *__s)) {
264 __err = ios_base::failbit;
268 while (__s != __end && __c_type.is(ctype_base::space, *__s))
271 case money_base::symbol: {
272 string_type __curs = __intl ? __punct_intl.curr_symbol()
273 : __punct.curr_symbol();
274 pair<iter_type, bool>
275 __result = __get_string(__s, __end, __curs.begin(), __curs.end());
276 if (!__result.second && __symbol_required)
277 __err = ios_base::failbit;
278 __s = __result.first;
281 case money_base::sign: {
286 __is_positive = false;
289 __err = ios_base::failbit;
296 if (*__s == __ns[0]) {
298 __is_positive = false;
303 if (*__s == __ps[0]) {
309 if (*__s == __ns[0]) {
311 __is_positive = false;
314 __err = ios_base::failbit;
319 case money_base::value: {
320 char_type __point = __intl ? __punct_intl.decimal_point()
321 : __punct.decimal_point();
322 int __frac_digits = __intl ? __punct_intl.frac_digits()
323 : __punct.frac_digits();
324 string __grouping = __intl ? __punct_intl.grouping()
325 : __punct.grouping();
326 bool __syntax_ok = true;
330 char_type __sep = __grouping.empty() ? char_type() :
331 __intl ? __punct_intl.thousands_sep() : __punct.thousands_sep();
333 __result = __get_monetary_value(__s, __end, __out_ite, __c_type,
334 __point, __frac_digits,
336 __grouping, __syntax_ok);
339 __err |= ios_base::failbit;
341 __err = ios_base::failbit;
346 } // Close money_base::value case
347 } // Close switch statement
351 if (__ps.size() > 1) {
352 pair<_InputIter, bool>
353 __result = __get_string(__s, __end, __ps.begin() + 1, __ps.end());
354 __s = __result.first;
355 if (!__result.second)
356 __err |= ios::failbit;
358 if (!(__err & ios_base::failbit))
362 if (__ns.size() > 1) {
363 pair<_InputIter, bool>
364 __result = __get_string(__s, __end, __ns.begin() + 1, __ns.end());
365 __s = __result.first;
366 if (!__result.second)
367 __err |= ios::failbit;
369 if (!(__err & ios::failbit)) {
370 __digits = __c_type.widen('-');
375 __err |= ios::eofbit;
380 _STLP_MOVE_TO_STD_NAMESPACE
382 //===== methods ======
383 template <class _CharT, class _InputIter>
385 money_get<_CharT, _InputIter>::do_get(_InputIter __s, _InputIter __end, bool __intl,
386 ios_base& __str, ios_base::iostate& __err_,
387 _STLP_LONGEST_FLOAT_TYPE& __units) const {
388 ios_base::iostate __err = 0;
390 bool __is_positive = true;
391 __s = _STLP_PRIV __money_do_get(__s, __end, __intl, __str, __err, __buf, __is_positive, (_CharT*)0);
393 if (__err == ios_base::goodbit || __err == ios_base::eofbit) {
394 typename string_type::iterator __b = __buf.begin(), __e = __buf.end();
396 if (!__is_positive) ++__b;
397 // Can't use atold, since it might be wchar_t. Don't get confused by name below :
398 // it's perfectly capable of reading long double.
399 _STLP_PRIV __get_decimal_integer(__b, __e, __units, (_CharT*)0);
401 if (!__is_positive) {
405 if ( (__err & ios_base::failbit == 1) || (__err & ios_base::eofbit == 1) ) {
411 template <class _CharT, class _InputIter>
413 money_get<_CharT, _InputIter>::do_get(iter_type __s, iter_type __end, bool __intl,
414 ios_base& __str, ios_base::iostate& __err,
415 string_type& __digits) const {
416 bool __is_positive = true;
417 return _STLP_PRIV __money_do_get(__s, __end, __intl, __str, __err, __digits, __is_positive, (_CharT*)0);
422 _STLP_MOVE_TO_PRIV_NAMESPACE
424 template <class _CharT, class _OutputIter, class _Str_Type, class _Str>
425 _OutputIter __money_do_put(_OutputIter __s, bool __intl, ios_base& __str,
426 _CharT __fill, const _Str& __digits, bool __check_digits,
427 _Str_Type * /*__dummy*/) {
428 typedef _CharT char_type;
429 typedef _Str_Type string_type;
430 typedef ctype<char_type> _Ctype;
431 typedef moneypunct<char_type, false> _Punct;
432 typedef moneypunct<char_type, true> _Punct_intl;
434 locale __loc = __str.getloc();
435 const _Ctype& __c_type = use_facet<_Ctype>(__loc) ;
436 const _Punct& __punct = use_facet<_Punct>(__loc) ;
437 const _Punct_intl& __punct_intl = use_facet<_Punct_intl>(__loc) ;
439 // some special characters
440 char_type __minus = __c_type.widen('-');
441 char_type __plus = __c_type.widen('+');
442 char_type __space = __c_type.widen(' ');
443 char_type __zero = __c_type.widen('0');
444 char_type __point = __intl ? __punct_intl.decimal_point()
445 : __punct.decimal_point();
447 char_type __sep = __intl ? __punct_intl.thousands_sep()
448 : __punct.thousands_sep();
450 string __grouping = __intl ? __punct_intl.grouping()
451 : __punct.grouping();
453 int __frac_digits = __intl ? __punct_intl.frac_digits()
454 : __punct.frac_digits();
456 string_type __curr_sym = __intl ? __punct_intl.curr_symbol()
457 : __punct.curr_symbol();
459 // if there are no digits we are going to return __s. If there
460 // are digits, but not enough to fill the frac_digits, we are
461 // going to add zeros. I don't know whether this is right or
463 if (__digits.empty())
466 typename string_type::const_iterator __digits_first = __digits.begin();
467 typename string_type::const_iterator __digits_last = __digits.end();
469 bool __is_negative = *__digits_first == __minus;
473 #if !defined (__BORLANDC__)
474 string_type __sign = __intl ? __is_negative ? __punct_intl.negative_sign()
475 : __punct_intl.positive_sign()
476 : __is_negative ? __punct.negative_sign()
477 : __punct.positive_sign();
482 __sign = __punct_intl.negative_sign();
484 __sign = __punct_intl.positive_sign();
488 __sign = __punct.negative_sign();
490 __sign = __punct.positive_sign();
494 if (__check_digits) {
495 typename string_type::const_iterator __cp = __digits_first;
496 while (__cp != __digits_last && __c_type.is(ctype_base::digit, *__cp))
498 if (__cp == __digits_first)
500 __digits_last = __cp;
503 // If grouping is required, we make a copy of __digits and
504 // insert the grouping.
505 _STLP_BASIC_IOSTRING(char_type) __new_digits;
506 if (!__grouping.empty()) {
507 __new_digits.assign(__digits_first, __digits_last);
508 __insert_grouping(__new_digits,
509 __new_digits.size() - __frac_digits,
511 __sep, __plus, __minus, 0);
512 __digits_first = __new_digits.begin(); // <<--
513 __digits_last = __new_digits.end(); // <<--
516 // Determine the amount of padding required, if any.
517 streamsize __width = __str.width();
519 #if defined (_STLP_DEBUG) && (defined(__HP_aCC) && (__HP_aCC <= 1))
520 size_t __value_length = operator -(__digits_last, __digits_first);
522 size_t __value_length = __digits_last - __digits_first;
525 size_t __length = __value_length + __sign.size();
527 if (__frac_digits != 0)
530 bool __generate_curr = (__str.flags() & ios_base::showbase) !=0;
532 __length += __curr_sym.size();
533 money_base::pattern __format = __intl ? (__is_negative ? __punct_intl.neg_format()
534 : __punct_intl.pos_format())
535 : (__is_negative ? __punct.neg_format()
536 : __punct.pos_format());
538 //For the moment the following is commented for decoding reason.
539 //No reason to add a space last if the money symbol do not have to be display
540 //if (__format.field[3] == (char) money_base::symbol && !__generate_curr) {
541 // if (__format.field[2] == (char) money_base::space) {
542 // __format.field[2] = (char) money_base::none;
545 //space can only be second or third and only once (22.2.6.3-1):
546 if ((__format.field[1] == (char) money_base::space) ||
547 (__format.field[2] == (char) money_base::space))
551 const bool __need_fill = (((sizeof(streamsize) > sizeof(size_t)) && (__STATIC_CAST(streamsize, __length) < __width)) ||
552 ((sizeof(streamsize) <= sizeof(size_t)) && (__length < __STATIC_CAST(size_t, __width))));
553 streamsize __fill_amt = __need_fill ? __width - __length : 0;
555 ios_base::fmtflags __fill_pos = __str.flags() & ios_base::adjustfield;
557 if (__fill_amt != 0 &&
558 !(__fill_pos & (ios_base::left | ios_base::internal)))
559 __s = __fill_n(__s, __fill_amt, __fill);
561 for (int __i = 0; __i < 4; ++__i) {
562 char __ffield = __format.field[__i];
564 case money_base::none:
565 if (__fill_amt != 0 && __fill_pos == ios_base::internal)
566 __s = __fill_n(__s, __fill_amt, __fill);
568 case money_base::space:
577 if (__fill_amt != 0 && __fill_pos == ios_base::internal)
578 __s = __fill_n(__s, __fill_amt, __fill);
580 case money_base::symbol:
582 __s = copy(__curr_sym.begin(), __curr_sym.end(), __s);
584 case money_base::sign:
588 case money_base::value:
589 if (__frac_digits == 0) {
590 __s = copy(__digits_first, __digits_last, __s);
592 if ((int)__value_length <= __frac_digits) {
593 // if we see '9' here, we should out 0.09
594 *__s++ = __zero; // integer part is zero
595 *__s++ = __point; // decimal point
596 __s = __fill_n(__s, __frac_digits - __value_length, __zero); // zeros
597 __s = copy(__digits_first, __digits_last, __s); // digits
599 __s = copy(__digits_first, __digits_last - __frac_digits, __s);
600 if (__frac_digits != 0) {
602 __s = copy(__digits_last - __frac_digits, __digits_last, __s);
610 // Ouput rest of sign if necessary.
611 if (__sign.size() > 1)
612 __s = copy(__sign.begin() + 1, __sign.end(), __s);
613 if (__fill_amt != 0 &&
614 !(__fill_pos & (ios_base::right | ios_base::internal)))
615 __s = __fill_n(__s, __fill_amt, __fill);
620 _STLP_MOVE_TO_STD_NAMESPACE
622 template <class _CharT, class _OutputIter>
624 money_put<_CharT, _OutputIter>
625 ::do_put(_OutputIter __s, bool __intl, ios_base& __str,
626 char_type __fill, _STLP_LONGEST_FLOAT_TYPE __units) const {
627 _STLP_BASIC_IOSTRING(char_type) __digits;
628 _STLP_PRIV __get_money_digits(__digits, __str, __units);
629 return _STLP_PRIV __money_do_put(__s, __intl, __str, __fill, __digits, false, __STATIC_CAST(string_type*, 0));
632 template <class _CharT, class _OutputIter>
634 money_put<_CharT, _OutputIter>
635 ::do_put(_OutputIter __s, bool __intl, ios_base& __str,
636 char_type __fill, const string_type& __digits) const {
637 return _STLP_PRIV __money_do_put(__s, __intl, __str, __fill, __digits, true, __STATIC_CAST(string_type*, 0));
642 #endif /* _STLP_MONETARY_C */