home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 8.ddi / IOSTRSR1.ZIP / FSBUFLOW.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  2.1 KB  |  79 lines

  1. /*[]------------------------------------------------------------[]*/
  2. /*|                                                              |*/
  3. /*|     fsbuflow.cpp                                             |*/
  4. /*|                                                              |*/
  5. /*|     Class filebuf                                            |*/
  6. /*|          int filebuf::underflow()                            |*/
  7. /*|                                                              |*/
  8. /*[]------------------------------------------------------------[]*/
  9.  
  10. /*
  11.  *      C/C++ Run Time Library - Version 5.0
  12.  *
  13.  *      Copyright (c) 1990, 1992 by Borland International
  14.  *      All Rights Reserved.
  15.  *
  16.  */
  17.  
  18. #include <ioconfig.h>
  19. #include "filesys.h"
  20. #include <fstream.h>
  21.  
  22. // return the next available character, reading in a buffer if needed
  23.  
  24. int filebuf::underflow()
  25. {
  26.     if( ! opened  ||  (mode & (ios::in | ios::out)) == ios::out )
  27.         return EOF;
  28.  
  29.     if( in_avail() )    // no action needed
  30.         return (unsigned char) *gptr();
  31.  
  32.     int c;  // the return value
  33.     int count;  // input character count
  34.  
  35.     if ( ! unbuffered()  &&  base() )
  36.         {     // this is buffered
  37.  
  38.         if (sync() != 0)
  39.                 return EOF;
  40.  
  41.         // find buffer data
  42.         int pb = (blen() > 8) ? 4 : 1;  // putback area size
  43.         char *b = base();
  44.  
  45.         // read in a new buffer
  46.         count = ::read(xfd, b+pb, blen()-pb);
  47.         if( count == OS_err )
  48.             return EOF;
  49.  
  50.         // set up get and put areas
  51.         setg(b, b+pb, b+pb+count);
  52.         setp(b+pb, b+pb);
  53.  
  54.         if( count )
  55.             c = (unsigned char) *gptr();
  56.         }
  57.     else
  58.         {     // this is not buffered
  59.         count = ::read(xfd, lahead, 1);
  60.         if( count == OS_err)
  61.             {
  62.             c = EOF;
  63.             setg(0, 0, 0);
  64.             }
  65.         else
  66.             {
  67.             c = (unsigned char)lahead[0];
  68.             setg(lahead, lahead, lahead+1);
  69.             }
  70.         }
  71.  
  72.     if( ! count )
  73.         c = EOF;    // end of file
  74.  
  75.     return c;
  76. }
  77.  
  78.  
  79.