From 86210d19fd6028f4954c4278dbef620734a896e1 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 6 Oct 2014 15:02:07 -0400 Subject: [PATCH] Added new convenience function for checking an options value --- Gemfile.lock | 1 + source/opts.c | 18 +++++++++++------- source/opts.h | 8 +++++--- tests/test_opts.c | 2 +- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index d8548a3..1539f10 100755 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,4 +1,5 @@ GEM + remote: https://rubygems.org/ specs: json (1.8.1) rake (10.3.2) diff --git a/source/opts.c b/source/opts.c index de76086..26e9a63 100755 --- a/source/opts.c +++ b/source/opts.c @@ -22,7 +22,7 @@ typedef struct { unsigned int arg_count; char** arg_vect; int current; - OptionConfig_T* options; + opts_cfg_t* options; } StreamContext_T; static entry_t* Options = NULL; @@ -34,7 +34,7 @@ static char* opts_parse_optarg(StreamContext_T* ctx, char* opt_name); static void opts_parse_argument( StreamContext_T* ctx ); static void opts_parse_error(const char* msg, char* opt_name); -static OptionConfig_T* opts_get_option_config( OptionConfig_T* opts, OptionType_T typ, char* name ); +static opts_cfg_t* opts_get_option_config( opts_cfg_t* opts, OptionType_T typ, char* name ); static char* opts_next_token( StreamContext_T* ctx ); static void opts_consume_ws( StreamContext_T* ctx ); static char opts_next_char( StreamContext_T* ctx ); @@ -44,7 +44,7 @@ static char* strclone(const char* p_old); static void opts_add_option(char* name, char* tag, char* arg); static void opts_add_argument(char* arg); -void opts_parse( OptionConfig_T* opts, int argc, char** argv ) { +void opts_parse( opts_cfg_t* opts, int argc, char** argv ) { /* Setup the stream */ StreamContext_T ctx; ctx.line_idx = 0; @@ -100,7 +100,7 @@ void opts_reset(void) { static void opts_parse_short_option( StreamContext_T* ctx ) { char opt[2] = { ctx->current, '\0' }; char* opt_name = strclone(opt); - OptionConfig_T* config = opts_get_option_config( ctx->options, SHORT, opt_name ); + opts_cfg_t* config = opts_get_option_config( ctx->options, SHORT, opt_name ); if (config != NULL) { char* opt_arg = NULL; (void)opts_next_char( ctx ); @@ -116,7 +116,7 @@ static void opts_parse_short_option( StreamContext_T* ctx ) { static void opts_parse_long_option( StreamContext_T* ctx ) { char* opt_name = opts_next_token( ctx ); - OptionConfig_T* config = opts_get_option_config( ctx->options, LONG, opt_name ); + opts_cfg_t* config = opts_get_option_config( ctx->options, LONG, opt_name ); if (config != NULL) { char* opt_arg = NULL; if (config->has_arg) @@ -146,8 +146,8 @@ static void opts_parse_argument( StreamContext_T* ctx ) { opts_add_argument(arg_val); } -static OptionConfig_T* opts_get_option_config( OptionConfig_T* opts, OptionType_T type, char* name ) { - OptionConfig_T* cfg = NULL; +static opts_cfg_t* opts_get_option_config( opts_cfg_t* opts, OptionType_T type, char* name ) { + opts_cfg_t* cfg = NULL; int i = 0; while( opts[i].name != NULL ) { OptionType_T curr_type = (strlen(opts[i].name) > 1) ? LONG : SHORT; @@ -270,6 +270,10 @@ const char* opts_get_value(const char* name, const char* tag) { return (NULL == p_opt) ? NULL : p_opt->value; } +bool opts_equal(const char* name, const char* tag, const char* value) { + return (0 == strcmp(value, opts_get_value(name,tag))); +} + const char** opts_select(const char* name, const char* tag) { size_t index = 0; const char** ret = (const char**)malloc(sizeof(const char*)); diff --git a/source/opts.h b/source/opts.h index 18022a6..7e51bdc 100755 --- a/source/opts.h +++ b/source/opts.h @@ -8,19 +8,21 @@ extern "C" { #include #include -typedef struct OptionConfig_T { +typedef struct { char* name; bool has_arg; char* tag; char* desc; -} OptionConfig_T; +} opts_cfg_t; -void opts_parse( OptionConfig_T* opts, int argc, char** argv ); +void opts_parse(opts_cfg_t* opts, int argc, char** argv); void opts_reset(void); bool opts_is_set(const char* name, const char* tag); +bool opts_equal(const char* name, const char* tag, const char* value); + const char* opts_get_value(const char* name, const char* tag); const char** opts_select(const char* name, const char* tag); diff --git a/tests/test_opts.c b/tests/test_opts.c index e9a3b19..5feceed 100755 --- a/tests/test_opts.c +++ b/tests/test_opts.c @@ -12,7 +12,7 @@ //----------------------------------------------------------------------------- // Sample Option Configuration //----------------------------------------------------------------------------- -OptionConfig_T Options_Config[] = { +opts_cfg_t Options_Config[] = { { "a", false, "test_a", "A simple test option" }, { "b", true, "test_b", "A simple test option" }, { "c", false, "test_c", "A simple test option" }, -- 2.54.0