home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__inc / ostream.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  5.9 KB  |  271 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 the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. /* *** Version 1.2 -- nearly 100% AT&T 1.2 compatible *** */
  20.  
  21. /* ostream.h now  separately includable */
  22.  
  23. #ifndef _ostream_h
  24. #ifdef __GNUG__
  25. #pragma once
  26. #pragma interface
  27. #endif
  28. #define _ostream_h 1
  29.  
  30. /* uncomment the next line to disable    ostream << char */
  31. //#define NO_OUTPUT_CHAR
  32.  
  33. #ifdef atarist
  34. #include <compiler.h>
  35. #endif
  36.  
  37. #include <xfile.h>
  38. #include <streambu.h>
  39. #include <filebuf.h>
  40. #include <xfilebuf.h>
  41.  
  42. class istream;
  43.  
  44. class ostream
  45. {
  46.   friend class istream;
  47. protected:
  48.   streambuf*    bp;
  49.   state_value   state;           // _good/_eof/_fail/_bad
  50.   char          ownbuf;          // true if we own *bp
  51.   
  52. public:
  53.                 ostream(const char* filename, io_mode m, access_mode a);
  54.                 ostream(const char* filename, const char* m);
  55.                 ostream(int filedesc, io_mode m);
  56.                 ostream(FILE* fileptr);
  57. #ifdef atarist
  58.                 ostream(size_t sz, char* buf);
  59.                 ostream(int filedesc, char* buf, size_t buflen);
  60. #else
  61.                 ostream(int sz, char* buf);
  62.                 ostream(int filedesc, char* buf, int buflen);
  63. #endif
  64.                 ostream(int filedesc);
  65.                 ostream(streambuf* s);
  66.  
  67.                ~ostream();
  68.  
  69.   ostream&      open(const char* filename, io_mode m, access_mode a);
  70.   ostream&      open(const char* filename, const char* m);
  71.   ostream&      open(int  filedesc, io_mode m);
  72.   ostream&      open(FILE* fileptr);
  73.   ostream&      open(const char* filenam, open_mode m);
  74.  
  75.   ostream&      close();
  76.   ostream&      flush();
  77.  
  78. // stream status
  79.  
  80.   int           rdstate();
  81.   int           eof();
  82.   int           fail();
  83.   int           bad();
  84.   int           good();
  85.  
  86. // other status queries
  87.  
  88.   int           readable();
  89.   int           writable();
  90.   int           is_open();
  91.  
  92.                 operator void*();
  93.   int           operator !();
  94.  
  95.   const char*   name();
  96.  
  97.   char*         bufptr();
  98.  
  99. // error handling
  100.  
  101.   void          error();
  102.   void          clear(state_value f = _good); // poorly named
  103.   void          set(state_value f); // set corresponding bit
  104.   void          unset(state_value); // clear corresponding bit
  105.   ostream&      failif(int cond);
  106.  
  107. // unformatted IO
  108.  
  109.   ostream&      put(char  c);
  110.   ostream&      put(const char* s);
  111. #ifdef atarist
  112.   ostream&      put(const char* s, size_t slen);
  113. #else
  114.   ostream&      put(const char* s, int slen);
  115. #endif
  116.            
  117. // formatted IO
  118.  
  119.   ostream&      form(const char* fmt, ...);           
  120.  
  121.   ostream&      operator << (short  n);
  122.   ostream&      operator << (unsigned short n);
  123.   ostream&      operator << (int    n);
  124.   ostream&      operator << (unsigned int n);
  125.   ostream&      operator << (long   n);
  126.   ostream&      operator << (unsigned long n);
  127. #ifdef __GNUG__
  128.   ostream&      operator << (long long n);
  129.   ostream&      operator << (unsigned long long n);
  130. #endif __GNUG__
  131.   ostream&      operator << (float  n);
  132.   ostream&      operator << (double n);
  133.   ostream&      operator << (const char* s);
  134.   ostream&      operator << (const void* ptr);
  135.  
  136. #ifndef NO_OUTPUT_CHAR
  137.   ostream&      operator << (char   c);
  138. #endif
  139.  
  140. };
  141.  
  142. extern ostream  cout;            // stdout
  143. extern ostream  cerr;            // stderr
  144.  
  145. #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
  146.  
  147.  
  148. inline void ostream::clear(state_value flag)
  149. {
  150.   state = flag;
  151. }
  152.  
  153. inline void ostream::set(state_value flag)
  154. {
  155.   state = state_value(int(state) | int(flag));
  156. }
  157.  
  158. inline void ostream::unset(state_value flag)
  159. {
  160.   state = state_value(int(state) & ~int(flag));
  161. }
  162.  
  163. inline int ostream::rdstate()
  164. {
  165.   return int(state);
  166. }
  167.  
  168. inline int ostream::good()
  169. {
  170.   return state == _good;
  171. }
  172.  
  173. inline int ostream::eof()
  174. {
  175.   return int(state) & int(_eof);
  176. }
  177.  
  178. inline int ostream::fail()
  179. {
  180.   return int(state) & int(_fail);
  181. }
  182.  
  183. inline int ostream::bad()
  184. {
  185.   return int(state) & int(_bad);
  186. }
  187.  
  188. inline ostream::operator void*()
  189. {
  190.   return (state == _good)? this : 0;
  191. }
  192.  
  193. inline int ostream::operator !()
  194. {
  195.   return (state != _good);
  196. }
  197.  
  198. inline ostream& ostream::failif(int cond)
  199. {
  200.   if (cond) set(_fail); return *this;
  201. }
  202.  
  203. inline int ostream::is_open()
  204. {
  205.   return bp->is_open();
  206. }
  207.  
  208. inline int ostream::readable()
  209. {
  210.   return 0;
  211. }
  212.  
  213. inline int ostream::writable()
  214. {
  215.   return (bp != 0) && (state == _good);
  216. }
  217.  
  218.  
  219. inline char* ostream::bufptr()
  220. {
  221.   return bp->base;
  222. }
  223.  
  224. inline ostream& ostream::flush()
  225. {
  226.   bp->overflow(); return *this;
  227. }
  228.  
  229. inline ostream& ostream::close()
  230. {
  231.   bp->overflow(); bp->close();  return *this;
  232. }
  233.  
  234. inline ostream& ostream::put(char ch)
  235. {
  236.   return failif((state != _good) || bp->sputc((int)ch &0xff) == EOF);
  237. }
  238.  
  239. #ifndef NO_OUTPUT_CHAR
  240. inline ostream& ostream::operator << (char ch)
  241. {
  242.   return failif((state != _good) || bp->sputc((int)ch &0xff) == EOF);
  243. }
  244. #endif
  245.  
  246. inline ostream& ostream::put(const char* s)
  247. {
  248.   return failif((state != _good) || bp->sputs(s) == EOF);
  249. }
  250.  
  251. #ifdef atarist
  252. inline ostream& ostream::put(const char* s, size_t len)
  253. {
  254.   return failif((state != _good) || bp->sputsn(s, len) == EOF);
  255. }
  256. #else
  257. inline ostream& ostream::put(const char* s, int len)
  258. {
  259.   return failif((state != _good) || bp->sputsn(s, len) == EOF);
  260. }
  261. #endif
  262.  
  263. inline ostream& ostream::operator << (const char* s)
  264. {
  265.   return failif((state != _good) || bp->sputs(s) == EOF);
  266. }
  267.  
  268. #endif
  269.  
  270. #endif
  271.