home *** CD-ROM | disk | FTP | other *** search
/ Computer Club Elmshorn Atari PD / CCE_PD.iso / pc / 0600 / CCE_0639.ZIP / CCE_0639 / GPP / GXXINC.ZOO / istream.h < prev    next >
C/C++ Source or Header  |  1991-07-10  |  6KB  |  269 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. /* istream.h now separately includable */
  27.  
  28. #ifndef _istream_h
  29. #ifdef __GNUG__
  30. #pragma once
  31. #pragma interface
  32. #endif
  33. #define _istream_h 1
  34.  
  35. #ifdef atarist
  36. #include <compiler.h>
  37. #endif
  38.  
  39. #include <xfile.h>
  40. #include <streambu.h>
  41. #include <filebuf.h>
  42. #include <xfilebuf.h>
  43.  
  44. class whitespace                // a class used only to input and
  45. {                               // discard white space characters
  46.   char filler;                     
  47. };
  48.  
  49. class ostream;
  50.  
  51. class istream
  52. {
  53.   friend void   eatwhite(istream& s);
  54. protected:
  55.   streambuf*    bp;
  56.   state_value   state;           // _good/_eof/_fail/_bad
  57.   ostream*      tied_to;
  58.   char          skipws;
  59.   char          ownbuf;
  60.   void          _flush();
  61.   char*         readline (int chunk_number, char terminator);
  62.   
  63. public:
  64.                 istream(const char* filename, io_mode m, access_mode a, 
  65.                         int sk=1, ostream* t = 0);
  66.                 istream(const char* filename, const char* m, 
  67.                         int sk=1, ostream* t = 0);
  68.                 istream(int filedesc, io_mode m, int sk=1, ostream* t = 0);
  69.                 istream(FILE* fileptr, int sk=1, ostream* t = 0);
  70.                 istream(int filedesc, int sk=1, ostream* t = 0);
  71. #ifdef atarist
  72.                 istream(size_t sz, char* buf, int sk=1, ostream* t = 0);
  73.                 istream(int filedesc, char* buf, size_t buflen, 
  74.                         int sk, ostream* t = 0);
  75. #else
  76.                 istream(int sz, char* buf, int sk=1, ostream* t = 0);
  77.                 istream(int filedesc, char* buf, int buflen, 
  78.                         int sk, ostream* t = 0);
  79. #endif
  80.                 istream(streambuf* s, int sk=1, ostream* t = 0);
  81.  
  82.                ~istream();
  83.  
  84.   istream&      open(const char* filename, io_mode m, access_mode a);
  85.   istream&      open(const char* filename, const char* m);
  86.   istream&      open(int  filedesc, io_mode m);
  87.   istream&      open(FILE* fileptr);
  88.   istream&      open(const char* filenam, open_mode m);
  89.  
  90.   istream&      close();
  91.  
  92.   ostream*      tie(ostream* s);
  93.   int           skip(int);
  94.  
  95. // stream status
  96.  
  97.   int           rdstate();
  98.   int           eof();
  99.   int           fail();
  100.   int           bad();
  101.   int           good();
  102.  
  103. // other status queries
  104.  
  105.   int           readable();
  106.   int           writable();
  107.   int           is_open();
  108.  
  109.                 operator void*();
  110.   int           operator !();
  111.  
  112.   const char*   name();
  113.  
  114.   char*         bufptr();
  115.  
  116. // error handling
  117.  
  118.   void          error();
  119.   void          clear(state_value f = _good); // poorly named
  120.   void          set(state_value f); // set corresponding bit
  121.   void          unset(state_value f); // clear corresponding bit
  122.   istream&      failif(int cond);
  123.  
  124. // unformatted IO
  125.  
  126.   istream&      get(char& c);
  127.   istream&      unget(char c);
  128.   istream&      putback(char c); // a synonym for unget
  129.  
  130.   istream&      get    (char* s, int n, char terminator = '\n');
  131.   istream&      getline(char* s, int n, char terminator = '\n');
  132.   istream&      gets   (char **s, char terminator = '\n');
  133.  
  134.  
  135.   istream&      operator >> (char&   c);
  136.   istream&      operator >> (short&  n);
  137.   istream&      operator >> (unsigned short& n);
  138.   istream&      operator >> (int&    n);
  139.   istream&      operator >> (unsigned int& n);
  140.   istream&      operator >> (long&   n);
  141.   istream&      operator >> (unsigned long& n);
  142. #ifdef __GNUG__
  143.   istream&      operator >> (long long& n);
  144.   istream&      operator >> (unsigned long long& n);
  145. #endif
  146.   istream&      operator >> (float&  n);
  147.   istream&      operator >> (double& n);
  148.   istream&      operator >> (char*   s);
  149.   istream&      operator >> (whitespace& w);
  150. };
  151.  
  152. // pre-declared streams
  153.  
  154. extern istream  cin;             // stdin
  155.  
  156. extern whitespace WS;            // for convenience
  157.  
  158. #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
  159.  
  160.  
  161. inline void istream::clear(state_value flag)
  162. {
  163.   state = flag;
  164. }
  165.  
  166. inline void istream::set(state_value flag)
  167. {
  168.   state = state_value(int(state) | int(flag));
  169. }
  170.  
  171. inline void istream::unset(state_value flag)
  172. {
  173.   state = state_value(int(state) & ~int(flag));
  174. }
  175.  
  176. inline int istream::rdstate()
  177. {
  178.   return int(state);
  179. }
  180.  
  181. inline int istream::good()
  182. {
  183.   return state == _good;
  184. }
  185.  
  186. inline int istream::eof()
  187. {
  188.   return int(state) & int(_eof);
  189. }
  190.  
  191. inline int istream::fail()
  192. {
  193.   return int(state) & int(_fail);
  194. }
  195.  
  196. inline int istream::bad()
  197. {
  198.   return int(state) & int(_bad);
  199. }
  200.  
  201. inline istream::operator void*()
  202. {
  203.   return (state == _good)? this : 0;
  204. }
  205.  
  206. inline int istream::operator !()
  207. {
  208.   return (state != _good);
  209. }
  210.  
  211. inline istream& istream::failif(int cond)
  212. {
  213.   if (cond) set(_fail); return *this;
  214. }
  215.  
  216. inline int istream::is_open()
  217. {
  218.   return bp->is_open();
  219. }
  220.  
  221. inline int istream::readable()
  222. {
  223.   return (bp != 0) && (bp->is_open()) && (state == _good);
  224. }
  225.  
  226. inline int istream::writable()
  227. {
  228.   return 0;
  229. }
  230.  
  231.  
  232. inline char* istream::bufptr()
  233. {
  234.   return bp->base;
  235. }
  236.  
  237.  
  238. inline istream& istream::close()
  239. {
  240.   bp->close();  return *this;
  241. }
  242.  
  243.  
  244. inline int istream::skip(int sk)
  245. {
  246.   int was = skipws; skipws = sk; return was;
  247. }
  248.  
  249.  
  250. inline istream& istream::unget(char c)
  251. {
  252.   if (bp->sputbackc(c) == EOF) set(_fail); return *this;
  253. }
  254.  
  255. inline istream& istream::putback(char c)
  256. {
  257.   if (bp->sputbackc(c) == EOF) set(_fail); return *this;
  258. }
  259.  
  260. inline void eatwhite(istream& s)
  261. {
  262.   s >> WS;
  263. }
  264.  
  265. #endif
  266.  
  267.  
  268. #endif
  269.