_istream.c
来自「stl的源码」· C语言 代码 · 共 1,430 行 · 第 1/4 页
C
1,430 行
/* * Copyright (c) 1999 * Silicon Graphics Computer Systems, Inc. * * Copyright (c) 1999 * Boris Fomitchev * * This material is provided "as is", with absolutely no warranty expressed * or implied. Any use is at your own risk. * * Permission to use or copy this software for any purpose is hereby granted * without fee, provided the above notices are retained on all copies. * Permission to modify the code and to distribute modified code is granted, * provided the above notices are retained, and a notice that the code was * modified is included with the above copyright notice. * */#ifndef _STLP_ISTREAM_C#define _STLP_ISTREAM_C#ifndef _STLP_INTERNAL_ISTREAM# include <stl/_istream.h>#endif#ifndef _STLP_INTERNAL_LIMITS# include <stl/_limits.h>#endif#ifndef _STLP_INTERNAL_NUM_GET_H# include <stl/_num_get.h>#endif#if defined ( _STLP_NESTED_TYPE_PARAM_BUG )// no wchar_t is supported for this mode# define __BIS_int_type__ int# define __BIS_pos_type__ streampos# define __BIS_off_type__ streamoff#else# define __BIS_int_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_istream<_CharT, _Traits>::int_type# define __BIS_pos_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_istream<_CharT, _Traits>::pos_type# define __BIS_off_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_istream<_CharT, _Traits>::off_type#endif_STLP_BEGIN_NAMESPACE//----------------------------------------------------------------------// Function object structs used by some member functions._STLP_MOVE_TO_PRIV_NAMESPACEtemplate <class _Traits>struct _Is_not_wspace { typedef typename _Traits::char_type argument_type; typedef bool result_type; const ctype<argument_type>* _M_ctype; _Is_not_wspace(const ctype<argument_type>* __c_type) : _M_ctype(__c_type) {} bool operator()(argument_type __c) const { return !_M_ctype->is(ctype_base::space, __c); }};template <class _Traits>struct _Is_wspace_null { typedef typename _Traits::char_type argument_type; typedef bool result_type; const ctype<argument_type>* _M_ctype; _Is_wspace_null(const ctype<argument_type>* __c_type) : _M_ctype(__c_type) {} bool operator()(argument_type __c) const { return _Traits::eq(__c, argument_type()) || _M_ctype->is(ctype_base::space, __c); }};template <class _Traits>struct _Scan_for_wspace { typedef typename _Traits::char_type char_type; typedef char_type* first_argument_type; typedef char_type* second_argument_type; typedef char_type* result_type; const ctype<char_type>* _M_ctype; _Scan_for_wspace(const ctype<char_type>* __ctype) : _M_ctype(__ctype) {} const char_type* operator()(const char_type* __first, const char_type* __last) const { return _M_ctype->scan_is(ctype_base::space, __first, __last); }};template <class _Traits>struct _Scan_wspace_null { typedef typename _Traits::char_type char_type; typedef char_type* first_argument_type; typedef char_type* second_argument_type; typedef char_type* result_type; const ctype<char_type>* _M_ctype; _Scan_wspace_null(const ctype<char_type>* __c_type) : _M_ctype(__c_type) {} const char_type* operator()(const char_type* __first, const char_type* __last) const { __last = find_if(__first, __last, _Eq_char_bound<_Traits>(char_type())); return _M_ctype->scan_is(ctype_base::space, __first, __last); }};template <class _Traits>struct _Scan_for_not_wspace { typedef typename _Traits::char_type char_type; typedef char_type* first_argument_type; typedef char_type* second_argument_type; typedef char_type* result_type; const ctype<char_type>* _M_ctype; _Scan_for_not_wspace(const ctype<char_type>* __c_type) : _M_ctype(__c_type) {} const char_type* operator()(const char_type* __first, const char_type* __last) const { return _M_ctype->scan_not(ctype_base::space, __first, __last); }};template <class _Traits>struct _Scan_for_char_val { typedef typename _Traits::char_type char_type; typedef char_type* first_argument_type; typedef char_type* second_argument_type; typedef char_type* result_type; char_type _M_val; _Scan_for_char_val(char_type __val) : _M_val(__val) {} const char_type* operator()(const char_type* __first, const char_type* __last) const { return find_if(__first, __last, _Eq_char_bound<_Traits>(_M_val)); }};template <class _Traits>struct _Scan_for_int_val { typedef typename _Traits::char_type char_type; typedef typename _Traits::int_type int_type; typedef char_type* first_argument_type; typedef char_type* second_argument_type; typedef char_type* result_type; int_type _M_val; _Scan_for_int_val(int_type __val) : _M_val(__val) {} const char_type* operator()(const char_type* __first, const char_type* __last) const { return find_if(__first, __last, _Eq_int_bound<_Traits>(_M_val)); }};// Helper function: try to push back a character to a streambuf,// return true if the pushback succeeded. Does not throw.template <class _CharT, class _Traits>bool _STLP_CALL__pushback(basic_streambuf<_CharT, _Traits>* __buf, _CharT __c) { bool ret; _STLP_TRY { const typename _Traits::int_type __eof = _Traits::eof(); ret = !_Traits::eq_int_type(__buf->sputbackc(__c), __eof); } _STLP_CATCH_ALL { ret = false; } return ret;}//----------------------------------------------------------------------// Definitions of basic_istream<>'s noninline member functions.// Helper function for formatted input of numbers.template <class _CharT, class _Traits, class _Number>ios_base::iostate _STLP_CALL__get_num(basic_istream<_CharT, _Traits>& __that, _Number& __val) { typedef typename basic_istream<_CharT, _Traits>::sentry _Sentry; ios_base::iostate __err = 0; _Sentry __sentry( __that ); // Skip whitespace. if (__sentry) { typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> > _Num_get; _STLP_TRY { // Do not remove additional parenthesis around use_facet instanciation, some compilers (VC6) // require it when building the library. (use_facet<_Num_get>(__that.getloc())).get(istreambuf_iterator<_CharT, _Traits>(__that.rdbuf()), 0, __that, __err, __val); } _STLP_CATCH_ALL { __that._M_handle_exception(ios_base::badbit); } if (__err) __that.setstate(__err); } return __err;}_STLP_MOVE_TO_STD_NAMESPACEtemplate <class _CharT, class _Traits>basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (short& __val) { long __lval; _STLP_PRIV __get_num(*this, __lval); if ( this->fail() ) { return *this; } short __tmp = __STATIC_CAST(short, __lval); unsigned short __uval = __STATIC_CAST(unsigned short, __lval); // check if we lose digits // if ((__val != __lval) && ((unsigned short)__val != __lval)) if ((__tmp != __lval) && ((long)__uval != __lval)) this->setstate(ios_base::failbit); else __val = __tmp; return *this;}template <class _CharT, class _Traits>basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (int& __val) { long __lval; _STLP_PRIV __get_num(*this, __lval); if ( this->fail() ) { return *this; } int __tmp = __lval; unsigned int __uval = __lval; // check if we lose digits // if ((__val != __lval) && ((unsigned int)__val != __lval)) if ((__tmp != __lval) && ((long)__uval != __lval)) this->setstate(ios_base::failbit); else __val = __tmp; return *this;}template <class _CharT, class _Traits>basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (unsigned short& __val) { _STLP_PRIV __get_num(*this, __val); return *this;}template <class _CharT, class _Traits>basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (unsigned int& __val) { _STLP_PRIV __get_num(*this, __val); return *this;}template <class _CharT, class _Traits>basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (long& __val) { _STLP_PRIV __get_num(*this, __val); return *this;}template <class _CharT, class _Traits>basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (unsigned long& __val) { _STLP_PRIV __get_num(*this, __val); return *this;}#if defined (_STLP_LONG_LONG)template <class _CharT, class _Traits>basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (_STLP_LONG_LONG& __val) { _STLP_PRIV __get_num(*this, __val); return *this;}template <class _CharT, class _Traits>basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (unsigned _STLP_LONG_LONG& __val) { _STLP_PRIV __get_num(*this, __val); return *this;}#endiftemplate <class _CharT, class _Traits>basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (float& __val) { _STLP_PRIV __get_num(*this, __val); return *this;}template <class _CharT, class _Traits>basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (double& __val) { _STLP_PRIV __get_num(*this, __val); return *this;}#if !defined (_STLP_NO_LONG_DOUBLE)template <class _CharT, class _Traits>basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (long double& __val) { _STLP_PRIV __get_num(*this, __val); return *this;}#endif#if !defined (_STLP_NO_BOOL)template <class _CharT, class _Traits>basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (bool& __val) { _STLP_PRIV __get_num(*this, __val); return *this;}#endiftemplate <class _CharT, class _Traits>basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>> (void*& __val) { _STLP_PRIV __get_num(*this, __val); return *this;}// Unformatted inputtemplate <class _CharT, class _Traits>__BIS_int_type__basic_istream<_CharT, _Traits>::peek() { typename _Traits::int_type __tmp = _Traits::eof(); this->_M_gcount = 0; sentry __sentry(*this, _No_Skip_WS()); if (__sentry) { _STLP_TRY { __tmp = this->rdbuf()->sgetc(); } _STLP_CATCH_ALL { this->_M_handle_exception(ios_base::badbit); } if (this->_S_eof(__tmp)) this->setstate(ios_base::eofbit); } return __tmp;}template <class _CharT, class _Traits>__BIS_int_type__basic_istream<_CharT, _Traits>::get() { typename _Traits::int_type __tmp = _Traits::eof(); sentry __sentry(*this, _No_Skip_WS()); this->_M_gcount = 0; if (__sentry) { _STLP_TRY { __tmp = this->rdbuf()->sbumpc(); } _STLP_CATCH_ALL { this->_M_handle_exception(ios_base::badbit); } if (!this->_S_eof(__tmp)) this->_M_gcount = 1; } if (_M_gcount == 0) this->setstate(ios_base::eofbit | ios_base::failbit);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?