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

📄 istream.cc

📁 realview22.rar
💻 CC
📖 第 1 页 / 共 2 页
字号:

    return *this;
}



// 27.6.1.3, p28
template<class _CharT, class _Traits>
basic_istream<_CharT, _Traits>&
basic_istream<_CharT, _Traits>::read (char_type *__s, streamsize __n)
{
    _RWSTD_ASSERT (0 <= __n);
    _RWSTD_ASSERT (0 != this->rdbuf ());

    // 27.6.1.3, p28: sets ios_base::failbit if !this->good()
    const sentry __ipfx (*this, true);

    if (__ipfx) {
        streamsize __nread = this->rdbuf ()->sgetn (__s, __n);

        if (__nread >= 0)
            _C_chcount = __nread;

        // 27.6.1.2.1, p1 and 27.6.1.3, p1: proceed iff sentry is okay
        if (__n != __nread)
            this->setstate (ios_base::eofbit | ios_base::failbit);
    }

    return *this;
}


template<class _CharT, class _Traits>
streamsize basic_istream<_CharT, _Traits>::
readsome (char_type *__s, streamsize __n)
{
    _RWSTD_ASSERT (0 != __s);
    _RWSTD_ASSERT (0 <= __n);
    _RWSTD_ASSERT (0 != this->rdbuf());

    _C_chcount = 0;

    if (!this->good ()) {
        this->setstate (ios_base::failbit);
        return 0;
    }
    
    streamsize __navail = this->rdbuf ()->in_avail ();
   
    if(-1 == __navail) {   
        this->setstate (ios_base::eofbit);
        return 0;
    }

    if (0 == __navail)
        return 0;

    if (__n > __navail)
        __n = __navail;

    read (__s, __n);
    return __n;
}


template<class _CharT, class _Traits>
_TYPENAME basic_istream<_CharT, _Traits>::pos_type
basic_istream<_CharT, _Traits>::tellg ()
{
    pos_type __p = off_type (-1);

    if (!this->fail ()) {

        _TRY {
            __p = this->rdbuf ()->pubseekoff (0, ios_base::cur, ios_base::in);
          }
        _CATCH (...) {
            if (this->setstate (ios_base::badbit, 0 /* no throw */))
                _RETHROW;
        }
    }

    return __p;
}


template<class _CharT, class _Traits>
basic_istream<_CharT, _Traits>&
basic_istream<_CharT, _Traits>::putback (char_type __c)
{
    ios_base::iostate __err = ios_base::goodbit;

    if (this->rdbuf ()) {
        
        sentry __ipfx (*this, true);

        if (__ipfx) {
      
            _TRY {
                if (traits_type::eq_int_type (this->rdbuf ()->sputbackc (__c), 
                                              traits_type::eof ()))
                    __err = ios_base::badbit;
            }
            _CATCH (...) {
                if (this->setstate (ios_base::badbit, 0 /* no throw */))
                    _RETHROW;
            }
        }
    }
    else
        __err = ios_base::badbit;

    if (__err)
        this->setstate (__err);

    return *this;
}


template<class _CharT, class _Traits>
basic_istream<_CharT, _Traits>&
basic_istream<_CharT, _Traits>::unget ()
{
    ios_base::iostate __err = ios_base::goodbit;

    if (this->rdbuf ()) {

        sentry __ipfx (*this, true);

        if (__ipfx) {

            _TRY {

                if (traits_type::eq_int_type (this->rdbuf ()->sungetc (), 
                                              traits_type::eof ()))
                    __err = ios_base::badbit;  
            }
            _CATCH (...) {
                if (this->setstate (ios_base::badbit, 0 /* no throw */))
                    _RETHROW;
            }
        }
    }
    else
        __err = ios_base::badbit;

    if (__err)
        this->setstate (__err);
    
    return *this;
}


template<class _CharT, class _Traits>
int basic_istream<_CharT, _Traits>::sync ()
{
    if (!this->rdbuf ())
        return -1;

    sentry __ipfx (*this, true);

    if (__ipfx) {

        _TRY {
            if (-1 != this->rdbuf ()->pubsync ())
                return 0;
        }
        _CATCH (...) {
            if (this->setstate (ios_base::badbit, 0 /* no throw */))
                _RETHROW;
        }

        this->setstate (ios_base::badbit);
    }

    return traits_type::eof ();
}


template<class _CharT, class _Traits, class _Allocator>
basic_istream<_CharT, _Traits>&
operator>> (basic_istream<_CharT, _Traits>&            __is, 
            basic_string<_CharT, _Traits, _Allocator>& __str)
{
    _RWSTD_ASSERT (0 != __is.rdbuf ());

    ios_base::iostate __err = ios_base::goodbit;

    _TRY {

        _TYPENAME basic_istream<_CharT, _Traits>::sentry __ipfx (__is);

        if (__ipfx) {

            // FIXME: code commented out to work around an HP aCC 3.14.10
            // bug #JAGac86264

            // typedef _TYPENAME
            //     basic_string<_CharT, _Traits, _Allocator>::size_type

            size_t __maxlen =
                __is.width () ? __is.width () : __str.max_size ();

            size_t __i = 0;

            __str.erase ();
            __str.resize (32);

            while (__maxlen != __i) {

                _TYPENAME _Traits::int_type __c = __is.rdbuf ()->sgetc ();

                if (_Traits::eq_int_type (__c, _Traits::eof ())) {
                    __err = ios_base::eofbit;
                    break;
                }

                // convert to char_type so that isspace works correctly
                _TYPENAME _Traits::char_type
                    __ch = _Traits::to_char_type (__c);

                if ((isspace)(__ch, __is.getloc ()))
                    break;

                __is.rdbuf ()->sbumpc ();

                if (__str.size () == __i)
                    __str.resize (__i * 2);

                _Traits::assign (__str [__i++], __ch);
            }
            __str.resize (__i);

            __is.width (0);

            if (!__i)
                __err |= ios_base::failbit;
        }
    }
    _CATCH (...) {
        if (__is.setstate (ios_base::badbit, 0 /* no throw */))
            _RETHROW;
    }

    if (__err)
        __is.setstate (__err);

    return __is;
}  


template<class _CharT, class _Traits, class _Allocator>
basic_istream<_CharT, _Traits>&
getline (basic_istream<_CharT, _Traits>&            __is, 
         basic_string<_CharT, _Traits, _Allocator>& __str, 
         _CharT                                     __delim)
{
    _RWSTD_ASSERT (0 != __is.rdbuf ());

    _TYPENAME basic_istream<_CharT, _Traits>::sentry __ipfx (__is, true);

    if (__ipfx) { 

        ios_base::iostate __err = ios_base::failbit;

        _TRY {

            // FIXME: code commented out to work around an HP aCC 3.14.10
            // bug #JAGac86264

            // typedef _TYPENAME
            //     basic_string<_CharT, _Traits, _Allocator>::size_type

            size_t __i = 0;

            __str.erase ();
            __str.resize (32);

            while (__str.max_size () != __i) {

                _TYPENAME _Traits::int_type __c = __is.rdbuf ()->sbumpc ();

                if (_Traits::eq_int_type (__c, _Traits::eof ())) {
                    // 21.3.7.9, p7
                    __err =   ios_base::eofbit
                            | (__i ? ios_base::goodbit : ios_base::failbit);
                    break;
                }

                if (_Traits::eq (_Traits::to_char_type (__c), __delim)) {
                    __err = ios_base::goodbit;
                    break;
                }

                if (__str.size () == __i)
                    __str.resize (__i * 2);

                _Traits::assign (__str [__i++], _Traits::to_char_type (__c));
            }
            __str.resize (__i);   // excluding __delim
        }
        _CATCH (...) {
            if (__is.setstate (ios_base::badbit, 0 /* no throw */))
                _RETHROW;
        }

        if (__err)
            __is.setstate (__err);
    }

    return __is;
}  


_RWSTD_NAMESPACE_END   // std

⌨️ 快捷键说明

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