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

  1. /*
  2.     mylib.c and mylib.h are two files I wrote to make my life easier.
  3. They provide error checking for functions that are likely to generate errors,
  4. so I don't have to worry about it each time I call fread or malloc.  The way
  5. it is set up is as follows.
  6.     For every error-prone function I use, mylib.c provides a similarly
  7. named "jacket", which calls the standard function, checks its return value,
  8. and reports any errors.  In most cases, this also causes the program to
  9. terminate.  The only exception to this rule is when fopen cannot open a file.
  10. If no errors are detected, the result is simpley returned to the calling
  11. function.
  12.     Mylib.h contains the prototypes of the functions in mylib.c.  For each
  13. standard functions for which mylib.c provides a "jacket", mylib.h also
  14. contains a #define that replaces any call to the standard function by a
  15. call to the corresponding jacket.  Thus, the program files look as if they
  16. were using the standard fuctions, except that each on them #includes wads.h,
  17. which in turn #includes mylib.c.
  18.     Since read/write errors are among the most frequent (especially during
  19. debugging), I wanted them to be reported as gracefully as possible.  One thing
  20. I found particularly useful was to report the name of the file in which the
  21. error occurred.  However, as far as I know there's no wasy way to get that
  22. information from the file handle.  Thus, I decided to comlicate the matters
  23. a bit by using my own FILE structure (which includes a pointer to a regular
  24. FILE structure).  All of my functions actually work with structures of type
  25. MILE (a clumsy abbreviation for MY FILE).  To make the change transparent to
  26. the programs, I re'#defined FILE to mean MILE in mylib.h.
  27.     This #definition has a couple of nasty side effects.  First, my
  28. functions do not work with stdin, stdout, and stderr, as these are always
  29. declared to be of type FILE*.  Second, functions like gets and fprintf (for
  30. which I haven't written a jacket) do not work with file handles declared as
  31. FILE* (which now means MILE*).
  32.     In my case it didn't matter (I just dodn't use those functions),
  33. but I figured it might be useful to include a switch that would let me use
  34. the error-checking memory allocation functions, but not mess with the
  35. standard file IO functions.  Thus, the redefinition of FILE and certain
  36. file IO functions takes place only if MYFILEIO_YES is defined.  This macro
  37. must be defined BEFORE #including "mylib.h", since otherwise it will have
  38. no effect on the directives inside mylib.h.
  39.     Another switch I thought might be useful is MYLIB_YES.  It also must
  40. be defined before the #include.  If it is not, the entire file mylib.h has
  41. no effect, except to #include some standard headers like <stdio.h> and
  42. <ctype.h>.  Thus, if you don't like this business of renaming standard
  43. functions and are willing to give up the error checking it provides, all you
  44. need to do is go tp WADS.H and comment out the line
  45.  
  46. #define MYLIB_YES
  47.  
  48. (it's not necessary to undefine MYFILEIO_YES -- MYLIB_YES takes precedence)
  49. */
  50.  
  51. #ifndef MYLIB_H
  52. #define MYLIB_H
  53.  
  54. #include <stdlib.h>
  55. #include <stdio.h>
  56. #include <string.h>
  57. #include <malloc.h>
  58. #include <stdarg.h>
  59. #include <ctype.h>
  60.  
  61. #ifdef MYLIB_YES
  62.  
  63. #define PROG_ERROR prog_error (__FILE__, __LINE__)
  64.  
  65. #if (sizeof(size_t)==2)
  66.   #define size_t_short
  67.   #define MAX_MALLOC 65536L
  68. #elif
  69.   #define huge
  70.   #define MAX_MALLOC 0x100000000-4L
  71. #endif
  72.  
  73. #define MAX_FILES 10 /* how many files can be open simultaneously */
  74. #define FRDWR_BLOCKSIZE 32768
  75.  
  76. typedef struct {
  77.   FILE *fp;
  78.   char *name;
  79. } MILE;
  80.  
  81. /* the following #define's do not apply inside mylib.c */
  82. #ifndef mylib_c
  83.   #define malloc     memalloc
  84.   #define free       memfree
  85.   #define realloc    memrealloc
  86.   #define farmalloc  farmemalloc
  87.   #define farrealloc farmemrealloc
  88.   #define farfree    farmemfree
  89. #endif
  90.  
  91. #ifdef MYFILEIO_YES /* MYFILEIO_YES must not be defined in mylib.c */
  92.   #define FILE   MILE
  93.   #define fopen   fileopen
  94.   #define fclose  fileclose
  95.   #define fread   fileread
  96.   #define fwrite  filewrite
  97.   #define fseek   fileseek
  98.   #undef  feof
  99.   #define feof    fileeof
  100.   #define ftell   filetell
  101.   #define fgets   filegets
  102. #endif
  103.  
  104. #define strdup strdp
  105.  
  106. char *strdp (const char *s);
  107. char huge *mymemcpy (char huge *dest, char huge *src, unsigned long nbytes);
  108. int mymemcmp (char huge *str1, char huge *str2, unsigned long nbytes);
  109. void huge *farmemalloc(unsigned long size);
  110. void huge *farmemrealloc(void huge *block, unsigned long bytes);
  111. void farmemfree (void huge *block);
  112. void *memalloc (unsigned long size);
  113. void *memrealloc (void *block, unsigned long size);
  114. void memfree (void *block);
  115. MILE *fileopen(const char *name, const char *mode);
  116. void fileclose(MILE* handle);
  117. unsigned long fileread (void huge *ptr, unsigned long size, unsigned long n, MILE* handle);
  118. unsigned long filewrite(void huge *ptr, unsigned long size, unsigned long n, MILE* handle);
  119. int fileseek (MILE* stream, long offset, int whence);
  120. int fileeof (MILE* handle);
  121. long int filetell (MILE* handle);
  122. char *filegets(char *s, int n, MILE *stream);
  123.  
  124. void prog_error (char *file, long line);
  125. void sys_error (char *format_string, ...);
  126.  
  127. #endif /* #ifdef MYLIB_YES */
  128. #endif /* #ifndef MYLIB_H */