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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - ostreamn.cpp
  3.  * Class ostream member functions
  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. #undef _BIG_INLINE_
  17. #include <iostream.h>
  18. #include <string.h>
  19.  
  20.  
  21. ostream::ostream()
  22. {
  23. }
  24.  
  25.  
  26. ostream::ostream(streambuf* s)
  27. {
  28.     ios::init(s);
  29. }
  30.  
  31.  
  32. ostream::~ostream()
  33. {
  34.     flush();
  35. }
  36.  
  37.  
  38. // implementation of opfx -- output prefix operations
  39. int ostream::do_opfx()
  40. {
  41.     if( ! fail() ) {
  42.     if( x_tie )
  43.         x_tie->flush();
  44.     return 1;
  45.     }
  46.  
  47.     return 0;
  48. }
  49.  
  50.  
  51. // implementation of osfx -- output suffix operations
  52. void ostream::do_osfx()
  53. {
  54.     if( ! fail()  && (x_flags & ios::unitbuf) )
  55.     flush();
  56.     
  57.     if( x_flags & ios::stdio ) {
  58.     cout.flush();
  59.     clog.flush();
  60.     }
  61. }
  62.  
  63.  
  64. // set the put pointer's position
  65. ostream& ostream::seekp(streampos pos)
  66. {
  67.     if( bad()  ||  bp->seekpos(pos, ios::out) == EOF )
  68.     setstate(ios::failbit);
  69.     return *this;
  70. }
  71.  
  72.  
  73. // set the put pointer's position
  74. ostream& ostream::seekp(streamoff off, seek_dir dir)
  75. {
  76.     if( bad()  ||  bp->seekoff(off, dir, ios::out) == EOF )
  77.     setstate(ios::failbit);
  78.     return *this;
  79. }
  80.  
  81.  
  82. // read the put pointer's position
  83. streampos ostream::tellp()
  84. {
  85.     streampos p = EOF;
  86.     if( bad()  ||  (p = bp->seekoff(0, ios::cur, ios::out)) == EOF )
  87.     setstate(ios::failbit);
  88.     return p;
  89. }
  90.  
  91.  
  92. ostream& ostream::flush()
  93. {
  94.     if( bp->sync() == EOF )
  95.     setstate(ios::badbit);
  96.     return *this;
  97. }
  98.  
  99.  
  100. /*
  101.  * Formatted insertion operations
  102.  */
  103.  
  104. // Perform the prefix routine, output the string with any needed padding,
  105. // and perform the suffix routine.
  106. // 'd' is the data portion, 'p' is the prefix portion.
  107. void ostream::outstr(const char *d, const char *p)
  108. {
  109.     if( opfx() ) {
  110.     int plen = p ? strlen(p) : 0;
  111.     int dlen = d ? strlen(d) : 0;
  112.     int pad = width(0) - plen - dlen;
  113.  
  114.     // pad on left (right-adjust) if needed -- the default case
  115.     if( ! (x_flags & (ios::left | ios::internal)) ) {
  116.         while( --pad >= 0 )
  117.         if( bp->sputc(x_fill) == EOF ) {
  118.             setstate(ios::badbit);
  119.             break;
  120.         }
  121.     }
  122.  
  123.     // output the prefix
  124.     if( ! fail()  &&  plen )
  125.         if( bp->sputn(p, plen) != plen )
  126.         setstate(ios::badbit);
  127.  
  128.     // internal padding if needed
  129.     if( ! fail()  &&  (x_flags & ios::internal) ) {
  130.         while( --pad >= 0 )
  131.         if( bp->sputc(x_fill) == EOF ) {
  132.             setstate(ios::badbit);
  133.             break;
  134.         }
  135.     }
  136.  
  137.     // output the data
  138.     if( ! fail()  &&  dlen )
  139.         if( bp->sputn(d, dlen) != dlen )
  140.         setstate(ios::badbit);
  141.  
  142.     // pad on right (left-adjust) if needed
  143.     if( ! fail()  &&  (x_flags & ios::left) ) {
  144.         while( --pad >= 0 )
  145.         if( bp->sputc(x_fill) == EOF ) {
  146.             setstate(ios::badbit);
  147.             break;
  148.         }
  149.     }
  150.     }
  151.     osfx();
  152. }
  153.  
  154.  
  155. // extract from streambuf, insert into this ostream
  156. ostream& ostream::operator<< (streambuf* s)
  157. {
  158.     if( opfx() ) {
  159.     int c;
  160.     while( (c = s->sbumpc()) != EOF )
  161.         if( bp->sputc(c) == EOF ) {
  162.         setstate(ios::badbit);
  163.         break;
  164.         }
  165.     }
  166.     osfx();
  167.     return *this;
  168. }
  169.  
  170.  
  171. // manipulators
  172. ostream& ostream::operator<< (ios& (*f)(ios&))
  173. {
  174.     (*f)(*((ios*)this));
  175.     return *this;
  176. }
  177.  
  178.  
  179. // insert newline and flush
  180. ostream& endl(ostream& os)
  181. {
  182.     os << '\n';
  183.     os.flush();
  184.     return os;
  185. }
  186.  
  187.  
  188. // insert null to terminate string
  189. ostream& ends(ostream& os)
  190. {
  191.     os << char(0);
  192.     return os;
  193. }
  194.  
  195.  
  196. // flush the ostream
  197. ostream& flush(ostream& os)
  198. {
  199.     os.flush();
  200.     return os;
  201. }
  202.  
  203.  
  204. /*
  205.  * ostream with assign
  206.  */
  207.  
  208. // does no initialization
  209. ostream_withassign::ostream_withassign() :
  210.         ostream()
  211. {
  212. }
  213.  
  214.  
  215. ostream_withassign::~ostream_withassign()
  216. {
  217. }
  218.  
  219.  
  220. // gets buffer from ostream and does entire initialization
  221. ostream_withassign& ostream_withassign::operator= (ostream& os)
  222. {
  223.     ios::init(os.rdbuf());
  224.     return *this;
  225. }
  226.  
  227.  
  228. // associates streambuf with stream and does entire initialization
  229. ostream_withassign& ostream_withassign::operator= (streambuf* s)
  230. {
  231.     ios::init(s);
  232.     return *this;
  233. }
  234.  
  235.  
  236. /*
  237.  * The following functions are expected to be inline, but just in case
  238.  * they aren't, and they generate a LOT of code, here they are as
  239.  * global closed subroutines.
  240.  */
  241.  
  242. ostream& ostream::operator<< (signed char c)
  243. {
  244.     if( opfx() )
  245.     if( bp->sputc(c) == EOF ) setstate(badbit);
  246.     osfx();
  247.     return *this;
  248. }
  249.  
  250.  
  251. ostream& ostream::write(const signed char* s, int n)
  252. {
  253.     if( ! fail() )
  254.     if( bp->sputn((const char*)s, n) != n )
  255.         setstate(badbit);
  256.     return *this;
  257. }
  258.