parse params from string using bx_param method - avoid unnecessary switch walking over param types

This commit is contained in:
Stanislav Shwartsman 2016-05-03 19:15:09 +00:00
parent e1532260e4
commit 3ffe5d3d87
3 changed files with 95 additions and 50 deletions

View File

@ -2173,52 +2173,16 @@ int bx_parse_param_from_list(const char *context, const char *input, bx_list_c *
free(propval);
return 0;
}
switch (param->get_type()) {
case BXT_PARAM_NUM:
if (value != NULL) {
if ((value[0] == '0') && (value[1] == 'x')) {
((bx_param_num_c*)param)->set(strtoul(value, NULL, 16));
} else {
if (value[strlen(value)-1] == 'K') {
((bx_param_num_c*)param)->set(1000 * strtoul(value, NULL, 10));
}
else if (value[strlen(value)-1] == 'M') {
((bx_param_num_c*)param)->set(1000000 * strtoul(value, NULL, 10));
}
else {
((bx_param_num_c*)param)->set(strtoul(value, NULL, 10));
}
}
}
break;
case BXT_PARAM_BOOL:
if (value != NULL) {
if (!strcmp(value, "0") || !strcmp(value, "1")) {
((bx_param_bool_c*)param)->set(atol(value));
} else {
PARSE_WARN(("%s: wrong value for parameter '%s'", context, property));
ret = -1;
}
}
break;
case BXT_PARAM_ENUM:
if (value != NULL) {
if (!((bx_param_enum_c*)param)->set_by_name(value)) {
PARSE_WARN(("%s: invalid choice '%s' parameter '%s'", context, value, property));
ret = -1;
}
}
break;
case BXT_PARAM_STRING:
if (value != NULL) {
((bx_param_string_c*)param)->set(value);
} else {
((bx_param_string_c*)param)->set("");
}
break;
default:
PARSE_WARN(("%s: parameter '%s': unknown type", context, property));
int res = param->parse_param(value);
if (res != -1) {
if (res == 0) {
PARSE_WARN(("%s: wrong value for parameter '%s'", context, property));
ret = -1;
}
}
else {
PARSE_WARN(("%s: parameter '%s': unknown type", context, property));
ret = -1;
}
} else {
PARSE_WARN(("%s: unknown parameter '%s'", context, property));

View File

@ -279,6 +279,29 @@ void bx_param_num_c::set_enabled(int en)
update_dependents();
}
int bx_param_num_c::parse_param(const char *value)
{
if (value != NULL) {
if ((value[0] == '0') && (value[1] == 'x')) {
set(strtoul(value, NULL, 16));
} else {
if (value[strlen(value)-1] == 'K') {
set(1000 * strtoul(value, NULL, 10));
}
else if (value[strlen(value)-1] == 'M') {
set(1000000 * strtoul(value, NULL, 10));
}
else {
set(strtoul(value, NULL, 10));
}
}
return 1;
}
return 0;
}
// Signed 64 bit
bx_shadow_num_c::bx_shadow_num_c(bx_param_c *parent,
const char *name,
@ -516,6 +539,20 @@ bx_param_bool_c::bx_param_bool_c(bx_param_c *parent,
set_type(BXT_PARAM_BOOL);
}
int bx_param_bool_c::parse_param(const char *value)
{
if (value != NULL) {
if (!strcmp(value, "0") || !stricmp(value, "false")) {
set(0); return 1;
}
if (!strcmp(value, "1") || !stricmp(value, "true")) {
set(1); return 1;
}
}
return 0;
}
bx_shadow_bool_c::bx_shadow_bool_c(bx_param_c *parent,
const char *name,
const char *label,
@ -670,6 +707,15 @@ void bx_param_enum_c::set_enabled(int en)
update_dependents();
}
int bx_param_enum_c::parse_param(const char *value)
{
if (value != NULL) {
return set_by_name(value);
}
return 0;
}
bx_param_string_c::bx_param_string_c(bx_param_c *parent,
const char *name,
const char *label,
@ -833,6 +879,17 @@ bx_bool bx_param_string_c::isempty()
}
}
int bx_param_string_c::parse_param(const char *value)
{
if (value != NULL) {
set(value);
} else {
set("");
}
return 1;
}
int bx_param_string_c::sprint(char *buf, int len, bx_bool dquotes)
{
char tmpbyte[4];

View File

@ -117,41 +117,61 @@ public:
// indirectly from one or more other options (e.g. cpu count)
CI_ONLY = (1<<31)
} bx_param_opt_bits;
bx_param_c(Bit32u id, const char *name, const char *description);
bx_param_c(Bit32u id, const char *name, const char *label, const char *description);
virtual ~bx_param_c();
virtual void reset() {}
const char *get_name() const { return name; }
bx_param_c *get_parent() { return (bx_param_c *) parent; }
int get_param_path(char *path_out, int maxlen);
void set_format(const char *format) {text_format = format;}
const char *get_format() const {return text_format;}
void set_long_format(const char *format) {long_text_format = format;}
const char *get_long_format() const {return long_text_format;}
void set_ask_format(const char *format);
const char *get_ask_format() const {return ask_format;}
void set_label(const char *text);
void set_description(const char *text);
const char *get_label() const {return label;}
void set_description(const char *text);
const char *get_description() const { return description; }
virtual void set_runtime_param(int val) { runtime_param = val; }
int get_runtime_param() { return runtime_param; }
void set_group(const char *group);
const char *get_group() const {return group_name;}
const char *get_name() const { return name; }
const char *get_description() const { return description; }
int get_enabled() const { return enabled; }
virtual void set_enabled(int enabled) { this->enabled = enabled; }
virtual void reset() {}
int getint() const {return -1;}
static const char* set_default_format(const char *f);
static const char *get_default_format() { return default_text_format; }
bx_list_c *get_dependent_list() { return dependent_list; }
void set_options(Bit32u options) { this->options = options; }
Bit32u get_options() const { return options; }
void set_device_param(void *dev) { device = dev; }
void *get_device_param() { return device; }
#if BX_USE_TEXTCONFIG
virtual void text_print(FILE *fp) {}
virtual int text_ask(FILE *fpin, FILE *fpout) {return -1;}
virtual int text_ask(FILE *fpin, FILE *fpout) { return -1; }
#endif
virtual int parse_param(const char *value) { return -1; }
};
typedef Bit64s (*param_event_handler)(class bx_param_c *, int set, Bit64s val);
@ -213,6 +233,7 @@ public:
virtual void text_print(FILE *fp);
virtual int text_ask(FILE *fpin, FILE *fpout);
#endif
virtual int parse_param(const char *value);
};
// a bx_shadow_num_c is like a bx_param_num_c except that it doesn't
@ -296,6 +317,7 @@ public:
virtual void text_print(FILE *fp);
virtual int text_ask(FILE *fpin, FILE *fpout);
#endif
virtual int parse_param(const char *value);
};
// a bx_shadow_bool_c is a shadow param based on bx_param_bool_c.
@ -344,6 +366,7 @@ public:
virtual void text_print(FILE *fp);
virtual int text_ask(FILE *fpin, FILE *fpout);
#endif
virtual int parse_param(const char *value);
};
typedef const char* (*param_string_event_handler)(class bx_param_string_c *,
@ -392,6 +415,7 @@ public:
virtual void text_print(FILE *fp);
virtual int text_ask(FILE *fpin, FILE *fpout);
#endif
virtual int parse_param(const char *value);
};
// Declare a filename class. It is identical to a string, except that