home *** CD-ROM | disk | FTP | other *** search
- /*-----------------------------------------------------------------------*
- * filename - ostreamn.cpp
- * Class ostream 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>
- #include <string.h>
-
-
- ostream::ostream()
- {
- }
-
-
- ostream::ostream(streambuf* s)
- {
- ios::init(s);
- }
-
-
- ostream::~ostream()
- {
- flush();
- }
-
-
- // implementation of opfx -- output prefix operations
- int ostream::do_opfx()
- {
- if( ! fail() ) {
- if( x_tie )
- x_tie->flush();
- return 1;
- }
-
- return 0;
- }
-
-
- // implementation of osfx -- output suffix operations
- void ostream::do_osfx()
- {
- if( ! fail() && (x_flags & ios::unitbuf) )
- flush();
-
- if( x_flags & ios::stdio ) {
- cout.flush();
- clog.flush();
- }
- }
-
-
- // set the put pointer's position
- ostream& ostream::seekp(streampos pos)
- {
- if( bad() || bp->seekpos(pos, ios::out) == EOF )
- setstate(ios::failbit);
- return *this;
- }
-
-
- // set the put pointer's position
- ostream& ostream::seekp(streamoff off, seek_dir dir)
- {
- if( bad() || bp->seekoff(off, dir, ios::out) == EOF )
- setstate(ios::failbit);
- return *this;
- }
-
-
- // read the put pointer's position
- streampos ostream::tellp()
- {
- streampos p = EOF;
- if( bad() || (p = bp->seekoff(0, ios::cur, ios::out)) == EOF )
- setstate(ios::failbit);
- return p;
- }
-
-
- ostream& ostream::flush()
- {
- if( bp->sync() == EOF )
- setstate(ios::badbit);
- return *this;
- }
-
-
- /*
- * Formatted insertion operations
- */
-
- // Perform the prefix routine, output the string with any needed padding,
- // and perform the suffix routine.
- // 'd' is the data portion, 'p' is the prefix portion.
- void ostream::outstr(const char *d, const char *p)
- {
- if( opfx() ) {
- int plen = p ? strlen(p) : 0;
- int dlen = d ? strlen(d) : 0;
- int pad = width(0) - plen - dlen;
-
- // pad on left (right-adjust) if needed -- the default case
- if( ! (x_flags & (ios::left | ios::internal)) ) {
- while( --pad >= 0 )
- if( bp->sputc(x_fill) == EOF ) {
- setstate(ios::badbit);
- break;
- }
- }
-
- // output the prefix
- if( ! fail() && plen )
- if( bp->sputn(p, plen) != plen )
- setstate(ios::badbit);
-
- // internal padding if needed
- if( ! fail() && (x_flags & ios::internal) ) {
- while( --pad >= 0 )
- if( bp->sputc(x_fill) == EOF ) {
- setstate(ios::badbit);
- break;
- }
- }
-
- // output the data
- if( ! fail() && dlen )
- if( bp->sputn(d, dlen) != dlen )
- setstate(ios::badbit);
-
- // pad on right (left-adjust) if needed
- if( ! fail() && (x_flags & ios::left) ) {
- while( --pad >= 0 )
- if( bp->sputc(x_fill) == EOF ) {
- setstate(ios::badbit);
- break;
- }
- }
- }
- osfx();
- }
-
-
- // extract from streambuf, insert into this ostream
- ostream& ostream::operator<< (streambuf* s)
- {
- if( opfx() ) {
- int c;
- while( (c = s->sbumpc()) != EOF )
- if( bp->sputc(c) == EOF ) {
- setstate(ios::badbit);
- break;
- }
- }
- osfx();
- return *this;
- }
-
-
- // manipulators
- ostream& ostream::operator<< (ios& (*f)(ios&))
- {
- (*f)(*((ios*)this));
- return *this;
- }
-
-
- // insert newline and flush
- ostream& endl(ostream& os)
- {
- os << '\n';
- os.flush();
- return os;
- }
-
-
- // insert null to terminate string
- ostream& ends(ostream& os)
- {
- os << char(0);
- return os;
- }
-
-
- // flush the ostream
- ostream& flush(ostream& os)
- {
- os.flush();
- return os;
- }
-
-
- /*
- * ostream with assign
- */
-
- // does no initialization
- ostream_withassign::ostream_withassign() :
- ostream()
- {
- }
-
-
- ostream_withassign::~ostream_withassign()
- {
- }
-
-
- // gets buffer from ostream and does entire initialization
- ostream_withassign& ostream_withassign::operator= (ostream& os)
- {
- ios::init(os.rdbuf());
- return *this;
- }
-
-
- // associates streambuf with stream and does entire initialization
- ostream_withassign& ostream_withassign::operator= (streambuf* s)
- {
- ios::init(s);
- return *this;
- }
-
-
- /*
- * 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.
- */
-
- ostream& ostream::operator<< (signed char c)
- {
- if( opfx() )
- if( bp->sputc(c) == EOF ) setstate(badbit);
- osfx();
- return *this;
- }
-
-
- ostream& ostream::write(const signed char* s, int n)
- {
- if( ! fail() )
- if( bp->sputn((const char*)s, n) != n )
- setstate(badbit);
- return *this;
- }
-