mirror of
https://github.com/gnif/LookingGlass.git
synced 2025-01-09 13:53:58 +00:00
[common] option: add support for float option types
This commit is contained in:
parent
37faccd014
commit
88eada3494
2 changed files with 37 additions and 0 deletions
|
@ -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[]);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue