Skip to content

Commit

Permalink
Support reading long long parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
wullm committed Apr 23, 2021
1 parent d41dfe9 commit 9a50cdd
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
74 changes: 74 additions & 0 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@

#define CHUNK 10

/* Alias of long long needed and used only for the preprocessor definitions */
typedef long long longlong;

/* Private functions. */
static int is_empty(const char *str);
static int count_indentation(const char *str);
Expand Down Expand Up @@ -601,10 +604,12 @@ PARSER_GET_VALUE(char, "%c", "char");
PARSER_GET_VALUE(int, "%d", "int");
PARSER_GET_VALUE(float, "%f", "float");
PARSER_GET_VALUE(double, "%lf", "double");
PARSER_GET_VALUE(longlong, "%lld", "long long");
PARSER_SAVE_VALUE(char, char, "%c");
PARSER_SAVE_VALUE(int, int, "%d");
PARSER_SAVE_VALUE(float, float, "%g");
PARSER_SAVE_VALUE(double, double, "%g");
PARSER_SAVE_VALUE(longlong, longlong, "%lld");
PARSER_SAVE_VALUE(string, const char *, "%s");

/**
Expand Down Expand Up @@ -659,6 +664,20 @@ double parser_get_param_double(struct swift_params *params, const char *name) {
return result;
}

/**
* @brief Retrieve long long parameter from structure.
*
* @param params Structure that holds the parameters
* @param name Name of the parameter to be found
* @return Value of the parameter found
*/
long long parser_get_param_longlong(struct swift_params *params,
const char *name) {
long long result = 0;
get_param_longlong(params, name, NULL, &result);
return result;
}

/**
* @brief Retrieve string parameter from structure.
*
Expand Down Expand Up @@ -754,6 +773,23 @@ double parser_get_opt_param_double(struct swift_params *params,
return def;
}

/**
* @brief Retrieve optional long long parameter from structure.
*
* @param params Structure that holds the parameters
* @param name Name of the parameter to be found
* @param def Default value of the parameter of not found.
* @return Value of the parameter found
*/
long long parser_get_opt_param_longlong(struct swift_params *params,
const char *name, long long def) {
long long result = 0;
if (get_param_longlong(params, name, &def, &result)) return result;
save_param_longlong(params, name, def);
params->data[params->paramCount - 1].is_default = 1;
return def;
}

/**
* @brief Retrieve string parameter from structure.
*
Expand Down Expand Up @@ -877,10 +913,12 @@ PARSER_GET_ARRAY(char, "%c", "char");
PARSER_GET_ARRAY(int, "%d", "int");
PARSER_GET_ARRAY(float, "%f", "float");
PARSER_GET_ARRAY(double, "%lf", "double");
PARSER_GET_ARRAY(longlong, "%lld", "long long");
PARSER_SAVE_ARRAY(char, "%c");
PARSER_SAVE_ARRAY(int, "%d");
PARSER_SAVE_ARRAY(float, "%g");
PARSER_SAVE_ARRAY(double, "%g");
PARSER_SAVE_ARRAY(longlong, "%lld");

/**
* @brief Retrieve char array parameter from structure.
Expand Down Expand Up @@ -1020,6 +1058,42 @@ int parser_get_opt_param_double_array(struct swift_params *params,
return 1;
}

/**
* @brief Retrieve long long array parameter from structure.
*
* @param params Structure that holds the parameters
* @param name Name of the parameter to be found
* @param nval number of values expected.
* @param values Values of the parameter found, of size at least nvals.
*/
void parser_get_param_longlong_array(struct swift_params *params,
const char *name, int nval,
long long *values) {
get_param_longlong_array(params, name, 1, nval, values);
}

/**
* @brief Retrieve optional long long array parameter from structure.
*
* @param params Structure that holds the parameters
* @param name Name of the parameter to be found
* @param nval number of values expected.
* @param values Values of the parameter found, of size at least nvals. If the
* parameter is not found these values will be returned
* unmodified, so should be set to the default values.
* @return whether the parameter has been found.
*/
int parser_get_opt_param_longlong_array(struct swift_params *params,
const char *name, int nval,
long long *values) {
if (get_param_longlong_array(params, name, 0, nval, values) != 1) {
save_param_longlong_array(params, name, nval, values);
params->data[params->paramCount - 1].is_default = 1;
return 0;
}
return 1;
}

/**
* @brief Retrieve string array parameter from structure.
*
Expand Down
12 changes: 10 additions & 2 deletions src/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ char parser_get_param_char(struct swift_params *params, const char *name);
int parser_get_param_int(struct swift_params *params, const char *name);
float parser_get_param_float(struct swift_params *params, const char *name);
double parser_get_param_double(struct swift_params *params, const char *name);
long long parser_get_param_longlong(struct swift_params *params,
const char *name);
void parser_get_param_string(struct swift_params *params, const char *name,
char *retParam);

Expand All @@ -81,6 +83,8 @@ float parser_get_opt_param_float(struct swift_params *params, const char *name,
float def);
double parser_get_opt_param_double(struct swift_params *params,
const char *name, double def);
long long parser_get_opt_param_longlong(struct swift_params *params,
const char *name, long long def);
void parser_get_opt_param_string(struct swift_params *params, const char *name,
char *retParam, const char *def);
void parser_get_param_char_array(struct swift_params *params, const char *name,
Expand All @@ -91,7 +95,9 @@ void parser_get_param_float_array(struct swift_params *params, const char *name,
int nval, float *values);
void parser_get_param_double_array(struct swift_params *params,
const char *name, int nval, double *values);

void parser_get_param_longlong_array(struct swift_params *params,
const char *name, int nval,
long long *values);
int parser_get_opt_param_char_array(struct swift_params *params,
const char *name, int nval, char *values);
int parser_get_opt_param_int_array(struct swift_params *params,
Expand All @@ -101,7 +107,9 @@ int parser_get_opt_param_float_array(struct swift_params *params,
int parser_get_opt_param_double_array(struct swift_params *params,
const char *name, int nval,
double *values);

int parser_get_opt_param_longlong_array(struct swift_params *params,
const char *name, int nval,
long long *values);
void parser_get_param_string_array(struct swift_params *params,
const char *name, int *nval, char ***values);
int parser_get_opt_param_string_array(struct swift_params *params,
Expand Down
4 changes: 4 additions & 0 deletions tests/testParser.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,5 +228,9 @@ int main(int argc, char *argv[]) {
"Untitled SWIFT simulation");
assert(strcmp(run_name_1, run_name_2) == 0);

/* Read a very big number */
long long bignum = parser_get_param_longlong(&param_file, "LongLong:bignum");
assert(bignum == 1152921504606846975LL);

return 0;
}
3 changes: 3 additions & 0 deletions tests/testParserInput.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ IO:
#Input file
ic_file: ic_file.ini

LongLong:
bignum: 1152921504606846975 # 2^60-1

Words:
list: ['xyz', 'ABC', "ab'c", "de:f", "g,hi", "zzz", Hello World, once-again]
nonstdlist: 'xyz', 'ABC', "ab'c", "de:f", "g,hi", "zzz", Hello World, once-again
Expand Down

0 comments on commit 9a50cdd

Please sign in to comment.