home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / file.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  7.6 KB  |  317 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.     de-eunuchs-ified for the Atari ST by JRD.
  6.  
  7. This file is part of GNU CC.
  8.  
  9. GNU CC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the GNU CC General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. GNU CC, but only under the conditions described in the
  18. GNU CC General Public License.   A copy of this license is
  19. supposed to have been given to you along with GNU CC so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies.  
  23. */
  24.  
  25. #ifndef _File_h 
  26. #pragma once
  27. #define _File_h 1
  28.  
  29. #include <builtin.h>
  30. #include <stdio.h>
  31. #include <stddef.h>
  32.  
  33. enum io_mode                    // known unix file IO modes
  34. {
  35.   io_readonly   = 0,            
  36.   io_writeonly  = 1,
  37.   io_readwrite  = 2, 
  38.   io_appendonly = 3,
  39.   io_append     = 4,            // append, plus allow reads
  40. };
  41.  
  42. enum access_mode                // ways to open a file
  43. {
  44.   a_createonly  = 0,            // create, fail if file exists
  45.   a_create      = 1,            // create if doesn't exist, else truncate
  46.   a_useonly     = 2,            // use (no truncate)  fail if doesn't exist
  47.   a_use         = 3,            // use (no truncate), create if doesn't exist
  48. };
  49.  
  50. enum state_value                // File states
  51.   _good         = 0,            // all is well
  52.   _eof          = 1,            // at eof
  53.   _fail         = 2,            // logical or physical IO error
  54.   _bad          = 4             // unopened/corrupted
  55. };
  56.  
  57. // Stuff added for ST version by jrd
  58. //
  59. // values passable to setbuf method
  60. #define UNBUFFERED 0
  61. #define BUFFERED 1
  62. typedef int bufferedness_t;
  63.  
  64. class File
  65. {
  66. protected:
  67.   FILE*         fp;              // struct streamoid file pointer
  68.   char*         nm;              // file name (dynamically allocated)
  69.   char          rw;              //  1 = read; 2 = write; 3 = readwrite
  70.                                  //  bit 2 (4) means read/write into string
  71.   state_value   state;           // _good/_eof/_fail/_bad
  72.   long          stat;            // last read/write/... return value
  73.  
  74.   void          initialize();
  75.   void          reinitialize(const char*);
  76.   char         *readline (int chunk_number, char terminator);
  77.  
  78. public:
  79.                 File();
  80.                 File(const char* filename, io_mode m, access_mode a);
  81.                 File(const char* filename, const char* m);   
  82.                 File(int filedesc, io_mode m);
  83.                 File(FILE* fileptr);
  84.                 File(int sz, char* buf, io_mode m);
  85.  
  86.                 ~File();
  87.  
  88. // binding, rebinding, unbinding to physical files
  89.  
  90.   File&         open(const char* filename, io_mode m, access_mode a);
  91.   File&         open(const char* filename, const char* m);
  92.   File&         open(int  filedesc, io_mode m);
  93.   File&         open(FILE* fileptr);
  94.  
  95.   File&         close();
  96.   File&         remove();
  97.  
  98. // class variable access
  99.  
  100.   int           filedesc();
  101.   const char*   name();
  102.   void          setname(const char* newname);
  103.   int           iocount();
  104.  
  105.   int           rdstate();
  106.   int           eof();
  107.   int           fail();
  108.   int           bad();
  109.   int           good();
  110.  
  111. // other status queries
  112.  
  113.   int           readable();
  114.   int           writable();
  115.   int           is_open();
  116.  
  117.   void*         operator void*();
  118.  
  119. // error handling
  120.  
  121.   void          error();
  122.   void          clear(state_value f = 0); // poorly named
  123.   File&         failif(int cond);
  124.   void          check_state();
  125.  
  126. // character IO
  127.  
  128.   File&         get(char& c);
  129.   File&         put(char  c);
  130.   File&         unget(char c);
  131.   File&         putback(char c); // a synonym for unget
  132.  
  133. // char* IO
  134.  
  135.   File&         put(const char* s);
  136.   File&         get    (char* s, int n, char terminator = '\n');
  137.   File&         getline(char* s, int n, char terminator = '\n');
  138.   File&         gets   (char **s, char terminator = '\n');
  139.  
  140. // binary IO
  141.  
  142.   File&         read(void* x, int sz, int n);
  143.   File&         write(void* x, int sz, int n);
  144.  
  145.  
  146. // file control
  147.  
  148.   File&         seek(long pos, int seek_mode=0); // default seek mode=absolute
  149.   long          tell();
  150.  
  151.   File&         flush();
  152.   File&         setbuf(bufferedness_t buffer_kind); // legal vals: UNBUFFERED, BUFFERED
  153.   File&         setbuf(int size, char* buf);
  154.   File&         raw();
  155. };
  156.  
  157.  
  158. // error handlers
  159.  
  160. extern void  verbose_File_error_handler(char*);
  161. extern void  quiet_File_error_handler(char*);
  162. extern void  fatal_File_error_handler(char*);
  163. extern one_arg_error_handler_t File_error_handler;
  164. extern one_arg_error_handler_t set_File_error_handler(one_arg_error_handler_t);
  165.  
  166. //#ifdef __OPTIMIZE__
  167.  
  168.  
  169.  
  170. inline int File::filedesc()
  171.   return fileno(fp);
  172. }
  173.  
  174. inline const char* File::name()
  175.   return nm; 
  176. }
  177.  
  178. inline int File::iocount()
  179.   return stat; 
  180. }
  181.  
  182. inline int File::readable()
  183.   if (fp != 0) { if (feof(fp)) state |= _eof; if (ferror(fp)) state |= _bad;}
  184.   return (state == _good && (rw & 01));
  185. }
  186.  
  187. inline int File::writable()
  188.   if (fp != 0 && ferror(fp)) state |= _bad;
  189.   return ((state & (_fail|_bad)) == 0 && (rw & 02));
  190. }
  191.  
  192. inline int File::is_open()
  193.   return (fp != 0);
  194. }
  195.  
  196. inline void File::clear(state_value flag = 0)
  197.   state = flag;
  198. }
  199.  
  200. inline File& File::raw()
  201.   return this->File::setbuf(UNBUFFERED); 
  202. }
  203.  
  204. inline void File::check_state() // ensure fp & state agree about eof
  205. {
  206.   if (fp != 0)
  207.   {
  208.     if (feof(fp))
  209.       state |= _eof;
  210.     else
  211.       state &= ~_eof;
  212.     if (ferror(fp))
  213.       state |= _bad;
  214.   }
  215. }
  216.  
  217. inline File& File::failif(int cond)
  218.   if (cond) state |= _fail;  return *this; 
  219. }
  220.  
  221. inline File& File::get(char& c)
  222.   if (readable())
  223.   {
  224.     int ch = getc(fp);
  225.     c = ch;
  226.     failif (ch == EOF);
  227.   }
  228.   return *this;
  229. }
  230.  
  231. inline File& File::put(char  c) 
  232.   return failif (!writable() ||  putc(c, fp) == EOF);
  233. }
  234.  
  235. inline File& File::unget(char c)
  236.   return failif(!is_open() || !(rw & 01) || ungetc(c, fp) == EOF);
  237.  
  238. inline File& File::putback(char c)
  239.   return failif (!is_open() || !(rw & 01) || ungetc(c, fp) == EOF);
  240. }
  241.  
  242. inline File& File::read(void* x, int sz, int n)
  243.   return failif (!readable() || (stat = fread(x, sz, n, fp)) != n);
  244.  
  245. inline File& File::write(void* x, int sz, int n) 
  246.   return failif (!writable() || (stat = fwrite(x, sz, n, fp)) != n);
  247. }
  248.  
  249. inline File& File::flush()
  250.   return failif(!is_open() || fflush(fp) == EOF);
  251. }
  252.  
  253. inline File& File::seek(long pos, int seek_mode = 0)
  254.   return failif (!is_open() || fseek(fp, pos, seek_mode) < 0); 
  255. }
  256.  
  257. inline long File::tell()
  258.   failif (!is_open() || (stat = ftell(fp) < 0));
  259.   return stat;
  260. }
  261.  
  262. inline int File::rdstate()
  263.   check_state();  return state; // check_state is necessary in rare but
  264. }                               // possible circumstances
  265.  
  266. inline void* File::operator void*()
  267.   check_state();  return (state & (_bad|_fail))? 0 : this ; 
  268. }
  269.  
  270. inline int File::eof()
  271.   check_state(); return state & _eof; 
  272. }
  273.  
  274. inline int File::fail()
  275.   check_state(); return state & _fail; 
  276. }
  277.  
  278. inline int File::bad()
  279.   check_state(); return state & _bad; 
  280. }
  281.  
  282. inline int File::good()
  283.   check_state(); return rdstate() == _good; 
  284. }
  285.  
  286.  
  287. //#endif
  288.  
  289. #endif
  290.