home *** CD-ROM | disk | FTP | other *** search
- /* Title: file_utils.c
- * Purpose: Generalised file I/O routines + specifics for !STDFinder
- * Author: Julyan Bristow
- * Date: 23 September 1992
- *
- * Version History: 0.01 First version
- */
-
- #include "werr.h"
- #include "stdio.h"
- #include "stdlib.h"
- #include "string.h"
- #include "ctype.h"
-
- #define Data_file "<STDFinder$Dir>.Directorys.NewCodes"
- #define mtnl 50
- #define mcl 8
-
- extern struct std {
- char town[mtnl];
- char code[mcl];
- struct std *next;
- struct std *previous;
- }*stdstart,*stdlast;
-
- FILE *file_open(char *open_name,char *mode)
- {
- FILE *temp;
- if ((temp=fopen(open_name,mode)) == NULL) {
- werr(1,"Error - cannot open data file %s",Data_file);
- exit(1);
- }
- return temp;
- }
-
- void file_close(FILE *close_file,char *close_name)
- {
- fclose(close_file);
- }
-
- int file_read(FILE *from_file,char *read_name,int action)
- {
- char original[mtnl + mcl],town[mtnl],code[mcl];
- int length = 0;
- struct std *node,*temp = NULL;
- void add_entry(char *town,char *code);
-
- if (action) { /* firstly free any existing structures */
- while (stdstart) {
- node = stdstart->next;
- free(node);
- stdstart = node;
- }
- if ((stdstart = (struct std *)
- calloc(1, sizeof(struct std))) == NULL)
- { werr(0,"Unable to assign memory for first node (start).\n"); exit(0);}
-
- node = stdstart;
- }
- else node = stdlast; /* if to be appended */
-
- while (!feof(from_file)) {
- fgets(original,(mtnl + mcl),from_file);
-
- strncpy(town,strtok(original,","),(mtnl-1));
- strncpy(code,strtok(NULL,"\n"),(mcl-1));
- if (strlen(town) > length) length = strlen(town);
-
- add_entry(town,code);
-
- if (ferror(from_file)) werr(1,"Error reading from file");
- }
- /* for the last entry */
- temp->next = NULL;
- stdstart->previous = NULL;
- stdlast = node;
- return(length);
- }
- int initial_file_read(FILE *from_file,char *read_name,int action)
- {
- char original[50];
- struct std *node,*temp = NULL;
- int length = 0;
-
- if ((stdstart = (struct std *)
- calloc(1, sizeof(struct std))) == NULL)
- { werr(0,"Unable to assign memory for first node (start).\n"); exit(0);}
-
- node = stdstart;
-
- while (!feof(from_file)) {
- fgets(original,(60),from_file);
-
- if ((node->next = (struct std *)
- calloc(1, sizeof(struct std))) == NULL)
- { werr(0,"Unable to assign memory for next node.\n"); return(0);}
-
- strncpy(node->town,strtok(original,","),(mtnl-1));
- strncpy(node->code,strtok(NULL,"\n"),(mcl-1));
- if (strlen(node->town) > length) length = strlen(node->town);
- node->previous = temp;
- temp = node;
- node = node->next;
-
- if (ferror(from_file)) werr(1,"Error reading from file");
- }
- /* for the last entry */
- temp->next = NULL;
- stdstart->previous = NULL;
- stdlast = node;
- return(length);
- }
-