#ifdef __CCFRONT__ #include <14:pragma.h> #endif #include "common.h" #include #include #include /* * char **loadarray (unsigned long n, char *filename, size_t maxlinelen); * * Pre: is the name of a file containing lines of text, * the number of lines. The file must be closed. * is the length of the longest line in * * Post: Returns a pointer to an array of pointers to malloc'd strings, * where the strings are successive lines from the file . * On return will be closed. If loadarray() fails for * any reason, it will return NULL. * * Warning: The use of realloc() with a NULL pointer for the initial * allocation may not be portable. If this is not valid for your * current libraries, then #define BROKEN_REALLOC. * * Uses Globals: * v_flag */ char **loadarray (unsigned long n, char *filename, size_t maxlinelen) { char **result; unsigned long i,j; FILE *in_fp; static char *inbuf=NULL; static size_t previous_size = 0; char *p; #ifndef BROKEN_REALLOC /* realloc() is ANSI-compliant with NULL first arg */ /* reallocate the input buffer if necessary */ if (maxlinelen > previous_size) { if ((p = realloc(inbuf,maxlinelen+1)) == NULL) { if (v_flag) perror("loadarray: couldn't (re)allocate input buffer"); return NULL; } previous_size = maxlinelen; inbuf = p; } #else /* BROKEN_REALLOC */ /* reallocate the input buffer if necessary */ if (maxlinelen > previous_size) { if (previous_size == 0) { if ((p = malloc(maxlinelen+1)) == NULL) { if (v_flag) perror("loadarray: couldn't allocate input buffer"); return NULL; } } else { if ((p = realloc(inbuf,maxlinelen+1)) == NULL) { if (v_flag) perror("loadarray: couldn't reallocate input buffer"); return NULL; } } previous_size = maxlinelen; inbuf = p; } #endif /* BROKEN_REALLOC */ /* allocate the array */ if ((result = malloc (n * sizeof(char *)))==NULL) { if (v_flag) perror("loadarray: couldn't allocate base array"); return NULL; } /* set up the input stream */ in_fp = fopen(filename,"r"); if (in_fp == NULL) { /* open failed */ free(result); if (v_flag) perror("loadarray: couldn't open input file"); return NULL; } /* allocate and copy elements */ for (i=0; i