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