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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - istreamn.cpp
  3.  * Class istream member functions
  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. #undef _BIG_INLINE_
  17. #include <iostream.h>
  18. #include <ctype.h>
  19. #include <stdlib.h>
  20.  
  21.  
  22. istream::istream()
  23. {
  24.     gcount_ = 0;
  25. }
  26.  
  27.  
  28. istream::istream(streambuf* s)
  29. {
  30.     ios::init(s);
  31.     gcount_ = 0;
  32. }
  33.  
  34.  
  35. istream::~istream()
  36. {
  37. }
  38.  
  39.  
  40. // obsolete: set skip via format, tie via tie() function
  41. istream::istream(streambuf* s, int sk, ostream* t)
  42. {
  43.     ios::init(s);
  44.     gcount_ = 0;
  45.     skip(sk);
  46.     tie(t);
  47. }
  48.  
  49.  
  50. // set/read the get pointer's position
  51. istream& istream::seekg(streampos pos)
  52. {
  53.     if( bad()  ||  bp->seekpos(pos, ios::in) == EOF )
  54.     setstate(ios::failbit);
  55.     return *this;
  56. }
  57.  
  58.  
  59. istream& istream::seekg(streamoff off, seek_dir dir)
  60. {
  61.     if( bad()  ||  bp->seekoff(off, dir, ios::in) == EOF )
  62.     setstate(ios::failbit);
  63.     return *this;
  64. }
  65.  
  66.  
  67. streampos istream::tellg()
  68. {
  69.     streampos p = EOF;
  70.     if( bad()  ||  (p = bp->seekoff(0, ios::cur, ios::in)) == EOF )
  71.     setstate(ios::failbit);
  72.     return p;
  73. }
  74.  
  75.  
  76. /*
  77. * Unformatted extraction operations
  78. */
  79.  
  80. // extract consecutive whitespace
  81. void istream::eatwhite()
  82. {
  83.     int c;
  84.     while( (c = bp->sgetc()), isspace(c) ) {
  85.     bp->stossc();
  86.     ++gcount_;
  87.     }
  88.  
  89.     if( c == EOF )
  90.     setstate(ios::eofbit);
  91. }
  92.  
  93.  
  94. // implementation of ipfx
  95. // flush tied stream if needed, skip whitespace if needed
  96. int istream::do_ipfx(int need)
  97. {
  98.     gcount_ = 0;
  99.  
  100.     if( ! good() )
  101.     return 0;
  102.  
  103.     // flush tied stream if needed
  104.     if ( tie()  &&  (! need  ||  need > bp->in_avail()) )
  105.     tie()->flush();
  106.  
  107.     if( ! need  &&  (ispecial & skipping) )
  108.     eatwhite();
  109.  
  110.     return good();
  111. }
  112.  
  113.  
  114. // get next char, not immediately available
  115. signed char istream::do_get()
  116. {
  117.     int c = bp->sbumpc();
  118.     if( c == EOF )
  119.     setstate(ios::eofbit | ios::failbit);
  120.     else
  121.     ++gcount_;
  122.     return c;
  123. }
  124.  
  125.  
  126. // extract characters into an array, up to len chars or delimiter d
  127. istream& istream::get(signed char* p, int len, char d)
  128. {
  129.     if( ipfx1() ) {
  130.     signed char* op = p;
  131.     int c = 0;
  132.     while( --len > 0  &&  (c = bp->sgetc()) != d  &&  c != EOF ) {
  133.         *p++ = c;
  134.         ++gcount_;
  135.         bp->stossc();
  136.     }
  137.     if( c == EOF )
  138.         setstate((p == op) ? (ios::eofbit | ios::failbit) : ios::eofbit);
  139.     }
  140.     *p = 0;    // always terminate with a null
  141.     return *this;
  142. }
  143.  
  144.  
  145. istream& istream::read(signed char* p, int len)
  146. {
  147.     if( ipfx1() ) {
  148.     int c = 0;
  149.     while( --len >= 0  &&  (c = bp->sgetc()) != EOF ) {
  150.         *p++ = c;
  151.         ++gcount_;
  152.         bp->stossc();
  153.     }
  154.     if( c == EOF )
  155.         setstate((len >= 0) ? (ios::eofbit | ios::failbit) : ios::eofbit);
  156.     }
  157.     return *this;
  158. }
  159.  
  160.  
  161. // extract characters into an array up to and including delimeter d
  162. istream& istream::getline(signed char* p, int len, char d)
  163. {
  164.     if( ipfx1() ) {
  165.     signed char* op = p;
  166.     int c = 0;
  167.     while( --len > 0  &&  (c = bp->sgetc()) != EOF ) {
  168.         *p++ = c;
  169.         ++gcount_;
  170.         bp->stossc();
  171.         if( c == d )
  172.         break;
  173.     }
  174.     if( c == EOF )
  175.         setstate((p == op) ? (ios::eofbit | ios::failbit) : ios::eofbit);
  176.     }
  177.     *p = 0;    // always terminate with a null
  178.     return *this;
  179. }
  180.  
  181.  
  182. // extract characters into a streambuf up to delimiter
  183. istream& istream::get(streambuf& s, char d)
  184. {
  185.     if( ipfx1() ) {
  186.     int c;
  187.     int err = 0;
  188.     while( (c = bp->sgetc()) != d  &&  c != EOF ) {
  189.         if( s.sputc(c) == EOF ) {
  190.         err = 1;
  191.         break;
  192.         }
  193.         ++gcount_;
  194.         bp->stossc();
  195.     }
  196.     if( c == EOF )
  197.         setstate(ios::eofbit);
  198.     if( err )
  199.         setstate(ios::failbit);
  200.     }
  201.     return *this;
  202. }
  203.  
  204.  
  205. // push back char into input
  206. istream& istream::putback(char c)
  207. {
  208.     if( ! fail() )
  209.     if( bp->sputbackc(c) == EOF )
  210.         setstate(ios::failbit);
  211.     else
  212.         clear(rdstate() & ~ios::eofbit);
  213.     return *this;
  214. }
  215.  
  216.  
  217. // extract and discard chars but stop at delim
  218. istream& istream::ignore(int n, int d)
  219. {
  220.     if( ipfx1() ) {
  221.     int c = 0;
  222.     while( --n >= 0  &&  (c = bp->sgetc()) != d  &&  c != EOF ) {
  223.         ++gcount_;
  224.         bp->stossc();
  225.     }
  226.     if( c == EOF )
  227.         setstate(ios::eofbit);
  228.     }
  229.     return *this;
  230. }
  231.  
  232.  
  233. // manipulators
  234. istream& istream::operator>> (ios& (*f)(ios&) )
  235. {
  236.     (*f)(*((ios*)this));
  237.     return *this;
  238. }
  239.  
  240.  
  241. // extract whitespace characters
  242. istream& ws(istream& is) 
  243. {
  244.     long f = is.flags();
  245.     is.setf(ios::skipws, ios::skipws);
  246.     is.ipfx0();
  247.     is.flags(f);
  248.     return is;
  249. }
  250.  
  251.  
  252. /*
  253. * Formatted extraction operations
  254. */
  255.  
  256. istream& istream::operator>> (signed char* p)
  257. {
  258.     if( ipfx0() ) {
  259.     signed char* op = p;
  260.     int c = 0;
  261.     int len = width(0);    // <=0 means no limit
  262.     while( --len  &&  (c = bp->sgetc(), ! isspace(c))  &&  c != EOF ) {
  263.         *p++ = c;
  264.         bp->stossc();
  265.     }
  266.     if( c == EOF )
  267.         setstate((p == op) ? (ios::eofbit | ios::failbit) : ios::eofbit);
  268.     *p = 0;    // always terminate with a null
  269.     }
  270.     return *this;
  271. }
  272.  
  273.  
  274. // extract from this istream, insert into streambuf
  275. istream& istream::operator>> (streambuf* s)
  276. {
  277.     enum errkind { noerr, nodata, putfail } err;
  278.     if( ipfx0() ) {
  279.     err = nodata;
  280.     int c;
  281.     while( (c = bp->sgetc()) != EOF ) {
  282.         if( s->sputc(c) == EOF ) {
  283.         err = putfail;
  284.         break;
  285.         }
  286.         err = noerr;
  287.         bp->stossc();
  288.     }
  289.     int errstate = ( err == noerr ) ? 0 : ios::failbit;
  290.     if( c == EOF ) {
  291.         errstate |= ios::eofbit;
  292.         if( err == nodata )
  293.         errstate |= ios::badbit;
  294.     }
  295.     if( errstate )
  296.         setstate(errstate);
  297.     }
  298.     return *this;
  299. }
  300.  
  301.  
  302. /*
  303.  * istream with assign
  304.  */
  305.  
  306. // does no initialization
  307. istream_withassign::istream_withassign() :
  308.         istream()
  309. {
  310. }
  311.  
  312.  
  313. istream_withassign::~istream_withassign()
  314. {
  315. }
  316.  
  317.  
  318. // gets buffer from istream and does entire initialization
  319. istream_withassign& istream_withassign::operator= (istream& is)
  320. {
  321.     ios::init(is.rdbuf());
  322.     return *this;
  323. }
  324.  
  325.  
  326. // associates streambuf with stream and does entire initialization
  327. istream_withassign& istream_withassign::operator= (streambuf* s)
  328. {
  329.     ios::init(s);
  330.     return *this;
  331. }
  332.  
  333.  
  334. /*
  335.  * The following functions may be inline, but in case they aren't,
  336.  * here they are as global closed subroutines.
  337.  */
  338.  
  339. istream& istream::operator>> (unsigned char& c)
  340. {
  341.     if( ipfx0() )
  342.     c = bp->in_avail() ? bp->sbumpc() : do_get();
  343.     return *this;
  344. }
  345.  
  346.  
  347. istream& istream::operator>> (signed char& c)
  348. {
  349.     if( ipfx0() )
  350.     c = bp->in_avail() ? bp->sbumpc() : do_get();
  351.     return *this;
  352. }
  353.  
  354.  
  355. istream& istream::get(unsigned char& c)
  356. {
  357.     if( ipfx1() )
  358.     if( bp->in_avail() ) {
  359.         gcount_ = 1;
  360.         c = bp->sbumpc();
  361.     }
  362.     else c = do_get();
  363.     return *this;
  364. }
  365.  
  366.  
  367. istream& istream::get(signed char& c)
  368. {
  369.     if( ipfx1() )
  370.     if( bp->in_avail()) {
  371.         gcount_ = 1;
  372.         c = bp->sbumpc();
  373.     }
  374.     else c = do_get();
  375.     return *this;
  376. }
  377.  
  378.  
  379. int istream::get()
  380. {
  381.     if( ipfx1() ) {
  382.     int c = bp->sbumpc();
  383.     if( c == EOF )
  384.         setstate(ios::eofbit);
  385.     else
  386.         gcount_ = 1;
  387.     return c;
  388.     }
  389.     else return EOF;
  390. }
  391.