home *** CD-ROM | disk | FTP | other *** search
- /*-----------------------------------------------------------------------*
- * filename - istreami.cpp
- * Class istream member functions for extracting integral types
- *-----------------------------------------------------------------------*/
-
- /*[]------------------------------------------------------------[]*/
- /*| |*/
- /*| Turbo C++ Run Time Library - Version 1.0 |*/
- /*| |*/
- /*| |*/
- /*| Copyright (c) 1990 by Borland International |*/
- /*| All Rights Reserved. |*/
- /*| |*/
- /*[]------------------------------------------------------------[]*/
-
- /*
- To reduce the total amount of code, shorts and ints are extracted into
- a long, and the result stored in the proper type. If long arithmetic
- creates an unacceptable overhead, you can duplicate the code with
- the obvious modifications for shorts or ints.
- */
-
- #include <iostream.h>
- #include <ctype.h>
-
-
- istream& istream::operator>> (short& i)
- {
- long l;
- *this >> l;
- if( ! fail() )
- i = (short) l;
- return *this;
- }
-
-
- istream& istream::operator>> (unsigned short& i)
- {
- unsigned long l;
- *this >> l;
- if( ! fail() )
- i = (unsigned short) l;
- return *this;
- }
-
-
- istream& istream::operator>> (int& i)
- {
- long l;
- *this >> l;
- if( ! fail() )
- i = (int) l;
- return *this;
- }
-
-
- istream& istream::operator>> (unsigned int& i)
- {
- unsigned long l;
- *this >> l;
- if( ! fail() )
- i = (unsigned int) l;
- return *this;
- }
-
-
- istream& istream::operator>> (unsigned long& u)
- {
- long l;
- *this >> l;
- if( ! fail() )
- u = l;
- return *this;
- }
-
-
- /*
- * This is the actual code which does the extraction
- */
-
- // extract decimal value from istream, given first character c
- // error if c is not a valid character
- static unsigned long getdec(istream& i, int c)
- {
- unsigned long l = 0;
- if( ! isdigit(c) ) {
- if( c == EOF )
- i.clear(i.rdstate() | ios::eofbit | ios::badbit);
- else
- i.clear(i.rdstate() | ios::failbit);
- }
- else {
- do {
- l = 10 * l + c - '0';
- c = i.rdbuf()->snextc();
- } while( isdigit(c) );
- }
- return l;
- }
-
-
- // extract octal value from istream, given first character c
- // error if c is not a valid character
- static unsigned long getoct(istream& i, int c)
- {
- unsigned long l = 0;
- if( c < '0' || '7' < c ) {
- if( c == EOF )
- i.clear(i.rdstate() | ios::eofbit | ios::badbit);
- else
- i.clear(i.rdstate() | ios::failbit);
- }
- else {
- do {
- l = 8 * l + c - '0';
- c = i.rdbuf()->snextc();
- } while( '0' <= c && c <= '7' );
- }
- return l;
- }
-
-
- // extract hex value from istream, given first character c
- // error if c is not a valid character
- static unsigned long gethex(istream& i, int c)
- {
- unsigned long l = 0;
- if( ! isxdigit(c) ) {
- if( c == EOF )
- i.clear(i.rdstate() | ios::eofbit | ios::badbit);
- else
- i.clear(i.rdstate() | ios::failbit);
- }
- else {
- do {
- if( isupper(c) ) c -= 'A';
- else if( islower(c) ) c -= 'a';
- else c -= '0';
- l = 16 * l + c;
- c = i.rdbuf()->snextc();
- } while( isxdigit(c) );
- }
- return l;
- }
-
-
- istream& istream::operator>> (long& l)
- {
-
- if( ipfx0() ) {
- unsigned long u = 0; // result
- int c = bp->sgetc();
- int neg = c == '-';
- if( c == '+' || neg )
- c = bp->snextc();
- if( flags() & ios::hex )
- u = gethex(*this, c);
- else if( flags() & ios::oct )
- u = getoct(*this, c);
- else if( (flags() & ios::dec) || c != '0' )
- u = getdec(*this, c);
- else { // c == '0'
- c = bp->snextc();
- if( c == 'x' || c == 'X' ) {
- c = bp->snextc();
- u = gethex(*this, c);
- }
- else if( '0' <= c && c <= '7' )
- u = getoct(*this, c);
- else // a lone '0'
- u = 0;
- }
-
- l = neg ? -long(u) : long(u);
- }
- return *this;
- }
-