home *** CD-ROM | disk | FTP | other *** search
/ Qu-ake / Qu-ake.iso / qu_ke / editor / 017 / IO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-13  |  1.9 KB  |  77 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. 11-12-96 Did not do anything here, left for you
  23. -------------------------------------------------------------------------*/
  24.  
  25. #include <stdio.h> 
  26. #include <process.h> 
  27. #include <malloc.h> 
  28. #include <string.h>
  29.  
  30.  
  31. /*--------------------------------------
  32. Write an error message and abort
  33. --------------------------------------*/
  34. void Error (int line, char *message)
  35. {
  36.   printf("Error at Line %d: %s",line,message);
  37.   exit (1);
  38. }
  39.  
  40.  
  41. /*--------------------------------------
  42. Loads a map file into a continuous string
  43. of characters excluding newlines.
  44. --------------------------------------*/
  45. char *Load_File (char *fname, char *dat)
  46. {
  47.   FILE *fd;
  48.   char line [100];
  49.   int sz, total_size;
  50.  
  51.   total_size = 0;
  52.  
  53.   fd = fopen (fname, "r"); 
  54.   if (fd==NULL)
  55.       {
  56.           printf("Can not open file %s\n",fname);          
  57.           return 0;
  58.       }
  59.           
  60.   while (fgets(line, 1000, fd) != NULL)
  61.   {
  62.     sz = strlen (line);
  63.  
  64.     if (total_size == 0)
  65.       dat = (char *)malloc(sizeof(char)*sz);
  66.     else
  67.       dat = (char *)realloc (dat, sizeof(char)*(total_size+sz));
  68.  
  69.     total_size = total_size + sz;
  70.     strncpy (dat+total_size-sz, line, sz);
  71.   }
  72.  
  73.   fclose (fd);
  74.   return dat;
  75. }
  76.  
  77.