Commit 297e8813 authored by Peter W. Draper's avatar Peter W. Draper
Browse files

Convert _array function into a generic macro and add support for the other simple types

parent 5eee032d
...@@ -1957,7 +1957,7 @@ MACRO_EXPANSION = YES ...@@ -1957,7 +1957,7 @@ MACRO_EXPANSION = YES
# The default value is: NO. # The default value is: NO.
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES. # This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
EXPAND_ONLY_PREDEF = YES EXPAND_ONLY_PREDEF = NO
# If the SEARCH_INCLUDES tag is set to YES, the include files in the # If the SEARCH_INCLUDES tag is set to YES, the include files in the
# INCLUDE_PATH will be searched if a #include is found. # INCLUDE_PATH will be searched if a #include is found.
......
...@@ -550,63 +550,6 @@ float parser_get_param_float(const struct swift_params *params, ...@@ -550,63 +550,6 @@ float parser_get_param_float(const struct swift_params *params,
return 0.f; return 0.f;
} }
/**
* @brief Retrieve float array parameter from structure.
*
* @param params Structure that holds the parameters
* @param name Name of the parameter to be found
* @param required whether it is an error if the parameter has not been set
* @param nval number of values expected.
* @param values Values of the parameter found, of size at least nvals.
* @return whether the parameter has been found.
*/
int parser_get_param_float_array(const struct swift_params *params,
const char *name, int required,
int nval, float *values) {
char str[PARSER_MAX_LINE_SIZE];
char cpy[PARSER_MAX_LINE_SIZE];
char *cp = cpy;
for (int i = 0; i < params->paramCount; i++) {
if (!strcmp(name, params->data[i].name)) {
strcpy(cp, params->data[i].value);
cp = trim_both(cp);
/* Strip off []. */
if (cp[0] != '[')
error("Array '%s' does not start with '['", name);
cp++;
int l = strlen(cp);
if (cp[l-1] != ']')
error("Array '%s' does not end with ']'", name);
cp[l-1] = '\0';
cp = trim_both(cp);
/* Parse out values which should now be "v, v, v" with internal
* whitespace variations. */
char *p = strtok(cp, ",");
for (int k = 0; k < nval; k++) {
if (p != NULL) {
if (sscanf(p, " %f%s ", &values[k], str) != 1) {
error("Tried parsing float '%s' but found '%s' with "
"illegal float characters '%s'.", name, p, str);
}
} else {
error("Array '%s' with value '%s' has too few values, expected %d",
name, params->data[i].value, nval);
}
if (k < nval - 1) p = strtok(NULL, ",");
}
return 1;
}
}
if (required)
error("Cannot find '%s' in the structure, in file '%s'.", name,
params->fileName);
return 0;
}
/** /**
* @brief Retrieve double parameter from structure. * @brief Retrieve double parameter from structure.
* *
...@@ -801,6 +744,108 @@ void parser_get_opt_param_string(const struct swift_params *params, ...@@ -801,6 +744,108 @@ void parser_get_opt_param_string(const struct swift_params *params,
strcpy(retParam, def); strcpy(retParam, def);
} }
/* Macro defining functions that get primitive types as simple one-line YAML
* arrays, that is SEC: [v1,v2,v3...] format. TYPE is the data type, float
* etc. FMT a format to parse a single value with the possibility of
* extraneous characters after the value, so " %f%s " for a float (spaces can
* be eaten so safest to have one at beginning and end) and DESC the type
* description i.e. "float".
*/
#define PARSER_GET_ARRAY(TYPE, FMT, DESC) \
int parser_get_param_##TYPE##_array(const struct swift_params *params,\
const char *name, int required, \
int nval, TYPE *values) { \
char str[PARSER_MAX_LINE_SIZE]; \
char cpy[PARSER_MAX_LINE_SIZE]; \
char *cp = cpy; \
\
for (int i = 0; i < params->paramCount; i++) { \
if (!strcmp(name, params->data[i].name)) { \
strcpy(cp, params->data[i].value); \
cp = trim_both(cp); \
\
/* Strip off []. */ \
if (cp[0] != '[') \
error("Array '%s' does not start with '['", name); \
cp++; \
int l = strlen(cp); \
if (cp[l-1] != ']') \
error("Array '%s' does not end with ']'", name); \
cp[l-1] = '\0'; \
cp = trim_both(cp); \
\
/* Parse out values which should now be "v, v, v" with \
* internal whitespace variations. */ \
char *p = strtok(cp, ","); \
for (int k = 0; k < nval; k++) { \
if (p != NULL) { \
if (sscanf(p, FMT, &values[k], str) != 1) { \
error("Tried parsing " DESC " '%s' but found '%s' with " \
"illegal " DESC " characters '%s'.", name, p, str); \
} \
} else { \
error("Array '%s' with value '%s' has too few values, " \
"expected %d", name, params->data[i].value, nval); \
} \
if (k < nval - 1) p = strtok(NULL, ","); \
} \
return 1; \
} \
} \
if (required) \
error("Cannot find '%s' in the structure, in file '%s'.", name, \
params->fileName); \
return 0; \
}
/**
* @brief Retrieve int char parameter from structure.
*
* @param params Structure that holds the parameters
* @param name Name of the parameter to be found
* @param required whether it is an error if the parameter has not been set
* @param nval number of values expected.
* @param values Values of the parameter found, of size at least nvals.
* @return whether the parameter has been found.
*/
PARSER_GET_ARRAY(char, " %c%s ", "int");
/**
* @brief Retrieve int array parameter from structure.
*
* @param params Structure that holds the parameters
* @param name Name of the parameter to be found
* @param required whether it is an error if the parameter has not been set
* @param nval number of values expected.
* @param values Values of the parameter found, of size at least nvals.
* @return whether the parameter has been found.
*/
PARSER_GET_ARRAY(int, " %d%s ", "int");
/**
* @brief Retrieve float array parameter from structure.
*
* @param params Structure that holds the parameters
* @param name Name of the parameter to be found
* @param required whether it is an error if the parameter has not been set
* @param nval number of values expected.
* @param values Values of the parameter found, of size at least nvals.
* @return whether the parameter has been found.
*/
PARSER_GET_ARRAY(float, " %f%s ", "float");
/**
* @brief Retrieve double array parameter from structure.
*
* @param params Structure that holds the parameters
* @param name Name of the parameter to be found
* @param required whether it is an error if the parameter has not been set
* @param nval number of values expected.
* @param values Values of the parameter found, of size at least nvals.
* @return whether the parameter has been found.
*/
PARSER_GET_ARRAY(double, " %lf%s ", "double");
/** /**
* @brief Prints the contents of the parameter structure. * @brief Prints the contents of the parameter structure.
* *
......
...@@ -67,9 +67,6 @@ int parser_get_param_int(const struct swift_params *params, const char *name); ...@@ -67,9 +67,6 @@ int parser_get_param_int(const struct swift_params *params, const char *name);
float parser_get_param_float(const struct swift_params *params, float parser_get_param_float(const struct swift_params *params,
const char *name); const char *name);
int parser_get_param_float_array(const struct swift_params *params,
const char *name, int required,
int nval, float *values);
double parser_get_param_double(const struct swift_params *params, double parser_get_param_double(const struct swift_params *params,
const char *name); const char *name);
...@@ -88,6 +85,19 @@ void parser_get_opt_param_string(const struct swift_params *params, ...@@ -88,6 +85,19 @@ void parser_get_opt_param_string(const struct swift_params *params,
const char *name, char *retParam, const char *name, char *retParam,
const char *def); const char *def);
int parser_get_param_char_array(const struct swift_params *params,
const char *name, int required,
int nval, char *values);
int parser_get_param_int_array(const struct swift_params *params,
const char *name, int required,
int nval, int *values);
int parser_get_param_float_array(const struct swift_params *params,
const char *name, int required,
int nval, float *values);
int parser_get_param_double_array(const struct swift_params *params,
const char *name, int required,
int nval, double *values);
#if defined(HAVE_HDF5) #if defined(HAVE_HDF5)
void parser_write_params_to_hdf5(const struct swift_params *params, hid_t grp); void parser_write_params_to_hdf5(const struct swift_params *params, hid_t grp);
#endif #endif
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment