#include #include "gpi.h" #include #include static int opened=0; char gp_parfilename[132] = "No_such_file"; FILE * find_parfilefd() { if(!strcmp(gp_parfilename,"No_such_file")) return NULL; return fopen(gp_parfilename,"r"); } void GP_parfile(char * parfilename) { int len = 132; if(!parfilename) return; if(!strcmp(parfilename,"No_such_file")) { printf("\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\nparfile name is %s \n",parfilename); printf("Wow do you really want to call your parameter file that ?\n\n"); exit(1); } if(strlen(parfilename)>131) { fprintf(stderr,"File name too long: %s\n",parfilename); exit(1); } /* fprintf(stderr,"debug parfilename is %sEOC\n",parfilename); */ strncpy(gp_parfilename,parfilename,((size_t) len)); /* should check if null-terminated */ /* fprintf(stderr,"debug gp_parfilename set to %s\n",gp_parfilename); */ } void strcpy_noblanks(char *target, char *source, int input_count); int GP_read_parfile(void) /* puts legal lines of input into gp_lines_of_text */ { char * line, * line_aux; char *p2, *p; int count = 0, input_count=0; FILE * parfilefd=NULL; line = (char *) malloc(((size_t) MAX_LINE_LENGTH)); line_aux = (char *) malloc(((size_t) MAX_LINE_LENGTH)); parname = (char **) malloc(sizeof (char *) * (NLINES_MAX + 1)); parvalue = (char **) malloc(sizeof (char *) * (NLINES_MAX + 1)); if(opened == 1) { printf("GetparC: Warning: read_parfile already called\n"); return 0; } /* Get a line from standard input */ parfilefd = find_parfilefd(); if(parfilefd != NULL) printf("Getpar: reading parfile %s \n",gp_parfilename); else { if(strcmp(gp_parfilename,"No_such_file")) fprintf(stderr,"Getpar: failed to open %s \n",gp_parfilename); parfilefd=stdin; fprintf(stderr,"Getpar: using standard input\n"); } while (fgets(line,MAX_LINE_LENGTH,parfilefd) && (count < NLINES_MAX)) { input_count++; /* eventual comments at end of line are discarded */ if((p = strchr(line,'#')) != NULL) *p = '\0'; if((p = strchr(line,'!')) != NULL) *p = '\0'; strcpy(line_aux,line); /* check that line contains the = sign */ if((p = strchr(line,'=')) != NULL) { /* Divide the line into tokens separated by the = sign */ if((p = strtok(line_aux,"=")) == NULL) { fprintf(stderr,"GP: line %d: illegaly formatted line\n",input_count); exit(-1); } else { /* Check that there is a second word */ if((p2 = strtok(NULL,"=")) == NULL) { fprintf(stderr,"GP: line %d: illegaly formatted line\n",input_count); exit(-1); } else { parname[count] = (char *) malloc(((size_t) MAX_STRING_LENGTH)); strcpy_noblanks(parname[count],p,input_count); parvalue[count] = (char *) malloc(((size_t) MAX_STRING_LENGTH)); strcpy(parvalue[count],p2); count++; } } } } parname[count] = NULL; parvalue[count] = NULL; free(line); free(line_aux); /* Record that parfile was read */ opened=1; return count; } void read_stdin_() { int lines; lines = GP_read_parfile(); } void strcpy_noblanks(char *target, char *source, int input_count) { /* Copies string source to string target stripping all leading and trailing blanks. If more than one word is present, complain. */ char *p, *p2, *string_aux; string_aux = (char *) malloc(((size_t) MAX_STRING_LENGTH)); strcpy(string_aux,source); if((p = strchr(source,' ')) != NULL) { /* Divide the line into tokens separated by blanks */ if((p = strtok(string_aux," ")) == NULL) { fprintf(stderr,"GP: line %d: blank parameter name?\n",input_count); exit(-1); } else { /* Check that there no second word */ if((p2 = strtok(NULL," ")) != NULL) { fprintf(stderr,"GP: line %d: multi-word parameter name\n",input_count); exit(-1); } else { strcpy(target,p); } } } else strcpy(target,source); free(string_aux); } int GP_opened() { return opened; }