diff --git a/src/parser.c b/src/parser.c index 2111caf1c815633883f1dc86540cf199a6fc115c..6988a82217c03f3da6730fc1ff59e295ca30f9a5 100644 --- a/src/parser.c +++ b/src/parser.c @@ -289,30 +289,30 @@ static void parse_section_param(char *line, int *isFirstParam, * * @param params Structure that holds the parameters * @param name Name of the parameter to be found - * @param retParam Value of the parameter found - * + * @return Value of the parameter found */ +int parser_get_param_int(const struct swift_params *params, const char *name) { -void parser_get_param_int(const struct swift_params *params, char *name, - int *retParam) { char str[PARSER_MAX_LINE_SIZE]; + int retParam = 0; 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) { + 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; + return retParam; } } error("Cannot find '%s' in the structure.", name); + return 0; } /** @@ -320,30 +320,31 @@ void parser_get_param_int(const struct swift_params *params, char *name, * * @param params Structure that holds the parameters * @param name Name of the parameter to be found - * @param retParam Value of the parameter found - * + * @return Value of the parameter found */ +float parser_get_param_float(const struct swift_params *params, + const char *name) { -void parser_get_param_float(const struct swift_params *params, char *name, - float *retParam) { char str[PARSER_MAX_LINE_SIZE]; + float retParam = 0.f; 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) { + 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; + return retParam; } } error("Cannot find '%s' in the structure.", name); + return 0.f; } /** @@ -351,30 +352,30 @@ void parser_get_param_float(const struct swift_params *params, char *name, * * @param params Structure that holds the parameters * @param name Name of the parameter to be found - * @param retParam Value of the parameter found - * + * @return Value of the parameter found */ +double parser_get_param_double(const struct swift_params *params, + const char *name) { -void parser_get_param_double(const struct swift_params *params, char *name, - double *retParam) { char str[PARSER_MAX_LINE_SIZE]; + double retParam = 0.; 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) { + 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; + return retParam; } } error("Cannot find '%s' in the structure.", name); + return 0.; } /** @@ -382,12 +383,10 @@ void parser_get_param_double(const struct swift_params *params, char *name, * * @param params Structure that holds the parameters * @param name Name of the parameter to be found - * @param retParam Value of the parameter found - * + * @param retParam of the parameter found */ - -void parser_get_param_string(const struct swift_params *params, char *name, - char *retParam) { +void parser_get_param_string(const struct swift_params *params, + const char *name, char *retParam) { for (int i = 0; i < params->count; i++) { /*strcmp returns 0 if both strings are the same.*/ if (!strcmp(name, params->data[i].name)) { @@ -425,7 +424,7 @@ void parser_print_params(const struct swift_params *params) { * */ -void parser_write_params_to_file(const struct swift_params *params, +void parser_write_params_to_file(const struct swift_params *params, const char *file_name) { FILE *file = fopen(file_name, "w"); char section[PARSER_MAX_LINE_SIZE]; diff --git a/src/parser.h b/src/parser.h index e49d2b003d2523ed8dde3db77aa5cb34384a0030..cf178e7ce647c50aed5d36f2e88ff242d4055a9e 100644 --- a/src/parser.h +++ b/src/parser.h @@ -43,13 +43,12 @@ void parser_read_file(const char *file_name, struct swift_params *params); void parser_print_params(const struct swift_params *params); void parser_write_params_to_file(const struct swift_params *params, const char *file_name); -void parser_get_param_int(const struct swift_params *params, char *name, - int *retParam); -void parser_get_param_float(const struct swift_params *params, char *name, - float *retParam); -void parser_get_param_double(const struct swift_params *params, char *name, - double *retParam); -void parser_get_param_string(const struct swift_params *params, char *name, - char *retParam); +int parser_get_param_int(const struct swift_params *params, const char *name); +float parser_get_param_float(const struct swift_params *params, + const char *name); +double parser_get_param_double(const struct swift_params *params, + const char *name); +void parser_get_param_string(const struct swift_params *params, + const char *name, char *retParam); #endif /* SWIFT_PARSER_H */ diff --git a/src/units.c b/src/units.c index 68bb90f64567390e8fa27245a7dafcadeff13752..a637d3c8b2e194a7f8ef1e2e2e54461f02033e4c 100644 --- a/src/units.c +++ b/src/units.c @@ -49,18 +49,17 @@ void initUnitSystem(struct UnitSystem* us, const struct swift_params* params) { - parser_get_param_double(params, "UnitSystem:UnitMass_in_cgs", - &us->UnitMass_in_cgs); - parser_get_param_double(params, "UnitSystem:UnitLength_in_cgs", - &us->UnitLength_in_cgs); - double unitVelocity; - parser_get_param_double(params, "UnitSystem:UnitVelocity_in_cgs", - &unitVelocity); + us->UnitMass_in_cgs = + parser_get_param_double(params, "UnitSystem:UnitMass_in_cgs"); + us->UnitLength_in_cgs = + parser_get_param_double(params, "UnitSystem:UnitLength_in_cgs"); + const double unitVelocity = + parser_get_param_double(params, "UnitSystem:UnitVelocity_in_cgs"); us->UnitTime_in_cgs = us->UnitLength_in_cgs / unitVelocity; - parser_get_param_double(params, "UnitSystem:UnitCurrent_in_cgs", - &us->UnitCurrent_in_cgs); - parser_get_param_double(params, "UnitSystem:UnitTemp_in_cgs", - &us->UnitTemperature_in_cgs); + us->UnitCurrent_in_cgs = + parser_get_param_double(params, "UnitSystem:UnitCurrent_in_cgs"); + us->UnitTemperature_in_cgs = + parser_get_param_double(params, "UnitSystem:UnitTemp_in_cgs"); } /**