Skip to content
Snippets Groups Projects
Commit b65e4002 authored by James Willis's avatar James Willis
Browse files

Added function to write contents of structure to a file in YAML format.

parent 6967f09c
No related branches found
No related tags found
1 merge request!140First version of main() using a parameter file to get constants.
......@@ -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);
}
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment