static char rcsver[] = "$Id: dataset.c,v 1.5 2000/07/07 17:15:22 saadat Exp $"; /** ** $Source: /tes/cvs/vanilla/dataset.c,v $ ** ** $Log: dataset.c,v $ ** Revision 1.5 2000/07/07 17:15:22 saadat ** Added support for Windows MS Visual C++ memory-mapped file support. ** Removed some unused variables from various files. ** ** Revision 1.4 2000/05/26 15:04:51 asbms ** Added fixes for smoother return from errors ** ** Revision 1.3 2000/05/25 02:24:23 saadat ** Fixed IRTM temperature generation. ** ** Revision 1.2 1999/11/19 21:19:43 gorelick ** Version 3.1, post PDS delivery of 3.0 ** ** Revision 1.1.1.1 1999/10/15 19:30:35 gorelick ** Version 3.0 ** ** Revision 2.2 1999/07/12 23:14:08 gorelick ** *** empty log message *** ** ** Revision 2.1 1999/02/10 04:00:50 gorelick ** *** empty log message *** ** ** Revision 2.0 1998/12/22 22:47:04 gorelick ** release version ** ** Revision 2.0 1998/12/18 01:26:03 gorelick ** release version ** ** Revision 1.5 1998/12/01 22:42:06 gorelick ** *** empty log message *** ** ** Revision 1.4 1998/11/12 22:58:55 gorelick ** first release version ** **/ #include #include #include #include "header.h" #include "proto.h" #ifdef _WINDOWS #include "dos.h" #else #include #endif #include "system.h" int LoadTable(DATASET *dataset, char *path, char *name); /** ** Load dataset stuff ** ** Options: ** ** table ** path/table ** path ** **/ DATASET * LoadDataset(DATASET *dataset, char *fname) { char *path, *file, *dir, *tbl_name, *dir_name; struct stat sbuf; char tmppath[256], newpath[256], buf[256], *p; FILE *fp; if (dataset == NULL) { dataset = calloc(1, sizeof(DATASET)); dataset->tables = new_list(); dataset->tablenames = new_list(); } path = strdup(fname); /** ** Split fname into path/file **/ if ((file = strrchr(path, '/')) == NULL) { dir = "."; file = path; } else { *file = '\0'; file++; dir = path; } if (stat(fname, &sbuf) != 0) { fname = find_file(fname); if (stat(fname, &sbuf) != 0) { fprintf(stderr, "Bad path: %s\n", fname); return (NULL); } } if ((sbuf.st_mode & S_IFDIR) != 0) { dir = fname; file = "DATASET"; } sprintf(newpath, "%s/%s", dir, file); fname = find_file(newpath); if (access(fname, F_OK)) { sprintf(newpath, "%s/%s.LST", dir, file); fname = find_file(newpath); } if (access(fname, F_OK)) { sprintf(newpath, "%s/%s.TXT", dir, file); fname = find_file(newpath); } /** ** Open the DATASET file and extract its contents **/ if ((fp = fopen(fname, "r")) == NULL) { fprintf(stderr, "Unable to open DATASET file: %s\n", fname); return (NULL); } while (fgets(buf, 256, fp) != NULL) { if ((p = strrchr(buf, '\r')) != NULL) *p = '\0'; if ((p = strrchr(buf, '\n')) != NULL) *p = '\0'; if (buf[strlen(buf) -1] == '/') { /** ** This is a path to another dataset in another directory **/ LoadDataset(dataset, buf); } else if (strchr(buf, '/')) { /** ** This is an explicit path to another table **/ tbl_name = (char *)basename(buf); dir_name = (char *)dirname(buf); if (buf[0] == '/') { if(LoadTable(dataset, dir_name, tbl_name)){ fclose(fp); return(NULL); } } else { sprintf(tmppath, "%s/%s", dir, dir_name); if(LoadTable(dataset, tmppath, tbl_name)){ fclose(fp); return(NULL); } } } else { /** ** This is the name of a table, load it directly. **/ if(LoadTable(dataset, dir, buf)){ fclose(fp); return(NULL); } } } fclose(fp); return(dataset); } TABLE * FindTable(DATASET *dataset, char *name) { int i; for (i = 0 ; i < dataset->tables->number ; i++) { if (!strcmp(((char **)dataset->tablenames->ptr)[i], name)) { return(((TABLE **)dataset->tables->ptr)[i]); } } return(NULL); } /** ** Search the dataset to see if this table already exists in it. ** If not, make a new TABLE ** Add the file entries from and sort. ** ** Changed function from void to int for error checking purposes ** Thu May 25 15:46:38 MST 2000 **/ int LoadTable(DATASET *dataset, char *path, char *name) { TABLE *table = FindTable(dataset, name); if (table == NULL) { table = calloc(1, sizeof(TABLE)); table->files = LoadFilenames(path, name); if (table->files->number != 0) { list_add(dataset->tables, table); list_add(dataset->tablenames, (void *)strdup(name)); table->label = LoadLabel((table->files->ptr)[0]); /* Added check on LoadLabel return ** (file can be rejected resulting in a NULL label ** Thu May 25 15:47:25 MST 2000 */ if (table->label == NULL) { free(table); return(1); } /*End Added*/ table->label->table = table; } else { free(table); /* this should be FreeTable */ } } else { LIST *files = LoadFilenames(path, name); list_merge(table->files, files); SortFiles(table->files); /* Check for duplicate file names? */ } return(0); }