diff --git a/src/parser.c b/src/parser.c index 177f84df38977b7823c8541af396b0d0c3f91f8f..936167cc28153bc6364139f23d19bba01c288949 100644 --- a/src/parser.c +++ b/src/parser.c @@ -44,23 +44,23 @@ static void parse_line(FILE *fp, struct swift_params *params); void parser_read_file(const char *file_name, struct swift_params *params) { - FILE *fp; - + /* Open file for reading */ + FILE *file = fopen(file_name, "r"); + + /* Initialise parameter count. */ params->count = 0; - /* Open file for reading */ - fp = fopen(file_name, "r"); - - if(fp == NULL) { + /* Check if parameter file exits. */ + if(file == 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); + while(!feof(file)) { + parse_line(file,params); } - fclose(fp); + fclose(file); } /** @@ -253,3 +253,20 @@ void parser_print_params(struct swift_params *params) { } } + +void parser_write_params_to_file(struct swift_params *params, const char *file_name) { + + FILE *file = fopen(file_name, "w"); + + /* Start of file identifier in YAML. */ + fprintf(file,"%s\n",PARSER_START_OF_FILE); + + for(int i=0; i<params->count; i++) { + fprintf(file,"%s%c %s\n",params->data[i].name,PARSER_VALUE_CHAR,params->data[i].value); + } + + /* End of file identifier in YAML. */ + fprintf(file,PARSER_END_OF_FILE); + + fclose(file); +} diff --git a/src/parser.h b/src/parser.h index b4b50dddb4c7c807b034c4fd9c86035f275afa75..2246a8913d8fa428a0f5b00294197420af79ed99 100644 --- a/src/parser.h +++ b/src/parser.h @@ -27,6 +27,7 @@ #define PARSER_COMMENT_CHAR "#" #define PARSER_VALUE_CHAR ':' #define PARSER_VALUE_STRING ":" +#define PARSER_START_OF_FILE "---" #define PARSER_END_OF_FILE "..." struct parameter { @@ -42,6 +43,7 @@ struct swift_params { /* Public API. */ void parser_read_file(const char *file_name, struct swift_params *params); void parser_print_params(struct swift_params *params); +void parser_write_params_to_file(struct swift_params *params, const char *file_name); 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);