Commit 1e8800ee authored by James Willis's avatar James Willis
Browse files

Merge branch 'master' into swift-velociraptor-C

parents 713e5db0 71cc1cc5
......@@ -258,6 +258,7 @@ void parser_set_param(struct swift_params *params, const char *namevalue) {
strcpy(params->data[params->paramCount].name, name);
strcpy(params->data[params->paramCount].value, value);
params->data[params->paramCount].used = 0;
params->data[params->paramCount].is_default = 0;
params->paramCount++;
if (params->paramCount == PARSER_MAX_NO_OF_PARAMS)
error("Too many parameters, current maximum is %d.", params->paramCount);
......@@ -451,6 +452,7 @@ static void parse_value(char *line, struct swift_params *params) {
strcpy(params->data[params->paramCount].name, tmpStr);
strcpy(params->data[params->paramCount].value, token);
params->data[params->paramCount].used = 0;
params->data[params->paramCount].is_default = 0;
if (params->paramCount == PARSER_MAX_NO_OF_PARAMS - 1) {
error(
"Maximal number of parameters in parameter file reached. Aborting "
......@@ -511,6 +513,7 @@ static void parse_section_param(char *line, int *isFirstParam,
strcpy(params->data[params->paramCount].name, paramName);
strcpy(params->data[params->paramCount].value, token);
params->data[params->paramCount].used = 0;
params->data[params->paramCount].is_default = 0;
if (params->paramCount == PARSER_MAX_NO_OF_PARAMS - 1) {
error("Maximal number of parameters in parameter file reached. Aborting !");
} else {
......@@ -523,7 +526,7 @@ static void parse_section_param(char *line, int *isFirstParam,
// a one word description of the type, "float", "int" etc.
#define PARSER_GET_VALUE(TYPE, FMT, DESC) \
static int get_param_##TYPE(struct swift_params *params, const char *name, \
int required, TYPE *result) { \
TYPE *def, TYPE *result) { \
char str[PARSER_MAX_LINE_SIZE]; \
for (int i = 0; i < params->paramCount; i++) { \
if (strcmp(name, params->data[i].name) == 0) { \
......@@ -534,12 +537,20 @@ static void parse_section_param(char *line, int *isFirstParam,
"illegal trailing characters '%s'.", \
params->data[i].name, params->data[i].value, str); \
} \
/* Ensure same behavior if called multiple times for same parameter */\
if (params->data[i].is_default && def == NULL) \
error("Tried parsing %s again but cannot parse a default " \
"parameter as mandatory", name); \
if (params->data[i].is_default && *def != *result) \
error("Tried parsing %s again but cannot parse a parameter with " \
"two different default value (" FMT "!=" FMT ")", \
name, *def, *result); \
/* This parameter has been used */ \
params->data[i].used = 1; \
return 1; \
} \
} \
if (required) \
if (def == NULL) \
error("Cannot find '%s' in the structure, in file '%s'.", name, \
params->fileName); \
return 0; \
......@@ -553,6 +564,7 @@ static void parse_section_param(char *line, int *isFirstParam,
sprintf(str, "%s: " FMT, name, value); \
parser_set_param(params, str); \
params->data[params->paramCount - 1].used = 1; \
params->data[params->paramCount - 1].is_default = 0; \
}
/* Instantiations. */
......@@ -575,7 +587,7 @@ PARSER_SAVE_VALUE(string, const char *, "%s");
*/
int parser_get_param_int(struct swift_params *params, const char *name) {
int result = 0;
get_param_int(params, name, 1, &result);
get_param_int(params, name, NULL, &result);
return result;
}
......@@ -588,7 +600,7 @@ int parser_get_param_int(struct swift_params *params, const char *name) {
*/
char parser_get_param_char(struct swift_params *params, const char *name) {
char result = 0;
get_param_char(params, name, 1, &result);
get_param_char(params, name, NULL, &result);
return result;
}
......@@ -601,7 +613,7 @@ char parser_get_param_char(struct swift_params *params, const char *name) {
*/
float parser_get_param_float(struct swift_params *params, const char *name) {
float result = 0;
get_param_float(params, name, 1, &result);
get_param_float(params, name, NULL, &result);
return result;
}
......@@ -614,7 +626,7 @@ float parser_get_param_float(struct swift_params *params, const char *name) {
*/
double parser_get_param_double(struct swift_params *params, const char *name) {
double result = 0;
get_param_double(params, name, 1, &result);
get_param_double(params, name, NULL, &result);
return result;
}
......@@ -630,6 +642,9 @@ void parser_get_param_string(struct swift_params *params, const char *name,
for (int i = 0; i < params->paramCount; i++) {
if (!strcmp(name, params->data[i].name)) {
if (params->data[i].is_default)
error("Tried parsing %s again but cannot parse a "
"default parameter as mandatory", name);
strcpy(retParam, params->data[i].value);
/* this parameter has been used */
params->data[i].used = 1;
......@@ -651,8 +666,9 @@ void parser_get_param_string(struct swift_params *params, const char *name,
int parser_get_opt_param_int(struct swift_params *params, const char *name,
int def) {
int result = 0;
if (get_param_int(params, name, 0, &result)) return result;
if (get_param_int(params, name, &def, &result)) return result;
save_param_int(params, name, def);
params->data[params->paramCount - 1].is_default = 1;
return def;
}
......@@ -667,8 +683,9 @@ int parser_get_opt_param_int(struct swift_params *params, const char *name,
char parser_get_opt_param_char(struct swift_params *params, const char *name,
char def) {
char result = 0;
if (get_param_char(params, name, 0, &result)) return result;
if (get_param_char(params, name, &def, &result)) return result;
save_param_char(params, name, def);
params->data[params->paramCount - 1].is_default = 1;
return def;
}
......@@ -683,8 +700,9 @@ char parser_get_opt_param_char(struct swift_params *params, const char *name,
float parser_get_opt_param_float(struct swift_params *params, const char *name,
float def) {
float result = 0;
if (get_param_float(params, name, 0, &result)) return result;
if (get_param_float(params, name, &def, &result)) return result;
save_param_float(params, name, def);
params->data[params->paramCount - 1].is_default = 1;
return def;
}
......@@ -699,8 +717,9 @@ float parser_get_opt_param_float(struct swift_params *params, const char *name,
double parser_get_opt_param_double(struct swift_params *params,
const char *name, double def) {
double result = 0;
if (get_param_double(params, name, 0, &result)) return result;
if (get_param_double(params, name, &def, &result)) return result;
save_param_double(params, name, def);
params->data[params->paramCount - 1].is_default = 1;
return def;
}
......@@ -719,12 +738,18 @@ void parser_get_opt_param_string(struct swift_params *params, const char *name,
if (!strcmp(name, params->data[i].name)) {
strcpy(retParam, params->data[i].value);
/* Ensure same behavior if called multiple times for same parameter */
if (params->data[i].is_default && !strcmp(def, retParam))
error("Tried parsing %s again but cannot parse a parameter with "
"two different default value ('%s' != '%s')",
name, def, retParam);
/* this parameter has been used */
params->data[i].used = 1;
return;
}
}
save_param_string(params, name, def);
params->data[params->paramCount - 1].is_default = 1;
strcpy(retParam, def);
}
......@@ -743,6 +768,9 @@ void parser_get_opt_param_string(struct swift_params *params, const char *name,
\
for (int i = 0; i < params->paramCount; i++) { \
if (!strcmp(name, params->data[i].name)) { \
if (params->data[i].is_default && required) \
error("Tried parsing %s again but cannot parse a default " \
"parameter as mandatory", name); \
char *cp = cpy; \
strcpy(cp, params->data[i].value); \
cp = trim_both(cp); \
......@@ -761,13 +789,19 @@ void parser_get_opt_param_string(struct swift_params *params, const char *name,
* 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) { \
if (p != NULL) { \
TYPE tmp_value; \
if (sscanf(p, fmt, &tmp_value, str) != 1) { \
error("Tried parsing " DESC \
" '%s' but found '%s' with " \
"illegal " DESC " characters '%s'.", \
name, p, str); \
} \
} \
if (params->data[i].is_default && tmp_value != values[k]) \
error("Tried parsing %s again but cannot parse a " \
"parameter with two different default value " \
"(" FMT "!=" FMT ")", name, tmp_value, values[k]);\
values[k] = tmp_value; \
} else { \
error( \
"Array '%s' with value '%s' has too few values, " \
......@@ -798,6 +832,7 @@ void parser_get_opt_param_string(struct swift_params *params, const char *name,
sprintf(&str[k], FMT "]", values[nval - 1]); \
parser_set_param(params, str); \
params->data[params->paramCount - 1].used = 1; \
params->data[params->paramCount - 1].is_default = 0; \
return 0; \
}
......@@ -839,6 +874,7 @@ int parser_get_opt_param_char_array(struct swift_params *params,
const char *name, int nval, char *values) {
if (get_param_char_array(params, name, 0, nval, values) != 1) {
save_param_char_array(params, name, nval, values);
params->data[params->paramCount - 1].is_default = 1;
return 0;
}
return 1;
......@@ -872,6 +908,7 @@ int parser_get_opt_param_int_array(struct swift_params *params,
const char *name, int nval, int *values) {
if (get_param_int_array(params, name, 0, nval, values) != 1) {
save_param_int_array(params, name, nval, values);
params->data[params->paramCount - 1].is_default = 1;
return 0;
}
return 1;
......@@ -906,6 +943,7 @@ int parser_get_opt_param_float_array(struct swift_params *params,
float *values) {
if (get_param_float_array(params, name, 0, nval, values) != 1) {
save_param_float_array(params, name, nval, values);
params->data[params->paramCount - 1].is_default = 1;
return 0;
}
return 1;
......@@ -940,6 +978,7 @@ int parser_get_opt_param_double_array(struct swift_params *params,
double *values) {
if (get_param_double_array(params, name, 0, nval, values) != 1) {
save_param_double_array(params, name, nval, values);
params->data[params->paramCount - 1].is_default = 1;
return 0;
}
return 1;
......@@ -1041,6 +1080,8 @@ int parser_get_opt_param_string_array(struct swift_params *params,
else
sprintf(&cpy[k], "\"%s\"]", def[i]);
parser_set_param(params, cpy);
params->data[params->paramCount - 1].is_default = 1;
params->data[params->paramCount - 1].used = 1;
/* Now copy to output space. */
char **strings;
......@@ -1052,7 +1093,6 @@ int parser_get_opt_param_string_array(struct swift_params *params,
*values = strings;
*nval = ndef;
params->data[params->paramCount - 1].used = 1;
return 0;
}
......
......@@ -40,6 +40,7 @@ struct parameter {
char name[PARSER_MAX_LINE_SIZE];
char value[PARSER_MAX_LINE_SIZE];
int used;
int is_default;
};
struct section {
......
......@@ -49,9 +49,13 @@ int main(int argc, char *argv[]) {
parser_get_param_double(&param_file, "Simulation:start_time");
const int kernel = parser_get_param_int(&param_file, "kernel");
const int optional =
const int optional1 =
parser_get_opt_param_int(&param_file, "Simulation:optional", 1);
/* Check if we can read it again */
const int optional2 =
parser_get_opt_param_int(&param_file, "Simulation:optional", 1);
/* Optional things not mentioned in parameter file. Should be in output
* files.*/
parser_get_opt_param_int(&param_file, "Virtual:param1", 1);
......@@ -59,23 +63,29 @@ int main(int argc, char *argv[]) {
parser_get_opt_param_int(&param_file, "Virtual:param3", 3);
parser_get_opt_param_int(&param_file, "Virtual:param4", 4);
/* Check if we can read it again */
parser_get_opt_param_int(&param_file, "Virtual:param1", 1);
char ic_file[PARSER_MAX_LINE_SIZE];
parser_get_param_string(&param_file, "IO:ic_file", ic_file);
char csides[3];
parser_get_param_char_array(&param_file, "Box:csides", 3, csides);
int isides[3];
parser_get_param_int_array(&param_file, "Box:isides", 3, isides);
float fsides[3];
parser_get_param_float_array(&param_file, "Box:fsides", 3, fsides);
double dsides[3];
parser_get_param_double_array(&param_file, "Box:dsides", 3, dsides);
int optsides[5] = {1, 2, 3, 4, 5};
int haveopt =
int haveopt1 =
parser_get_opt_param_int_array(&param_file, "Box:moresides", 5, optsides);
/* Check if we can read it again */
int haveopt2 =
parser_get_opt_param_int_array(&param_file, "Box:moresides", 5, optsides);
char **var_result;
......@@ -101,7 +111,11 @@ int main(int argc, char *argv[]) {
const char *optwords[4] = {"Word1", "Word2", "Word3", "Word4"};
int noptwords = 4;
int haveoptwords = parser_get_opt_param_string_array(
int haveoptwords1 = parser_get_opt_param_string_array(
&param_file, "Simulation:optwords", &nvar_result, &var_result, noptwords,
optwords);
/* Check if we can read it again */
int haveoptwords2 = parser_get_opt_param_string_array(
&param_file, "Simulation:optwords", &nvar_result, &var_result, noptwords,
optwords);
printf("\nList from Simulation:optwords parameter (%d of %d)\n", nvar_result,
......@@ -137,7 +151,7 @@ int main(int argc, char *argv[]) {
"no_of_threads: %d, no_of_time_steps: %d, max_h: %f, start_time: %lf,"
" ic_file: %s, kernel: %d optional: %d\n",
no_of_threads, no_of_time_steps, max_h, start_time, ic_file, kernel,
optional);
optional1);
printf("Box: [%d, %d, %d]\n", isides[0], isides[1], isides[2]);
/* Print the contents of the structure to a file in YAML format. */
......@@ -150,7 +164,8 @@ int main(int argc, char *argv[]) {
assert(fabs(start_time - 1.23456789) < 0.00001);
assert(strcmp(ic_file, "ic_file.ini") == 0); /*strcmp returns 0 if correct.*/
assert(kernel == 4);
assert(optional == 1);
assert(optional1 == 1);
assert(optional2 == 1);
assert(csides[0] == 'a');
assert(csides[1] == 'b');
......@@ -168,14 +183,16 @@ int main(int argc, char *argv[]) {
assert(dsides[1] == 3.0);
assert(dsides[2] == 4.0);
assert(haveopt == 0);
assert(haveopt1 == 0);
assert(haveopt2 == 1);
assert(optsides[0] == 1);
assert(optsides[1] == 2);
assert(optsides[2] == 3);
assert(optsides[3] == 4);
assert(optsides[4] == 5);
assert(haveoptwords == 0);
assert(haveoptwords1 == 0);
assert(haveoptwords2 == 1);
return 0;
}
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