home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / WIN_NT / SED.ZIP / UTILS.C < prev   
C/C++ Source or Header  |  1993-05-17  |  5KB  |  317 lines

  1. /*  Functions from hack's utils library.
  2.     Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* These routines were written as part of a library (by hack), but since most
  19.    people don't have the library, here they are.  */
  20.  
  21. #ifdef __STDC__
  22. #define VOID void
  23. #else
  24. #define VOID char
  25. #endif
  26.  
  27. #include <stdio.h>
  28. #if HAVE_STRING_H || defined(STDC_HEADERS)
  29. #include <string.h>
  30. #else
  31. #include <strings.h>
  32. #endif
  33. #if defined(STDC_HEADERS)
  34. #include <stdlib.h>
  35. #else
  36. VOID *malloc();
  37. VOID *realloc();
  38. #endif
  39.  
  40. VOID *ck_malloc();
  41.  
  42. char *myname;
  43.  
  44. #ifdef __STDC__
  45. #include <stdarg.h>
  46.  
  47. /* Print an error message and exit */
  48. void
  49. panic(char *str, ...)
  50. {
  51.     va_list iggy;
  52.  
  53.     fprintf(stderr,"%s: ",myname);
  54.     va_start(iggy,str);
  55. #ifdef HAVE_VPRINTF
  56.     vfprintf(stderr,str,iggy);
  57. #else
  58. #ifdef HAVE_DOPRNT
  59.     _doprnt(str,&iggy,stderr);
  60. #endif
  61. #endif
  62.     va_end(iggy);
  63.     putc('\n',stderr);
  64.     exit(4);
  65. }
  66.  
  67. #else
  68. #include <varargs.h>
  69.  
  70. void
  71. panic(str,va_alist)
  72. char *str;
  73. va_dcl
  74. {
  75.     va_list iggy;
  76.  
  77.     fprintf(stderr,"%s: ",myname);
  78.     va_start(iggy);
  79. #ifdef HAVE_VPRINTF
  80.     vfprintf(stderr,str,iggy);
  81. #else
  82. #ifdef HAVE_DOPRNT
  83.     _doprnt(str,&iggy,stderr);
  84. #endif
  85. #endif
  86.     va_end(iggy);
  87.     putc('\n',stderr);
  88.     exit(4);
  89. }
  90.  
  91. #endif
  92.  
  93. /* Store information about files opened with ck_fopen
  94.    so that error messages from ck_fread, etc can print the
  95.    name of the file that had the error */
  96. #define N_FILE 32
  97.  
  98. struct id {
  99.     FILE *fp;
  100.     char *name;
  101. };
  102.  
  103. static struct id __id_s[N_FILE];
  104.  
  105. /* Internal routine to get a filename from __id_s */
  106. char *
  107. __fp_name(fp)
  108. FILE *fp;
  109. {
  110.     int n;
  111.  
  112.     for(n=0;n<N_FILE;n++) {
  113.         if(__id_s[n].fp==fp)
  114.             return __id_s[n].name;
  115.     }
  116.     return "{Unknown file pointer}";
  117. }
  118.  
  119. /* Panic on failing fopen */
  120. FILE *
  121. ck_fopen(name,mode)
  122. char *name;
  123. char *mode;
  124. {
  125.     FILE    *ret;
  126.     int    n;
  127.  
  128.     ret=fopen(name,mode);
  129.     if(ret==(FILE *)0)
  130.         panic("Couldn't open file %s",name);
  131.     for(n=0;n<N_FILE;n++) {
  132.         if(ret==__id_s[n].fp) {
  133.             free((VOID *)__id_s[n].name);
  134.             __id_s[n].name=(char *)ck_malloc(strlen(name)+1);
  135.             strcpy(__id_s[n].name,name);
  136.             break;
  137.         }
  138.     }
  139.     if(n==N_FILE) {
  140.         for(n=0;n<N_FILE;n++)
  141.             if(__id_s[n].fp==(FILE *)0)
  142.                 break;
  143.         if(n==N_FILE)
  144.             panic("Internal error: too many files open");
  145.         __id_s[n].fp=ret;
  146.         __id_s[n].name=(char *)ck_malloc(strlen(name)+1);
  147.         strcpy(__id_s[n].name,name);
  148.     }
  149.     return ret;
  150. }
  151.  
  152. /* Panic on failing fwrite */
  153. void
  154. ck_fwrite(ptr,size,nmemb,stream)
  155. char *ptr;
  156. int size,nmemb;
  157. FILE *stream;
  158. {
  159.     if(fwrite(ptr,size,nmemb,stream)!=nmemb)
  160.         panic("couldn't write %d items to %s",nmemb,__fp_name(stream));
  161. }
  162.  
  163. /* Panic on failing fclose */
  164. void
  165. ck_fclose(stream)
  166. FILE *stream;
  167. {
  168.     if(fclose(stream)==EOF)
  169.         panic("Couldn't close %s",__fp_name(stream));
  170. }
  171.  
  172. /* Panic on failing malloc */
  173. VOID *
  174. ck_malloc(size)
  175. int size;
  176. {
  177.     VOID *ret;
  178.  
  179.     if(!size)
  180.         size++;
  181.     ret=malloc(size);
  182.     if(ret==(VOID *)0)
  183.         panic("Couldn't allocate memory");
  184.     return ret;
  185. }
  186.  
  187. /* Panic on failing malloc */
  188. VOID *
  189. xmalloc(size)
  190. int size;
  191. {
  192.   return ck_malloc (size);
  193. }
  194.  
  195. /* Panic on failing realloc */
  196. VOID *
  197. ck_realloc(ptr,size)
  198. VOID *ptr;
  199. int size;
  200. {
  201.     VOID *ret;
  202.  
  203.     ret=realloc(ptr,size);
  204.     if(ret==(VOID *)0)
  205.         panic("Couldn't re-allocate memory");
  206.     return ret;
  207. }
  208.  
  209. /* Return a malloc()'d copy of a string */
  210. char *
  211. ck_strdup(str)
  212. char *str;
  213. {
  214.     char *ret;
  215.  
  216.     ret=(char *)ck_malloc(strlen(str)+2);
  217.     strcpy(ret,str);
  218.     return ret;
  219. }
  220.  
  221.  
  222. /* Implement a variable sized buffer of 'stuff'.  We don't know what it is,
  223.    nor do we care, as long as it doesn't mind being aligned by malloc. */
  224.  
  225. struct buffer {
  226.     int    allocated;
  227.     int    length;
  228.     char    *b;
  229. };
  230.  
  231. #define MIN_ALLOCATE 50
  232.  
  233. VOID *
  234. init_buffer()
  235. {
  236.     struct buffer *b;
  237.  
  238.     b=(struct buffer *)ck_malloc(sizeof(struct buffer));
  239.     b->allocated=MIN_ALLOCATE;
  240.     b->b=(char *)ck_malloc(MIN_ALLOCATE);
  241.     b->length=0;
  242.     return (VOID *)b;
  243. }
  244.  
  245. void
  246. flush_buffer(bb)
  247. VOID *bb;
  248. {
  249.     struct buffer *b;
  250.  
  251.     b=(struct buffer *)bb;
  252.     free(b->b);
  253.     b->b=0;
  254.     b->allocated=0;
  255.     b->length=0;
  256.     free(b);
  257. }
  258.  
  259. int
  260. size_buffer(b)
  261. VOID *b;
  262. {
  263.     struct buffer *bb;
  264.  
  265.     bb=(struct buffer *)b;
  266.     return bb->length;
  267. }
  268.  
  269. void
  270. add_buffer(bb,p,n)
  271. VOID *bb;
  272. char *p;
  273. int n;
  274. {
  275.     struct buffer *b;
  276.     int x;
  277.     char * cp;
  278.  
  279.     b=(struct buffer *)bb;
  280.     if(b->length+n>b->allocated) {
  281.         b->allocated*=2;
  282.         b->b=(char *)ck_realloc(b->b,b->allocated);
  283.     }
  284.     
  285.     x = n;
  286.     cp = b->b + b->length;
  287.     while (x--)
  288.       *cp++ = *p++;
  289.     b->length+=n;
  290. }
  291.  
  292. void
  293. add1_buffer(bb,ch)
  294. VOID *bb;
  295. int ch;
  296. {
  297.     struct buffer *b;
  298.  
  299.     b=(struct buffer *)bb;
  300.     if(b->length+1>b->allocated) {
  301.         b->allocated*=2;
  302.         b->b=(char *)ck_realloc(b->b,b->allocated);
  303.     }
  304.     b->b[b->length]=ch;
  305.     b->length++;
  306. }
  307.  
  308. char *
  309. get_buffer(bb)
  310. VOID *bb;
  311. {
  312.     struct buffer *b;
  313.  
  314.     b=(struct buffer *)bb;
  315.     return b->b;
  316. }
  317.