home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / istream.cpp < prev    next >
C/C++ Source or Header  |  1998-06-17  |  5KB  |  229 lines

  1. /***
  2. * istream.cpp - definitions for istream and istream_withassign classes
  3. *
  4. *       Copyright (c) 1991-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Definitions of member functions for istream and istream_withassign
  8. *       classes.
  9. *       [AT&T C++]
  10. *
  11. *******************************************************************************/
  12.  
  13. #include <cruntime.h>
  14. #include <internal.h>
  15. #include <stdlib.h>
  16. #include <ctype.h>
  17. #include <iostream.h>
  18. #include <dbgint.h>
  19. #pragma hdrstop
  20.  
  21.         istream::istream()
  22. // : ios()
  23. {
  24.         x_flags |= ios::skipws;
  25.         x_gcount = 0;
  26.         _fGline = 0;
  27. }
  28.  
  29.         istream::istream(streambuf* _inistbf)
  30. // : ios()
  31. {
  32.         init(_inistbf);
  33.  
  34.         x_flags |= ios::skipws;
  35.         x_gcount = 0;
  36.         _fGline = 0;
  37. }
  38.  
  39.         istream::istream(const istream& _istrm)
  40. // : ios()
  41. {
  42.         init(_istrm.rdbuf());
  43.  
  44.         x_flags |= ios::skipws;
  45.         x_gcount = 0;
  46.         _fGline = 0;
  47. }
  48.  
  49.         istream::~istream()
  50. // : ~ios()
  51. {
  52. }
  53.  
  54. // used by ios::sync_with_stdio()
  55. istream& istream::operator=(streambuf * _sbuf)
  56. {
  57.         if (delbuf() && rdbuf())
  58.             delete rdbuf();
  59.  
  60.         bp = 0;
  61.  
  62.         this->ios::operator=(ios());    // initialize ios members
  63.         delbuf(0);                      // important!
  64.         init(_sbuf);    // set up bp
  65.  
  66.         x_flags |= ios::skipws;         // init istream members too
  67.         x_gcount = 0;
  68.  
  69.         return *this;
  70. }
  71. int istream::ipfx(int need)
  72. {
  73.     lock();
  74.     if (need)           // reset gcount if unformatted input
  75.         x_gcount = 0;
  76.     if (state)          // return 0 iff error condition
  77.         {
  78.         state |= ios::failbit;  // solves cin>>buf problem
  79.         unlock();
  80.         return 0;
  81.         }
  82.     if (x_tie && ((need==0) || (need > bp->in_avail())))
  83.         {
  84.         x_tie->flush();
  85.         }
  86.     lockbuf();
  87.     if ((need==0) && (x_flags & ios::skipws))
  88.         {
  89.         eatwhite();
  90.         if (state)      // eof or error
  91.             {
  92.             state |= ios::failbit;
  93.             unlockbuf();
  94.             unlock();
  95.             return 0;
  96.             }
  97.         }
  98.     // leave locked ; isfx() will unlock
  99.     return 1;           // return nz if okay
  100. }
  101.  
  102. // formatted input functions
  103.  
  104. istream& istream::operator>>(char * s)
  105. {
  106.     int c;
  107.     unsigned int i, lim;
  108.     if (ipfx(0))
  109.         {
  110.         lim = (unsigned)(x_width-1);
  111.         x_width = 0;
  112.         if (!s)
  113.             {
  114.             state |= ios::failbit;
  115.             }
  116.         else
  117.             {
  118.             for (i=0; i< lim; i++)
  119.                 {
  120.                 c=bp->sgetc();
  121.                 if (c==EOF)
  122.                     {
  123.                     state |= ios::eofbit;
  124.                     if (!i)
  125.                         state |= ios::failbit|ios::badbit;
  126.                     break;
  127.                     }
  128.                 else if (isspace(c))
  129.                     {
  130.                     break;
  131.                     }
  132.                 else
  133.                     {
  134.                     s[i] = (char)c;
  135.                     bp->stossc(); // advance pointer
  136.                     }
  137.                 }
  138.             if (!i)
  139.                 state |= ios::failbit;
  140.             else
  141.                 s[i] = '\0';
  142.             }
  143.         isfx();
  144.         }
  145.     return *this;
  146. }
  147.  
  148. int istream::peek()
  149. {
  150. int retval;
  151.     if (ipfx(1))
  152.         {
  153.         retval = (bp->sgetc());
  154.         isfx();
  155.         }
  156.     else
  157.         retval = EOF;
  158.     return retval;
  159. }
  160.  
  161. istream& istream::putback(char c)
  162. {
  163.       if (good())
  164.         {
  165.         lockbuf();
  166.  
  167.         if (bp->sputbackc(c)==EOF)
  168.             {
  169.             clear(state | ios::failbit);
  170.             }
  171.  
  172.         unlockbuf();
  173.         }
  174.     return *this;
  175. }
  176.  
  177. int istream::sync()
  178. {
  179.     int retval;
  180.     lockbuf();
  181.  
  182.     if ((retval=bp->sync())==EOF)
  183.         {
  184.         clear(state | (ios::failbit|ios::badbit));
  185.         }
  186.  
  187.     unlockbuf();
  188.     return retval;
  189. }
  190.  
  191. void istream::eatwhite()
  192. {
  193.     int c;
  194.     lockbuf();
  195.     c = bp->sgetc();
  196.     for ( ; ; )
  197.         {
  198.         if (c==EOF)
  199.             {
  200.             clear(state | ios::eofbit);
  201.             break;
  202.             }
  203.         if (isspace(c))
  204.             {
  205.             c = bp->snextc();
  206.             }
  207.         else
  208.             {
  209.             break;
  210.             }
  211.         }
  212.     unlockbuf();
  213. }
  214.  
  215.         istream_withassign::istream_withassign()
  216. : istream()
  217. {
  218. }
  219.  
  220.         istream_withassign::istream_withassign(streambuf* _is)
  221. : istream(_is)
  222. {
  223. }
  224.  
  225.         istream_withassign::~istream_withassign()
  226. // : ~istream()
  227. {
  228. }
  229.