home *** CD-ROM | disk | FTP | other *** search
/ Quake 'em / QUAKEEM.BIN / quake / programs / readmap / io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-29  |  1.6 KB  |  67 lines

  1. /*-------------------------------------------------------------------------
  2. File    : io.c
  3. Author  : Cameron Newham
  4. Version : 1.0
  5. Date    : 96/08/29
  6.  
  7. Description
  8. -----------
  9. An Error routine to handle fatal errors when reading the MAP file,
  10. and a routine to load a MAP file into an array of char.
  11.  
  12. Comments
  13. --------
  14. Load_File is probably inefficiently written - but then I am not
  15. a C coder by profession (I'm sure you can tell from the naming
  16. conventions I've used ;).
  17.  
  18. w.r.t Load_File and the memory allocation if (MAXTOKEN > ~90):
  19. I had some difficulty with size allocations with GNU on Linux - I
  20. have no idea why and don't have the time to find out. If you can
  21. tell me, please do so.
  22. -------------------------------------------------------------------------*/
  23.  
  24. #include <stdio.h>
  25.  
  26.  
  27. /*--------------------------------------
  28. Write an error message and abort
  29. --------------------------------------*/
  30. void Error (int line, char *message)
  31. {
  32.   printf("Error at Line %d: %s",line,message);
  33.   exit (1);
  34. }
  35.  
  36.  
  37. /*--------------------------------------
  38. Loads a map file into a continuous string
  39. of characters excluding newlines.
  40. --------------------------------------*/
  41. char *Load_File (char *fname, char *dat)
  42. {
  43.   FILE *fd;
  44.   char line [100];
  45.   int sz, total_size;
  46.  
  47.   total_size = 0;
  48.  
  49.   fd = fopen (fname, "r");
  50.   while (fgets(line, 1000, fd) != NULL)
  51.   {
  52.     sz = strlen (line);
  53.  
  54.     if (total_size == 0)
  55.       dat = (char *)malloc(sizeof(char)*sz);
  56.     else
  57.       dat = (char *)realloc (dat, sizeof(char)*(total_size+sz));
  58.  
  59.     total_size = total_size + sz;
  60.     strncpy (dat+total_size-sz, line, sz);
  61.   }
  62.  
  63.   fclose (fd);
  64.   return dat;
  65. }
  66.  
  67.