home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / file.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  10.2 KB  |  507 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. #ifdef atarist
  29. extern "C" {
  30. #include <sys-file.h>           // needed to determine values of O_RDONLY...
  31. }
  32. #else
  33. extern "C" {
  34. #include <sys/file.h>           // needed to determine values of O_RDONLY...
  35.  
  36. #ifndef sun
  37. #include "/usr/include/fcntl.h"
  38. #endif
  39.  
  40. }
  41. #endif
  42.  
  43. // error handlers
  44.  
  45. void verbose_File_error_handler(char* msg)
  46. {
  47.   perror(msg);
  48.   errno = 0;
  49. }
  50.  
  51. void quiet_File_error_handler(char*)
  52. {
  53.   errno = 0;
  54. }
  55.  
  56. void fatal_File_error_handler(char* msg)
  57. {
  58.   perror(msg);
  59.   exit(1);
  60. }
  61.  
  62. one_arg_error_handler_t File_error_handler = verbose_File_error_handler;
  63.  
  64.  
  65. one_arg_error_handler_t set_File_error_handler(one_arg_error_handler_t f)
  66. {
  67.   one_arg_error_handler_t old = File_error_handler;
  68.   File_error_handler = f;
  69.   return old;
  70. }
  71.  
  72.  
  73. /*
  74.  
  75.  Opening files. 
  76.  
  77.  open(filename, io_mode, access_mode) is done via system open 
  78.  command since fopen doesn't handle all of the cases possible 
  79.  with sys open. After a successful open, fdopen is called to 
  80.  attach an _iobuf to the file descriptor.
  81.  
  82.  All this requires a few decoding routines that can translate among our
  83.  enumerated types, system flags, and fopen modes.
  84.  
  85. */
  86.  
  87.  
  88. enum sys_open_cmd_io_mode  // These should be correct for most systems
  89. {                        
  90.   sio_read      = O_RDONLY,
  91.   sio_write     = O_WRONLY,
  92.   sio_readwrite = O_RDWR,
  93.   sio_append    = O_APPEND
  94. };
  95.  
  96. enum sys_open_cmd_access_mode
  97. {
  98.   sa_create     = O_CREAT,
  99.   sa_truncate   = O_TRUNC,
  100.   sa_createonly = O_EXCL
  101. };
  102.  
  103.   
  104. static int open_cmd_arg(io_mode i, access_mode a) // decode modes
  105. {
  106.   int arg;
  107.   switch(i)
  108.   {
  109.   case io_readonly:   arg = sio_read;                   break;
  110.   case io_writeonly:  arg = sio_write;                  break;
  111.   case io_readwrite:  arg = sio_readwrite;              break;
  112.   case io_appendonly: arg = sio_append | sio_write;     break;
  113.   case io_append:     arg = sio_append | sio_readwrite; break;
  114.   default:            return -1;
  115.   };
  116.   switch(a)
  117.   {
  118.   case a_createonly:  return arg | sa_createonly;
  119.   case a_create:      return arg | sa_create | sa_truncate;
  120.   case a_useonly:     return arg;
  121.   case a_use:         return arg | sa_create;
  122.   default:            return -1;
  123.   }
  124. }
  125.  
  126. static char* fopen_cmd_arg(io_mode i)
  127. {
  128.   switch(i)
  129.   {
  130.   case io_readonly:  return "r";
  131.   case io_writeonly: return "w";
  132.   case io_readwrite: return "r+";
  133.   case io_appendonly:return "a";
  134.   case io_append:    return "a+";
  135.   default:           return 0;
  136.   }
  137. }
  138.  
  139.  
  140. void File::initialize() 
  141.   fp = 0; nm = 0; stat = 0; state = _bad; rw = 0;
  142. }
  143.  
  144. // reset class vars after open
  145. // fp->_flag inspection is isolated here
  146.  
  147. void File::reinitialize(const char* filename)
  148. {
  149.   if (filename != 0)     setname(filename);
  150.   else if (fp == stdin)  setname("(stdin)");
  151.   else if (fp == stdout) setname("(stdout)");
  152.   else if (fp == stderr) setname("(stderr)");
  153.   else if (rw & 4)       setname("(string)");
  154.   else setname(0);
  155.  
  156.   if (fp != 0)
  157.   {
  158.     state = _good;
  159.     if (fp->_flag & (_IOREAD|_IORW))
  160.       rw |= 01;
  161.     if (fp->_flag & (_IOWRT|_IORW|_IOAPPEND))
  162.       rw |= 02;
  163.     check_state();
  164.   }
  165.   else
  166.   {
  167.     state = _fail|_bad;
  168.     error();
  169.   }
  170. }
  171.  
  172.  
  173. File& File::open(const char* filename, io_mode m, access_mode a)
  174. {                                   
  175.   close();
  176.   int open_arg = open_cmd_arg(m, a);
  177.   if (open_arg != -1)
  178.   {
  179.     int fd = ::open(filename, open_arg, 0666);
  180.     if (fd >= 0)
  181.       fp = fdopen(fd, fopen_cmd_arg(m));
  182.   }
  183.   reinitialize(filename);
  184.   return *this;
  185. }
  186.  
  187. File& File::open(const char* filename, const char* m)
  188. {                                   
  189.   close();
  190.   fp = fopen(filename, m);
  191.   reinitialize(filename);
  192.   return *this;
  193. }
  194.  
  195. File& File::open(FILE* fileptr)
  196. {
  197.   close();
  198.   fp = fileptr;
  199.   reinitialize(0);
  200.   return *this;
  201. }
  202.  
  203. File& File::open(int filedesc, io_mode m)
  204. {
  205.   close();
  206.   fp = fdopen(filedesc, fopen_cmd_arg(m));
  207.   reinitialize(0);
  208.   return *this;
  209. }
  210.  
  211. File& File::close()
  212. {
  213.   if (fp != 0)
  214.   {
  215.     if (rw & 4)                 // we own the iobuf, kill it
  216.       delete fp;
  217.     else
  218.       fclose(fp);
  219.   }
  220.   fp = 0;
  221.   rw = 0;
  222.   state |= _bad;
  223.   return *this;
  224. }
  225.  
  226. File& File::remove()
  227. {
  228.   close();
  229.   return failif (nm == 0 || unlink(nm) != 0);
  230. }
  231.  
  232.  
  233. File::File()
  234.   initialize(); 
  235. }
  236.  
  237. File::File(const char* filename, io_mode m, access_mode a)   
  238.   initialize(); 
  239.   open(filename, m, a); 
  240. }
  241.  
  242. File::File(const char* filename, const char* m)   
  243.   initialize(); 
  244.   open(filename, m); 
  245. }
  246.  
  247. File::File(int filedesc, io_mode m)
  248.   initialize(); 
  249.   open(filedesc, m); 
  250. }
  251.  
  252. File::File(FILE* fileptr)
  253.   initialize(); 
  254.   open(fileptr); 
  255. }
  256.  
  257. File::File(int sz, char* buf, io_mode m)
  258. {
  259.   if (m != io_readonly && m != io_writeonly)
  260.     (*File_error_handler) ("invalid io_mode for string IO");
  261.   initialize();
  262.   rw = 4;
  263.   fp = new _iobuf;
  264.   fp->_file = 255;          // any illegal value
  265.   fp->_ptr = fp->_base = buf;
  266. #ifdef HAVE_BUFSIZ
  267.   fp->_bufsiz = sz;
  268. #endif
  269.   if (m == io_readonly)
  270.   {
  271.     int len = 0;
  272.     while (len < sz && buf[len] != 0) ++len;
  273.     if (len == sz)
  274.       buf[sz - 1] = 0;            // force null-termination!
  275.     fp->_cnt = len;
  276.     fp->_flag = _IOREAD | _IOSTRG | _IOMYBUF;
  277.   }
  278.   else
  279.   {
  280.     bzero(buf, sz);             // so any result will be null-terminated
  281.     fp->_cnt = sz - 1;          // leave at least one null at end
  282.     fp->_flag = _IOWRT | _IOSTRG | _IOMYBUF;
  283.   }
  284.   reinitialize(0);
  285. }
  286.  
  287. File::~File()
  288. {
  289.   delete(nm);
  290.   if (fp == stdin || fp == stdout || fp == stderr)
  291.     flush();
  292.   else if (fp != 0)
  293.     close();
  294. }
  295.  
  296. void File::setname(const char* newname)
  297. {
  298.   if (nm != 0)
  299.     delete(nm);
  300.   if (newname != 0)
  301.   {
  302.     nm = new char[strlen(newname) + 1];
  303.     strcpy(nm, newname);
  304.   }
  305.   else
  306.     nm = 0;
  307. }
  308.  
  309.  
  310. File& File::setbuf(int buffer_kind)
  311. {                  
  312.   if (!is_open())
  313.   {
  314.     state |= _fail;
  315.     return *this;
  316.   }
  317.   switch(buffer_kind)
  318.   {
  319.   case _IOFBF:       
  320. #ifdef HAVE_SETVBUF
  321.     setvbuf(fp, 0, _IOFBF, 0);
  322. #endif
  323.     break;           
  324.   case _IONBF:       
  325.     ::setbuf(fp, 0); 
  326.     break;
  327.   case _IOLBF:
  328. #ifdef HAVE_SETLINEBUF
  329.     setlinebuf(fp);
  330. #else
  331. #ifdef HAVE_SETVBUF
  332.     setvbuf(fp, 0, _IOLBF, 0);
  333. #endif
  334. #endif    
  335.     break;
  336.   default:
  337.     break;
  338.   }
  339.   return *this;
  340. }
  341.  
  342. File& File::setbuf(int size, char* buf)
  343. {
  344.   if (!is_open())
  345.   {
  346.     state |= _fail;
  347.     return *this;
  348.   }
  349. #ifdef HAVE_SETVBUF
  350.   setvbuf(fp, buf, _IOFBF, size);
  351. #else
  352.   setbuffer(fp, buf, size);
  353. #endif
  354.   return *this;
  355. }
  356.  
  357. void File::error()
  358. {
  359.   check_state();
  360.   state |= _fail;
  361.   if (errno != 0)
  362.   {
  363.     char error_string[400];
  364.     strcpy(error_string, "\nerror in File ");
  365.     if (nm != 0)
  366.       strcat(error_string, nm);
  367.     (*File_error_handler)(error_string);
  368.   }
  369. }
  370.  
  371.  
  372. //------------------------------------------------------------------
  373.  
  374. File& File::put(const char* s)
  375.   return failif(!writable() || fputs(s, fp) == EOF);
  376. }
  377.  
  378. File& File::get(char* s, int n, char terminator = '\n')
  379. {
  380.   if (!readable())
  381.   {
  382.     state |= _fail;
  383.     return *this;
  384.   }
  385.  
  386.   char ch;
  387.   stat = n;
  388.  
  389.   if (n > 0 && (get(ch)))
  390.   {
  391.     if (ch == terminator) {
  392.       unget(ch);
  393.       stat= 0;    // This is not an error condition !
  394.     }
  395.     else
  396.     {
  397.       *s++ = ch; --n;
  398.       while (n > 0 && (get(ch)))
  399.       {
  400.         if (ch == terminator)
  401.         {
  402.           unget(ch);
  403.           break;
  404.         }
  405.         else
  406.         {
  407.           *s++ = ch; --n;
  408.         }
  409.       }
  410.     }
  411.   }
  412.  
  413.   *s = 0;
  414.   return failif((stat != 0) && ((stat -= n) == 0));
  415. }
  416.  
  417. File& File::getline(char* s, int n, char terminator = '\n')
  418. {
  419.   if (!readable())
  420.   {
  421.     state |= _fail;
  422.     return *this;
  423.   }
  424.  
  425.   char ch;
  426.   stat = n;
  427.  
  428.   while (n > 0 && (get(ch)))
  429.   {
  430.     --n;
  431.     if ((*s++ = ch) == terminator)
  432.       break;
  433.   }
  434.  
  435.   *s = 0;
  436.   return failif((stat != 0) && ((stat -= n) == 0));
  437. }
  438.  
  439. // from Doug Schmidt
  440.  
  441. // This should probably be a page size....
  442. #define CHUNK_SIZE 512
  443.  
  444. /* Reads an arbitrarily long input line terminated by a user-specified
  445.    TERMINATOR.  Super-nifty trick using recursion avoids unnecessary calls
  446.    to NEW! */
  447.  
  448. char *File::readline (int chunk_number, char terminator) 
  449. {
  450.   char buf[CHUNK_SIZE];
  451.   register char *bufptr = buf;
  452.   register char *ptr;
  453.   char ch;
  454.   int continu;
  455.  
  456.   while ((continu = get (ch)) && ch != terminator) /* fill the current buffer */
  457.     {
  458.       *bufptr++ = ch;
  459.       if (bufptr - buf >= CHUNK_SIZE) /* prepend remainder to ptr buffer */
  460.         {
  461.           if (ptr = readline (chunk_number + 1, terminator))
  462.  
  463.             for (; bufptr != buf; *--ptr = *--bufptr);
  464.  
  465.           return ptr;
  466.         }
  467.     }
  468.   if (!continu && bufptr == buf)
  469.     return NULL;
  470.  
  471.   int size = (chunk_number * CHUNK_SIZE + bufptr - buf) + 1;
  472.  
  473.   if (ptr = new char[size])
  474.     {
  475.  
  476.       for (*(ptr += (size - 1)) = '\0'; bufptr != buf; *--ptr = *--bufptr)
  477.         ;
  478.  
  479.       return ptr;
  480.     } 
  481.   else 
  482.     return NULL;
  483. }
  484.  
  485. /* Reads an arbitrarily long input line terminated by TERMINATOR.
  486.    This routine allocates its own memory, so the user should
  487.    only supply the address of a (char *). */
  488.  
  489. File& File::gets(char **s, char terminator)
  490. {
  491.   if (!readable())
  492.   {
  493.     state |= _fail;
  494.     return *this;
  495.   }
  496.  
  497.   return failif(!(*s = readline (0, terminator)));
  498. }
  499.   
  500.