home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / ISTREAM.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  4.7 KB  |  230 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - istream.cpp
  3.  * C++ stream I/O functions for handling istreams, except floating-point
  4.  *-----------------------------------------------------------------------*/
  5.  
  6. /*[]------------------------------------------------------------[]*/
  7. /*|                                                              |*/
  8. /*|     Turbo C++ Run Time Library - Version 1.0                 |*/
  9. /*|                                                              |*/
  10. /*|                                                              |*/
  11. /*|     Copyright (c) 1990 by Borland International              |*/
  12. /*|     All Rights Reserved.                                     |*/
  13. /*|                                                              |*/
  14. /*[]------------------------------------------------------------[]*/
  15.  
  16. #include <ctype.h>
  17. #include <stdlib.h>
  18. #include <stream.h>
  19.  
  20.  
  21. // connect to streambuf or filebuf
  22. _Cdecl istream::istream( streambuf *s, int skip, ostream *t )
  23. {
  24.     stream = s;
  25.     tied_to = t;
  26.     skipping = skip;
  27.     mystream = 0;
  28.     state = s ? _good : _fail;
  29. }
  30.  
  31.  
  32. // connect to string
  33. _Cdecl istream::istream( int size, char *buf, int skip)
  34. {
  35.     stream = new streambuf;
  36.     if( stream ) {
  37.     stream->setbuf(buf, size, size);
  38.     state = _good;
  39.     mystream = 1;
  40.     skipping = skip;
  41.     tied_to = 0;
  42.     }
  43.     else {
  44.     state = _fail;
  45.     mystream = 0;
  46.     }
  47. }
  48.  
  49.  
  50. // connect to open file
  51. _Cdecl istream::istream( int nfd, int skip, ostream *t)
  52. {
  53.     mystream = 0;
  54.     state = _fail;
  55.  
  56.     stream = new filebuf(nfd);
  57.     if( stream ) {
  58.     if( stream->file ) {
  59.         mystream = 1;
  60.         skipping = skip;
  61.         state = _good;
  62.         tied_to = t;
  63.     }
  64.     }
  65. }
  66.  
  67.  
  68.  
  69. /*
  70.  * input functions
  71.  */
  72. istream& _Cdecl istream::operator >> ( unsigned char& c )
  73. {
  74.     if( state == _good ) {
  75.     int i;
  76.     do {
  77.         i = stream->snextc();
  78.     } while( skipping && isspace(i) );
  79.     if( i == EOF ) {
  80.         state = _eof;
  81.         if( stream->file )
  82.         if( ferror(stream->file) )
  83.             state = _fail;
  84.     }
  85.     else
  86.         c = i;
  87.     }
  88.     return *this;
  89. }
  90.  
  91.  
  92. /* skip whitespace if required, verify stream status
  93.    return status (ok), and character found, if any
  94. */
  95. void _Cdecl istream::checkskip( int& ok, int& c )
  96. {
  97.     ok = 0;        // assume the worst
  98.     int i = 0;
  99.     if( state == _good ) {
  100.     do {
  101.         i = stream->snextc();
  102.     } while( skipping && isspace(i) );
  103.     if( i == EOF ) {
  104.         state = _eof;
  105.         if( stream->file )
  106.         if( ferror(stream->file) )
  107.             state = _fail;
  108.     }
  109.     }
  110.     if( state == _good ) {
  111.     if ( skipping || ! isspace(i) )
  112.         ok = 1;        // no whitespace, or was allowed
  113.     else {
  114.         putback(i);        // disallowed whitespace found
  115.         state = _fail;
  116.     }
  117.     }
  118.     c = i;
  119. }
  120.  
  121.  
  122. long _Cdecl istream::get_long( int isunsigned )
  123. {
  124.     long l = 0;
  125.     int ok, c;
  126.  
  127.     checkskip(ok, c);
  128.     if( ok ) {
  129.     putback(c);
  130.     if( stream->file ) {
  131.         if( fscanf(stream->file, isunsigned ? "%lu" : "%ld", &l) != 1 )
  132.         state = _fail;
  133.     }
  134.     else {
  135.         char *ogptr = stream->gptr;
  136.         if (isunsigned)
  137.         l = (long) strtoul(stream->gptr, &(stream->gptr), 10);
  138.         else
  139.         l = strtol(stream->gptr, &(stream->gptr), 10);
  140.         if( ogptr == stream->gptr )
  141.         state = _fail;
  142.     }
  143.     }
  144.     return l;
  145. }
  146.  
  147.  
  148. istream& _Cdecl istream::operator >> ( char *s )
  149. {
  150.     int ok, i;
  151.  
  152.     checkskip(ok, i);
  153.     if( ok ) {
  154.     do {
  155.         *s++ = (char) i;
  156.         i = stream->snextc();
  157.     } while( (i != EOF) && (! isspace(i)) );
  158.     if( i == EOF ) {
  159.         state = _eof;
  160.         if( stream->file )
  161.         if( ferror(stream->file) )
  162.             state = _fail;
  163.     }
  164.     else putback(i);
  165.     *s = 0;
  166.     }
  167.     return *this;
  168. }
  169.  
  170. #pragma argsused
  171. istream& _Cdecl istream::operator >> ( whitespace& w )
  172. {
  173.     if( state == _good ) {
  174.     int i;
  175.     do {
  176.         i = stream->snextc();
  177.     } while( isspace(i) );
  178.     if( i == EOF ) {
  179.         state = _eof;
  180.         if( stream->file )
  181.         if( ferror(stream->file) )
  182.             state = _fail;
  183.     }
  184.     else putback((char) i);
  185.     }
  186.     return *this;
  187. }
  188.  
  189.  
  190. istream& _Cdecl istream::get(char& c)
  191. {
  192.     if( state == _good ) {
  193.     int i = stream->snextc();
  194.     if( i == EOF ) {
  195.         state = _eof;
  196.         if( stream->file )
  197.         if( ferror(stream->file) )
  198.             state = _fail;
  199.     }
  200.     else
  201.         c = (char) i;
  202.     }
  203.     return *this;
  204. }
  205.  
  206.  
  207. istream& _Cdecl istream::get(char *buf, int max, int term)
  208. {
  209.     if( state == _good ) {
  210.     if( max > 0 ) {
  211.         int i = stream->snextc();
  212.         while( max > 0  &&  i != EOF  &&  i != term ) {
  213.         *buf = (char) i;
  214.         --max;
  215.         ++buf;
  216.         i = stream->snextc();
  217.         }
  218.         if( i == EOF ) {
  219.         state = _eof;
  220.         if( stream->file )
  221.             if( ferror(stream->file) )
  222.             state = _fail;
  223.         }
  224.         else putback(i);
  225.     }
  226.     *buf = 0;
  227.     }
  228.     return *this;
  229. }
  230.