diff --git a/src/parser.c b/src/parser.c index eec257823cee22fd6bed925f85b60b1f610c5749..548f02b35fa45f34e3e4302c9263f2d67e2dc2bf 100644 --- a/src/parser.c +++ b/src/parser.c @@ -24,6 +24,7 @@ #include <stdlib.h> #include "error.h" +/* Reads an input file and stores each parameter in a structure.*/ void parser_read_file(const char *file_name, struct swift_params *params) { FILE *fp; @@ -39,12 +40,13 @@ void parser_read_file(const char *file_name, struct swift_params *params) { /* Read until the end of the file is reached.*/ while(!feof(fp)) { - read_param(fp,params); + parse_line(fp,params); } fclose(fp); } +/* Counts the number of characters in a string.*/ static int count_char(char *str, char val) { int i, count = 0; @@ -59,7 +61,8 @@ static int count_char(char *str, char val) { return count; } -static void read_param(FILE *fp, struct swift_params *params) { +/* Parses a line from a file and stores any parameters in a structure. */ +static void parse_line(FILE *fp, struct swift_params *params) { char line [MAX_LINE_SIZE]; char trim_line [MAX_LINE_SIZE]; @@ -69,21 +72,21 @@ static void read_param(FILE *fp, struct swift_params *params) { char *token; /* Remove comments */ - token = strtok(line,COMMENT); + token = strtok(line,COMMENT_CHAR); strcpy(trim_line,token); /* Check if the line contains a value */ - if(strchr(trim_line,':')) { + if(strchr(trim_line,VALUE_CHAR)) { /* Check for more than one parameter on the same line. */ - if(count_char(trim_line,':') > 1) { + if(count_char(trim_line,VALUE_CHAR) > 1) { error("Found more than one parameter in '%s', only one allowed.",line); } else { - /*Take first token as the parameter name. */ - token = strtok(trim_line,":"); + /* Take first token as the parameter name. */ + token = strtok(trim_line,VALUE_STRING); strcpy(params->data[params->count].name,token); - /*Take second token as the parameter value. */ + /* Take second token as the parameter value. */ token = strtok (NULL, " #\n"); strcpy(params->data[params->count++].value,token); } @@ -91,27 +94,28 @@ static void read_param(FILE *fp, struct swift_params *params) { } } +/* Retrieve integer parameter from structure. */ void parser_get_param_int(struct swift_params *params, char * name, int * retParam) { int i; - for(i=0; i<MAX_NO_OF_PARAMS; i++) { + for(i=0; i<params->count; i++) { /*strcmp returns 0 if both strings are the same.*/ if(!strcmp(name,params->data[i].name)) { - /* Check if value is 0, to avoid integers of zero causing an error. */ + /* Check if value is 0, to avoid integers of 0 causing an error in the following checks. */ if(!strcmp("0",params->data[i].value)) { *retParam = 0; } /* Check if the value is a string. */ else if(!atoi(params->data[i].value)) { - error("Tried to parse '%s', when expecting an integer.",params->data[i].value); + error("Tried to parse '%s' but found '%s', when expecting an integer.",params->data[i].name,params->data[i].value); } /* Check if the value is a float.*/ - else if( strchr(params->data[i].value,'.') || strchr(params->data[i].value,'e')|| + else if( strchr(params->data[i].value,'.') || strchr(params->data[i].value,'e') || strchr(params->data[i].value,'E') ) { - error("Tried to parse '%s', when expecting an integer.",params->data[i].value); + error("Tried to parse '%s' but found '%s', when expecting an integer.",params->data[i].name,params->data[i].value); } else { *retParam = atoi(params->data[i].value); @@ -122,11 +126,12 @@ void parser_get_param_int(struct swift_params *params, char * name, int * retPar } } +/* Retrieve float parameter from structure. */ void parser_get_param_float(struct swift_params *params, char * name, float * retParam) { int i; - for(i=0; i<MAX_NO_OF_PARAMS; i++) { + for(i=0; i<params->count; i++) { /*strcmp returns 0 if both strings are the same.*/ if(!strcmp(name,params->data[i].name)) { @@ -136,11 +141,27 @@ void parser_get_param_float(struct swift_params *params, char * name, float * re } } +/* Retrieve double parameter from structure. */ +void parser_get_param_double(struct swift_params *params, char * name, double * retParam) { + + int i; + + for(i=0; i<params->count; i++) { + + /*strcmp returns 0 if both strings are the same.*/ + if(!strcmp(name,params->data[i].name)) { + *retParam = atof(params->data[i].value); + return; + } + } +} + +/* Retrieve string parameter from structure. */ void parser_get_param_string(struct swift_params *params, char * name, char * retParam) { int i; - for(i=0; i<MAX_NO_OF_PARAMS; i++) { + for(i=0; i<params->count; i++) { /*strcmp returns 0 if both strings are the same.*/ if(!strcmp(name,params->data[i].name)) { @@ -150,17 +171,18 @@ void parser_get_param_string(struct swift_params *params, char * name, char * re } } +/* Prints the contents of the parameter structure. */ void parser_print_params(struct swift_params *params) { int i; - printf("\n--------------------\n"); - printf("SWIFT Parameter File\n"); - printf("--------------------\n"); + printf("\n--------------------------\n"); + printf("| SWIFT Parameter File |\n"); + printf("--------------------------\n"); for(i=0; i<params->count; i++) { - printf("Name: %s\n",params->data[i].name); - printf("Value: %s\n",params->data[i].value); + printf("Parameter name: %s\n",params->data[i].name); + printf("Parameter value: %s\n",params->data[i].value); } } diff --git a/src/parser.h b/src/parser.h index 9cd1578dfe01a8659ace925f4344c8cf68bacf49..b935755d0c9ec1db35af2dd2d9d4abcef026f75c 100644 --- a/src/parser.h +++ b/src/parser.h @@ -25,8 +25,9 @@ #define MAX_LINE_SIZE 128 #define MAX_NO_OF_PARAMS 512 -#define COMMENT "#" -#define VALUE ':' +#define COMMENT_CHAR "#" +#define VALUE_CHAR ':' +#define VALUE_STRING ":" #define END_OF_FILE "..." struct parameter { @@ -44,10 +45,11 @@ void parser_read_file(const char *file_name, struct swift_params *params); void parser_print_params(struct swift_params *params); void parser_get_param_int(struct swift_params *params, char *name, int *retParam); void parser_get_param_float(struct swift_params *params, char *name, float *retParam); +void parser_get_param_double(struct swift_params *params, char * name, double * retParam); void parser_get_param_string(struct swift_params *params, char *name, char *retParam); /* Private functions. */ -static void read_param(FILE *fp, struct swift_params *params); +static void parse_line(FILE *fp, struct swift_params *params); static int count_char(char *str, char val); #endif /* SWIFT_PARSER_H */