home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / djgpp / include / ostream.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-03  |  5.8 KB  |  257 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1989 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY.  No author or distributor
  10. accepts responsibility to anyone for the consequences of using it
  11. or for whether it serves any particular purpose or works at all,
  12. unless he says so in writing.  Refer to the GNU CC General Public
  13. License for full details.
  14.  
  15. Everyone is granted permission to copy, modify and redistribute
  16. GNU CC, but only under the conditions described in the
  17. GNU CC General Public License.   A copy of this license is
  18. supposed to have been given to you along with GNU CC so you
  19. can know your rights and responsibilities.  It should be in a
  20. file named COPYING.  Among other things, the copyright notice
  21. and this notice must be preserved on all copies.  
  22. */
  23.  
  24. /* *** Version 1.2 -- nearly 100% AT&T 1.2 compatible *** */
  25.  
  26. /* ostream.h now  separately includable */
  27.  
  28. #ifndef _ostream_h
  29. #ifdef __GNUG__
  30. #pragma once
  31. #pragma interface
  32. #endif
  33. #define _ostream_h 1
  34.  
  35. /* uncomment the next line to disable    ostream << char */
  36. //#define NO_OUTPUT_CHAR
  37.  
  38.  
  39. #include <_File.h>
  40. #include <streambuf.h>
  41. #include <filebuf.h>
  42. #include <_Filebuf.h>
  43.  
  44. class istream;
  45.  
  46. class ostream
  47. {
  48.   friend class istream;
  49. protected:
  50.   streambuf*    bp;
  51.   state_value   state;           // _good/_eof/_fail/_bad
  52.   char          ownbuf;          // true if we own *bp
  53.   
  54. public:
  55.                 ostream(const char* filename, io_mode m, access_mode a);
  56.                 ostream(const char* filename, const char* m);
  57.                 ostream(int filedesc, io_mode m);
  58.                 ostream(FILE* fileptr);
  59.                 ostream(int sz, char* buf);
  60.                 ostream(int filedesc, char* buf, int buflen);
  61.                 ostream(int filedesc);
  62.                 ostream(streambuf* s);
  63.  
  64.                ~ostream();
  65.  
  66.   ostream&      open(const char* filename, io_mode m, access_mode a);
  67.   ostream&      open(const char* filename, const char* m);
  68.   ostream&      open(int  filedesc, io_mode m);
  69.   ostream&      open(FILE* fileptr);
  70.   ostream&      open(const char* filenam, open_mode m);
  71.  
  72.   ostream&      close();
  73.   ostream&      flush();
  74.  
  75. // stream status
  76.  
  77.   int           rdstate();
  78.   int           eof();
  79.   int           fail();
  80.   int           bad();
  81.   int           good();
  82.  
  83. // other status queries
  84.  
  85.   int           readable();
  86.   int           writable();
  87.   int           is_open();
  88.  
  89.                 operator void*();
  90.   int           operator !();
  91.  
  92.   const char*   name();
  93.  
  94.   char*         bufptr();
  95.  
  96. // error handling
  97.  
  98.   void          error();
  99.   void          clear(state_value f = _good); // poorly named
  100.   void          set(state_value f); // set corresponding bit
  101.   void          unset(state_value); // clear corresponding bit
  102.   ostream&      failif(int cond);
  103.  
  104. // unformatted IO
  105.  
  106.   ostream&      put(char  c);
  107.   ostream&      put(const char* s);
  108.   ostream&      put(const char* s, int slen);
  109.            
  110. // formatted IO
  111.  
  112.   ostream&      form(const char* fmt, ...);           
  113.  
  114.   ostream&      operator << (short  n);
  115.   ostream&      operator << (unsigned short n);
  116.   ostream&      operator << (int    n);
  117.   ostream&      operator << (unsigned int n);
  118.   ostream&      operator << (long   n);
  119.   ostream&      operator << (unsigned long n);
  120. #ifdef __GNUG__
  121.   ostream&      operator << (long long n);
  122.   ostream&      operator << (unsigned long long n);
  123. #endif __GNUG__
  124.   ostream&      operator << (float  n);
  125.   ostream&      operator << (double n);
  126.   ostream&      operator << (const char* s);
  127.   ostream&      operator << (const void* ptr);
  128.  
  129. #ifndef NO_OUTPUT_CHAR
  130.   ostream&      operator << (char   c);
  131. #endif
  132.  
  133. };
  134.  
  135. extern ostream  cout;            // stdout
  136. extern ostream  cerr;            // stderr
  137.  
  138. #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
  139.  
  140.  
  141. inline void ostream::clear(state_value flag)
  142. {
  143.   state = flag;
  144. }
  145.  
  146. inline void ostream::set(state_value flag)
  147. {
  148.   state = state_value(int(state) | int(flag));
  149. }
  150.  
  151. inline void ostream::unset(state_value flag)
  152. {
  153.   state = state_value(int(state) & ~int(flag));
  154. }
  155.  
  156. inline int ostream::rdstate()
  157. {
  158.   return int(state);
  159. }
  160.  
  161. inline int ostream::good()
  162. {
  163.   return state == _good;
  164. }
  165.  
  166. inline int ostream::eof()
  167. {
  168.   return int(state) & int(_eof);
  169. }
  170.  
  171. inline int ostream::fail()
  172. {
  173.   return int(state) & int(_fail);
  174. }
  175.  
  176. inline int ostream::bad()
  177. {
  178.   return int(state) & int(_bad);
  179. }
  180.  
  181. inline ostream::operator void*()
  182. {
  183.   return (state == _good)? this : 0;
  184. }
  185.  
  186. inline int ostream::operator !()
  187. {
  188.   return (state != _good);
  189. }
  190.  
  191. inline ostream& ostream::failif(int cond)
  192. {
  193.   if (cond) set(_fail); return *this;
  194. }
  195.  
  196. inline int ostream::is_open()
  197. {
  198.   return bp->is_open();
  199. }
  200.  
  201. inline int ostream::readable()
  202. {
  203.   return 0;
  204. }
  205.  
  206. inline int ostream::writable()
  207. {
  208.   return (bp != 0) && (state == _good);
  209. }
  210.  
  211.  
  212. inline char* ostream::bufptr()
  213. {
  214.   return bp->base;
  215. }
  216.  
  217. inline ostream& ostream::flush()
  218. {
  219.   bp->overflow(); return *this;
  220. }
  221.  
  222. inline ostream& ostream::close()
  223. {
  224.   bp->overflow(); bp->close();  return *this;
  225. }
  226.  
  227. inline ostream& ostream::put(char ch)
  228. {
  229.   return failif((state != _good) || bp->sputc((int)ch &0xff) == EOF);
  230. }
  231.  
  232. #ifndef NO_OUTPUT_CHAR
  233. inline ostream& ostream::operator << (char ch)
  234. {
  235.   return failif((state != _good) || bp->sputc((int)ch &0xff) == EOF);
  236. }
  237. #endif
  238.  
  239. inline ostream& ostream::put(const char* s)
  240. {
  241.   return failif((state != _good) || bp->sputs(s) == EOF);
  242. }
  243.  
  244. inline ostream& ostream::put(const char* s, int len)
  245. {
  246.   return failif((state != _good) || bp->sputsn(s, len) == EOF);
  247. }
  248.  
  249. inline ostream& ostream::operator << (const char* s)
  250. {
  251.   return failif((state != _good) || bp->sputs(s) == EOF);
  252. }
  253.  
  254. #endif
  255.  
  256. #endif
  257.