home *** CD-ROM | disk | FTP | other *** search
/ Crazy Collection 12 / CC-12_1.iso / update / doompack / data.a00 / CLEANWAD.ZIP / SRC.ZIP / MYLIB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-24  |  8.8 KB  |  350 lines

  1. /* mylib.c -- a collection
  2.    of 'jackets' for some standard functions.  These jackets provide
  3.    additional error checking and report errors gracefully.
  4.    To use this mini-library, #include "mylib\mylib.h" into your source
  5.    code.  If you wish to use my file IO routines, #define MYFILEIO_YES
  6.    BEFORE the #include statement.
  7.    The header #defines all standard functions for which this library
  8.    provides jackets to the names of the corresponding jackets.  Instead of
  9.    the standard FILE structure, my file IO routines use my own structure
  10.    called MILE, which actually includes a pointer to a FILE structure.
  11.    However, the header also redefines the meaning of the identifier FILE,
  12.    so all you need to do is
  13.      #define MYFILEIO_YES
  14.      #include "mylib.h"
  15.  
  16.    NOTE: You will encounter problems if you define MYFILEIO_YES and try to
  17.    use any function for which this library does not provide a jacket, and
  18.    which requires you to define variables of type FILE*.  An example is
  19.    getc().  If you wish to use such functions as well as my functions,
  20.    you have two options:
  21.      (1) Do not define MYFILEIO_YES and call my functions by their real
  22.      names (e. g., filewrite instead of fwrite);
  23.      (2) Define MYFILIO_YES, but #undef FILE, so that you can still use it.
  24.      You will need to explicitly use MILE instead of FILE for those
  25.      files accessed through my functions.
  26.    Similarly, confusion may arise if you try to use stdin and stdout
  27.    with any of my functions.  This may be fixed in the future.  */
  28.  
  29. #define mylib_c
  30.  
  31. #define MYLIB_YES
  32. #include "mylib.h"
  33.  
  34. void prog_error (char *file, long line)
  35. {
  36.   fprintf (stderr, "\7*** PROGRAM ERROR *** in file %s line %ld\n",file,line);
  37.   exit (3);
  38. }
  39.  
  40. void mem_error (char *format_string, ...)
  41. {
  42.   va_list ap;
  43.  
  44.   fprintf (stderr,"out of memory: ");
  45.   va_start (ap, format_string);
  46.   vfprintf (stderr,format_string, ap);
  47.   va_end (ap);
  48.   fprintf (stderr, "\n");
  49.   exit(2);
  50. }
  51.  
  52. void file_error (char *format_string, ...)
  53. {
  54.   va_list ap;
  55.  
  56.   fprintf (stderr,"file IO error: ");
  57.   va_start (ap, format_string);
  58.   vfprintf (stderr,format_string, ap);
  59.   va_end (ap);
  60.   fprintf (stderr, "\n");
  61.   exit(2);
  62. }
  63.  
  64. MILE *fps[MAX_FILES];
  65. int first_time=1;
  66.  
  67. void file_init (void)
  68. { /* initialize the fps table */
  69.   int i;
  70.   first_time=0;
  71.   for (i=0;i<MAX_FILES;i++)
  72.     fps[i]=NULL;
  73. }
  74.  
  75. void file_shutdown (void)
  76. {
  77.   int i;
  78.   for (i=0;i<MAX_FILES;i++)
  79.     if (fps[i]!=NULL) {
  80.       fileclose (fps[i]);
  81.       free (fps[i]);
  82.     }
  83.   first_time=1;
  84. }
  85.  
  86. char *strdp (const char *str)
  87. {
  88.   return (strcpy (memalloc(strlen(str)+1),str));
  89. }
  90.  
  91. char huge *mymemcpy (char huge *dest, char huge *src, unsigned long nbytes)
  92. {
  93.   unsigned long register k;
  94.   for (k=0;k<nbytes;k++)
  95.     *dest++ = *src++;
  96.   return dest;
  97. }
  98.  
  99. int mymemcmp (char huge *str1, char huge *str2, unsigned long nbytes)
  100. {
  101.   unsigned long register k;
  102.   for (k=0;k<nbytes;k++)
  103.     if (*str1++ != *str2++)
  104.       return 1;
  105.   return 0;
  106. }
  107.  
  108. void huge *farmemalloc (unsigned long size)
  109. {
  110.   void huge *p;
  111.   p=farmalloc(size);
  112.   if (p==NULL)
  113.     mem_error ("error farallocating %lu bytes",size);
  114.   return (p);
  115. }
  116.  
  117. void huge *farmemrealloc (void huge *block, unsigned long size)
  118. {
  119.   void huge *p;
  120.  
  121.   if ((p=farrealloc(block,size))==NULL)
  122.     mem_error ("error farreallocating to %lu bytes",size);
  123.   return (p);
  124. }
  125.  
  126. void farmemfree (void huge *ptr)
  127. {
  128.   if (ptr!=NULL)
  129.     farfree (ptr);
  130. }
  131.  
  132. void *memalloc (unsigned long size)
  133. {
  134.   void *p;
  135.   #ifdef size_t_short
  136.     if (size>MAX_MALLOC)
  137.       mem_error ("will not attempt to allocate %lu bytes",size);
  138.   #endif
  139.   if ((p=malloc(size))==NULL)
  140.     mem_error ("error allocating %lu bytes",size);
  141.   return (p);
  142. }
  143.  
  144. void *memrealloc (void *block, unsigned long size)
  145. {
  146.   void *p;
  147.  
  148.   #ifdef size_t_short
  149.     if (size>MAX_MALLOC)
  150.       mem_error ("will not attempt to reallocate to %lu bytes",size);
  151.   #endif
  152.   if ((p=realloc(block,size))==NULL)
  153.     mem_error ("error reallocating to %lu bytes",size);
  154.   return (p);
  155. }
  156.  
  157. void memfree (void *ptr)
  158. {
  159.   if (ptr!=NULL)
  160.     free (ptr);
  161. }
  162.  
  163. MILE *fileopen(const char *name, const char *mode)
  164. {
  165.   int i;
  166.   if (first_time)
  167.     file_init();
  168.   for (i=0;i<MAX_FILES;i++) { /* find first available handle */
  169.     if (fps[i]==NULL) { /* use it */
  170.       fps[i]=memalloc(sizeof(MILE));
  171.       if ((fps[i]->fp=fopen(name,mode))==NULL) {
  172.         memfree (fps[i]);
  173.         fps[i]=NULL;
  174.         return(NULL);
  175.       }
  176.       else {
  177.         fps[i]->name=strdup(name);
  178.         return fps[i];
  179.       }
  180.     }
  181.   };
  182.   /* no more handles */
  183.   file_error ("no available FILE slots to open %s",name);
  184.   return (NULL);
  185. }
  186.  
  187. void fileclose(MILE *handle)
  188. {
  189.   int i;
  190.   int found=0;
  191.  
  192.   if (first_time)
  193.     file_init();
  194.   if (handle==NULL)
  195.     return;
  196.   for (i=0;i<MAX_FILES;i++)
  197.     if (fps[i]==handle) {
  198.       found=1;
  199.       break;
  200.     };
  201.   if (!found)
  202.     PROG_ERROR;
  203.   if (fps[i]->fp==NULL)
  204.     PROG_ERROR;
  205.   if (fclose(fps[i]->fp)!=0)
  206.     file_error ("error closing file %s",fps[i]->name);
  207.   memfree (fps[i]->name);
  208.   memfree (fps[i]);
  209.   fps[i]=NULL;
  210. }
  211.  
  212. unsigned long fileread(void huge *ptr, unsigned long size, unsigned long n, MILE* handle)
  213. {
  214.   long init_pos;
  215.   unsigned long i;
  216.  
  217.   if (first_time)
  218.     file_init();
  219.   if (handle==NULL)
  220.     PROG_ERROR;
  221.   if (handle->fp==NULL)
  222.     PROG_ERROR;
  223.   if ((size==0)||(n==0))
  224.     return(n);
  225.   init_pos=filetell (handle);
  226.   #ifdef size_t_short
  227.     for (i=0;i<((size*n)/FRDWR_BLOCKSIZE);i++) {
  228.       if ((fread(ptr,FRDWR_BLOCKSIZE,1,handle->fp))!=1)
  229.         file_error ("error reading %lu %lu-byte entries at %ld from file %s",n,size,init_pos,handle->name);
  230.       (char huge *)ptr+=FRDWR_BLOCKSIZE;
  231.     }
  232.     if (((size*n)%FRDWR_BLOCKSIZE)!=0)
  233.       if ((fread(ptr,(size*n)%FRDWR_BLOCKSIZE,1,handle->fp))!=1)
  234.         file_error ("error reading %lu %lu-byte entries at %ld from file %s",n,size,init_pos,handle->name);
  235.   #elif
  236.     if ((fread(ptr, size, n, handle->fp)) != n) {
  237.       file_error ("error reading %lu %lu-byte entries at %ld from file %s",n,size,init_pos,handle->name);
  238.       return 0;
  239.     }
  240.   #endif
  241.   return n;
  242. }
  243.  
  244. unsigned long filewrite(void huge *ptr, unsigned long size, unsigned long n, MILE* handle)
  245. {
  246.   long init_pos;
  247.   unsigned long i;
  248.  
  249.   if (first_time)
  250.     file_init();
  251.   if (handle==NULL)
  252.     PROG_ERROR;
  253.   if (handle->fp==NULL)
  254.     PROG_ERROR;
  255.   if ((size==0)||(n==0))
  256.     return(n);
  257.   init_pos=filetell (handle);
  258.   #ifdef size_t_short
  259.     for (i=0;i<(size*n)/FRDWR_BLOCKSIZE;i++) {
  260.       if ((fwrite(ptr,FRDWR_BLOCKSIZE,1,handle->fp))!=1)
  261.         file_error ("error writing %lu %lu-byte entries at %ld to file %s",n,size,init_pos,handle->name);
  262.       (char huge *)ptr+=FRDWR_BLOCKSIZE;
  263.     }
  264.     if (((size*n)%FRDWR_BLOCKSIZE)!=0)
  265.       if ((fwrite(ptr,(size*n)%FRDWR_BLOCKSIZE,1,handle->fp))!=1)
  266.         file_error ("error writing %lu %lu-byte entries at %ld to file %s",n,size,init_pos,handle->name);
  267.   #elif
  268.     if ((fwrite(ptr, size, n, handle->fp)) != n) {
  269.       file_error ("error writing %lu %lu-byte entries at %ld to file %s",n,size,init_pos,handle->name);
  270.       return 0;
  271.     }
  272.   #endif
  273.   return n;
  274. }
  275.  
  276. int fileseek (MILE* handle, long offset, int whence)
  277. {
  278.   if (first_time)
  279.     file_init();
  280.   if (handle==NULL)
  281.     PROG_ERROR;
  282.   if (handle->fp==NULL)
  283.     PROG_ERROR;
  284.   if (whence==SEEK_CUR) { /* remember the current location */
  285.     long oldpos,newpos;
  286.     if (offset==0)
  287.       return (0);
  288.     if ((oldpos=ftell(handle->fp))<0)
  289.       PROG_ERROR; /* hey, if even ftell doesn't work... */
  290.     if (fseek(handle->fp,offset,whence)!=0)
  291.       file_error ("error incrementing current position %ld by %ld in file %s",oldpos,offset,handle->name);
  292.     if ((newpos=ftell(handle->fp))<0)
  293.       PROG_ERROR;
  294.     if (newpos-oldpos!=offset)
  295.       file_error ("fseek failed to set the pointer to %ld in file %s",oldpos+offset,handle->name);
  296.     return (0);
  297.   };
  298.   if (whence==SEEK_SET) {
  299.     if (filetell(handle)==offset)
  300.       return(0);
  301.     if (fseek(handle->fp,offset,whence)!=0)
  302.       file_error ("error fseeking to %ld in file %s",offset,handle->name);
  303.     if (filetell(handle)!=offset)
  304.       file_error ("fseek failed to set the pointer to %ld in file %s",offset,handle->name);
  305.     return(0);
  306.   }
  307.   if (whence==SEEK_END) { /* not as much error checking in this case */
  308.     if (fseek(handle->fp,offset,whence)!=0)
  309.       file_error ("error fseeking %ld from end of file %s",offset,handle->name);
  310.     return(0);
  311.   };
  312.   PROG_ERROR;
  313.   return(-1);
  314. }
  315.  
  316. int fileeof (MILE* handle)
  317. {
  318.   if (first_time)
  319.     file_init();
  320.   if (handle==NULL)
  321.     PROG_ERROR;
  322.   if (handle->fp==NULL)
  323.     PROG_ERROR;
  324.   return (feof(handle->fp));
  325. }
  326.  
  327. long int filetell (MILE* handle)
  328. {
  329.   long int temp;
  330.   if (first_time)
  331.     file_init();
  332.   if (handle==NULL)
  333.     PROG_ERROR;
  334.   if (handle->fp==NULL)
  335.     PROG_ERROR;
  336.   if ((temp=ftell(handle->fp))<0) {
  337.     PROG_ERROR;
  338.     return(-1L);
  339.   }
  340.   else return(temp);
  341. }
  342.  
  343. char *filegets(char *s, int n, MILE *stream)
  344. {
  345.   if ((n==0)||(s==NULL)||(stream==NULL))
  346.     PROG_ERROR;
  347.   return(fgets(s, n, stream->fp));
  348. }
  349.  
  350. #undef mylib_c