home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / gxxinc / xfile.h < prev    next >
C/C++ Source or Header  |  1991-07-08  |  8KB  |  376 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 _File_h 
  25. #ifdef __GNUG__
  26. #pragma once
  27. #pragma interface
  28. #endif
  29. #define _File_h 1
  30.  
  31. #include <builtin.h>
  32. #include <stdio.h>
  33. #include <stddef.h>
  34.  
  35. #include <xfmodes.h>
  36.  
  37. class Filebuf;
  38.  
  39. class File
  40. {
  41.   friend class  Filebuf;
  42. protected:
  43.   FILE*         fp;              // _iobuf file pointer
  44.   char*         nm;              // file name (dynamically allocated)
  45.   char          rw;              //  1 = read; 2 = write; 3 = readwrite
  46.                                  //  bit 2 (4) means read/write into string
  47.   state_value   state;           // _good/_eof/_fail/_bad
  48.   long          stat;            // last read/write/... return value
  49.  
  50.   void          initialize();
  51.   void          reinitialize(const char*);
  52.   char         *readline (int chunk_number, char terminator);
  53.  
  54. public:
  55.                 File();
  56.                 File(const char* filename, io_mode m, access_mode a);
  57.                 File(const char* filename, const char* m);   
  58.                 File(int filedesc, io_mode m);
  59.                 File(FILE* fileptr);
  60. #ifdef atarist
  61.                 File(size_t sz, char* buf, io_mode m);
  62. #else
  63.                 File(int sz, char* buf, io_mode m);
  64. #endif
  65.                 ~File();
  66.  
  67. // binding, rebinding, unbinding to physical files
  68.  
  69.   File&         open(const char* filename, io_mode m, access_mode a);
  70.   File&         open(const char* filename, const char* m);
  71.   File&         open(int  filedesc, io_mode m);
  72.   File&         open(FILE* fileptr);
  73.  
  74.   File&         close();
  75.   File&         remove();
  76.  
  77. // class variable access
  78.  
  79.   int           filedesc();
  80.   const char*   name();
  81.   void          setname(const char* newname);
  82.   long          iocount();    /* outright wrong in orig */
  83.  
  84.   int           rdstate();
  85.   int           eof();
  86.   int           fail();
  87.   int           bad();
  88.   int           good();
  89. #ifdef atarist
  90.   int           bin();
  91.   int           text();
  92. #endif
  93.  
  94. // other status queries
  95.  
  96.   int           readable();
  97.   int           writable();
  98.   int           is_open();
  99.  
  100.                 operator void*();
  101.  
  102. // error handling
  103.  
  104.   void          error();
  105.   void          clear(state_value f = _good); // poorly named
  106.   void          set(state_value f); // set corresponding but
  107.   void          unset(state_value f); // clear corresponding bit
  108.   File&         failif(int cond);
  109.   void          check_state();
  110.  
  111. // character IO
  112.  
  113.   File&         get(char& c);
  114.   File&         put(char  c);
  115.   File&         unget(char c);
  116.   File&         putback(char c); // a synonym for unget
  117.  
  118. // char* IO
  119.  
  120.   File&         put(const char* s);
  121.   File&         get    (char* s, int n, char terminator = '\n');
  122.   File&         getline(char* s, int n, char terminator = '\n');
  123.   File&         gets   (char **s, char terminator = '\n');
  124.  
  125. // binary IO
  126.  
  127. #ifdef atarist
  128.   File&         read(void* x, size_t sz, size_t n);
  129.   File&         write(void* x, size_t sz, size_t n);
  130. #else
  131.   File&         read(void* x, int sz, int n);
  132.   File&         write(void* x, int sz, int n);
  133. #endif
  134.  
  135. // formatted IO
  136.  
  137.   File&         form(const char* ...);
  138.   File&         scan(const char* ...);
  139.  
  140. // buffer IO
  141.  
  142.   File&         flush();
  143.   File&         flush(char ch); // call stdio _flsbuf
  144.   int           fill();         // call stdio _filbuf
  145.  
  146. // position control
  147.  
  148.   File&         seek(long pos, int seek_mode=0); // default seek mode=absolute
  149.   long          tell();
  150.  
  151. // buffer control
  152.  
  153.   File&         setbuf(int buffer_kind); // legal vals: _IONBF, _IOFBF, _IOLBF
  154. #ifdef atarist
  155.   File&         setbuf(size_t size, char* buf);
  156. #else
  157.   File&         setbuf(int size, char* buf);
  158. #endif
  159.   File&         raw();
  160. };
  161.  
  162.  
  163. // error handlers
  164.  
  165. extern void  verbose_File_error_handler(const char*);
  166. extern void  quiet_File_error_handler(const char*);
  167. extern void  fatal_File_error_handler(const char*);
  168. extern one_arg_error_handler_t File_error_handler;
  169. extern one_arg_error_handler_t set_File_error_handler(one_arg_error_handler_t);
  170.  
  171. #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
  172.  
  173.  
  174.  
  175. inline int File::filedesc()
  176.   return fileno(fp);
  177. }
  178.  
  179. inline const char* File::name()
  180.   return nm; 
  181. }
  182.  
  183. inline long File::iocount()
  184.   return stat; 
  185. }
  186.  
  187. inline void File::clear(state_value flag)
  188.   state = flag;
  189. }
  190.  
  191. inline void File::set(state_value flag)
  192.   state = state_value(int(state) | int(flag));
  193. }
  194.  
  195. inline void File::unset(state_value flag)
  196.   state = state_value(int(state) & ~int(flag));
  197. }
  198.  
  199. inline int File::readable()
  200.   if (fp != 0) { if (feof(fp)) set(_eof); if (ferror(fp)) set(_bad);}
  201. #ifndef atarist
  202.   return (state == _good && (rw & 01));
  203. #else
  204.   return (state_value(rdstate()) == _good && (rw & 01));
  205. #endif
  206. }
  207.  
  208. inline int File::writable()
  209.   if (fp != 0 && ferror(fp)) set(_bad);
  210.   return ((int(state) & (int(_fail)|int(_bad))) == 0 && (rw & 02));
  211. }
  212.  
  213. inline int File::is_open()
  214.   return (fp != 0);
  215. }
  216.  
  217.  
  218. inline File& File::raw()
  219.   return this->File::setbuf(_IONBF); 
  220. }
  221.  
  222.  
  223. inline File& File::failif(int cond)
  224.   if (cond) set(_fail);  return *this; 
  225. }
  226.  
  227. inline File& File::get(char& c)
  228.   if (readable())
  229.   {
  230.     int ch = getc(fp);
  231.     c = ch;
  232.     failif (ch == EOF);
  233.   }
  234.   return *this;
  235. }
  236.  
  237. inline File& File::put(char  c) 
  238.   return failif (!writable() ||  putc(c, fp) == EOF);
  239. }
  240.  
  241. inline File& File::unget(char c)
  242.   return failif(!is_open() || !(rw & 01) || ungetc(c, fp) == EOF);
  243.  
  244. inline File& File::putback(char c)
  245.   return failif (!is_open() || !(rw & 01) || ungetc(c, fp) == EOF);
  246. }
  247.  
  248. #ifdef atarist
  249. inline File& File::read(void* x, size_t sz, size_t n)
  250.   return failif (!readable() || (stat = fread(x, sz, n, fp)) != n);
  251.  
  252. inline File& File::write(void* x, size_t sz, size_t n) 
  253.   return failif (!writable() || (stat = fwrite(x, sz, n, fp)) != n);
  254. }
  255. #else
  256. inline File& File::read(void* x, int sz, int n)
  257.   return failif (!readable() || (stat = fread(x, sz, n, fp)) != n);
  258.  
  259. inline File& File::write(void* x, int sz, int n) 
  260.   return failif (!writable() || (stat = fwrite(x, sz, n, fp)) != n);
  261. }
  262. #endif
  263.  
  264. inline File& File::flush()
  265.   return failif(!is_open() || fflush(fp) == EOF);
  266. }
  267.  
  268. inline File& File::flush(char ch)
  269. #ifdef VMS
  270.   return failif(!is_open() || c$$flsbuf(ch, fp) == EOF);
  271. #else
  272. #ifdef atarist
  273.   return failif(!is_open() || fputc(ch, fp) == EOF || fflush(fp) == EOF);
  274. #else
  275.   return failif(!is_open() || _flsbuf(ch, fp) == EOF);
  276. #endif
  277. #endif
  278. }
  279.  
  280. inline int File::fill()
  281. #ifdef VMS
  282.   failif(!is_open() || (stat = c$$filbuf(fp)) == EOF);
  283. #else
  284.   failif(!is_open() || (stat = _filbuf(fp)) == EOF);
  285. #endif
  286.   return stat;
  287. }
  288.  
  289. inline File& File::seek(long pos, int seek_mode)
  290.   return failif (!is_open() || fseek(fp, pos, seek_mode) < 0); 
  291. }
  292.  
  293. inline long File::tell()
  294.   failif (!is_open() || ((stat = ftell(fp)) < 0));
  295.   return stat;
  296. }
  297.  
  298. inline int File::rdstate()
  299. #ifndef atarist
  300.   check_state();  return state; // check_state is necessary in rare but
  301. }                               // possible circumstances
  302. #else
  303.   check_state(); return (state & ~(_bin|_text));
  304. }
  305. #endif
  306.  
  307. inline File::operator void*()
  308.   check_state();  return (int(state) & (int(_bad)|int(_fail)))? 0 : this ; 
  309. }
  310.  
  311. inline int File::eof()
  312.   check_state(); return state & _eof; 
  313. }
  314.  
  315. inline int File::fail()
  316.   check_state(); return state & _fail; 
  317. }
  318.  
  319. inline int File::bad()
  320.   check_state(); return state & _bad; 
  321. }
  322.  
  323. inline int File::good()
  324.   check_state(); return rdstate() == _good; 
  325. }
  326.  
  327. #ifdef atarist
  328.  
  329. inline int File::bin()
  330. {
  331.   check_state(); return state & _bin;
  332. }
  333.  
  334. inline int File::text()
  335. {
  336.   check_state(); return state & _text;
  337. }
  338. #endif  // atarist
  339.  
  340. #endif
  341.  
  342. #endif
  343.