home *** CD-ROM | disk | FTP | other *** search
- /*-----------------------------------------------------------------------*
- * filename - istreamn.cpp
- * Class istream member functions
- *-----------------------------------------------------------------------*/
-
- /*[]------------------------------------------------------------[]*/
- /*| |*/
- /*| Turbo C++ Run Time Library - Version 1.0 |*/
- /*| |*/
- /*| |*/
- /*| Copyright (c) 1990 by Borland International |*/
- /*| All Rights Reserved. |*/
- /*| |*/
- /*[]------------------------------------------------------------[]*/
-
- #undef _BIG_INLINE_
- #include <iostream.h>
- #include <ctype.h>
- #include <stdlib.h>
-
-
- istream::istream()
- {
- gcount_ = 0;
- }
-
-
- istream::istream(streambuf* s)
- {
- ios::init(s);
- gcount_ = 0;
- }
-
-
- istream::~istream()
- {
- }
-
-
- // obsolete: set skip via format, tie via tie() function
- istream::istream(streambuf* s, int sk, ostream* t)
- {
- ios::init(s);
- gcount_ = 0;
- skip(sk);
- tie(t);
- }
-
-
- // set/read the get pointer's position
- istream& istream::seekg(streampos pos)
- {
- if( bad() || bp->seekpos(pos, ios::in) == EOF )
- setstate(ios::failbit);
- return *this;
- }
-
-
- istream& istream::seekg(streamoff off, seek_dir dir)
- {
- if( bad() || bp->seekoff(off, dir, ios::in) == EOF )
- setstate(ios::failbit);
- return *this;
- }
-
-
- streampos istream::tellg()
- {
- streampos p = EOF;
- if( bad() || (p = bp->seekoff(0, ios::cur, ios::in)) == EOF )
- setstate(ios::failbit);
- return p;
- }
-
-
- /*
- * Unformatted extraction operations
- */
-
- // extract consecutive whitespace
- void istream::eatwhite()
- {
- int c;
- while( (c = bp->sgetc()), isspace(c) ) {
- bp->stossc();
- ++gcount_;
- }
-
- if( c == EOF )
- setstate(ios::eofbit);
- }
-
-
- // implementation of ipfx
- // flush tied stream if needed, skip whitespace if needed
- int istream::do_ipfx(int need)
- {
- gcount_ = 0;
-
- if( ! good() )
- return 0;
-
- // flush tied stream if needed
- if ( tie() && (! need || need > bp->in_avail()) )
- tie()->flush();
-
- if( ! need && (ispecial & skipping) )
- eatwhite();
-
- return good();
- }
-
-
- // get next char, not immediately available
- signed char istream::do_get()
- {
- int c = bp->sbumpc();
- if( c == EOF )
- setstate(ios::eofbit | ios::failbit);
- else
- ++gcount_;
- return c;
- }
-
-
- // extract characters into an array, up to len chars or delimiter d
- istream& istream::get(signed char* p, int len, char d)
- {
- if( ipfx1() ) {
- signed char* op = p;
- int c = 0;
- while( --len > 0 && (c = bp->sgetc()) != d && c != EOF ) {
- *p++ = c;
- ++gcount_;
- bp->stossc();
- }
- if( c == EOF )
- setstate((p == op) ? (ios::eofbit | ios::failbit) : ios::eofbit);
- }
- *p = 0; // always terminate with a null
- return *this;
- }
-
-
- istream& istream::read(signed char* p, int len)
- {
- if( ipfx1() ) {
- int c = 0;
- while( --len >= 0 && (c = bp->sgetc()) != EOF ) {
- *p++ = c;
- ++gcount_;
- bp->stossc();
- }
- if( c == EOF )
- setstate((len >= 0) ? (ios::eofbit | ios::failbit) : ios::eofbit);
- }
- return *this;
- }
-
-
- // extract characters into an array up to and including delimeter d
- istream& istream::getline(signed char* p, int len, char d)
- {
- if( ipfx1() ) {
- signed char* op = p;
- int c = 0;
- while( --len > 0 && (c = bp->sgetc()) != EOF ) {
- *p++ = c;
- ++gcount_;
- bp->stossc();
- if( c == d )
- break;
- }
- if( c == EOF )
- setstate((p == op) ? (ios::eofbit | ios::failbit) : ios::eofbit);
- }
- *p = 0; // always terminate with a null
- return *this;
- }
-
-
- // extract characters into a streambuf up to delimiter
- istream& istream::get(streambuf& s, char d)
- {
- if( ipfx1() ) {
- int c;
- int err = 0;
- while( (c = bp->sgetc()) != d && c != EOF ) {
- if( s.sputc(c) == EOF ) {
- err = 1;
- break;
- }
- ++gcount_;
- bp->stossc();
- }
- if( c == EOF )
- setstate(ios::eofbit);
- if( err )
- setstate(ios::failbit);
- }
- return *this;
- }
-
-
- // push back char into input
- istream& istream::putback(char c)
- {
- if( ! fail() )
- if( bp->sputbackc(c) == EOF )
- setstate(ios::failbit);
- else
- clear(rdstate() & ~ios::eofbit);
- return *this;
- }
-
-
- // extract and discard chars but stop at delim
- istream& istream::ignore(int n, int d)
- {
- if( ipfx1() ) {
- int c = 0;
- while( --n >= 0 && (c = bp->sgetc()) != d && c != EOF ) {
- ++gcount_;
- bp->stossc();
- }
- if( c == EOF )
- setstate(ios::eofbit);
- }
- return *this;
- }
-
-
- // manipulators
- istream& istream::operator>> (ios& (*f)(ios&) )
- {
- (*f)(*((ios*)this));
- return *this;
- }
-
-
- // extract whitespace characters
- istream& ws(istream& is)
- {
- long f = is.flags();
- is.setf(ios::skipws, ios::skipws);
- is.ipfx0();
- is.flags(f);
- return is;
- }
-
-
- /*
- * Formatted extraction operations
- */
-
- istream& istream::operator>> (signed char* p)
- {
- if( ipfx0() ) {
- signed char* op = p;
- int c = 0;
- int len = width(0); // <=0 means no limit
- while( --len && (c = bp->sgetc(), ! isspace(c)) && c != EOF ) {
- *p++ = c;
- bp->stossc();
- }
- if( c == EOF )
- setstate((p == op) ? (ios::eofbit | ios::failbit) : ios::eofbit);
- *p = 0; // always terminate with a null
- }
- return *this;
- }
-
-
- // extract from this istream, insert into streambuf
- istream& istream::operator>> (streambuf* s)
- {
- enum errkind { noerr, nodata, putfail } err;
- if( ipfx0() ) {
- err = nodata;
- int c;
- while( (c = bp->sgetc()) != EOF ) {
- if( s->sputc(c) == EOF ) {
- err = putfail;
- break;
- }
- err = noerr;
- bp->stossc();
- }
- int errstate = ( err == noerr ) ? 0 : ios::failbit;
- if( c == EOF ) {
- errstate |= ios::eofbit;
- if( err == nodata )
- errstate |= ios::badbit;
- }
- if( errstate )
- setstate(errstate);
- }
- return *this;
- }
-
-
- /*
- * istream with assign
- */
-
- // does no initialization
- istream_withassign::istream_withassign() :
- istream()
- {
- }
-
-
- istream_withassign::~istream_withassign()
- {
- }
-
-
- // gets buffer from istream and does entire initialization
- istream_withassign& istream_withassign::operator= (istream& is)
- {
- ios::init(is.rdbuf());
- return *this;
- }
-
-
- // associates streambuf with stream and does entire initialization
- istream_withassign& istream_withassign::operator= (streambuf* s)
- {
- ios::init(s);
- return *this;
- }
-
-
- /*
- * The following functions may be inline, but in case they aren't,
- * here they are as global closed subroutines.
- */
-
- istream& istream::operator>> (unsigned char& c)
- {
- if( ipfx0() )
- c = bp->in_avail() ? bp->sbumpc() : do_get();
- return *this;
- }
-
-
- istream& istream::operator>> (signed char& c)
- {
- if( ipfx0() )
- c = bp->in_avail() ? bp->sbumpc() : do_get();
- return *this;
- }
-
-
- istream& istream::get(unsigned char& c)
- {
- if( ipfx1() )
- if( bp->in_avail() ) {
- gcount_ = 1;
- c = bp->sbumpc();
- }
- else c = do_get();
- return *this;
- }
-
-
- istream& istream::get(signed char& c)
- {
- if( ipfx1() )
- if( bp->in_avail()) {
- gcount_ = 1;
- c = bp->sbumpc();
- }
- else c = do_get();
- return *this;
- }
-
-
- int istream::get()
- {
- if( ipfx1() ) {
- int c = bp->sbumpc();
- if( c == EOF )
- setstate(ios::eofbit);
- else
- gcount_ = 1;
- return c;
- }
- else return EOF;
- }
-