home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c017 / 7.ddi / GPPLIB.ZIP / STREAM.H < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-17  |  6.0 KB  |  246 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 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. #ifndef _stream_h
  25. #define _stream_h 1
  26.  
  27. #include <File.h>
  28.  
  29. class whitespace                // a class used only to input and
  30. {                               // discard white space characters
  31.   char filler;                     
  32. };
  33.  
  34. class ostream: File
  35. {
  36. public:
  37.            File::open;      File::close;
  38.            File::remove;    File::filedesc;  File::is_open;
  39.            File::raw;       File::put;       
  40.            File::iocount;   File::error;     File::name;
  41.            File::setname;   File::rdstate;   File::flush;
  42.            File::eof;       File::fail;      File::bad;
  43.            File::good;      File::clear;     File::failif;
  44.            File::setbuf;    File::writable;  File::readable;
  45.            
  46.  
  47.            ostream();
  48.            ostream(const char* filename, io_mode m, access_mode a);
  49.            ostream(const char* filename, const char* m);
  50.            ostream(int filedesc, io_mode m = io_writeonly);
  51.            ostream(FILE* fileptr);
  52.  
  53.            ~ostream();
  54.  
  55.   void*    operator void*();
  56.  
  57.   ostream& form(const char* fmt, ...);           
  58.  
  59.   ostream& operator << (char   c);
  60.   ostream& operator << (short  n);
  61.   ostream& operator << (unsigned short n);
  62.   ostream& operator << (int    n);
  63.   ostream& operator << (unsigned int n);
  64.   ostream& operator << (long   n);
  65.   ostream& operator << (unsigned long n);
  66.   ostream& operator << (float  n);
  67.   ostream& operator << (double n);
  68.   ostream& operator << (const char* s);
  69. };
  70.  
  71.  
  72. class istream: File
  73. {
  74.   ostream* tied_to;        // unused, but maintained for AT&T compatibility
  75.   void     _flush();
  76.  
  77. public:
  78.            File::open;      File::close;     File::get;
  79.            File::remove;    File::filedesc;  File::is_open;
  80.            File::raw;       File::unget;     File::getline;
  81.            File::iocount;   File::error;     File::name;
  82.            File::setname;   File::rdstate;   File::putback;
  83.            File::eof;       File::fail;      File::bad;
  84.            File::good;      File::clear;     File::failif;
  85.            File::setbuf;    File::writable;  File::readable;
  86.  
  87.  
  88.            istream();
  89.            istream(const char* filename, io_mode m, access_mode a);
  90.            istream(const char* filename, const char* m);
  91.            istream(int filedesc, io_mode m = io_readonly);
  92.            istream(FILE* fileptr);
  93.  
  94.            ~istream();
  95.  
  96.   void*    operator void*();
  97.  
  98.   ostream* tie(ostream* s);
  99.  
  100.   istream& scan(const char* fmt, ...);
  101.  
  102.   istream& operator >> (char&   c);
  103.   istream& operator >> (short&  n);
  104.   istream& operator >> (unsigned short& n);
  105.   istream& operator >> (int&    n);
  106.   istream& operator >> (unsigned int& n);
  107.   istream& operator >> (long&   n);
  108.   istream& operator >> (unsigned long& n);
  109.   istream& operator >> (float&  n);
  110.   istream& operator >> (double& n);
  111.   istream& operator >> (char*   s);
  112.   istream& operator >> (whitespace& w);
  113. };
  114.  
  115. void eatwhite(istream& s);
  116.  
  117.  
  118. // pre-declared streams
  119.  
  120. extern istream  cin;             // stdin
  121. extern ostream  cout;            // stdout
  122. extern ostream  cerr;            // stderr
  123.  
  124. extern whitespace WS;            // for convenience
  125.  
  126. //#ifdef __OPTIMIZE__
  127.  
  128.  
  129. inline void* ostream::operator void*()
  130.   return (state & (_bad|_fail))? 0 : this ; 
  131. }
  132.  
  133. inline ostream& ostream::operator<<(char   c)
  134.   put(c);
  135.   return *this;
  136. }
  137.  
  138. inline ostream& ostream::operator<<(short  n)
  139.   return form("%d",(int)n);
  140. }
  141.  
  142. inline ostream& ostream::operator<<(unsigned short n)
  143.   return form("%u",(unsigned)n);
  144. }
  145.  
  146. inline ostream& ostream::operator<<(int    n)
  147.   return form("%d",n);
  148. }
  149.  
  150. inline ostream& ostream::operator<<(unsigned int n)
  151.   return form("%u",n);
  152. }
  153.  
  154. inline ostream& ostream::operator<<(long   n)
  155.   return form("%ld",n);
  156. }
  157.  
  158. inline ostream& ostream::operator<<(unsigned long n)
  159.   return form("%lu",n);
  160. }
  161.  
  162. inline ostream& ostream::operator<<(float  n)
  163.   return form("%g",(double)n);
  164. }
  165.  
  166. inline ostream& ostream::operator<<(double n)
  167.   return form("%g",n);
  168. }
  169.  
  170. inline ostream& ostream::operator<<(const char* s)
  171.   put(s);
  172.   return *this;
  173. }
  174.  
  175. inline void*    istream::operator void*()
  176.   return (state & (_bad|_fail))? 0 : this ; 
  177. }
  178.  
  179. inline istream& istream::operator>>(char&   c)
  180.   _flush();
  181.   get(c);
  182.   return *this;
  183. }
  184.  
  185. inline istream& istream::operator>>(short&  n)
  186.   return scan("%hd", &n); 
  187. }
  188.  
  189. inline istream& istream::operator>>(unsigned short& n)
  190.   return scan("%hd", &n); 
  191. }
  192.  
  193. inline istream& istream::operator>>(int&    n)
  194.   return scan("%d",  &n); 
  195. }
  196.  
  197. inline istream& istream::operator>>(unsigned int& n)
  198.   return scan("%d",  &n); 
  199. }
  200.  
  201. inline istream& istream::operator>>(long&   n)
  202.   return scan("%ld", &n); 
  203. }
  204.  
  205. inline istream& istream::operator>>(unsigned long& n)
  206.   return scan("%ld", &n); 
  207. }
  208.  
  209. inline istream& istream::operator>>(float&  n)
  210.   return scan("%f",  &n); 
  211. }
  212.  
  213. inline istream& istream::operator>>(double& n)
  214.   return scan("%lf", &n); 
  215. }
  216.  
  217. inline istream& istream::operator>>(char*   s)
  218.   return scan("%s",   s); 
  219. }
  220.  
  221. //#endif
  222.  
  223. #endif
  224.