diff --git a/src/parser.c b/src/parser.c index 177f84df38977b7823c8541af396b0d0c3f91f8f..06dc819842d54d952704e4e0c40ebec5b561f691 100644 --- a/src/parser.c +++ b/src/parser.c @@ -36,31 +36,31 @@ static int count_char(char *str, char val); static void parse_line(FILE *fp, struct swift_params *params); /** - * @brief Reads an input file and stores each parameter in a structure. - * + * @brief Reads an input file and stores each parameter in a structure. + * * @param file_name Name of file to be read * @param params Structure to be populated from file */ void parser_read_file(const char *file_name, struct swift_params *params) { - - FILE *fp; - - params->count = 0; - - /* Open file for reading */ - fp = fopen(file_name, "r"); - - if(fp == NULL) { - error("Error opening parameter file: %s",file_name); - } - - /* Read until the end of the file is reached.*/ - while(!feof(fp)) { - parse_line(fp,params); - } - fclose(fp); + FILE *fp; + + params->count = 0; + + /* Open file for reading */ + fp = fopen(file_name, "r"); + + if (fp == NULL) { + error("Error opening parameter file: %s", file_name); + } + + /* Read until the end of the file is reached.*/ + while (!feof(fp)) { + parse_line(fp, params); + } + + fclose(fp); } /** @@ -72,57 +72,56 @@ void parser_read_file(const char *file_name, struct swift_params *params) { static int count_char(char *str, char val) { - int count = 0; + int count = 0; - /* Check if the line contains the character */ - while(*str) { - if (*str++ == val) ++count; - } + /* Check if the line contains the character */ + while (*str) { + if (*str++ == val) ++count; + } - return count; + return count; } -/** +/** * @brief Parses a line from a file and stores any parameters in a structure. * * @param fp File pointer to file to be read * @param params Structure to be populated from file - * + * */ static void parse_line(FILE *fp, struct swift_params *params) { - char line [PARSER_MAX_LINE_SIZE]; - char trim_line [PARSER_MAX_LINE_SIZE]; - - /* Read a line of the file */ - if(fgets(line,PARSER_MAX_LINE_SIZE,fp) != NULL) { - - char *token; - /* Remove comments */ - token = strtok(line,PARSER_COMMENT_CHAR); - strcpy(trim_line,token); - - /* Check if the line contains a value */ - if(strchr(trim_line,PARSER_VALUE_CHAR)) { - /* Check for more than one parameter on the same line. */ - if(count_char(trim_line,PARSER_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,PARSER_VALUE_STRING); - strcpy(params->data[params->count].name,token); - - /* Take second token as the parameter value. */ - token = strtok (NULL, " #\n"); - strcpy(params->data[params->count++].value,token); - } - } + char line[PARSER_MAX_LINE_SIZE]; + char trim_line[PARSER_MAX_LINE_SIZE]; + + /* Read a line of the file */ + if (fgets(line, PARSER_MAX_LINE_SIZE, fp) != NULL) { + + char *token; + /* Remove comments */ + token = strtok(line, PARSER_COMMENT_CHAR); + strcpy(trim_line, token); + + /* Check if the line contains a value */ + if (strchr(trim_line, PARSER_VALUE_CHAR)) { + /* Check for more than one parameter on the same line. */ + if (count_char(trim_line, PARSER_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, PARSER_VALUE_STRING); + strcpy(params->data[params->count].name, token); + + /* Take second token as the parameter value. */ + token = strtok(NULL, " #\n"); + strcpy(params->data[params->count++].value, token); + } } + } } -/** +/** * @brief Retrieve integer parameter from structure. * * @param params Structure that holds the parameters @@ -131,28 +130,32 @@ static void parse_line(FILE *fp, struct swift_params *params) { * */ -void parser_get_param_int(struct swift_params *params, char * name, int * retParam) { - - char str [128]; +void parser_get_param_int(struct swift_params *params, char *name, + int *retParam) { + + char str[128]; - for(int i=0; i<params->count; i++) { + for (int i = 0; i < params->count; i++) { - /*strcmp returns 0 if both strings are the same.*/ - if(!strcmp(name,params->data[i].name)) { - - /* Check that exactly one number is parsed. */ - if(sscanf(params->data[i].value,"%d%s",retParam,str) != 1) { - error("Tried parsing int '%s' but found '%s' with illegal integer characters '%s'.",params->data[i].name,params->data[i].value,str); - } + /*strcmp returns 0 if both strings are the same.*/ + if (!strcmp(name, params->data[i].name)) { - return; - } - } + /* Check that exactly one number is parsed. */ + if (sscanf(params->data[i].value, "%d%s", retParam, str) != 1) { + error( + "Tried parsing int '%s' but found '%s' with illegal integer " + "characters '%s'.", + params->data[i].name, params->data[i].value, str); + } + + return; + } + } - message("Cannot find '%s' in the structure.",name); + message("Cannot find '%s' in the structure.", name); } -/** +/** * @brief Retrieve float parameter from structure. * * @param params Structure that holds the parameters @@ -161,29 +164,32 @@ void parser_get_param_int(struct swift_params *params, char * name, int * retPar * */ -void parser_get_param_float(struct swift_params *params, char * name, float * retParam) { - - char str [128]; - - for(int i=0; i<params->count; i++) { - - /*strcmp returns 0 if both strings are the same.*/ - if(!strcmp(name,params->data[i].name)) { - - /* Check that exactly one number is parsed. */ - if(sscanf(params->data[i].value,"%f%s",retParam,str) != 1) { - error("Tried parsing float '%s' but found '%s' with illegal float characters '%s'.",params->data[i].name,params->data[i].value,str); - } - - return; - } - } - - message("Cannot find '%s' in the structure.",name); -} +void parser_get_param_float(struct swift_params *params, char *name, + float *retParam) { + + char str[128]; + + for (int i = 0; i < params->count; i++) { + /*strcmp returns 0 if both strings are the same.*/ + if (!strcmp(name, params->data[i].name)) { -/** + /* Check that exactly one number is parsed. */ + if (sscanf(params->data[i].value, "%f%s", retParam, str) != 1) { + error( + "Tried parsing float '%s' but found '%s' with illegal float " + "characters '%s'.", + params->data[i].name, params->data[i].value, str); + } + + return; + } + } + + message("Cannot find '%s' in the structure.", name); +} + +/** * @brief Retrieve double parameter from structure. * * @param params Structure that holds the parameters @@ -192,28 +198,32 @@ void parser_get_param_float(struct swift_params *params, char * name, float * re * */ -void parser_get_param_double(struct swift_params *params, char * name, double * retParam) { - - char str [128]; - - for(int i=0; i<params->count; i++) { - - /*strcmp returns 0 if both strings are the same.*/ - if(!strcmp(name,params->data[i].name)) { - - /* Check that exactly one number is parsed. */ - if(sscanf(params->data[i].value,"%lf",retParam) != 1) { - error("Tried parsing double '%s' but found '%s' with illegal double characters '%s'.",params->data[i].name,params->data[i].value,str); - } - - return; - } - } - - message("Cannot find '%s' in the structure.",name); +void parser_get_param_double(struct swift_params *params, char *name, + double *retParam) { + + char str[128]; + + for (int i = 0; i < params->count; i++) { + + /*strcmp returns 0 if both strings are the same.*/ + if (!strcmp(name, params->data[i].name)) { + + /* Check that exactly one number is parsed. */ + if (sscanf(params->data[i].value, "%lf", retParam) != 1) { + error( + "Tried parsing double '%s' but found '%s' with illegal double " + "characters '%s'.", + params->data[i].name, params->data[i].value, str); + } + + return; + } + } + + message("Cannot find '%s' in the structure.", name); } -/** +/** * @brief Retrieve string parameter from structure. * * @param params Structure that holds the parameters @@ -222,19 +232,20 @@ void parser_get_param_double(struct swift_params *params, char * name, double * * */ -void parser_get_param_string(struct swift_params *params, char * name, char * retParam) { - - for(int i=0; i<params->count; i++) { +void parser_get_param_string(struct swift_params *params, char *name, + char *retParam) { - /*strcmp returns 0 if both strings are the same.*/ - if(!strcmp(name,params->data[i].name)) { - strcpy(retParam, params->data[i].value); - return; - } - } + for (int i = 0; i < params->count; i++) { + + /*strcmp returns 0 if both strings are the same.*/ + if (!strcmp(name, params->data[i].name)) { + strcpy(retParam, params->data[i].value); + return; + } + } } -/** +/** * @brief Prints the contents of the parameter structure. * * @param params Structure that holds the parameters @@ -243,13 +254,12 @@ void parser_get_param_string(struct swift_params *params, char * name, char * re void parser_print_params(struct swift_params *params) { - printf("\n--------------------------\n"); - printf("| SWIFT Parameter File |\n"); - printf("--------------------------\n"); - - for(int i=0; i<params->count; i++) { - printf("Parameter name: %s\n",params->data[i].name); - printf("Parameter value: %s\n",params->data[i].value); - } + printf("\n--------------------------\n"); + printf("| SWIFT Parameter File |\n"); + printf("--------------------------\n"); + for (int i = 0; i < params->count; i++) { + 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 b4b50dddb4c7c807b034c4fd9c86035f275afa75..2fb4148944cd423da016341744cb6d58e222182e 100644 --- a/src/parser.h +++ b/src/parser.h @@ -22,7 +22,7 @@ #include <stdio.h> #define PARSER_MAX_LINE_SIZE 128 -#define PARSER_MAX_NO_OF_PARAMS 512 +#define PARSER_MAX_NO_OF_PARAMS 512 #define PARSER_COMMENT_CHAR "#" #define PARSER_VALUE_CHAR ':' @@ -30,21 +30,25 @@ #define PARSER_END_OF_FILE "..." struct parameter { - char name [PARSER_MAX_LINE_SIZE]; - char value [PARSER_MAX_LINE_SIZE]; + char name[PARSER_MAX_LINE_SIZE]; + char value[PARSER_MAX_LINE_SIZE]; }; struct swift_params { - struct parameter data [PARSER_MAX_NO_OF_PARAMS]; - int count; + struct parameter data[PARSER_MAX_NO_OF_PARAMS]; + int count; }; /* Public API. */ 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); +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); #endif /* SWIFT_PARSER_H */ diff --git a/tests/testParser.c b/tests/testParser.c index 55b3adb6102a5bdf976c77ff4be64837a3f67cc8..a4b8789fca056fef659bca78eae9d0effb2ceb66 100644 --- a/tests/testParser.c +++ b/tests/testParser.c @@ -24,41 +24,44 @@ int main(int argc, char *argv[]) { - const char * input_file = argv[1]; - - /* Create a structure to read file into. */ + const char *input_file = argv[1]; + + /* Create a structure to read file into. */ struct swift_params param_file; - + /* Create variables that will be set from the parameter file. */ int no_of_threads = 0; int no_of_time_steps = 0; float max_h = 0.0f; double start_time = 0.0; - char ic_file [PARSER_MAX_LINE_SIZE]; + char ic_file[PARSER_MAX_LINE_SIZE]; /* Read the parameter file. */ - parser_read_file(input_file,¶m_file); + parser_read_file(input_file, ¶m_file); /* Print the contents of the structure. */ parser_print_params(¶m_file); - - /* Retrieve parameters and store them in variables defined above. - * Have to specify the name of the parameter as it appears in the + + /* Retrieve parameters and store them in variables defined above. + * Have to specify the name of the parameter as it appears in the * input file: testParserInput.yaml.*/ - parser_get_param_int(¶m_file,"no_of_threads",&no_of_threads); - parser_get_param_int(¶m_file,"no_of_time_steps",&no_of_time_steps); - parser_get_param_float(¶m_file,"max_h",&max_h); - parser_get_param_double(¶m_file,"start_time",&start_time); - parser_get_param_string(¶m_file,"ic_file",ic_file); - + parser_get_param_int(¶m_file, "no_of_threads", &no_of_threads); + parser_get_param_int(¶m_file, "no_of_time_steps", &no_of_time_steps); + parser_get_param_float(¶m_file, "max_h", &max_h); + parser_get_param_double(¶m_file, "start_time", &start_time); + parser_get_param_string(¶m_file, "ic_file", ic_file); + /* Print the variables to check their values are correct. */ - printf("no_of_threads: %d, no_of_time_steps: %d, max_h: %f, start_time: %lf, ic_file: %s\n",no_of_threads, no_of_time_steps, max_h, start_time, ic_file); + printf( + "no_of_threads: %d, no_of_time_steps: %d, max_h: %f, start_time: %lf, " + "ic_file: %s\n", + no_of_threads, no_of_time_steps, max_h, start_time, ic_file); assert(no_of_threads == 16); assert(no_of_time_steps == 10); assert(fabs(max_h - 1.1255) < 0.00001); assert(fabs(start_time - 1.23456789) < 0.00001); - assert(strcmp(ic_file,"ic_file.ini") == 0); /*strcmp returns 0 if correct.*/ + assert(strcmp(ic_file, "ic_file.ini") == 0); /*strcmp returns 0 if correct.*/ return 0; }