home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / file.old < prev    next >
Encoding:
Text File  |  1993-07-23  |  7.1 KB  |  310 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. #pragma once
  26. #define _File_h 1
  27.  
  28. #include <builtin.h>
  29. #include <stdio.h>
  30. #include <stddef.h>
  31.  
  32. enum io_mode                    // known unix file IO modes
  33. {
  34.   io_readonly   = 0,            
  35.   io_writeonly  = 1,
  36.   io_readwrite  = 2, 
  37.   io_appendonly = 3,
  38.   io_append     = 4,            // append, plus allow reads
  39. };
  40.  
  41. enum access_mode                // ways to open a file
  42. {
  43.   a_createonly  = 0,            // create, fail if file exists
  44.   a_create      = 1,            // create if doesn't exist, else truncate
  45.   a_useonly     = 2,            // use (no truncate)  fail if doesn't exist
  46.   a_use         = 3,            // use (no truncate), create if doesn't exist
  47. };
  48.  
  49. enum state_value                // File states
  50.   _good         = 0,            // all is well
  51.   _eof          = 1,            // at eof
  52.   _fail         = 2,            // logical or physical IO error
  53.   _bad          = 4             // unopened/corrupted
  54. };
  55.  
  56.  
  57. class File
  58. {
  59. protected:
  60.   FILE*         fp;              // _iobuf file pointer
  61.   char*         nm;              // file name (dynamically allocated)
  62.   char          rw;              //  1 = read; 2 = write; 3 = readwrite
  63.                                  //  bit 2 (4) means read/write into string
  64.   state_value   state;           // _good/_eof/_fail/_bad
  65.   long          stat;            // last read/write/... return value
  66.  
  67.   void          initialize();
  68.   void          reinitialize(const char*);
  69.   char         *readline (int chunk_number, char terminator);
  70.  
  71. public:
  72.                 File();
  73.                 File(const char* filename, io_mode m, access_mode a);
  74.                 File(const char* filename, const char* m);   
  75.                 File(int filedesc, io_mode m);
  76.                 File(FILE* fileptr);
  77.                 File(int sz, char* buf, io_mode m);
  78.  
  79.                 ~File();
  80.  
  81. // binding, rebinding, unbinding to physical files
  82.  
  83.   File&         open(const char* filename, io_mode m, access_mode a);
  84.   File&         open(const char* filename, const char* m);
  85.   File&         open(int  filedesc, io_mode m);
  86.   File&         open(FILE* fileptr);
  87.  
  88.   File&         close();
  89.   File&         remove();
  90.  
  91. // class variable access
  92.  
  93.   int           filedesc();
  94.   const char*   name();
  95.   void          setname(const char* newname);
  96.   int           iocount();
  97.  
  98.   int           rdstate();
  99.   int           eof();
  100.   int           fail();
  101.   int           bad();
  102.   int           good();
  103.  
  104. // other status queries
  105.  
  106.   int           readable();
  107.   int           writable();
  108.   int           is_open();
  109.  
  110.   void*         operator void*();
  111.  
  112. // error handling
  113.  
  114.   void          error();
  115.   void          clear(state_value f = 0); // poorly named
  116.   File&         failif(int cond);
  117.   void          check_state();
  118.  
  119. // character IO
  120.  
  121.   File&         get(char& c);
  122.   File&         put(char  c);
  123.   File&         unget(char c);
  124.   File&         putback(char c); // a synonym for unget
  125.  
  126. // char* IO
  127.  
  128.   File&         put(const char* s);
  129.   File&         get    (char* s, int n, char terminator = '\n');
  130.   File&         getline(char* s, int n, char terminator = '\n');
  131.   File&         gets   (char **s, char terminator = '\n');
  132.  
  133. // binary IO
  134.  
  135.   File&         read(void* x, int sz, int n);
  136.   File&         write(void* x, int sz, int n);
  137.  
  138.  
  139. // file control
  140.  
  141.   File&         seek(long pos, int seek_mode=0); // default seek mode=absolute
  142.   long          tell();
  143.  
  144.   File&         flush();
  145.   File&         setbuf(int buffer_kind); // legal vals: _IONBF, _IOFBF, _IOLBF
  146.   File&         setbuf(int size, char* buf);
  147.   File&         raw();
  148. };
  149.  
  150.  
  151. // error handlers
  152.  
  153. extern void  verbose_File_error_handler(char*);
  154. extern void  quiet_File_error_handler(char*);
  155. extern void  fatal_File_error_handler(char*);
  156. extern one_arg_error_handler_t File_error_handler;
  157. extern one_arg_error_handler_t set_File_error_handler(one_arg_error_handler_t);
  158.  
  159. //#ifdef __OPTIMIZE__
  160.  
  161.  
  162.  
  163. inline int File::filedesc()
  164.   return fileno(fp);
  165. }
  166.  
  167. inline const char* File::name()
  168.   return nm; 
  169. }
  170.  
  171. inline int File::iocount()
  172.   return stat; 
  173. }
  174.  
  175. inline int File::readable()
  176.   if (fp != 0) { if (feof(fp)) state |= _eof; if (ferror(fp)) state |= _bad;}
  177.   return (state == _good && (rw & 01));
  178. }
  179.  
  180. inline int File::writable()
  181.   if (fp != 0 && ferror(fp)) state |= _bad;
  182.   return ((state & (_fail|_bad)) == 0 && (rw & 02));
  183. }
  184.  
  185. inline int File::is_open()
  186.   return (fp != 0);
  187. }
  188.  
  189. inline void File::clear(state_value flag = 0)
  190.   state = flag;
  191. }
  192.  
  193. inline File& File::raw()
  194.   return this->File::setbuf(_IONBF); 
  195. }
  196.  
  197. inline void File::check_state() // ensure fp & state agree about eof
  198. {
  199.   if (fp != 0)
  200.   {
  201.     if (feof(fp))
  202.       state |= _eof;
  203.     else
  204.       state &= ~_eof;
  205.     if (ferror(fp))
  206.       state |= _bad;
  207.   }
  208. }
  209.  
  210. inline File& File::failif(int cond)
  211.   if (cond) state |= _fail;  return *this; 
  212. }
  213.  
  214. inline File& File::get(char& c)
  215.   if (readable())
  216.   {
  217.     int ch = getc(fp);
  218.     c = ch;
  219.     failif (ch == EOF);
  220.   }
  221.   return *this;
  222. }
  223.  
  224. inline File& File::put(char  c) 
  225.   return failif (!writable() ||  putc(c, fp) == EOF);
  226. }
  227.  
  228. inline File& File::unget(char c)
  229.   return failif(!is_open() || !(rw & 01) || ungetc(c, fp) == EOF);
  230.  
  231. inline File& File::putback(char c)
  232.   return failif (!is_open() || !(rw & 01) || ungetc(c, fp) == EOF);
  233. }
  234.  
  235. inline File& File::read(void* x, int sz, int n)
  236.   return failif (!readable() || (stat = fread(x, sz, n, fp)) != n);
  237.  
  238. inline File& File::write(void* x, int sz, int n) 
  239.   return failif (!writable() || (stat = fwrite(x, sz, n, fp)) != n);
  240. }
  241.  
  242. inline File& File::flush()
  243.   return failif(!is_open() || fflush(fp) == EOF);
  244. }
  245.  
  246. inline File& File::seek(long pos, int seek_mode = 0)
  247.   return failif (!is_open() || fseek(fp, pos, seek_mode) < 0); 
  248. }
  249.  
  250. inline long File::tell()
  251.   failif (!is_open() || (stat = ftell(fp) < 0));
  252.   return stat;
  253. }
  254.  
  255. inline int File::rdstate()
  256.   check_state();  return state; // check_state is necessary in rare but
  257. }                               // possible circumstances
  258.  
  259. inline void* File::operator void*()
  260.   check_state();  return (state & (_bad|_fail))? 0 : this ; 
  261. }
  262.  
  263. inline int File::eof()
  264.   check_state(); return state & _eof; 
  265. }
  266.  
  267. inline int File::fail()
  268.   check_state(); return state & _fail; 
  269. }
  270.  
  271. inline int File::bad()
  272.   check_state(); return state & _bad; 
  273. }
  274.  
  275. inline int File::good()
  276.   check_state(); return rdstate() == _good; 
  277. }
  278.  
  279.  
  280. //#endif
  281.  
  282. #endif
  283.