📄 istream.cc
字号:
#ifndef __STD_RW_ISTREAM_CC__
#define __STD_RW_ISTREAM_CC__
/***************************************************************************
*
* istream.cc - istream definitions
*
***************************************************************************
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Hewlett-Packard Company makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
*
***************************************************************************
*
* Copyright (c) 1994-1999 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.
*
**************************************************************************/
#ifndef _RWSTD_NO_NEW_HEADER
#include <cctype>
#else
#include <ctype.h>
#endif
#include <ios>
#ifndef _RWSTD_NO_NAMESPACE
namespace std {
#endif
/*
* basic_istream manipulators
* skips the next available white spaces
*/
template<class charT, class traits>
basic_istream<charT, traits>&
_RWSTDExportTemplate ws(basic_istream<charT, traits>& is)
{
_TYPENAME traits::int_type c;
#ifdef _RWSTD_MULTI_THREAD
if ( is.rdbuf() )
is.istream_sentry_guard = new _RWSTDGuard(is.rdbuf()->buffer_mutex_);
#ifndef _RWSTD_NO_EXCEPTIONS
try {
#endif
#endif // _RWSTD_MULTI_THREAD
#ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
const ctype<charT>& ct = use_facet< ctype<charT> >(is.getloc());
#else
const ctype<charT>& ct = use_facet(is.getloc(),(ctype<charT>*)0);
#endif
while((c = is.rdbuf()->sgetc()),
( !traits::eq_int_type(c,traits::eof()) && ct.is(ct.space,c) ))
is.rdbuf()->snextc();
if( traits::eq_int_type(c,traits::eof()) )
is.setstate(ios_base::eofbit);
#if defined (_RWSTD_MULTI_THREAD) && !defined (_RWSTD_NO_EXCEPTIONS)
} catch(...)
{
if ( is.rdbuf() )
delete is.istream_sentry_guard;
throw;
}
#endif // _RWSTD_MULTI_THREAD etc.
#ifdef _RWSTD_MULTI_THREAD
if ( is.rdbuf() )
delete is.istream_sentry_guard;
#endif
return is;
}
/*
* basic_istream(basic_streambuf *)
*/
template<class charT, class traits>
basic_istream<charT, traits>::
basic_istream(basic_streambuf<charT, traits> *sb)
: __chcount(0)
{
if ( sb )
{
if ( sb->which_open_mode() & ios_base::in )
this->init(sb);
else
this->init(0);
}
else
this->init(0);
}
/*
* basic_istream( )
*/
template<class charT, class traits>
basic_istream<charT, traits>::
basic_istream( )
: __chcount(0)
{
}
/*
* ~basic_istream();
*/
template<class charT, class traits>
basic_istream<charT, traits>::~basic_istream()
{
}
/*
* istream_type& operator>>(istream_type& (*pf)(istream_type&))
* for functions relating to istreams
*/
template<class charT, class traits>
basic_istream<charT, traits>&
basic_istream<charT, traits>::
operator>>(basic_istream<charT,traits>& (*pf)(basic_istream<charT,traits>&))
{
(*pf)(*this);
return *this;
}
/*
* istream_type& operator>>(ios_base& (*pf)(ios_base&))
* for manipulators relating to the ios_base class
*/
template<class charT, class traits>
basic_istream<charT, traits>&
basic_istream<charT, traits>::
operator>>(ios_base& (*pf)(ios_base&))
{
(*pf)(*this);
return *this;
}
/*
* istream_type& operator>>(ios_type& (*pf)(ios_type&))
* used to set one of the ios states (ios manipulator)
*/
template<class charT, class traits>
basic_istream<charT, traits>&
basic_istream<charT, traits>::
operator>>(basic_ios<charT, traits>& (*pf)(basic_ios<charT, traits>&))
{
(*pf)(*this);
return *this;
}
/*
* basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>, charT *)
* read in a charT *.
*/
template<class charT, class traits>
basic_istream<charT, traits>&
_RWSTDExportTemplate operator>>(basic_istream<charT, traits>& is, charT *p)
{
ios_base::iostate err = 0;
#ifndef _RWSTD_NO_EXCEPTIONS
try {
#endif
if ( p!=0 )
{
_TYPENAME basic_istream<charT, traits>::sentry ipfx(is);
if(ipfx)
{
charT *op = p;
_TYPENAME traits::int_type c = 0;
int len;
if ( is.width() == 0 ) len=0;
else len=is.width();
#ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
const ctype<charT>& ct = use_facet< ctype<charT> >(is.getloc());
#else
const ctype<charT>& ct = use_facet(is.getloc(), (ctype<charT>*)0);
#endif
while(--len &&
(c = is.rdbuf()->sgetc(),!( traits::eq_int_type(c,traits::eof()) ||
ct.is(ct.space,_RWSTD_STATIC_CAST(charT, c)))))
{
*p++ = _RWSTD_STATIC_CAST(charT, c);
is.rdbuf()->sbumpc();
}
if( traits::eq_int_type(c,traits::eof()) )
err = ((p == op) ?
(ios_base::eofbit | ios_base::failbit) : ios_base::eofbit);
*p = charT ('\0');
is.width(0);
}
}
else
err = ios_base::failbit;
#ifndef _RWSTD_NO_EXCEPTIONS
} // end try block
#endif
#ifndef _RWSTD_NO_EXCEPTIONS
catch(...)
{
bool flag = false;
try {
is.setstate(ios_base::badbit);
}
catch( ios_base::failure ) { flag= true; }
if ( flag ) throw;
}
#endif // _RWSTD_NO_EXCEPTIONS
if ( err ) is.setstate(err);
return is;
}
/*
* basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT& )
* read in a character
*/
template<class charT, class traits>
basic_istream<charT, traits>&
_RWSTDExportTemplate operator>>(basic_istream<charT,traits>& is, charT& c)
{
ios_base::iostate err = 0;
#ifndef _RWSTD_NO_EXCEPTIONS
try {
#endif
_TYPENAME basic_istream<charT, traits>::sentry ipfx(is);
if(ipfx)
{
_TYPENAME traits::int_type tmp = is.rdbuf()->sbumpc();
if ( traits::eq_int_type(traits::eof(),tmp) )
err = ios_base::failbit | ios_base::eofbit;
else
c = traits::to_char_type(tmp);
}
#ifndef _RWSTD_NO_EXCEPTIONS
} // end try block
#endif
#ifndef _RWSTD_NO_EXCEPTIONS
catch(...)
{
bool flag = false;
try {
is.setstate(ios_base::badbit);
}
catch( ios_base::failure ) { flag= true; }
if ( flag ) throw;
}
#endif // _RWSTD_NO_EXCEPTIONS
if ( err ) is.setstate(err);
return is;
}
#ifndef _RWSTD_NO_SIGNED_CHAR_IN_STREAMS
/*
* istream& operator>>(basic_istream<char,traits>&,unsigned char& )
*/
template <class traits>
basic_istream<char, traits>&
_RWSTDExportTemplate operator>>(basic_istream<char, traits>& is, unsigned char& uc)
{
is >> (char &)uc;
return is;
}
/*
* istream& operator>>(basic_istream<char, traits>&, signed char& )
*/
template <class traits>
basic_istream<char, traits>&
_RWSTDExportTemplate operator>>(basic_istream<char, traits>& is, signed char& sc)
{
is >> (char &)sc;
return is;
}
/*
* istream& operator>>(basic_istream<char, traits>&, unsigned char* )
*/
template <class traits>
basic_istream<char, traits>&
_RWSTDExportTemplate operator>>(basic_istream<char, traits>& is,unsigned char* uc)
{
is >> (char *)uc;
return is;
}
/*
* istream& operator>>(basic_istream<char, traits>&,signed char* )
*/
template <class traits>
basic_istream<char, traits>&
_RWSTDExportTemplate operator>>(basic_istream<char, traits>& is, signed char* sc)
{
is >> (char *)sc;
return is;
}
#endif // _RWSTD_NO_SIGNED_CHAR_IN_STREAMS
#ifndef _RWSTD_NO_BOOL
/*
* istream_type& operator>>(bool&)
*/
template<class charT, class traits>
basic_istream<charT, traits>&
basic_istream<charT, traits>::operator>>(bool& n)
{
ios_base::iostate err = 0;
#ifndef _RWSTD_NO_EXCEPTIONS
try {
#endif
_TYPENAME basic_istream<charT, traits>::sentry ipfx(*this);
if(ipfx)
{
#ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
use_facet<num_get<charT,istreambuf_iterator<charT,traits> > >(this->getloc())
#else
use_facet(this->getloc(),(num_get<charT,istreambuf_iterator<charT,traits> >*)0)
#endif
.get(istreambuf_iterator<charT,traits>(*this),istreambuf_iterator<charT,traits>(),*this,err,n);
}
#ifndef _RWSTD_NO_EXCEPTIONS
} // end of try block
#endif
#ifndef _RWSTD_NO_EXCEPTIONS
catch(...)
{
bool flag = false;
try {
this->setstate(ios_base::badbit);
}
catch( ios_base::failure ) { flag= true; }
if ( flag ) throw;
}
#endif // _RWSTD_NO_EXCEPTIONS
if ( err ) this->setstate(err);
return *this;
}
#endif // _RWSTD_NO_BOOL
/*
* istream_type& operator>>(short&)
*/
template<class charT, class traits>
basic_istream<charT, traits>&
basic_istream<charT, traits>::operator>>(short& n)
{
ios_base::iostate err = 0;
#ifndef _RWSTD_NO_EXCEPTIONS
try {
#endif
_TYPENAME basic_istream<charT, traits>::sentry ipfx(*this);
if(ipfx)
{
long l;
#ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
use_facet<num_get<charT,istreambuf_iterator<charT,traits> > >(this->getloc())
#else
use_facet(this->getloc(),(num_get<charT,istreambuf_iterator<charT,traits> >*)0)
#endif
.get(istreambuf_iterator<charT,traits>(*this),istreambuf_iterator<charT,traits>(),*this,err,l);
if ((err & (ios_base::failbit | ios_base::badbit)) == ios_base::goodbit)
{ if (this->flags() & (ios_base::hex | ios_base::oct))
{
unsigned short tmp_n = (unsigned short)l;
if ( tmp_n != l )
err |= ios_base::failbit;
else
n = _RWSTD_STATIC_CAST(short ,tmp_n);
}
else
{
short tmp_n = (short)l;
if ( tmp_n != l )
err |= ios_base::failbit;
else
n = tmp_n;
}
}
}
#ifndef _RWSTD_NO_EXCEPTIONS
} // end of try block
#endif
#ifndef _RWSTD_NO_EXCEPTIONS
catch(...)
{
bool flag = false;
try {
this->setstate(ios_base::badbit);
}
catch( ios_base::failure ) { flag= true; }
if ( flag ) throw;
}
#endif // _RWSTD_NO_EXCEPTIONS
if ( err ) this->setstate(err);
return *this;
}
/*
* istream_type& operator>>(unsigned short&)
*/
template<class charT, class traits>
basic_istream<charT, traits>&
basic_istream<charT, traits>::operator>>(unsigned short& n)
{
ios_base::iostate err = 0;
#ifndef _RWSTD_NO_EXCEPTIONS
try {
#endif
_TYPENAME basic_istream<charT, traits>::sentry ipfx(*this);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -