From 88eada3494be6290240e233fdef6e48d58ef41cc Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Tue, 10 Aug 2021 01:09:40 +1000 Subject: [PATCH] [common] option: add support for float option types --- common/include/common/option.h | 3 +++ common/src/option.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/common/include/common/option.h b/common/include/common/option.h index d2c26440..738e3410 100644 --- a/common/include/common/option.h +++ b/common/include/common/option.h @@ -30,6 +30,7 @@ enum OptionType OPTION_TYPE_INT, OPTION_TYPE_STRING, OPTION_TYPE_BOOL, + OPTION_TYPE_FLOAT, OPTION_TYPE_CUSTOM }; @@ -55,6 +56,7 @@ struct Option int x_int; char * x_string; bool x_bool; + float x_float; void * x_custom; } value; @@ -78,6 +80,7 @@ struct Option * option_get (const char * module, const char * name); int option_get_int (const char * module, const char * name); const char * option_get_string(const char * module, const char * name); bool option_get_bool (const char * module, const char * name); +bool option_get_float (const char * module, const char * name); // called by the main application to parse the command line arguments bool option_parse(int argc, char * argv[]); diff --git a/common/src/option.c b/common/src/option.c index 44123d03..b3f0e78f 100644 --- a/common/src/option.c +++ b/common/src/option.c @@ -70,6 +70,12 @@ static bool bool_parser(struct Option * opt, const char * str) return true; } +static bool float_parser(struct Option * opt, const char * str) +{ + opt->value.x_float = atof(str); + return true; +} + static bool string_parser(struct Option * opt, const char * str) { free(opt->value.x_string); @@ -90,6 +96,14 @@ static char * bool_toString(struct Option * opt) return strdup(opt->value.x_bool ? "yes" : "no"); } +static char * float_toString(struct Option * opt) +{ + int len = snprintf(NULL, 0, "%f", opt->value.x_float); + char * ret = malloc(len + 1); + sprintf(ret, "%f", opt->value.x_float); + return ret; +} + static char * string_toString(struct Option * opt) { if (!opt->value.x_string) @@ -131,6 +145,10 @@ bool option_register(struct Option options[]) o->parser = bool_parser; break; + case OPTION_TYPE_FLOAT: + o->parser = float_parser; + break; + default: DEBUG_ERROR("BUG: Non int/string/bool option types must have a parser"); continue; @@ -153,6 +171,10 @@ bool option_register(struct Option options[]) o->toString = bool_toString; break; + case OPTION_TYPE_FLOAT: + o->toString = float_toString; + break; + default: DEBUG_ERROR("BUG: Non int/string/bool option types must implement toString"); continue; @@ -770,3 +792,15 @@ bool option_get_bool(const char * module, const char * name) assert(o->type == OPTION_TYPE_BOOL); return o->value.x_bool; } + +bool option_get_float(const char * module, const char * name) +{ + struct Option * o = option_get(module, name); + if (!o) + { + DEBUG_ERROR("BUG: Failed to get the value for option %s:%s", module, name); + return false; + } + assert(o->type == OPTION_TYPE_FLOAT); + return o->value.x_float; +}