home *** CD-ROM | disk | FTP | other *** search
- /*-----------------------------------------------------------------------*
- * filename - stdiostr.cpp
- * Class stdiobuf and stdiostream implementation
- *-----------------------------------------------------------------------*/
-
- /*[]------------------------------------------------------------[]*/
- /*| |*/
- /*| Turbo C++ Run Time Library - Version 1.0 |*/
- /*| |*/
- /*| |*/
- /*| Copyright (c) 1990 by Borland International |*/
- /*| All Rights Reserved. |*/
- /*| |*/
- /*[]------------------------------------------------------------[]*/
-
- /*
- We assume an ANSI-conforming implementation of standard I/O.
- In particular, see the note in stdiobuf::sync() below.
- */
-
- #include <filesys.h>
- #include <stdiostr.h>
-
-
- stdiobuf::stdiobuf(FILE* f)
- {
- sio = f;
- char *p = new char [B_size];
- if( p ) {
- setb(p, p+B_size+4, 1); // ~streambuf() will delete it
- setp(p+4, p+4);
- setg(p, p+4, p+4);
- }
- }
-
-
- stdiobuf::~stdiobuf()
- {
- overflow(EOF);
- }
-
-
- int stdiobuf::overflow(int c)
- {
- if ( ferror(sio) )
- return EOF;
-
- char *p;
- int count = out_waiting();
- if( count > 0 ) {
- p = pbase();
- do {
- putc(*p, sio);
- ++p;
- } while( --count );
- }
- if( c != EOF )
- putc(c, sio);
-
- char *b = base();
- setp(b+4, b+blen());
- setg(b, b+4, b+4);
-
- return ferror(sio) ? EOF : 1;
- }
-
-
- int stdiobuf::pbackfail(int c)
- {
- return ungetc(c, sio);
- }
-
-
- int stdiobuf::sync()
- {
- int count;
- if( out_waiting() ) {
- if( overflow(EOF) == EOF )
- return EOF;
- }
- else if( (count = in_avail()) > 0 ) {
- char *p = egptr();
- do {
- --p;
- ungetc(*p, sio);
- --count;
- } while( count );
- p = egptr();
- setp(p, p);
- setg(eback(), p, p);
- }
- /*
- * We assume that fseek() does any needed synchronization.
- */
- fseek(sio, 0L, SEEK_CUR); // should flush stdio buffer
- return ferror(sio) ? EOF : 0;
- }
-
-
- streampos stdiobuf::seekoff(streamoff off, seek_dir dir, int /*mode ignored*/)
- {
- if( out_waiting() || in_avail() ) {
- if( sync() == EOF )
- return EOF;
- }
-
- int w;
- if( dir == ios::beg ) w = SEEK_SET;
- else if( dir == ios::end ) w = SEEK_END;
- else w = SEEK_CUR;
- w = fseek(sio, off, w);
- char *b = base();
- if( ! unbuffered() && b ) { // reset get and put areas
- setp(b+4, b+4);
- setg(b, b+4, b+4);
- }
- return w ? EOF : ftell(sio);
- }
-
-
- int stdiobuf::underflow()
- {
- if( in_avail() ) // no action needed
- return (unsigned char) *gptr();
-
- int c; // the return value
-
- if ( ! unbuffered() && base() ) { // this is buffered
- // flush any output buffer
- if( out_waiting() )
- if( overflow(EOF) == EOF )
- return EOF;
-
- // read in a character
- c = getc(sio);
-
- // find buffer
- char *b = base();
- *b = c;
-
- // set up get and put areas
- setg(b, b+4, b+5);
- setp(b+4, b+4);
- }
- else { // this is not buffered
- // read in a character
- c = getc(sio);
- if( c == EOF )
- setg(0, 0, 0);
- else {
- lahead[0] = c;
- setg(lahead, lahead, lahead+1);
- }
- }
-
- return c;
- }
-
-
- stdiostream::stdiostream(FILE* f) : buf(f)
- {
- ios::init(&buf);
- }
-
-
- stdiostream::~stdiostream()
- {
- }
-