Skip to content
Snippets Groups Projects
Commit 357989a2 authored by Matthieu Schaller's avatar Matthieu Schaller
Browse files

Change in the parser's public interface to simplify calls.

parent 4be8ec86
No related branches found
No related tags found
1 merge request!140First version of main() using a parameter file to get constants.
...@@ -289,30 +289,30 @@ static void parse_section_param(char *line, int *isFirstParam, ...@@ -289,30 +289,30 @@ static void parse_section_param(char *line, int *isFirstParam,
* *
* @param params Structure that holds the parameters * @param params Structure that holds the parameters
* @param name Name of the parameter to be found * @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]; char str[PARSER_MAX_LINE_SIZE];
int retParam = 0;
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.*/ /*strcmp returns 0 if both strings are the same.*/
if (!strcmp(name, params->data[i].name)) { if (!strcmp(name, params->data[i].name)) {
/* Check that exactly one number is parsed. */ /* 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( error(
"Tried parsing int '%s' but found '%s' with illegal integer " "Tried parsing int '%s' but found '%s' with illegal integer "
"characters '%s'.", "characters '%s'.",
params->data[i].name, params->data[i].value, str); params->data[i].name, params->data[i].value, str);
} }
return; return retParam;
} }
} }
error("Cannot find '%s' in the structure.", name); 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, ...@@ -320,30 +320,31 @@ void parser_get_param_int(const struct swift_params *params, char *name,
* *
* @param params Structure that holds the parameters * @param params Structure that holds the parameters
* @param name Name of the parameter to be found * @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]; char str[PARSER_MAX_LINE_SIZE];
float retParam = 0.f;
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.*/ /*strcmp returns 0 if both strings are the same.*/
if (!strcmp(name, params->data[i].name)) { if (!strcmp(name, params->data[i].name)) {
/* Check that exactly one number is parsed. */ /* 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( error(
"Tried parsing float '%s' but found '%s' with illegal float " "Tried parsing float '%s' but found '%s' with illegal float "
"characters '%s'.", "characters '%s'.",
params->data[i].name, params->data[i].value, str); params->data[i].name, params->data[i].value, str);
} }
return; return retParam;
} }
} }
error("Cannot find '%s' in the structure.", name); 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, ...@@ -351,30 +352,30 @@ void parser_get_param_float(const struct swift_params *params, char *name,
* *
* @param params Structure that holds the parameters * @param params Structure that holds the parameters
* @param name Name of the parameter to be found * @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]; char str[PARSER_MAX_LINE_SIZE];
double retParam = 0.;
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.*/ /*strcmp returns 0 if both strings are the same.*/
if (!strcmp(name, params->data[i].name)) { if (!strcmp(name, params->data[i].name)) {
/* Check that exactly one number is parsed. */ /* 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( error(
"Tried parsing double '%s' but found '%s' with illegal double " "Tried parsing double '%s' but found '%s' with illegal double "
"characters '%s'.", "characters '%s'.",
params->data[i].name, params->data[i].value, str); params->data[i].name, params->data[i].value, str);
} }
return retParam;
return;
} }
} }
error("Cannot find '%s' in the structure.", name); 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, ...@@ -382,12 +383,10 @@ void parser_get_param_double(const struct swift_params *params, char *name,
* *
* @param params Structure that holds the parameters * @param params Structure that holds the parameters
* @param name Name of the parameter to be found * @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,
void parser_get_param_string(const struct swift_params *params, char *name, const char *name, char *retParam) {
char *retParam) {
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.*/ /*strcmp returns 0 if both strings are the same.*/
if (!strcmp(name, params->data[i].name)) { if (!strcmp(name, params->data[i].name)) {
...@@ -425,7 +424,7 @@ void parser_print_params(const struct swift_params *params) { ...@@ -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) { const char *file_name) {
FILE *file = fopen(file_name, "w"); FILE *file = fopen(file_name, "w");
char section[PARSER_MAX_LINE_SIZE]; char section[PARSER_MAX_LINE_SIZE];
......
...@@ -43,13 +43,12 @@ void parser_read_file(const char *file_name, struct swift_params *params); ...@@ -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_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); const char *file_name);
void parser_get_param_int(const struct swift_params *params, char *name, int parser_get_param_int(const struct swift_params *params, const char *name);
int *retParam); float parser_get_param_float(const struct swift_params *params,
void parser_get_param_float(const struct swift_params *params, char *name, const char *name);
float *retParam); double parser_get_param_double(const struct swift_params *params,
void parser_get_param_double(const struct swift_params *params, char *name, const char *name);
double *retParam); void parser_get_param_string(const struct swift_params *params,
void parser_get_param_string(const struct swift_params *params, char *name, const char *name, char *retParam);
char *retParam);
#endif /* SWIFT_PARSER_H */ #endif /* SWIFT_PARSER_H */
...@@ -49,18 +49,17 @@ ...@@ -49,18 +49,17 @@
void initUnitSystem(struct UnitSystem* us, const struct swift_params* params) { void initUnitSystem(struct UnitSystem* us, const struct swift_params* params) {
parser_get_param_double(params, "UnitSystem:UnitMass_in_cgs", us->UnitMass_in_cgs =
&us->UnitMass_in_cgs); parser_get_param_double(params, "UnitSystem:UnitMass_in_cgs");
parser_get_param_double(params, "UnitSystem:UnitLength_in_cgs", us->UnitLength_in_cgs =
&us->UnitLength_in_cgs); parser_get_param_double(params, "UnitSystem:UnitLength_in_cgs");
double unitVelocity; const double unitVelocity =
parser_get_param_double(params, "UnitSystem:UnitVelocity_in_cgs", parser_get_param_double(params, "UnitSystem:UnitVelocity_in_cgs");
&unitVelocity);
us->UnitTime_in_cgs = us->UnitLength_in_cgs / unitVelocity; us->UnitTime_in_cgs = us->UnitLength_in_cgs / unitVelocity;
parser_get_param_double(params, "UnitSystem:UnitCurrent_in_cgs", us->UnitCurrent_in_cgs =
&us->UnitCurrent_in_cgs); parser_get_param_double(params, "UnitSystem:UnitCurrent_in_cgs");
parser_get_param_double(params, "UnitSystem:UnitTemp_in_cgs", us->UnitTemperature_in_cgs =
&us->UnitTemperature_in_cgs); parser_get_param_double(params, "UnitSystem:UnitTemp_in_cgs");
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment