2 * © Portions copyright (c) 2006-2007 Nokia Corporation. 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 # if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION)
29 #ifndef _STLP_INTERNAL_IOS_H
30 # include <stl/_ios.h>
33 #ifndef _STLP_INTERNAL_NUM_PUT_H
34 # include <stl/_num_put.h>
37 #ifndef _STLP_INTERNAL_NUM_GET_H
38 # include <stl/_num_get.h>
43 # if ( _STLP_STATIC_TEMPLATE_DATA > 0 )
44 # if !defined(__LIBSTD_CPP_SYMBIAN32_WSD__) && !defined(_STLP_LIBSTD_CPP_NO_STATIC_VAR_)
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;
51 # else /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
53 typedef money_get<char, const char*> money_get_char;
54 typedef money_put<char, char*> money_put_char;
55 typedef money_get<char, istreambuf_iterator<char, char_traits<char> > > money_get_char_2;
56 typedef money_put<char, ostreambuf_iterator<char, char_traits<char> > > money_put_char_2;
59 __DECLARE_INSTANCE(locale::id, money_get_char::id, );
60 __DECLARE_INSTANCE(locale::id, money_put_char::id, );
61 __DECLARE_INSTANCE(locale::id, money_get_char_2::id, );
62 __DECLARE_INSTANCE(locale::id, money_put_char_2::id, );
65 # ifndef _STLP_NO_WCHAR_T
67 typedef money_get<wchar_t, const wchar_t*> money_get_wchar_t;
68 typedef money_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > > money_get_wchar_t_2;
69 typedef money_put<wchar_t, wchar_t*> money_put_wchar_t;
70 typedef money_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > > money_put_wchar_t_2;
73 __DECLARE_INSTANCE(locale::id, money_get_wchar_t::id, );
74 __DECLARE_INSTANCE(locale::id, money_put_wchar_t::id, );
75 __DECLARE_INSTANCE(locale::id, money_get_wchar_t_2::id, );
76 __DECLARE_INSTANCE(locale::id, money_put_wchar_t_2::id, );
80 # endif /* ( _STLP_STATIC_TEMPLATE_DATA > 0 ) */
85 // helper functions for do_get
86 template <class _InIt1, class _InIt2>
87 pair<_InIt1, bool> __get_string(_InIt1 __first, _InIt1 __last,
88 _InIt2 __str_first, _InIt2 __str_last) {
89 pair<_InIt1, _InIt2> __pr = mismatch(__first, __last, __str_first);
90 return make_pair(__pr.first, __pr.second == __str_last);
93 template <class _InIt, class _OuIt, class _CharT>
95 __get_monetary_value(_InIt& __first, _InIt __last, _OuIt __stl_out,
96 const ctype<_CharT>& _c_type,
100 const string& __grouping,
106 if (__first == __last || !_c_type.is(ctype_base::digit, *__first))
109 char __group_sizes[128];
110 char* __group_sizes_end = __grouping.size() == 0 ? 0 : __group_sizes;
111 char __current_group_size = 0;
113 while (__first != __last) {
114 if (_c_type.is(ctype_base::digit, *__first)) {
115 ++__current_group_size;
116 *__stl_out++ = *__first++;
120 else if ( (__current_group_size)&&(__group_sizes_end) ){
122 else if (__group_sizes_end) {
124 if (*__first == __sep) {
125 *__group_sizes_end++ = __current_group_size;
126 __current_group_size = 0;
135 if (__grouping.size() == 0)
138 if (__group_sizes_end != __group_sizes)
139 *__group_sizes_end++ = __current_group_size;
141 __syntax_ok = __valid_grouping(__group_sizes, __group_sizes_end,
142 __grouping.data(), __grouping.data()+ __grouping.size());
144 if (__first == __last || *__first != __point) {
145 for (int __digits = 0; __digits != __frac_digits; ++__digits)
146 *__stl_out++ = _CharT('0');
147 return true; // OK not to have decimal point
150 //bug fix testcase_22427 - commented
154 //bug fix testcase_22427 - added if
156 if (__first != __last && *__first == __point && __frac_digits)
158 //bug fix testcase_22427 - commented
160 while (__first != __last && _c_type.is(ctype_base::digit, *__first)) {
161 *__stl_out++ = *__first++;
166 //bug fix testcase_22427 - condition changed
167 // __syntax_ok = __syntax_ok && (__digits == __frac_digits);
175 # ifndef _STLP_NO_LONG_DOUBLE
177 //===== methods ======
178 template <class _CharT, class _InputIter>
180 money_get<_CharT, _InputIter>::do_get(_InputIter __s, _InputIter __end, bool __intl,
181 ios_base& __str, ios_base::iostate& __err,
182 long double& __units) const {
184 __s = do_get(__s, __end, __intl, __str, __err, __buf);
186 if (__err == ios_base::goodbit || __err == ios_base::eofbit) {
188 typename string_type::iterator __b = __buf.begin(), __e = __buf.end();
189 // Can't use atold, since it might be wchar_t. Don't get confused by name below :
190 // it's perfectly capable of reading long double.
191 __get_decimal_integer(__b, __e, __units);
194 __err |= ios_base::eofbit;
199 template <class _CharT, class _InputIter>
201 money_get<_CharT, _InputIter>::do_get(iter_type __s,
202 iter_type __end, bool __intl,
203 ios_base& __str, ios_base::iostate& __err,
204 string_type& __digits) const {
207 __err |= ios_base::eofbit;
211 typedef moneypunct<_CharT, false> _Punct;
212 typedef moneypunct<_CharT, true> _Punct_intl;
213 typedef ctype<_CharT> _Ctype;
215 bool __isSignSympresent = false;
218 bool __testvalid = true;
219 locale __loc = __str.getloc();
220 const _Punct& __punct = use_facet<_Punct>(__loc) ;
221 const _Punct_intl& __punct_intl = use_facet<_Punct_intl>(__loc) ;
222 const _Ctype& __c_type = use_facet<_Ctype>(__loc) ;
224 money_base::pattern __format = __intl ? __punct_intl.neg_format()
225 : __punct.neg_format();
226 string_type __ns = __intl ? __punct_intl.negative_sign()
227 : __punct.negative_sign();
228 string_type __ps = __intl ? __punct_intl.positive_sign()
229 : __punct.positive_sign();
230 string_type __sign = __ps;
232 bool __is_positive = true;
233 bool __symbol_required = (__str.flags() & ios_base::showbase) !=0;
235 back_insert_iterator<string_type> __stl_out(__buf);
236 // pair<iter_type, bool> __result;
238 for (__i = 0; __i < 4; ++__i) {
239 switch (__format.field[__i]) {
240 case (char) money_base::none:
241 #ifndef __SYMBIAN32__
242 case (char) money_base::space:
246 //if (__c_type.is(ctype_base::space, *__s)) { //gnu buf fix, 22/12/06
247 if((__s != __end) && (!__c_type.is(ctype_base::space, *__s)) && (__ps.size()<=1) && (__ns.size()<=1) ){
248 __err = ios_base::failbit;
251 // break; //gnu bug fix 2/1/07
253 while (__s != __end && __c_type.is(ctype_base::space, *__s))
257 case (char) money_base::space:
258 if (!__c_type.is(ctype_base::space, *__s)) {
260 if(!__isSignSympresent) //if no sign symbol present, space is not mandatory.
263 __err = ios_base::failbit;
267 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: {
283 if (__ps.size() == 0)
285 if (__ns.size() == 0) {
286 __is_positive = false;
290 __err = ios_base::failbit;
294 if (__ps.size() == 0) {
295 if (__ns.size() == 0)
297 if (*__s == __ns[0]) {
299 __is_positive = false;
302 __isSignSympresent = true;
307 // __err = ios_base::failbit; //if ps !=0 and ns!=0, and no negative sign mean it is positive, not fail.
312 if (*__s == __ps[0]) {
315 __isSignSympresent = true;
321 if (__ns.size() == 0) //here positive have symbol,negative have no symbol
323 __is_positive = false; // in this case if symbol not present means it is negative
329 if (__ns.size() == 0)
332 if (*__s == __ns[0]) {
334 __is_positive = false;
337 __isSignSympresent = true;
341 __err = ios_base::failbit;
348 case money_base::value: {
349 _CharT __point = __intl ? __punct_intl.decimal_point()
350 : __punct.decimal_point();
351 int __frac_digits = __intl ? __punct_intl.frac_digits()
352 : __punct.frac_digits();
353 string __grouping = __intl ? __punct_intl.grouping()
354 : __punct.grouping();
355 bool __syntax_ok = true;
359 _CharT __sep = __grouping.size() == 0 ? _CharT() :
360 __intl ? __punct_intl.thousands_sep() : __punct.thousands_sep();
362 __result = __get_monetary_value(__s, __end, __stl_out, __c_type,
363 __point, __frac_digits,
365 __grouping, __syntax_ok);
368 __err |= ios_base::failbit;
370 __err = ios_base::failbit;
375 } // Close money_base::value case
378 } // Close switch statement
381 #ifndef __SYMBIAN32__ //find the given number pos or neg, required for cases where sign is present at string starting and ending
385 int si = __ps.size();
386 for (int i = 1;i<__ps.size();i++)
388 if(__s[i-1]!=__ps[i])
395 if ((si!=0) && (type ==true))
400 if (__ns.size() == 0)
403 for (int i =1;i<__ns.size();i++)
405 if(__s[i-1]!=__ns[i])
412 __is_positive = false;
422 // Need to get the rest of the sign characters, if they exist.
423 if (__sign.size() > 1)
425 int __len = __sign.size();
427 char_type __c = *__s;
428 char_type __eol = '\0';
429 for (; __c != __eol && __i < __len; __i++)
435 //checking sign completely extracted successfully
444 if (__ps.size() > 1) {
445 #ifndef __SYMBIAN32__
446 pair<_InputIter, bool>
447 __result = __get_string(__s, __end, __ps.begin() + 1, __ps.end());
448 __s = __result.first;
449 if (!__result.second)
450 __err |= ios::failbit;
453 __err |= ios::failbit;
455 if (!(__err & ios_base::failbit))
459 if (__ns.size() > 1) {
460 #ifndef __SYMBIAN32__
461 pair<_InputIter, bool>
462 __result = __get_string(__s, __end, __ns.begin() + 1, __ns.end());
463 __s = __result.first;
464 if (!__result.second)
465 __err |= ios::failbit;
468 __err |= ios::failbit;
470 if (!(__err & ios::failbit) && (__ns == __sign)) {
471 __buf.insert(__buf.begin(),__c_type.widen('-'));
474 if (!(__err & ios_base::failbit))
481 __err |= ios::eofbit;
488 template <class _CharT, class _OutputIter>
490 money_put<_CharT, _OutputIter>
491 ::do_put(_OutputIter __s, bool __intl, ios_base& __str,
493 const string_type& __digits) const {
494 typedef ctype<_CharT> _Ctype;
495 typedef moneypunct<_CharT, false> _Punct;
496 typedef moneypunct<_CharT, true> _Punct_intl;
498 locale __loc = __str.getloc();
499 const _Ctype& __c_type = use_facet<_Ctype>(__loc) ;
500 const _Punct& __punct = use_facet<_Punct>(__loc) ;
501 const _Punct_intl& __punct_intl = use_facet<_Punct_intl>(__loc) ;
503 // some special characters
505 char_type __minus = __c_type.widen('-');
506 char_type __plus = __c_type.widen('+');
507 char_type __space = __c_type.widen(' ');
508 char_type __zero = __c_type.widen('0');
509 char_type __point = __intl ? __c_type.widen(__punct_intl.decimal_point())
510 : __c_type.widen(__punct.decimal_point());
512 char_type __sep = __intl ? __punct_intl.thousands_sep()
513 : __punct .thousands_sep();
515 string __grouping = __intl ? __punct_intl.grouping()
516 : __punct .grouping();
518 int __frac_digits = __intl ? __punct_intl.frac_digits()
519 : __punct.frac_digits();
521 string_type __curr_sym = __intl ? __punct_intl.curr_symbol()
522 : __punct.curr_symbol();
524 // if there are no digits we are going to return __s. If there
525 // are digits, but not enough to fill the frac_digits, we are
526 // going to add zeros. I don't know whether this is right or
529 if (__digits.size() == 0)
532 typename string_type::const_iterator __digits_first = __digits.begin();
533 typename string_type::const_iterator __digits_last = __digits.end();
535 bool __is_negative = *__digits_first == __minus;
539 string_type __sign = __intl ?
540 __is_negative ? __punct_intl.negative_sign()
541 : __punct_intl.positive_sign()
543 __is_negative ? __punct.negative_sign()
544 : __punct.positive_sign();
545 typename string_type::const_iterator __cp = __digits_first;
546 while (__cp != __digits_last && __c_type.is(ctype_base::digit, *__cp))
548 if (__cp == __digits_first)
550 __digits_last = __cp;
552 // If grouping is required, we make a copy of __digits and
553 // insert the grouping.
555 // To handle the fractional digits, we augment the first group
556 // by frac_digits. If there is only one group, we need first
559 string_type __new_digits(__digits_first, __digits_last);
561 int __numberofseperators = 0;
562 if (__grouping.size()>0)
563 __numberofseperators = (__new_digits.size()/__grouping[0])+1;
565 __numberofseperators = 0;
566 __new_digits.resize(__new_digits.size()+__numberofseperators);
568 if (__grouping.size() != 0) {
569 if (__grouping.size() == 1)
570 __grouping.push_back(__grouping[0]);
571 __grouping[0] += __frac_digits;
572 _CharT* __data_ptr = __CONST_CAST(_CharT*,__new_digits.data());
573 _CharT* __data_end = __data_ptr + __new_digits.size();
576 ptrdiff_t __value_length = __insert_grouping(__data_ptr,
578 __data_end-__numberofseperators,
585 __digits_first = __new_digits.begin();
586 __digits_last = __digits_first + __value_length;
589 // Determine the amount of padding required, if any.
591 int __width = __str.width(); //width returns signed value.
593 size_t __width = __str.width();
596 #if defined(_STLP_DEBUG) && (defined(__HP_aCC) || (__HP_aCC <= 1))
597 size_t __value_length = operator -(__digits_last, __digits_first);
599 size_t __value_length = __digits_last - __digits_first;
602 size_t __length = __value_length;
604 __length += __sign.size();
605 if (__frac_digits != 0)
608 bool __generate_curr = (__str.flags() & ios_base::showbase) !=0;
610 __length += __curr_sym.size();
611 money_base::pattern __format =
612 __intl ? (__is_negative ? __punct_intl.neg_format()
613 : __punct_intl.pos_format())
614 : (__is_negative ? __punct.neg_format()
615 : __punct.pos_format());
617 for (int __i = 0; __i < 4; ++__i)
618 if (__format.field[__i] == (char) money_base::space)
622 size_t __fill_amt = (int)__length < __width ? __width - __length : 0;
624 ios_base::fmtflags __fill_pos = __str.flags() & ios_base::adjustfield;
626 if (__fill_amt != 0 &&
627 !(__fill_pos & (ios_base::left | ios_base::internal)))
628 __s = fill_n(__s, __fill_amt, __fill);
630 for (int __i = 0; __i < 4; ++__i) {
631 char __ffield = __format.field[__i];
632 if (__ffield == money_base::none) {
633 if (__fill_amt != 0 && __fill_pos == ios_base::internal)
634 __s = fill_n(__s, __fill_amt, __fill);
636 else if (__ffield == money_base::space) {
638 if(__fill != __space)
643 if (__fill_amt != 0 && __fill_pos == ios_base::internal)
644 __s = fill_n(__s, __fill_amt, __fill);
646 else if (__ffield == money_base::symbol) {
648 __s = copy(__curr_sym.begin(), __curr_sym.end(), __s);
650 else if (__ffield == money_base::sign) {
651 if (__sign.size() != 0)
654 else if (__ffield == money_base::value) {
655 if (__frac_digits == 0)
656 __s = copy(__digits_first, __digits_last, __s);
658 if ((int)__value_length <= __frac_digits) {
660 __s = copy(__digits_first, __digits_last, __s);
661 __s = fill_n(__s, __frac_digits - __value_length, __zero);
668 __s = copy(__digits_first, __digits_last - __frac_digits, __s);
669 if (__frac_digits != 0) {
671 __s = copy(__digits_last - __frac_digits, __digits_last, __s);
681 // Ouput rest of sign if necessary.
683 if (__sign.size() > 1)
684 __s = copy(__sign.begin() + 1, __sign.end(), __s);
685 if (!(__fill_pos & (ios_base::right | ios_base::internal)))
686 __s = fill_n(__s, __fill_amt, __fill);
696 template<class _CharT, bool _International>//__DFL_NON_TYPE_PARAM(bool, _International, false)>
697 moneypunct<_CharT, _International>::moneypunct(size_t __refs = 0):_BaseFacet(__refs)
703 template<class _CharT, bool _International>//__DFL_NON_TYPE_PARAM(bool, _International, false)>
704 _CharT moneypunct<_CharT,_International>::do_decimal_point() const
709 template<class _CharT, bool _International>//__DFL_NON_TYPE_PARAM(bool, _International, false)>
710 _CharT moneypunct<_CharT,_International>::do_thousands_sep() const
714 template<class _CharT, bool _International>//__DFL_NON_TYPE_PARAM(bool, _International, false)>
715 basic_string<_CharT> moneypunct<_CharT,_International>::do_curr_symbol() const
720 template<class _CharT, bool _International>//__DFL_NON_TYPE_PARAM(bool, _International, false)>
721 basic_string<_CharT> moneypunct<_CharT,_International>::do_positive_sign() const
726 template<class _CharT, bool _International>//__DFL_NON_TYPE_PARAM(bool, _International, false)>
727 basic_string<_CharT> moneypunct<_CharT,_International>::do_negative_sign() const
732 template<class _CharT, bool _International>//__DFL_NON_TYPE_PARAM(bool, _International, false)>
733 string moneypunct<_CharT,_International>::do_grouping() const
738 template<class _CharT, bool _International>//__DFL_NON_TYPE_PARAM(bool, _International, false)>
739 money_base::pattern moneypunct<_CharT,_International>::do_pos_format() const
741 return _M_pos_format;
744 template<class _CharT, bool _International>//__DFL_NON_TYPE_PARAM(bool, _International, false)>
745 money_base::pattern moneypunct<_CharT,_International>::do_neg_format() const
747 return _M_neg_format;
750 template<class _CharT, bool _International>
751 int moneypunct<_CharT,_International>::do_frac_digits() const
757 //monetary_byname<any>
759 template<class _CharT, bool _International>
760 _CharT moneypunct_byname<_CharT, _International>::do_decimal_point() const
761 {return _Locale_mon_decimal_pointE(_M_monetary);}
763 template<class _CharT, bool _International>
764 _CharT moneypunct_byname<_CharT, _International>::do_thousands_sep() const
765 {return _Locale_mon_thousands_sepE(_M_monetary);}
767 template<class _CharT, bool _International>
768 string moneypunct_byname<_CharT, _International>::do_grouping() const
769 {return moneypunct<_CharT,_International>::_M_group;}
771 template<class _CharT, bool _International>
772 basic_string<_CharT> moneypunct_byname<_CharT, _International>::do_curr_symbol() const
773 {return moneypunct<_CharT,_International>::_M_currSym;}
775 template<class _CharT, bool _International>
776 basic_string<_CharT> moneypunct_byname<_CharT, _International>::do_positive_sign() const
777 {return moneypunct<_CharT,_International>::_M_psign;}
779 template<class _CharT, bool _International>
780 basic_string<_CharT> moneypunct_byname<_CharT, _International>::do_negative_sign() const
781 {return moneypunct<_CharT,_International>::_M_nsign;}
783 template<class _CharT, bool _International>
784 int moneypunct_byname<_CharT, _International>::do_frac_digits() const
785 {return _Locale_int_frac_digitsE(_M_monetary);}
787 template<class _CharT, bool _International>
788 void moneypunct_byname<_CharT, _International>::Convert_string2_string_chart(basic_string<_CharT> &dst, string src)
790 int length = src.length();
791 const char* str = src.c_str();
792 for(int i = 0; i<length;i++)
794 dst.append(1, (_CharT)*str++);
800 template<class _CharT, bool _International>
801 moneypunct_byname<_CharT, _International>::moneypunct_byname(const char * name,
803 moneypunct<_CharT, _International>(refs),
804 _M_monetary(__acquire_monetaryE(name))
807 locale::_M_throw_runtime_error();
809 moneypunct<_CharT,_International>::_M_group = _Locale_mon_groupingE(_M_monetary);
810 Convert_string2_string_chart(moneypunct<_CharT,_International>::_M_psign, _Locale_positive_signE(_M_monetary));
811 Convert_string2_string_chart(moneypunct<_CharT,_International>::_M_nsign, _Locale_negative_signE(_M_monetary));
812 Convert_string2_string_chart(moneypunct<_CharT,_International>::_M_currSym, _Locale_int_curr_symbolE(_M_monetary));
815 template<class _CharT, bool _International>
816 moneypunct_byname<_CharT, _International>::~moneypunct_byname()
818 __release_monetaryE(_M_monetary);
827 #endif /* _STLP_MONETARY_C */