home *** CD-ROM | disk | FTP | other *** search
- /*-----------------------------------------------------------------------*
- * filename - strmbfn.cpp
- * Class streambuf member functions
- *-----------------------------------------------------------------------*/
-
- /*[]------------------------------------------------------------[]*/
- /*| |*/
- /*| Turbo C++ Run Time Library - Version 1.0 |*/
- /*| |*/
- /*| |*/
- /*| Copyright (c) 1990 by Borland International |*/
- /*| All Rights Reserved. |*/
- /*| |*/
- /*[]------------------------------------------------------------[]*/
-
- #undef _BIG_INLINE_
- #include <iostream.h>
-
- const int streambuf_size = 1024;
-
- streambuf::streambuf()
- {
- alloc_ = 0;
- unbuf_ = 0;
- base_ = 0;
- pbase_ = 0;
- pptr_ = 0;
- epptr_ = 0;
- gptr_ = 0;
- egptr_ = 0;
- eback_ = 0;
- ebuf_ = 0;
- }
-
-
- streambuf::streambuf(char* b, int len)
- {
- alloc_ = 0;
- unbuf_ = 0;
- base_ = b;
- ebuf_ = (len > 0) ? b + len : b;
- pbase_ = 0;
- pptr_ = 0;
- epptr_ = 0;
- gptr_ = 0;
- egptr_ = 0;
- eback_ = 0;
- }
-
-
- streambuf::~streambuf()
- {
- if( alloc_ )
- delete base_;
- }
-
-
- int streambuf::doallocate()
- {
- char *p = new char[streambuf_size];
- if( ! p )
- return EOF;
- base_ = p;
- ebuf_ = p + streambuf_size;
- alloc_ = 1;
- return 1;
- }
-
-
- void streambuf::setb(char* b, char* eb, int a)
- {
- if( alloc_ && base_ && base_ != b )
- delete base_;
- base_ = b;
- ebuf_ = ( b && eb && (eb >= b) ) ? eb : b;
- unbuf_ = ! b;
- alloc_ = a && b;
- }
-
-
- void streambuf::setg(char* eb, char* g, char* eg)
- {
- eback_ = (g && eb) ? eb : g;
- gptr_ = g;
- egptr_ = (g && eg && (eg >= g)) ? eg : g;
- }
-
-
- void streambuf::setp(char* p, char* ep)
- {
- pbase_ = pptr_ = p;
- epptr_ = (p && ep && (ep >= p)) ? ep : p;
- }
-
-
- streambuf* streambuf::setbuf(signed char* p, int len)
- {
- setb((char*)p, (char*)p + len, 0);
- return this;
- }
-
-
- // obsolete. included for compatibility with version 1.2 streams
- streambuf* streambuf::setbuf(char* p, int len, int offset)
- {
- setb(p, p + len, 0);
- setp(p + offset, p + len);
- return this;
- }
-
-
- // called when sgetn finds not enough chars in the get area
- int streambuf::do_sgetn(char* s, int n)
- {
- int c, i;
-
- i = in_avail();
- if( i > 0 ) {
- memcpy(s, gptr_, i);
- s += i;
- gbump(i);
- }
- while( i < n && (c = sbumpc()) != EOF ) {
- *s++ = c;
- ++i;
- }
- return i;
- }
-
-
- // called by sputn when more chars are needed than are in the buffer
- int streambuf::do_sputn(const char* s, int n)
- {
- int i;
-
- i = (int)(epptr_ - pptr_);
- if( i > 0 ) {
- memcpy(pptr_, s, i);
- s += i;
- pbump(i);
- }
- while( i < n && sputc(*s++) != EOF )
- ++i;
- return i;
- }
-
-
- // called when snextc doesn't have an available character
- int streambuf::do_snextc()
- {
- return (underflow() == EOF) ? EOF : (unsigned char) *gptr_;
- }
-
-
- // streambuf can't do anything about underflow -- no place to get chars
- int streambuf::underflow()
- {
- if( egptr_ > gptr_ )
- return (unsigned char) *gptr_;
-
- gptr_ = egptr_;
- return EOF;
- }
-
-
- // streambuf can't do anything about overflow -- no place to put chars
- int streambuf::overflow(int)
- {
- return EOF;
- }
-
-
- int streambuf::pbackfail(int)
- {
- return EOF;
- }
-
-
- streampos streambuf::seekoff(streamoff, seek_dir, int)
- {
- return EOF;
- }
-
-
- streampos streambuf::seekpos(streampos pos, int mode)
- {
- return seekoff(streamoff(pos), ios::beg, mode);
- }
-
-
- int streambuf::sync()
- {
- return (in_avail() == 0 && out_waiting() == 0) ? 0 : EOF;
- }
-
-
- /*
- * The following functions are expected to be inline, but just in case
- * they aren't, and they generate a LOT of code, here they are as
- * global closed subroutines.
- */
-
- int streambuf::sputn(const char* s, int n)
- {
- if( n <= (epptr_ - pptr_) ) {
- memcpy(pptr_, s, n);
- pbump(n);
- return n;
- }
- return do_sputn(s, n);
- }
-
- int streambuf::sgetn(char* s, int n) {
- if( n <= (egptr_ - gptr_) ) {
- memcpy(s, gptr_, n);
- gbump(n);
- return n;
- }
- return do_sgetn(s, n);
- }
-