void fstreambase::setbuf( char *buf, int len ) { filebuf *fb; __lock_it( __i_lock ); fb = rdbuf(); if( fb == NULL ) { setstate( ios::failbit ); } else { __lock_it( fb->__b_lock ); if( fb->setbuf( buf, len ) == NULL ) { setstate( ios::failbit ); } } }
void fstreambase::close() { filebuf *fb; __lock_it( __i_lock ); fb = rdbuf(); if( fb == NULL ) { setstate( ios::failbit ); } else { __lock_it( fb->__b_lock ); if( fb->close() == NULL ) { setstate( ios::failbit ); } } }
streambuf *strstreambuf::setbuf( char *, streamsize size ) { __lock_it( __b_lock ); if( size > 0 ) { __allocation_size = size; } return( this ); }
streambuf *filebuf::setbuf( char *buf, int len ) { /************************************************/ // Set up the filebuf using the specified buffer. // The buffer can be used if: // (1) the filebuf is not attached to a file (may or may not have a buffer // already), // (2) the file is attached to a file and does not have a buffer. // If the buffer is too small (<= DEFAULT_PUTBACK_SIZE), then it cannot be // used. __lock_it( __b_lock ); if( (fd() != EOF) && (base() != NULL) ) { return( NULL ); } if( (buf == NULL) || (len <= 0) ) { setb( NULL, NULL, FALSE ); } else { if( len <= DEFAULT_PUTBACK_SIZE ) { setb( NULL, NULL, FALSE ); return( NULL ); } setb( buf, buf + len, FALSE ); } return( this ); }
streamsize streambuf::do_sgetn( char *buf, streamsize len ) { streamsize available; streamsize returned; returned = 0; __lock_it( __b_lock ); while( len > 0 ) { // # characters left in buffer available = (__huge_ptr_int)(egptr() - gptr()); if( available <= 0 ) { // nothing left? if( underflow() == EOF ) break; available = (__huge_ptr_int)(egptr() - gptr()); } if( available > len ) { // too many? available = len; } memcpy( buf, gptr(), available ); gbump( available ); returned += available; buf += available; len -= available; } return( returned ); }
void fstreambase::attach( filedesc fd ) { filebuf *fb; __lock_it( __i_lock ); fb = rdbuf(); if( fb == NULL ) { setstate( ios::failbit ); } else { __lock_it( fb->__b_lock ); if( fb->attach( fd ) == NULL ) { setstate( ios::failbit ); } else { clear(); } } }
ostream &ostream::do_lshift( char c ) { __lock_it( __i_lock ); char cbuf[2]; cbuf[0] = c; cbuf[1] = '\0'; setstate( __WATCOM_ios::writeitem( *this, cbuf, 1, 0 ) ); return( *this ); }
void fstreambase::open( char const *name, ios::openmode mode, int prot ) { filebuf *fb; __lock_it( __i_lock ); fb = rdbuf(); if( fb == NULL ) { setstate( ios::failbit ); } else { __lock_it( fb->__b_lock ); if( fb->open( name, mode, prot ) == NULL ) { setstate( ios::failbit ); } else { clear(); } } }
ostream &ostream::operator << ( char const *s ) { if( s != NULL ) { __lock_it( __i_lock ); if( opfx() ) { setstate( __WATCOM_ios::writeitem( *this, s, ::strlen( s ), 0 ) ); osfx(); } } return( *this ); }
istream &istream::seekg( streamoff offset, ios::seekdir dir ) { __lock_it( __i_lock ); if( ipfx( 1 ) ) { if( rdbuf()->seekoff( offset, dir, ios::in ) == EOF ) { setstate( ios::failbit ); } isfx(); } return( *this ); }
istream &istream::do_rshift( char &c ) { __lock_it( __i_lock ); int ch = rdbuf()->sgetchar(); if( ch == EOF ) { setstate( ios::eofbit|ios::failbit ); } else { c = (char)ch; } return( *this ); }
iostream::iostream( ios const &strm ) { /*************************************/ // Public constructor, making an iostream with a streambuf attached. // Associate the streambuf found in "strm" with the ios. // Takes an ios so that it can be used with istream and ostream. streambuf *sb; __lock_it( strm.__i_lock ); sb = strm.rdbuf(); ios::init( sb ); }
int streambuf::doallocate() { char *buf; __lock_it( __b_lock ); buf = (char *)_plib_malloc( DEFAULT_BUF_SIZE ); if( buf == NULL ) { return( EOF ); } setb( buf, buf + DEFAULT_BUF_SIZE, 1 ); return( __NOT_EOF ); // something other than EOF! }
ios::fmtflags ios::bitalloc() { /*****************************/ // Find an unused bit in "__format_flags". // Note that there is only one "__last_format_flag" value for all streams. __lock_it( __x_lock ); if( __last_format_flag == _LAST_FLAG_BIT ) { return( 0 ); } __last_format_flag <<= 1; return( __last_format_flag ); }
istream &istream::read( char *buf, int len ) { __lock_it( __i_lock ); if( ipfx1() ) { if( rdbuf()->in_avail() > len ) { __last_read_length = rdbuf()->sgetn( buf, len ); } else { do_read( buf , len ); } isfx(); } else { __last_read_length = 0; } return( *this ); }
static ios &__setbase( ios &strm, int base ) { /******************************************/ // Handles "setbase" manipulator. __lock_it( strm.__i_lock ); strm.unsetf( ios::basefield ); // turn off oct/dec/hex bits if( base == 8 ) { strm.setf( ios::oct ); } else if( base == 16 ) { strm.setf( ios::hex ); } else if( base == 10 ) { strm.setf( ios::dec ); } return( strm ); }
istream &istream::get( char &ch ) { __lock_it( __i_lock ); if( ipfx1() ) { if( rdbuf()->in_avail() ) { ch = (char)(rdbuf()->sgetchar()); } else { do_get( ch ); } isfx(); } else { __last_read_length = 0; } return( *this ); }
streampos strstreambuf::seekoff( streamoff offset, ios::seekdir direction, int mode ) { streampos newpos; char *endget; char *pos; mode &= (ios::in | ios::out); if( (mode == 0) || ( (direction==ios::cur) && (mode == (ios::in | ios::out)) ) ) { return( EOF ); } __lock_it( __b_lock ); // Move the get pointer: if( mode & ios::in ) { endget = pptr(); if( endget == NULL || endget < egptr() ) { endget = egptr(); } newpos = __get_position( offset, direction, eback(), gptr(), egptr(), endget ); if( newpos != EOF ) { // If the seek went beyond the end of the get area, extend the // get area to include the characters in the put area. pos = eback() + newpos; if( pos > egptr() ) { setg( eback(), pos, epptr() ); } else { setg( eback(), pos, egptr() ); } } } if( mode & ios::out ) { // Move the put pointer: newpos = __get_position( offset, direction, pbase(), pptr(), epptr(), epptr() ); if( newpos != EOF ) { setp( pbase(), epptr() ); pbump( newpos ); if( newpos > __minbuf_size ) { __minbuf_size = (int)newpos; } } } return( newpos ); }
int ostream::do_opfx() { /*******************/ // Prefix to all ostream activities. // Return a 0 (fail) if an error state is set. __lock_it( __i_lock ); if( tie() != NULL ) { tie()->flush(); } if( flags() & ios::stdio ) { ::__flush( __get_std_stream( STDOUT_FILENO ) ); ::__flush( __get_std_stream( STDERR_FILENO ) ); } return( good() ); }
ostream &ostream::write( char const *buf, int len ) { /***************************************************/ // Write a sequence of characters to the ostream. __lock_it( __i_lock ); if( opfx() ) { if( len ) { if( rdbuf()->sputn( buf, len ) != len ) { setstate( ios::failbit ); } } osfx(); } return( *this ); }
int streambuf::doallocate() { /***************************/ // Do the allocation required if allocate() thinks it's needed. // If the allocation fails, return EOF. char *buf; __lock_it( __b_lock ); buf = (char *)_plib_malloc( DEFAULT_BUF_SIZE ); if( buf == NULL ) { return( EOF ); } setb( buf, buf + DEFAULT_BUF_SIZE, 1 ); return( __NOT_EOF ); // something other than EOF! }
istream &istream::do_read( char *buf, streamsize len ) { __lock_it( __i_lock ); streamsize offset = rdbuf()->sgetn( buf, len ); if( offset < len ) { #if 0 setstate( ios::failbit ); #else setstate( ( rdbuf()->underflow() == EOF ) ? ios::failbit | ios::eofbit : ios::failbit ); #endif } __last_read_length = offset; return( *this ); }
ostream &ostream::operator << ( char c ) { /****************************************/ // Write a single character to the stream. __lock_it( __i_lock ); if( opfx() ) { if( width() == 0 ) { if( rdbuf()->sputc( c ) == EOF ) { setstate( ios::failbit ); } } else { do_lshift( c ); } osfx(); } return( *this ); }
streambuf *streambuf::setbuf( char *buf, int len ) { __lock_it( __b_lock ); if( base() != NULL ) { return( NULL ); } if( (buf == NULL) || (len <= 0) ) { setb( NULL, NULL, FALSE ); } else { if( len <= DEFAULT_PUTBACK_SIZE ) { setb( NULL, NULL, FALSE ); return( NULL ); } setb( buf, buf + len, FALSE ); } return( this ); }
streambuf *filebuf::setbuf( char *buf, int len ) { __lock_it( __b_lock ); if( (fd() != EOF) && (base() != NULL) ) { return( NULL ); } if( (buf == NULL) || (len <= 0) ) { setb( NULL, NULL, false ); } else { if( len <= DEFAULT_PUTBACK_SIZE ) { setb( NULL, NULL, false ); return( NULL ); } setb( buf, buf + len, false ); } return( this ); }
void streambuf::setb( char *buf, char *ebuf, int autodelete ) { __lock_it( __b_lock ); if( __delete_reserve ) { _plib_free( __reserve_base ); } if( (buf == NULL) || (ebuf <= buf) ) { __reserve_base = NULL; __reserve_end = NULL; __delete_reserve = 0; __unbuffered_state = 1; } else { __reserve_base = buf; __reserve_end = ebuf; __delete_reserve = (char)(autodelete ? 1 : 0); __unbuffered_state = 0; } }
ostream &ostream::operator << ( unsigned __int64 i ) { /****************************************************/ // Write an unsigned 64bit integer to the stream. // A signed 64bit integer displayed in base 8 or base 16 also uses // this routine. int base; char buffer[LONGEST_INT64 + 1]; int size; int digit_offset; __lock_it( __i_lock ); base = __FlagsToBase( flags() ); // Sign: if( i > 0 && (flags() & ios::showpos) && base == 10 ) { buffer[0] = '+'; size = 1; } else { size = 0; } // Prefix: __AddPrefix( i == 0, buffer, size, base, flags() ); // Digits: digit_offset = size; ulltoa( i, buffer + digit_offset, base ); if( flags() & ios::uppercase ) { strupr( buffer ); } size = ::strlen( buffer ); // Write the number: if( opfx() ) { setstate( __WATCOM_ios::writeitem( *this, buffer, size, digit_offset ) ); osfx(); } return( *this ); }
void streambuf::setb( char *buf, char *ebuf, int autodelete ) { /*************************************************************/ // Set up a new reserve area. // Delete the old one if necessary. // Turn buffering on. __lock_it( __b_lock ); if( __delete_reserve ) { _plib_free( __reserve_base ); } if( (buf == NULL) || (ebuf <= buf) ) { __reserve_base = NULL; __reserve_end = NULL; __delete_reserve = 0; __unbuffered_state = 1; } else { __reserve_base = buf; __reserve_end = ebuf; __delete_reserve = (char)(autodelete ? 1 : 0); __unbuffered_state = 0; } }
int istream::get() { /******************/ // Extract a single character from the input stream. // Don't set ios::failbit. int c = EOF; __lock_it( __i_lock ); if( ipfx( 1 ) ) { c = rdbuf()->sgetchar(); if( c == EOF ) { setstate( ios::eofbit ); __last_read_length = 0; } else { __last_read_length = 1; } isfx(); } else { __last_read_length = 0; } return( c ); }
ostream &ostream::operator << ( signed __int64 i ) { /**************************************************/ // Write a signed long integer to the stream. int base; char buffer[LONGEST_INT64 + 1]; int size; int digit_offset; __lock_it( __i_lock ); base = __FlagsToBase( flags() ); if( base != 10 ) { return( *this << (unsigned __int64) i ); } // Sign: if( i > 0 && (flags() & ios::showpos) ) { buffer[0] = '+'; digit_offset = 1; } else { digit_offset = 0; } // Digits: lltoa( i, buffer + digit_offset, base ); if( buffer[0] == '-' ) { digit_offset = 1; } size = ::strlen( buffer ); // Write the number: if( opfx() ) { setstate( __WATCOM_ios::writeitem( *this, buffer, size, digit_offset ) ); osfx(); } return( *this ); }