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

  1. /*-----------------------------------------------------------------------*
  2.  * filename -  stdiostr.cpp
  3.  * Class stdiobuf and stdiostream implementation
  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. /*
  17.     We assume an ANSI-conforming implementation of standard I/O.
  18.     In particular, see the note in stdiobuf::sync() below.
  19. */
  20.  
  21. #include <filesys.h>
  22. #include <stdiostr.h>
  23.  
  24.  
  25. stdiobuf::stdiobuf(FILE* f)
  26. {
  27.     sio = f;
  28.     char  *p = new char [B_size];
  29.     if( p ) {
  30.     setb(p, p+B_size+4, 1);    // ~streambuf() will delete it
  31.     setp(p+4, p+4);
  32.     setg(p, p+4, p+4);
  33.     }
  34. }
  35.  
  36.  
  37. stdiobuf::~stdiobuf()
  38. {
  39.     overflow(EOF);
  40. }
  41.  
  42.  
  43. int    stdiobuf::overflow(int c)
  44. {
  45.     if ( ferror(sio) )
  46.     return EOF;
  47.  
  48.     char *p;
  49.     int count = out_waiting();
  50.     if( count > 0 ) {
  51.     p = pbase();
  52.     do {
  53.         putc(*p, sio);
  54.         ++p;
  55.     } while( --count );
  56.     }
  57.     if( c != EOF )
  58.     putc(c, sio);
  59.  
  60.     char *b = base();
  61.     setp(b+4, b+blen());
  62.     setg(b, b+4, b+4);
  63.  
  64.     return ferror(sio) ? EOF : 1;
  65. }
  66.  
  67.  
  68. int    stdiobuf::pbackfail(int c)
  69. {
  70.     return ungetc(c, sio);
  71. }
  72.  
  73.  
  74. int    stdiobuf::sync()
  75. {
  76.     int count;
  77.     if( out_waiting() ) {
  78.     if( overflow(EOF) == EOF )
  79.         return EOF;
  80.     }
  81.     else if( (count = in_avail()) > 0 ) {
  82.     char *p = egptr();
  83.     do {
  84.         --p;
  85.         ungetc(*p, sio);
  86.         --count;
  87.     } while( count );
  88.     p = egptr();
  89.     setp(p, p);
  90.     setg(eback(), p, p);
  91.     }
  92.     /*
  93.      * We assume that fseek() does any needed synchronization.
  94.      */
  95.     fseek(sio, 0L, SEEK_CUR);    // should flush stdio buffer
  96.     return ferror(sio) ? EOF : 0;
  97. }
  98.  
  99.  
  100. streampos stdiobuf::seekoff(streamoff off, seek_dir dir, int /*mode ignored*/)
  101. {
  102.     if( out_waiting()  ||  in_avail() ) {
  103.     if( sync() == EOF )
  104.         return EOF;
  105.     }
  106.  
  107.     int w;
  108.     if( dir == ios::beg ) w = SEEK_SET;
  109.     else if( dir == ios::end ) w = SEEK_END;
  110.     else w = SEEK_CUR;
  111.     w = fseek(sio, off, w);
  112.     char *b = base();
  113.     if( ! unbuffered()  &&  b ) {    // reset get and put areas
  114.     setp(b+4, b+4);
  115.     setg(b, b+4, b+4);
  116.     }
  117.     return w ? EOF : ftell(sio);
  118. }
  119.  
  120.  
  121. int    stdiobuf::underflow()
  122. {
  123.     if( in_avail() )    // no action needed
  124.     return (unsigned char) *gptr();
  125.  
  126.     int c;    // the return value
  127.  
  128.     if ( ! unbuffered()  &&  base() ) {        // this is buffered
  129.     // flush any output buffer
  130.     if( out_waiting() )
  131.         if( overflow(EOF) == EOF )
  132.         return EOF;
  133.  
  134.     // read in a character
  135.     c = getc(sio);
  136.  
  137.     // find buffer
  138.     char *b = base();
  139.     *b = c;
  140.  
  141.     // set up get and put areas
  142.     setg(b, b+4, b+5);
  143.     setp(b+4, b+4);
  144.     }
  145.     else {        // this is not buffered
  146.     // read in a character
  147.     c = getc(sio);
  148.     if( c == EOF )
  149.         setg(0, 0, 0);
  150.     else {
  151.         lahead[0] = c;
  152.         setg(lahead, lahead, lahead+1);
  153.     }
  154.     }
  155.  
  156.     return c;
  157. }
  158.  
  159.  
  160. stdiostream::stdiostream(FILE* f) : buf(f)
  161. {
  162.     ios::init(&buf);
  163. }
  164.  
  165.  
  166. stdiostream::~stdiostream()
  167. {
  168. }
  169.