home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 2 / DATAFILE_PDCD2.iso / utilities2 / _stdfinder / !STDFinder / c / File_utils < prev    next >
Encoding:
Text File  |  1993-03-23  |  2.7 KB  |  113 lines

  1. /* Title:   file_utils.c
  2.  * Purpose: Generalised file I/O routines + specifics for !STDFinder
  3.  * Author:  Julyan Bristow
  4.  * Date:    23 September 1992
  5.  *
  6.  * Version History:  0.01 First version 
  7.  */
  8.  
  9. #include "werr.h"
  10. #include "stdio.h"
  11. #include "stdlib.h"
  12. #include "string.h"
  13. #include "ctype.h"
  14.  
  15. #define Data_file "<STDFinder$Dir>.Directorys.NewCodes"
  16. #define mtnl  50
  17. #define mcl   8
  18.  
  19. extern struct std {
  20.   char town[mtnl];
  21.   char code[mcl];
  22.   struct std *next;
  23.   struct std *previous;
  24.   }*stdstart,*stdlast;
  25.  
  26. FILE *file_open(char *open_name,char *mode)
  27. {
  28.     FILE *temp;
  29.     if ((temp=fopen(open_name,mode)) == NULL) {
  30.         werr(1,"Error - cannot open data file %s",Data_file);
  31.         exit(1);            
  32.         }
  33.     return temp;    
  34. }
  35.  
  36. void file_close(FILE *close_file,char *close_name)
  37. {
  38.     fclose(close_file);
  39. }
  40.  
  41. int file_read(FILE *from_file,char *read_name,int action)
  42. {
  43.   char original[mtnl + mcl],town[mtnl],code[mcl];
  44.   int length = 0;
  45.   struct std *node,*temp = NULL;
  46.   void add_entry(char *town,char *code);
  47.  
  48.   if (action) { /* firstly free any existing structures */
  49.   while (stdstart) {
  50.     node = stdstart->next;
  51.     free(node);
  52.     stdstart = node;
  53.     }
  54.   if ((stdstart = (struct std *) 
  55.          calloc(1, sizeof(struct std))) == NULL)
  56.   { werr(0,"Unable to assign memory for first node (start).\n"); exit(0);} 
  57.  
  58.   node = stdstart;
  59.   }
  60.   else node = stdlast; /* if to be appended */
  61.  
  62.   while (!feof(from_file)) {
  63.   fgets(original,(mtnl + mcl),from_file);
  64.  
  65.   strncpy(town,strtok(original,","),(mtnl-1));
  66.   strncpy(code,strtok(NULL,"\n"),(mcl-1));
  67.   if (strlen(town) > length) length = strlen(town);
  68.  
  69.   add_entry(town,code);
  70.  
  71.   if (ferror(from_file)) werr(1,"Error reading from file");
  72.   }
  73. /* for the last entry */
  74.   temp->next = NULL;
  75.   stdstart->previous = NULL;
  76.   stdlast = node;
  77.   return(length);
  78. }
  79. int initial_file_read(FILE *from_file,char *read_name,int action)
  80. {
  81.   char original[50];
  82.   struct std *node,*temp = NULL;
  83.   int length = 0;
  84.  
  85.   if ((stdstart = (struct std *) 
  86.          calloc(1, sizeof(struct std))) == NULL)
  87.   { werr(0,"Unable to assign memory for first node (start).\n"); exit(0);} 
  88.  
  89.   node = stdstart;
  90.  
  91.   while (!feof(from_file)) {
  92.     fgets(original,(60),from_file);
  93.  
  94.   if ((node->next = (struct std *) 
  95.          calloc(1, sizeof(struct std))) == NULL)
  96.       { werr(0,"Unable to assign memory for next node.\n"); return(0);}
  97.  
  98.   strncpy(node->town,strtok(original,","),(mtnl-1));
  99.   strncpy(node->code,strtok(NULL,"\n"),(mcl-1));
  100.   if (strlen(node->town) > length) length = strlen(node->town);
  101.   node->previous = temp;
  102.   temp = node;
  103.   node = node->next;
  104.  
  105.   if (ferror(from_file)) werr(1,"Error reading from file");
  106.   }
  107. /* for the last entry */
  108.   temp->next = NULL;
  109.   stdstart->previous = NULL;
  110.   stdlast = node;
  111.   return(length);
  112. }
  113.