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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - ostream.cpp
  3.  * C++ stream I/O functions for handling ostreams, except floating-point
  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. #include <stdlib.h>
  17. #include <stream.h>
  18.  
  19.  
  20. // connect to existing streambuf
  21. _Cdecl ostream::ostream( streambuf *s )
  22. {
  23.     stream = s;
  24.     mystream = 0;
  25.     state = s ? _good : _fail;
  26. }
  27.  
  28.  
  29. // constructor: connect to open file
  30. _Cdecl ostream::ostream( int nfd )
  31. {
  32.     mystream = 0;
  33.     state = _fail;
  34.  
  35.     stream = new filebuf(nfd);
  36.     if( stream ) {
  37.     if( stream->file ) {
  38.         mystream = 1;
  39.         state = _good;
  40.     }
  41.     }
  42. }
  43.  
  44.  
  45. // connect to char array
  46. _Cdecl ostream::ostream( int size, char *buf )
  47. {
  48.     mystream = 0;
  49.     state = _fail;
  50.  
  51.     if( buf ) {
  52.     stream = new streambuf(buf, size);
  53.     state = _good;
  54.     }
  55.     else {
  56.     stream = new streambuf();
  57.     if( stream ) {
  58.         stream->allocate();
  59.         mystream = 1;
  60.         state = _good;
  61.     }
  62.     }
  63. }
  64.  
  65.  
  66. // flush output buffer
  67. ostream& _Cdecl ostream::flush()
  68. {
  69.     if( state < _fail )
  70.     if( stream->file )
  71.         (void) fflush(stream->file);
  72.     else
  73.         (void) stream->overflow();
  74.     return *this;
  75. }
  76.  
  77.  
  78. // write a char, short, or int
  79. ostream& _Cdecl ostream::operator << ( int i )
  80. {
  81.     char b[8];
  82.     if( state < _fail) {
  83.     itoa(i, b, 10);
  84.     register char *p = b;
  85.     while( *p )
  86.         put(*p++);
  87.     }
  88.     return *this;
  89. }
  90.  
  91.  
  92. // write a long
  93. ostream& _Cdecl ostream::operator << ( long l )
  94. {
  95.     char b[12];
  96.     if( state < _fail) {
  97.     ltoa(l, b, 10);
  98.     register char *p = b;
  99.     while( *p )
  100.         put(*p++);
  101.     }
  102.     return *this;
  103. }
  104.  
  105.  
  106. // write an unsigned long
  107. ostream& _Cdecl ostream::operator << ( unsigned long l )
  108. {
  109.     char b[12];
  110.     if( state < _fail) {
  111.     ultoa(l, b, 10);
  112.     register char *p = b;
  113.     while( *p )
  114.         put(*p++);
  115.     }
  116.     return *this;
  117. }
  118.  
  119. // write a string
  120. ostream& _Cdecl ostream::operator << ( const char * s )
  121. {
  122.     if( state < _fail) {
  123.     while( *s ) put(*s++);
  124.     stream->terminate();
  125.     }
  126.     return *this;
  127. }
  128.