From: Philippe Proulx Date: Mon, 31 May 2021 20:12:07 +0000 (-0400) Subject: Make `struct argpar_item` opaque X-Git-Url: http://git.efficios.com/?p=argpar.git;a=commitdiff_plain;h=d4539a906f9b4114a3f0a40e33598cf8077767c2 Make `struct argpar_item` opaque This patch improves the encapsulation of argpar by making `struct argpar_item` opaque, and publicly removing `struct argpar_item_opt` and `struct argpar_item_non_opt`. Signed-off-by: Philippe Proulx Change-Id: I37ef6c386593e2bb8f817bf4d2bb464e9004615e --- diff --git a/argpar/argpar.c b/argpar/argpar.c index 4458d9a..1ee5539 100644 --- a/argpar/argpar.c +++ b/argpar/argpar.c @@ -63,6 +63,42 @@ struct argpar_iter { const char *short_opt_ch; }; +/* Base parsing item */ +struct argpar_item { + enum argpar_item_type type; +}; + +/* Option parsing item */ +struct argpar_item_opt { + struct argpar_item base; + + /* Corresponding descriptor */ + const struct argpar_opt_descr *descr; + + /* Argument, or `NULL` if none; owned by this */ + char *arg; +}; + +/* Non-option parsing item */ +struct argpar_item_non_opt { + struct argpar_item base; + + /* + * Complete argument, pointing to one of the entries of the + * original arguments (`argv`). + */ + const char *arg; + + /* + * Index of this argument amongst all original arguments + * (`argv`). + */ + unsigned int orig_index; + + /* Index of this argument amongst other non-option arguments */ + unsigned int non_opt_index; +}; + static __attribute__((format(ARGPAR_PRINTF_FORMAT, 1, 0))) char *argpar_vasprintf(const char * const fmt, va_list args) { @@ -136,6 +172,56 @@ end: return success; } +ARGPAR_HIDDEN +enum argpar_item_type argpar_item_type(const struct argpar_item * const item) +{ + ARGPAR_ASSERT(item); + return item->type; +} + +ARGPAR_HIDDEN +const struct argpar_opt_descr *argpar_item_opt_descr( + const struct argpar_item * const item) +{ + ARGPAR_ASSERT(item); + ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_OPT); + return ((const struct argpar_item_opt *) item)->descr; +} + +ARGPAR_HIDDEN +const char *argpar_item_opt_arg(const struct argpar_item * const item) +{ + ARGPAR_ASSERT(item); + ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_OPT); + return ((const struct argpar_item_opt *) item)->arg; +} + +ARGPAR_HIDDEN +const char *argpar_item_non_opt_arg(const struct argpar_item * const item) +{ + ARGPAR_ASSERT(item); + ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT); + return ((const struct argpar_item_non_opt *) item)->arg; +} + +ARGPAR_HIDDEN +unsigned int argpar_item_non_opt_orig_index( + const struct argpar_item * const item) +{ + ARGPAR_ASSERT(item); + ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT); + return ((const struct argpar_item_non_opt *) item)->orig_index; +} + +ARGPAR_HIDDEN +unsigned int argpar_item_non_opt_non_opt_index( + const struct argpar_item * const item) +{ + ARGPAR_ASSERT(item); + ARGPAR_ASSERT(item->type == ARGPAR_ITEM_TYPE_NON_OPT); + return ((const struct argpar_item_non_opt *) item)->non_opt_index; +} + ARGPAR_HIDDEN void argpar_item_destroy(const struct argpar_item * const item) { @@ -147,7 +233,7 @@ void argpar_item_destroy(const struct argpar_item * const item) struct argpar_item_opt * const opt_item = (struct argpar_item_opt *) item; - free((void *) opt_item->arg); + free(opt_item->arg); } free((void *) item); diff --git a/argpar/argpar.h b/argpar/argpar.h index a5a82be..1187def 100644 --- a/argpar/argpar.h +++ b/argpar/argpar.h @@ -79,10 +79,10 @@ * Both argpar_iter_create() and argpar_parse() accept duplicate options * (they produce one item for each instance). * - * A returned parsing item has the type `struct argpar_item *`. Each - * item is to be casted to the appropriate type - * (`struct argpar_item_opt *` or `struct argpar_item_non_opt *`) - * depending on its `type` member. + * A returned parsing item has the type `const struct argpar_item *`. + * Get the type (option or non-option) of an item with + * argpar_item_type(). Each item type has its set of dedicated methods + * (`argpar_item_opt_` and `argpar_item_non_opt_` prefixes). * * Both argpar_iter_create() and argpar_parse() produce the items in * the same order that the arguments were parsed, including non-option @@ -144,38 +144,49 @@ enum argpar_item_type { ARGPAR_ITEM_TYPE_NON_OPT, }; -/* Base item */ -struct argpar_item { - enum argpar_item_type type; -}; - -/* Option item */ -struct argpar_item_opt { - struct argpar_item base; +/* Parsing item, as created by argpar_parse() and argpar_iter_parse_next() */ +struct argpar_item; - /* Corresponding descriptor */ - const struct argpar_opt_descr *descr; +/* + * Returns the type of the parsing item `item`. + */ +ARGPAR_HIDDEN +enum argpar_item_type argpar_item_type(const struct argpar_item *item); - /* Argument, or `NULL` if none */ - const char *arg; -}; +/* + * Returns the option descriptor of the option parsing item `item`. + */ +ARGPAR_HIDDEN +const struct argpar_opt_descr *argpar_item_opt_descr( + const struct argpar_item *item); -/* Non-option item */ -struct argpar_item_non_opt { - struct argpar_item base; +/* + * Returns the argument of the option parsing item `item`, or `NULL` if + * none. + */ +ARGPAR_HIDDEN +const char *argpar_item_opt_arg(const struct argpar_item *item); - /* - * Complete argument, pointing to one of the entries of the - * original arguments (`argv`). - */ - const char *arg; +/* + * Returns the complete argument, pointing to one of the entries of the + * original arguments (`argv`), of the non-option parsing item `item`. + */ +ARGPAR_HIDDEN +const char *argpar_item_non_opt_arg(const struct argpar_item *item); - /* Index of this argument amongst all original arguments (`argv`) */ - unsigned int orig_index; +/* + * Returns the original index, within ALL the original arguments + * (`argv`), of the non-option parsing item `item`. + */ +ARGPAR_HIDDEN +unsigned int argpar_item_non_opt_orig_index(const struct argpar_item *item); - /* Index of this argument amongst other non-option arguments */ - unsigned int non_opt_index; -}; +/* + * Returns the index, within the non-option arguments, of the non-option + * parsing item `item`. + */ +ARGPAR_HIDDEN +unsigned int argpar_item_non_opt_non_opt_index(const struct argpar_item *item); struct argpar_item_array { const struct argpar_item **items; diff --git a/tests/test_argpar.c b/tests/test_argpar.c index b04b412..e8b362b 100644 --- a/tests/test_argpar.c +++ b/tests/test_argpar.c @@ -44,27 +44,26 @@ void append_to_res_str(GString * const res_str, g_string_append_c(res_str, ' '); } - switch (item->type) { + switch (argpar_item_type(item)) { case ARGPAR_ITEM_TYPE_OPT: { - const struct argpar_item_opt *const item_opt = - (const void *) item; + const struct argpar_opt_descr * const descr = + argpar_item_opt_descr(item); + const char * const arg = argpar_item_opt_arg(item); - if (item_opt->descr->long_name) { + if (descr->long_name) { g_string_append_printf(res_str, "--%s", - item_opt->descr->long_name); + descr->long_name); - if (item_opt->arg) { - g_string_append_printf(res_str, "=%s", - item_opt->arg); + if (arg) { + g_string_append_printf(res_str, "=%s", arg); } - } else if (item_opt->descr->short_name) { + } else if (descr->short_name) { g_string_append_printf(res_str, "-%c", - item_opt->descr->short_name); + descr->short_name); - if (item_opt->arg) { - g_string_append_printf(res_str, " %s", - item_opt->arg); + if (arg) { + g_string_append_printf(res_str, " %s", arg); } } @@ -72,12 +71,14 @@ void append_to_res_str(GString * const res_str, } case ARGPAR_ITEM_TYPE_NON_OPT: { - const struct argpar_item_non_opt * const item_non_opt = - (const void *) item; + const char * const arg = argpar_item_non_opt_arg(item); + const unsigned int orig_index = + argpar_item_non_opt_orig_index(item); + const unsigned int non_opt_index = + argpar_item_non_opt_non_opt_index(item); - g_string_append_printf(res_str, "%s<%u,%u>", - item_non_opt->arg, item_non_opt->orig_index, - item_non_opt->non_opt_index); + g_string_append_printf(res_str, "%s<%u,%u>", arg, orig_index, + non_opt_index); break; } default: