⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _money.cc

📁 realview22.rar
💻 CC
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************
 *
 * _money.cc - Definitions for the Standard Library money facets
 *
 * $Id: _money.cc,v 1.2 2002/12/09 16:32:01 vkorstan Exp $
 *
 ***************************************************************************
 *
 * Copyright (c) 1994-2001 Rogue Wave Software, Inc.  All Rights Reserved.
 *
 * This computer software is owned by Rogue Wave Software, Inc. and is
 * protected by U.S. copyright laws and other laws and by international
 * treaties.  This computer software is furnished by Rogue Wave Software,
 * Inc. pursuant to a written license agreement and may be used, copied,
 * transmitted, and stored only in accordance with the terms of such
 * license and with the inclusion of the above copyright notice.  This
 * computer software or any other copies thereof may not be provided or
 * otherwise made available to any other person.
 *
 * U.S. Government Restricted Rights.  This computer software is provided
 * with Restricted Rights.  Use, duplication, or disclosure by the
 * Government is subject to restrictions as set forth in subparagraph (c)
 * (1) (ii) of The Rights in Technical Data and Computer Software clause
 * at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
 * Commercial Computer Software--Restricted Rights at 48 CFR 52.227-19,
 * as applicable.  Manufacturer is Rogue Wave Software, Inc., 5500
 * Flatiron Parkway, Boulder, Colorado 80301 USA.
 *
 **************************************************************************/

#include _RWSTD_CWCHAR

_RWSTD_NAMESPACE_BEGIN (__rw)

_USING (_RWSTD_C::size_t);

// ------------------------------------------------------
// Template __rw_moneypunct_impl_data<_CharT> member templates.
// ------------------------------------------------------

template <class _CharT>
__rw_moneypunct_impl_data<_CharT>::__rw_moneypunct_impl_data
    (const __rw_moneypunct_init_data<_CharT> *__mp)
{
  if (!__mp) {
    this->_C_dp = _CharT('.');
    this->_C_ts = _CharT(',');
    _C_fd       = 0;
  } else {
    this->_C_dp = __mp->_C_dp;
    this->_C_ts = __mp->_C_ts;
    this->_C_gr = __mp->_C_gr;
    _C_cs       = __mp->_C_cs;
    _C_ps       = __mp->_C_ps;
    _C_ns       = __mp->_C_ns;
    _C_fd       = __mp->_C_fd;
    _C_pf       = __mp->_C_pf;
    _C_nf       = __mp->_C_nf;

    if (__mp->_C_del)
        delete[] _RWSTD_CONST_CAST (char*,
                                    _RWSTD_REINTERPRET_CAST (const char*,
                                                             __mp));
  }
}


template <class _CharT>
const __rw_moneypunct_init_data<_CharT>*
__rw_fixup_moneypunct_init (const __rw_moneypunct_init_data<char> *__mp,
                            _CharT*)
{
    if (!__mp)
        return 0;

    typedef _STD::char_traits<char>                      _CharTraits;
    typedef _STD::char_traits<_CharT>                    _Traits;
    typedef _STD::ctype<_CharT>                          _Ctype;
    typedef _STD::codecvt<_CharT, char, _STD::mbstate_t> _Codecvt;
    typedef _STD::basic_string<_CharT, _Traits, _STD::allocator<_CharT> >
        _String;

    _STD::locale __loc = _STD::locale::classic ();

    const _Codecvt &__cvt = _USE_FACET (_Codecvt, __loc);

    _String __cs = __cvt.in (__mp->_C_cs);
    _String __ps = __cvt.in (__mp->_C_ps);
    _String __ns = __cvt.in (__mp->_C_ns);

    size_t __xtra_chars  = _CharTraits::length (__mp->_C_gr);
    size_t __xtra_charTs = __cs.length () +__ps.length () + __ns.length() + 3;

    __rw_moneypunct_init_data<_CharT> *__res;
   __res = _RWSTD_REINTERPRET_CAST (__rw_moneypunct_init_data<_CharT>*, 
                                 new char [  sizeof (*__res)
                                           + __xtra_chars
                                           + __xtra_charTs * sizeof(_CharT)]);

    const _Ctype &__ctp = _USE_FACET (_Ctype, __loc);

    __res->_C_del = true;
    __res->_C_dp  = __ctp.widen (__mp->_C_dp);
    __res->_C_ts  = __ctp.widen (__mp->_C_ts);
    __res->_C_fd  = __mp->_C_fd;
    __res->_C_pf  = __mp->_C_pf;
    __res->_C_nf  = __mp->_C_nf;

    size_t __n;
    _CharT *__p = (_CharT*)(__res + 1);
    __res->_C_cs = _Traits::copy (__p, __cs.c_str (), __n = __cs.length ());
    *(__p += __n)++ = 0;
    __res->_C_ps = _Traits::copy (__p, __ps.c_str (), __n = __ps.length ());
    *(__p += __n)++ = 0;
    __res->_C_ns = _Traits::copy (__p, __ns.c_str (), __n = __ns.length ());
    *(__p += __n)++ = 0;

    __res->_C_gr = _CharTraits::copy (_RWSTD_REINTERPRET_CAST (char*,
                                                               __p + __n), 
                                      __mp->_C_gr, 
                                      _CharTraits::length (__mp->_C_gr));

    if (__mp->_C_del)
        // redundant const_cast for compilers such as MSVC 6.0
        // that do not conform to 5.3.5, p2, Note 2 of the IS
        delete[] _RWSTD_CONST_CAST (char*, 
                                    _RWSTD_REINTERPRET_CAST (const char*, 
                                                             __mp));

    return __res;
}


template <class _CharT>
const __rw_moneypunct_init_data<_CharT>*
__rw_moneypunct_impl_data<_CharT>::
_C_get_init_by_name_ (const char *__name, bool __intl)
{
    return __rw_fixup_moneypunct_init (_C_get_named_init (__name, __intl),
                                       (_CharT*)0);
}


// -----------------------------------------------------------------
// Template class __rw_money_handler_base_1<_CharT> member templates.
// -----------------------------------------------------------------

template <class _CharT>
const __rw_moneypunct_impl_data<_CharT>&
__rw_money_handler_base_1<_CharT>::
_C_get_punct_data (const locale &__loc, bool __intl)
{
    if (__intl) {
        typedef _STD::moneypunct<_CharT, true> _MoneyPunct;
        return _USE_FACET (_MoneyPunct, __loc);
    }
    else {
        typedef _STD::moneypunct<_CharT, false> _MoneyPunct;
        return _USE_FACET (_MoneyPunct, __loc);
    }
}

// ----------------------------------------------------------------
// Template class __rw_money_reader_base_1<_CharT> member templates.
// ----------------------------------------------------------------

template <class _CharT>
void __rw_money_reader_base_1<_CharT>::_C_get_money_string
    (string_type &__res, const char *__end)
{
    _RWSTD_ASSERT (__end >= reader._C_digits);

    _RWSTD_C::size_t __len = __end - reader._C_digits;
    if (reader._C_state & reader._C_negative)
        __len++;

  __res.resize(0);
  __res.reserve(__len);

  if (reader._C_state & reader._C_negative)
    __res+=reader._C_dmap._C_punct_chars[__rw_digit_map_base::_C_minus];

  const char *__d=reader._C_digits;
  const char *__last=__end-1;
  while (__d<__last && *__d==0) __d++;
  for ( ; __d<=__last; __d++)
    __res+=reader._C_dmap._C_digit_chars[*__d];
}

// ---------------------------------------------------------------------------
// Template class __rw_money_reader_base_2<CharT, InputIter> member templates.
// ---------------------------------------------------------------------------

template <class _CharT, class _InputIter>
__rw_money_reader_base_2<_CharT, _InputIter>::
__rw_money_reader_base_2 (_InputIter &__begin, _InputIter &__end, 
                          _STD::ios_base &__flags,
                          const __rw_moneypunct_impl_data<_CharT> &__mp)
 :  __rw_digit_reader<_CharT, _InputIter>(__begin, __end, __flags, __mp), 
    __rw_money_reader_base_1<_CharT>(_C_this_as_digit_reader(), __mp)
{
    this->_C_base   = 10;
    this->_C_state |= this->_C_signed;
}


template <class _CharT, class _InputIter>
char* __rw_money_reader_base_2<_CharT, _InputIter>::
    _C_get_money_digits (void)
{
  _CharT __c;
  // Always parse input according to negative format.
  const _STD::money_base::pattern &patt = this->_C_get_neg_format();

  // If this ends up non-0, it points to trailing sign char(s) that are
  // required at the end of the pattern.
  const _CharT *__sign = 0;

  bool need_curr = 0 != (this->_C_io.flags() & _STD::ios_base::showbase);
  bool got_curr=false;
  int __frac=this->_C_get_frac_digits();

  char *__end = this->_C_digits;
  const char *__p=patt.field, *__pend=__p+sizeof patt.field-1;

  for ( ; !this->_C_error && __p<=__pend; __p++) {
    switch (*__p) {
     case _STD::money_base::space:
     case _STD::money_base::none:
      while (   !this->eof()
             && this->_C_ctyp.is (_STD::ctype_base::space, *this->_C_in))
        this->_C_in++;
      break;

     case _STD::money_base::symbol:
      if (!this->eof()) {
        const _CharT *curr=this->_C_get_curr_symbol().c_str();
        if ((__c=*this->_C_in)==*curr) {
          // We ate a character, so rest of symbol must be present.
          need_curr=true;
          do {
            this->_C_in++;
            if (*++curr==_CharT('\0')) {
              got_curr=true;
              break;
            }
          } while (!this->eof() && (__c=*this->_C_in)==*curr);
        }
      }
      if (need_curr && !got_curr)
        this->_C_error|=this->_C_bad_curr_symbol;
      break;

     case _STD::money_base::sign:
      if (!this->eof()) {
        __sign = this->_C_get_negative_sign().c_str();
        if ((__c = *this->_C_in) == *__sign) {
          this->_C_state |= this->_C_negative;
          __sign++;
          this->_C_in++;
        } else {
          __sign=this->_C_get_positive_sign().c_str();
          if (__c == *__sign) {
            __sign++;
            this->_C_in++;
          } else
            __sign = 0;
        }
      }
      break;

     case _STD::money_base::value:
      __end=this->_C_get_digits (int ());
      if (!this->_C_error && __frac && !this->eof() &&
          *this->_C_in==this->_C_get_decimal_point())
      {
        this->_C_in++;
        char *frac_start=__end;
        __end=this->_C_get_digit_string(__end);
        if (!this->_C_error) {
          __frac-=int(__end-frac_start);
          if (__frac<0) {
            // Too many fractional digits in input.  We truncate.  The
            // alternative would be to return an error.
            __end+=__frac;
            __frac=0;
          }
        }
      }
      if (__end == this->_C_digits)
        this->_C_error|=this->_C_no_digits;
      else if (!this->_C_error && __frac > 0) {
        // Add trailing zeros until the correct number of fractional digits
        // is reached.

        if (__end == this->_C_digits + this->_C_bufsize)
            __end = this->_C_realloc ();

        do
            *__end++ = 0;
        while (--__frac);
      }
      break;
    }
  }

  if (__sign && !this->_C_error)
    // We ate one _CharT of a sign earlier, rest of sign must be present at end.
    while (*__sign)
      if (!this->eof() && *this->_C_in == *__sign++)
        this->_C_in++;
      else {
        this->_C_error|=this->_C_bad_sign;
        break;
      }

  this->_C_frac_beg=this->_C_exp_beg=__end;
  return __end;
}

// ------------------------------------------------------------------
// Template __rw_money_reader<_CharT, _InputIter> member templates.
// ------------------------------------------------------------------

// ----------------------------------------------------------
// Template __rw_money_writer_base_1<_CharT> member templates.
// ----------------------------------------------------------


template <class _CharT>
void __rw_money_writer_base_1<_CharT>::_C_put_money (_CharT __fill)
{
    const char *__start = writer._C_start;

    bool __neg = '-' == *__start;   // negative?

    if ('-' == *__start || '+' == *__start || ' ' == *__start)
        ++__start;

    // small auto buffer for the average case
    _CharT __wide_buf [__rw_digit_writer_base_1<_CharT>::_C_DEF_BUFSIZE];

    // required buffer size
    const size_t __size = writer._C_end - __start;

    // allocate more space if necessary
    _CharT *__pbuf = __size > __rw_digit_writer_base_1<_CharT>::_C_DEF_BUFSIZE ?
        new _CharT [__size] : __wide_buf;

    // widen into temporary buffer
    writer._C_ctyp.widen (__start, writer._C_end, __pbuf);

    // reformat as required

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -