From 879d526d7ee7ee7a876f284dac76075e7a7267ae Mon Sep 17 00:00:00 2001 From: katherine Date: Mon, 27 May 2019 18:45:21 -0700 Subject: move stores vals to named union for c99 compatibility --- README.md | 16 ++++++++-------- doc/example.c | 14 +++++++------- doc/interface.md | 20 ++++++++++---------- simple-opt.h | 38 +++++++++++++++++++------------------- 4 files changed, 44 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 4dc9861..a00b364 100644 --- a/README.md +++ b/README.md @@ -83,32 +83,32 @@ int main(int argc, char **argv) if (options[i].arg_is_stored) { switch (options[i].type) { case SIMPLE_OPT_BOOL: - printf(", val: %s", options[i].val_bool ? "true" : "false"); + printf(", val: %s", options[i].val.v_bool ? "true" : "false"); break; case SIMPLE_OPT_INT: - printf(", val: %ld", options[i].val_int); + printf(", val: %ld", options[i].val.v_int); break; case SIMPLE_OPT_UNSIGNED: - printf(", val: %lu", options[i].val_unsigned); + printf(", val: %lu", options[i].val.v_unsigned); break; case SIMPLE_OPT_DOUBLE: - printf(", val: %lf", options[i].val_double); + printf(", val: %lf", options[i].val.v_double); break; case SIMPLE_OPT_CHAR: - printf(", val: %c", options[i].val_char); + printf(", val: %c", options[i].val.v_char); break; case SIMPLE_OPT_STRING: - printf(", val: %s", options[i].val_string); + printf(", val: %s", options[i].val.v_string); break; case SIMPLE_OPT_STRING_SET: printf(", val: %s", - options[i].string_set[options[i].val_string_set_idx]); + options[i].string_set[options[i].val.v_string_set_idx]); break; default: @@ -142,7 +142,7 @@ by `simple_opt_print_usage`. the end of the array must be indicated with an option of type `SIMPLE_OPT_END`. this array is passed to `simple_opt_parse` and, if the parsing is successful, -relevant values (`was_seen`, `arg_is_stored`, `val_`) are in-place stored +relevant values (`was_seen`, `arg_is_stored`, `val.v_`) are in-place stored in the options. otherwise, the returned `struct simple_opt_result` will have a type other than `SIMPLE_OPT_RESULT_SUCCESS`, in which case error reporting occurs. diff --git a/doc/example.c b/doc/example.c index 324473e..71ce24b 100644 --- a/doc/example.c +++ b/doc/example.c @@ -63,32 +63,32 @@ int main(int argc, char **argv) if (options[i].arg_is_stored) { switch (options[i].type) { case SIMPLE_OPT_BOOL: - printf(", val: %s", options[i].val_bool ? "true" : "false"); + printf(", val: %s", options[i].val.v_bool ? "true" : "false"); break; case SIMPLE_OPT_INT: - printf(", val: %ld", options[i].val_int); + printf(", val: %ld", options[i].val.v_int); break; case SIMPLE_OPT_UNSIGNED: - printf(", val: %lu", options[i].val_unsigned); + printf(", val: %lu", options[i].val.v_unsigned); break; case SIMPLE_OPT_DOUBLE: - printf(", val: %lf", options[i].val_double); + printf(", val: %lf", options[i].val.v_double); break; case SIMPLE_OPT_CHAR: - printf(", val: %c", options[i].val_char); + printf(", val: %c", options[i].val.v_char); break; case SIMPLE_OPT_STRING: - printf(", val: %s", options[i].val_string); + printf(", val: %s", options[i].val.v_string); break; case SIMPLE_OPT_STRING_SET: printf(", val: %s", - options[i].string_set[options[i].val_string_set_idx]); + options[i].string_set[options[i].val.v_string_set_idx]); break; default: diff --git a/doc/interface.md b/doc/interface.md index f8d5886..978ecb3 100644 --- a/doc/interface.md +++ b/doc/interface.md @@ -52,21 +52,21 @@ the fields which are set by `simple_opt_parse` are: bool arg_is_stored; union { - bool val_bool; - long val_int; - unsigned long val_unsigned; - double val_double; - char val_char; - char val_string[SIMPLE_OPT_OPT_ARG_MAX_WIDTH]; - int val_string_set_idx; - }; + bool v_bool; + long v_int; + unsigned long v_unsigned; + double v_double; + char v_char; + char v_string[SIMPLE_OPT_OPT_ARG_MAX_WIDTH]; + int v_string_set_idx; + } val; ``` `was_seen` indicates if this option was encountered during parsing, -`arg_is_stored` if an argument was passed to the option, and the `val_` +`arg_is_stored` if an argument was passed to the option, and the `val.v_` fields contain the value passed (with the correct field to set being determined by the `type` field shown above) for all but `SIMPLE_OPT_STRING_SET`, for which -`val_string_set_idx` is set, an index into the `string_set` field's array, +`val.v_string_set_idx` is set, an index into the `string_set` field's array, indicating which possibility was matched. options of the following types: diff --git a/simple-opt.h b/simple-opt.h index 90b0b8d..fbd2d83 100644 --- a/simple-opt.h +++ b/simple-opt.h @@ -63,14 +63,14 @@ struct simple_opt { bool arg_is_stored; union { - bool val_bool; - long val_int; - unsigned long val_unsigned; - double val_double; - char val_char; - char val_string[SIMPLE_OPT_OPT_ARG_MAX_WIDTH]; - int val_string_set_idx; - }; + bool v_bool; + long v_int; + unsigned long v_unsigned; + double v_double; + char v_char; + char v_string[SIMPLE_OPT_OPT_ARG_MAX_WIDTH]; + int v_string_set_idx; + } val; }; enum simple_opt_result_type { @@ -153,9 +153,9 @@ loop: strmatch_out: if (match) { if (i < 3) - o->val_bool = true; + o->val.v_bool = true; else - o->val_bool = false; + o->val.v_bool = false; return true; } @@ -166,7 +166,7 @@ strmatch_out: case SIMPLE_OPT_INT: errno = 0; - o->val_int = strtol(s, &cp, 0); + o->val.v_int = strtol(s, &cp, 0); if (cp == s || *cp != '\0' || errno) return false; @@ -178,7 +178,7 @@ strmatch_out: return false; errno = 0; - o->val_unsigned = strtoul(s, &cp, 0); + o->val.v_unsigned = strtoul(s, &cp, 0); if (cp == s || *cp != '\0' || errno) return false; @@ -187,7 +187,7 @@ strmatch_out: case SIMPLE_OPT_DOUBLE: errno = 0; - o->val_double = strtod(s, &cp); + o->val.v_double = strtod(s, &cp); if (cp == s || *cp != '\0' || errno) return false; @@ -198,20 +198,20 @@ strmatch_out: if (strlen(s) != 1) return false; - o->val_char = s[0]; + o->val.v_char = s[0]; return true; case SIMPLE_OPT_STRING: if (strlen(s) + 1 >= SIMPLE_OPT_OPT_ARG_MAX_WIDTH) return false; - strcpy(o->val_string, s); + strcpy(o->val.v_string, s); return true; case SIMPLE_OPT_STRING_SET: for (i = 0; o->string_set[i] != NULL; i++) { if (!strcmp(s, o->string_set[i])) { - o->val_string_set_idx = i; + o->val.v_string_set_idx = i; return true; } } @@ -284,9 +284,9 @@ static struct simple_opt_result simple_opt_parse(int argc, char **argv, && options[j].short_name != '\0' && options[i].short_name == options[j].short_name) || ( options[i].long_name != NULL - && options[j].long_name != NULL - && !strcmp(options[i].long_name, options[j].long_name)) - ) + && options[j].long_name != NULL + && !strcmp(options[i].long_name, options[j].long_name)) + ) ) { r.result_type = SIMPLE_OPT_RESULT_MALFORMED_OPTION_STRUCT; goto end; -- cgit v1.2.3