home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c017 / 7.ddi / GPPLIB.ZIP / FILE.CC < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-17  |  9.4 KB  |  479 lines

  1. /* 
  2. Copyright (C) 1988 Free Software Foundation
  3.     written by Doug Lea (dl@rocky.oswego.edu)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY.  No author or distributor
  9. accepts responsibility to anyone for the consequences of using it
  10. or for whether it serves any particular purpose or works at all,
  11. unless he says so in writing.  Refer to the GNU CC General Public
  12. License for full details.
  13.  
  14. Everyone is granted permission to copy, modify and redistribute
  15. GNU CC, but only under the conditions described in the
  16. GNU CC General Public License.   A copy of this license is
  17. supposed to have been given to you along with GNU CC so you
  18. can know your rights and responsibilities.  It should be in a
  19. file named COPYING.  Among other things, the copyright notice
  20. and this notice must be preserved on all copies.  
  21. */
  22.  
  23. #include <File.h>
  24. #include <std.h>
  25. #include <stdarg.h>
  26. #include "libconfig.h"
  27.  
  28. #include <sys/file.h>           // needed to determine values of O_RDONLY...
  29.  
  30. #ifndef sun
  31. #include "/usr/include/fcntl.h"
  32. #endif
  33.  
  34.  
  35. // error handlers
  36.  
  37. void verbose_File_error_handler(char* msg)
  38. {
  39.   perror(msg);
  40.   errno = 0;
  41. }
  42.  
  43. void quiet_File_error_handler(char*)
  44. {
  45.   errno = 0;
  46. }
  47.  
  48. void fatal_File_error_handler(char* msg)
  49. {
  50.   perror(msg);
  51.   exit(1);
  52. }
  53.  
  54. one_arg_error_handler_t File_error_handler = verbose_File_error_handler;
  55.  
  56.  
  57. one_arg_error_handler_t set_File_error_handler(one_arg_error_handler_t f)
  58. {
  59.   one_arg_error_handler_t old = File_error_handler;
  60.   File_error_handler = f;
  61.   return old;
  62. }
  63.  
  64.  
  65. /*
  66.  
  67.  Opening files. 
  68.  
  69.  open(filename, io_mode, access_mode) is done via system open 
  70.  command since fopen doesn't handle all of the cases possible 
  71.  with sys open. After a successful open, fdopen is called to 
  72.  attach an _iobuf to the file descriptor.
  73.  
  74.  All this requires a few decoding routines that can translate among our
  75.  enumerated types, system flags, and fopen modes.
  76.  
  77. */
  78.  
  79.  
  80. enum sys_open_cmd_io_mode  // These should be correct for most systems
  81. {                        
  82.   sio_read      = O_RDONLY,
  83.   sio_write     = O_WRONLY,
  84.   sio_readwrite = O_RDWR,
  85.   sio_append    = O_APPEND
  86. };
  87.  
  88. enum sys_open_cmd_access_mode
  89. {
  90.   sa_create     = O_CREAT,
  91.   sa_truncate   = O_TRUNC,
  92.   sa_createonly = O_EXCL
  93. };
  94.  
  95.   
  96. static int open_cmd_arg(io_mode i, access_mode a) // decode modes
  97. {
  98.   int arg;
  99.   switch(i)
  100.   {
  101.   case io_readonly:   arg = sio_read;                   break;
  102.   case io_writeonly:  arg = sio_write;                  break;
  103.   case io_readwrite:  arg = sio_readwrite;              break;
  104.   case io_appendonly: arg = sio_append | sio_write;     break;
  105.   case io_append:     arg = sio_append | sio_readwrite; break;
  106.   default:            return -1;
  107.   };
  108.   switch(a)
  109.   {
  110.   case a_createonly:  return arg | sa_createonly;
  111.   case a_create:      return arg | sa_create | sa_truncate;
  112.   case a_useonly:     return arg;
  113.   case a_use:         return arg | sa_create;
  114.   default:            return -1;
  115.   }
  116. }
  117.  
  118. static char* fopen_cmd_arg(io_mode i)
  119. {
  120.   switch(i)
  121.   {
  122.   case io_readonly:  return "r";
  123.   case io_writeonly: return "w";
  124.   case io_readwrite: return "r+";
  125.   case io_appendonly:return "a";
  126.   case io_append:    return "a+";
  127.   default:           return 0;
  128.   }
  129. }
  130.  
  131.  
  132. void File::initialize() 
  133.   fp = 0; nm = 0; stat = 0; state = _bad; rw = 0;
  134. }
  135.  
  136. // reset class vars after open
  137. // fp->_flag inspection is isolated here
  138.  
  139. void File::reinitialize(const char* filename)
  140. {
  141.   if (filename != 0)     setname(filename);
  142.   else if (fp == stdin)  setname("(stdin)");
  143.   else if (fp == stdout) setname("(stdout)");
  144.   else if (fp == stderr) setname("(stderr)");
  145.   else setname(0);
  146.  
  147.   rw = 0;
  148.   if (fp != 0)
  149.   {
  150.     state = _good;
  151.     if (fp->_flag & (_IOREAD|_IORW))
  152.       rw |= 01;
  153.     if (fp->_flag & (_IOWRT|_IORW|_IOAPPEND))
  154.       rw |= 02;
  155.     check_state();
  156.   }
  157.   else
  158.   {
  159.     state = _fail|_bad;
  160.     error();
  161.   }
  162. }
  163.  
  164.  
  165. File& File::open(const char* filename, io_mode m, access_mode a)
  166. {                                   
  167.   close();
  168.   int open_arg = open_cmd_arg(m, a);
  169.   if (open_arg != -1)
  170.   {
  171.     int fd = ::open(filename, open_arg, 0666);
  172.     if (fd >= 0)
  173.       fp = fdopen(fd, fopen_cmd_arg(m));
  174.   }
  175.   reinitialize(filename);
  176.   return *this;
  177. }
  178.  
  179. File& File::open(const char* filename, const char* m)
  180. {                                   
  181.   close();
  182.   fp = fopen(filename, m);
  183.   reinitialize(filename);
  184.   return *this;
  185. }
  186.  
  187. File& File::open(FILE* fileptr)
  188. {
  189.   close();
  190.   fp = fileptr;
  191.   reinitialize(0);
  192.   return *this;
  193. }
  194.  
  195. File& File::open(int filedesc, io_mode m)
  196. {
  197.   close();
  198.   fp = fdopen(filedesc, fopen_cmd_arg(m));
  199.   reinitialize(0);
  200.   return *this;
  201. }
  202.  
  203. File& File::close()
  204. {
  205.   if (fp != 0)
  206.     fclose(fp);
  207.   fp = 0;
  208.   state = _bad;
  209.   return *this;
  210. }
  211.  
  212. File& File::remove()
  213. {
  214.   close();
  215.   return failif (nm == 0 || unlink(nm) != 0);
  216. }
  217.  
  218.  
  219. File::File()
  220.   initialize(); 
  221. }
  222.  
  223. File::File(const char* filename, io_mode m, access_mode a)   
  224.   initialize(); 
  225.   open(filename, m, a); 
  226. }
  227.  
  228. File::File(const char* filename, const char* m)   
  229.   initialize(); 
  230.   open(filename, m); 
  231. }
  232.  
  233. File::File(int filedesc, io_mode m)
  234.   initialize(); 
  235.   open(filedesc, m); 
  236. }
  237.  
  238. File::File(FILE* fileptr)
  239.   initialize(); 
  240.   open(fileptr); 
  241. }
  242.  
  243.  
  244. File::~File()
  245. {
  246.   delete(nm);
  247.   if (fp == stdin || fp == stdout || fp == stderr)
  248.     flush();
  249.   else if (fp != 0)
  250.     File::close();
  251. }
  252.  
  253. void File::setname(const char* newname)
  254. {
  255.   if (nm != 0)
  256.     delete(nm);
  257.   if (newname != 0)
  258.   {
  259.     nm = new char[strlen(newname) + 1];
  260.     strcpy(nm, newname);
  261.   }
  262.   else
  263.     nm = 0;
  264. }
  265.  
  266.  
  267. File& File::setbuf(int buffer_kind)
  268. {                  
  269.   if (!is_open())
  270.   {
  271.     error();
  272.     return *this;
  273.   }
  274.   switch(buffer_kind)
  275.   {
  276.   case _IOFBF:       
  277. #ifdef HAVE_SETVBUF
  278.     setvbuf(fp, 0, _IOFBF, 0);
  279. #endif
  280.     break;           
  281.   case _IONBF:       
  282.     ::setbuf(fp, 0); 
  283.     break;
  284.   case _IOLBF:
  285. #ifdef HAVE_SETLINEBUF
  286.     setlinebuf(fp);
  287. #else
  288. #ifdef HAVE_SETVBUF
  289.     setvbuf(fp, 0, _IOLBF, 0);
  290. #endif
  291. #endif    
  292.     break;
  293.   default:
  294.     break;
  295.   }
  296.   return *this;
  297. }
  298.  
  299. File& File::setbuf(int size, char* buf)
  300. {
  301.   if (!is_open())
  302.   {
  303.     error();
  304.     return *this;
  305.   }
  306. #ifdef HAVE_SETVBUF
  307.   setvbuf(fp, buf, _IOFBF, size);
  308. #else
  309.   setbuffer(fp, buf, size);
  310. #endif
  311.   return *this;
  312. }
  313.  
  314. void File::error()
  315. {
  316.   check_state();
  317.   state |= _fail;
  318.   if (errno != 0)
  319.   {
  320.     char error_string[400];
  321.     strcpy(error_string, "\nerror in File ");
  322.     if (nm != 0)
  323.       strcat(error_string, nm);
  324.     (*File_error_handler)(error_string);
  325.   }
  326. }
  327.  
  328. //-------------------------------------------------------------------
  329.  
  330. // All of the following could be inlined if desired.
  331. // However, all are a bit too long for routine inlining.
  332.  
  333. #define COULD_BE_INLINE
  334.  
  335. COULD_BE_INLINE void File::check_state() // ensure fp & state agree about eof
  336. {
  337.   if (fp != 0)
  338.   {
  339.     if (feof(fp))
  340.       state |= _eof;
  341.     else
  342.       state &= ~_eof;
  343.     if (ferror(fp))
  344.       state |= _fail;
  345.   }
  346. }
  347.  
  348. COULD_BE_INLINE File& File::failif(int cond)
  349.   if (cond) error(); else { errno = 0; state = _good; check_state(); }
  350.   return *this; 
  351. }
  352.  
  353. COULD_BE_INLINE File& File::get(char& c)
  354.   if (readable())
  355.   {
  356.     int ch = getc(fp);
  357.     c = ch;
  358.     failif (ch == EOF);
  359.   }
  360.   return *this;
  361. }
  362.  
  363. COULD_BE_INLINE File& File::put(char  c) 
  364.   return failif (!writable() ||  putc(c, fp) == EOF);
  365. }
  366.  
  367. COULD_BE_INLINE File& File::unget(char c)
  368.   return failif(!is_open() || !(rw & 01) || ungetc(c, fp) == EOF);
  369.  
  370. COULD_BE_INLINE File& File::putback(char c)
  371.   return failif (!is_open() || !(rw & 01) || ungetc(c, fp) == EOF);
  372. }
  373.  
  374. COULD_BE_INLINE File& File::read(void* x, int sz, int n)
  375.   return failif (!readable() || (stat = fread(x, sz, n, fp)) != n);
  376.  
  377. COULD_BE_INLINE File& File::write(void* x, int sz, int n) 
  378.   return failif (!writable() || (stat = fwrite(x, sz, n, fp)) != n);
  379. }
  380.  
  381. COULD_BE_INLINE  File& File::put(const char* s)
  382.   return failif(!writable() || fputs(s, fp) == EOF);
  383. }
  384.  
  385. COULD_BE_INLINE File& File::flush()
  386.   return failif(!is_open() || fflush(fp) == EOF);
  387. }
  388.  
  389. COULD_BE_INLINE File& File::seek(long pos, int seek_mode = 0)
  390.   return failif (!is_open() || fseek(fp, pos, seek_mode) < 0); 
  391. }
  392.  
  393. COULD_BE_INLINE long File::tell()
  394.   failif (!is_open() || (stat = ftell(fp) < 0));
  395.   return stat;
  396. }
  397.  
  398. //------------------------------------------------------------------
  399.  
  400. File& File::get(char* s, int n, char terminator = '\n')
  401. {
  402.   if (!readable())
  403.   {
  404.     error();
  405.     return *this;
  406.   }
  407.  
  408.   char ch;
  409.   stat = n;
  410.  
  411.   if (n > 0 && (get(ch)))
  412.   {
  413.     if (ch == terminator)
  414.       unget(ch);
  415.     else
  416.     {
  417.       *s++ = ch; --n;
  418.       while (n > 0 && (get(ch)))
  419.       {
  420.         if (ch == terminator)
  421.         {
  422.           unget(ch);
  423.           break;
  424.         }
  425.         else
  426.         {
  427.           *s++ = ch; --n;
  428.         }
  429.       }
  430.     }
  431.   }
  432.  
  433.   *s = 0;
  434.   return failif((stat != 0) && ((stat -= n) == 0));
  435. }
  436.  
  437. File& File::getline(char* s, int n, char terminator = '\n')
  438. {
  439.   if (!readable())
  440.   {
  441.     error();
  442.     return *this;
  443.   }
  444.  
  445.   char ch;
  446.   stat = n;
  447.  
  448.   while (n > 0 && (get(ch)))
  449.   {
  450.     --n;
  451.     if ((*s++ = ch) == terminator)
  452.       break;
  453.   }
  454.  
  455.   *s = 0;
  456.   return failif((stat != 0) && ((stat -= n) == 0));
  457. }
  458.  
  459.  
  460.