add new dump_param method to param object which dumps param into a string

use new method do dump param tree during save/restore, GUI and text debugger and other places
significantly reduce code duplication over multiple modules
This commit is contained in:
Stanislav Shwartsman 2018-01-10 20:05:59 +00:00
parent e05e6f58b0
commit 5f2bf81995
9 changed files with 54 additions and 71 deletions

View File

@ -3094,20 +3094,10 @@ int bx_write_param_list(FILE *fp, bx_list_c *base, const char *optname, bx_bool
strcat(bxrcline, tmpstr);
switch (param->get_type()) {
case BXT_PARAM_NUM:
if (((bx_param_num_c*)param)->get_base() == BASE_DEC) {
sprintf(tmpstr, FMT_LL "d", ((bx_param_num_c*)param)->get64());
} else {
sprintf(tmpstr, "0x" FMT_LL "x", ((bx_param_num_c*)param)->get64());
}
break;
case BXT_PARAM_BOOL:
sprintf(tmpstr, "%d", ((bx_param_bool_c*)param)->get());
break;
case BXT_PARAM_ENUM:
sprintf(tmpstr, "%s", ((bx_param_enum_c*)param)->get_selected());
break;
case BXT_PARAM_STRING:
((bx_param_string_c*)param)->sprint(tmpstr, BX_PATHNAME_LEN, 1);
param->dump_param(tmpstr, BX_PATHNAME_LEN, 1);
break;
default:
BX_ERROR(("bx_write_param_list(): unsupported parameter type"));
@ -3177,10 +3167,10 @@ int bx_write_usb_options(FILE *fp, int maxports, bx_list_c *base)
if (SIM->get_param_bool("enabled", base)->get()) {
for (i = 1; i <= maxports; i++) {
sprintf(tmpname, "port%d.device", i);
SIM->get_param_string(tmpname, base)->sprint(tmpstr, BX_PATHNAME_LEN, 1);
SIM->get_param_string(tmpname, base)->dump_param(tmpstr, BX_PATHNAME_LEN, 1);
fprintf(fp, ", port%d=%s", i, tmpstr);
sprintf(tmpname, "port%d.options", i);
SIM->get_param_string(tmpname, base)->sprint(tmpstr, BX_PATHNAME_LEN, 1);
SIM->get_param_string(tmpname, base)->dump_param(tmpstr, BX_PATHNAME_LEN, 1);
fprintf(fp, ", options%d=%s", i, tmpstr);
}
}

View File

@ -2396,28 +2396,24 @@ void MakeBL(TreeParent *h_P, bx_param_c *p)
int j = strlen (tmpcb);
switch (p->get_type())
{
case BXT_PARAM_NUM:
if (((bx_param_num_c*)p)->get_base() == BASE_DEC)
sprintf (tmpcb + j,": " FMT_LL "d",((bx_param_num_c*)p)->get64());
else
sprintf (tmpcb + j,": 0x" FMT_LL "X",((bx_param_num_c*)p)->get64());
break;
case BXT_LIST:
as_list = (bx_list_c *)p;
n = as_list->get_size();
break;
case BXT_PARAM_BOOL:
sprintf (tmpcb + j,": %s",((bx_param_bool_c*)p)->get()?"true":"false");
break;
case BXT_PARAM_ENUM:
sprintf (tmpcb + j,": %s",((bx_param_enum_c*)p)->get_selected());
break;
case BXT_PARAM_BOOL:
case BXT_PARAM_ENUM:
case BXT_PARAM_NUM:
case BXT_PARAM_STRING:
((bx_param_string_c*)p)->sprint(tmpstr, BX_PATHNAME_LEN, 0);
p->dump_param(tmpstr, BX_PATHNAME_LEN);
sprintf(tmpcb + j,": %s", tmpstr);
break;
case BXT_PARAM_DATA:
sprintf (tmpcb + j,": binary data, size=%d",((bx_shadow_data_c*)p)->get_size());
break;
}
MakeTreeChild (h_P, n, &h_new);
if (n > 0)

View File

@ -311,37 +311,46 @@ int bx_param_num_c::parse_param(const char *ptr)
}
void bx_param_num_c::dump_param(FILE *fp)
{
char tmpstr[BX_PATHNAME_LEN+1];
dump_param(tmpstr, BX_PATHNAME_LEN, 0);
fputs(tmpstr, fp);
}
int bx_param_num_c::dump_param(char *buf, int len, bx_bool dquotes)
{
Bit64s value = get64();
if (get_base() == BASE_DOUBLE) {
double f2value;
memcpy(&f2value, &value, sizeof(double));
fprintf(fp, "%f", f2value);
snprintf(buf, len, "%f", f2value);
} else if (get_base() == BASE_FLOAT) {
float f1value;
memcpy(&f1value, &value, sizeof(float));
fprintf(fp, "%f", f1value);
snprintf(buf, len, "%f", f1value);
} else if (get_base() == BASE_DEC) {
if (get_min() >= BX_MIN_BIT64U) {
if ((Bit64u) get_max() > BX_MAX_BIT32U) {
fprintf(fp, FMT_LL"u", value);
snprintf(buf, len, FMT_LL"u", value);
} else {
fprintf(fp, "%u", (Bit32u) value);
snprintf(buf, len, "%u", (Bit32u) value);
}
} else {
fprintf(fp, "%d", (Bit32s) value);
snprintf(buf, len, "%d", (Bit32s) value);
}
} else {
if (get_format()) {
fprintf(fp, get_format(), value);
snprintf(buf, len, get_format(), value);
} else {
if ((Bit64u)get_max() > BX_MAX_BIT32U) {
fprintf(fp, "0x" FMT_LL "x", (Bit64u) value);
snprintf(buf, len, "0x" FMT_LL "x", (Bit64u) value);
} else {
fprintf(fp, "0x%x", (Bit32u) value);
snprintf(buf, len, "0x%x", (Bit32u) value);
}
}
}
return strlen(buf);
}
// Signed 64 bit
@ -613,6 +622,12 @@ void bx_param_bool_c::dump_param(FILE *fp)
fprintf(fp, "%s", get()?"true":"false");
}
int bx_param_bool_c::dump_param(char *buf, int len, bx_bool dquotes)
{
snprintf(buf, len, "%s", get()?"true":"false");
return strlen(buf);
}
bx_shadow_bool_c::bx_shadow_bool_c(bx_param_c *parent,
const char *name,
const char *label,
@ -779,6 +794,12 @@ void bx_param_enum_c::dump_param(FILE *fp)
fprintf(fp, "%s", get_selected());
}
int bx_param_enum_c::dump_param(char *buf, int len, bx_bool dquotes)
{
snprintf(buf, len, "%s", get_selected());
return strlen(buf);
}
bx_param_string_c::bx_param_string_c(bx_param_c *parent,
const char *name,
const char *label,
@ -933,7 +954,7 @@ void bx_param_string_c::set_initial_val(const char *buf)
set(initial_val);
}
bx_bool bx_param_string_c::isempty()
bx_bool bx_param_string_c::isempty() const
{
if (options & RAW_BYTES) {
return (memcmp(val, initial_val, maxsize) == 0);
@ -956,11 +977,11 @@ int bx_param_string_c::parse_param(const char *ptr)
void bx_param_string_c::dump_param(FILE *fp)
{
char tmpstr[BX_PATHNAME_LEN+1];
sprint(tmpstr, BX_PATHNAME_LEN, 0);
dump_param(tmpstr, BX_PATHNAME_LEN, 0);
fputs(tmpstr, fp);
}
int bx_param_string_c::sprint(char *buf, int len, bx_bool dquotes)
int bx_param_string_c::dump_param(char *buf, int len, bx_bool dquotes)
{
char tmpbyte[4];

View File

@ -175,6 +175,7 @@ public:
virtual int parse_param(const char *value) { return -1; }
virtual void dump_param(FILE *fp) {}
virtual int dump_param(char *buf, int buflen, bx_bool dquotes = BX_FALSE) { return 0; }
};
typedef Bit64s (*param_event_handler)(class bx_param_c *, int set, Bit64s val);
@ -239,6 +240,7 @@ public:
#endif
virtual int parse_param(const char *value);
virtual void dump_param(FILE *fp);
virtual int dump_param(char *buf, int buflen, bx_bool dquotes = BX_FALSE);
};
// a bx_shadow_num_c is like a bx_param_num_c except that it doesn't
@ -327,6 +329,7 @@ public:
#endif
virtual int parse_param(const char *value);
virtual void dump_param(FILE *fp);
virtual int dump_param(char *buf, int buflen, bx_bool dquotes = BX_FALSE);
};
// a bx_shadow_bool_c is a shadow param based on bx_param_bool_c.
@ -377,6 +380,7 @@ public:
#endif
virtual int parse_param(const char *value);
virtual void dump_param(FILE *fp);
virtual int dump_param(char *buf, int buflen, bx_bool dquotes = BX_FALSE);
};
typedef const char* (*param_string_event_handler)(class bx_param_string_c *,
@ -419,14 +423,14 @@ public:
char get_separator() const {return separator; }
int get_maxsize() const {return maxsize; }
void set_initial_val(const char *buf);
bx_bool isempty();
int sprint(char *buf, int buflen, bx_bool dquotes);
bx_bool isempty() const;
#if BX_USE_TEXTCONFIG
virtual void text_print();
virtual int text_ask();
#endif
virtual int parse_param(const char *value);
virtual void dump_param(FILE *fp);
virtual int dump_param(char *buf, int buflen, bx_bool dquotes = BX_FALSE);
};
// Declare a filename class. It is identical to a string, except that

View File

@ -827,7 +827,7 @@ void bx_param_string_c::text_print()
{
char value[1024];
this->sprint(value, 1024, 0);
this->dump_param(value, 1024);
if (get_format()) {
bx_printf(get_format(), value);
} else {

View File

@ -1867,28 +1867,20 @@ void MakeBL(HTREEITEM *h_P, bx_param_c *p)
int j = strlen (tmpcb);
switch (p->get_type())
{
case BXT_PARAM_NUM:
if (((bx_param_num_c*)p)->get_base() == BASE_DEC)
sprintf (tmpcb + j,": " FMT_LL "d",((bx_param_num_c*)p)->get64());
else
sprintf (tmpcb + j,": 0x" FMT_LL "X",((bx_param_num_c*)p)->get64());
break;
case BXT_LIST:
as_list = (bx_list_c *)p;
i = as_list->get_size();
break;
case BXT_PARAM_NUM:
case BXT_PARAM_BOOL:
sprintf (tmpcb + j,": %s",((bx_param_bool_c*)p)->get()?"true":"false");
break;
case BXT_PARAM_ENUM:
sprintf (tmpcb + j,": %s",((bx_param_enum_c*)p)->get_selected());
break;
case BXT_PARAM_STRING:
((bx_param_string_c*)p)->sprint(tmpstr, BX_PATHNAME_LEN, 0);
p->dump_param(tmpstr, BX_PATHNAME_LEN);
sprintf(tmpcb + j,": %s", tmpstr);
break;
case BXT_PARAM_DATA:
sprintf (tmpcb + j,": binary data, size=%d",((bx_shadow_data_c*)p)->get_size());
break;
}
MakeTreeChild (h_P, i, &h_new);
if (i > 0)

View File

@ -529,7 +529,7 @@ HWND CreateInput(HWND hDlg, UINT cid, UINT xpos, UINT ypos, BOOL hide, bx_param_
style = WS_CHILD | WS_TABSTOP;
if (param->get_type() == BXT_PARAM_STRING) {
sparam = (bx_param_string_c*)param;
sparam->sprint(buffer, 512, 0);
sparam->dump_param(buffer, 512);
if ((sparam->get_options() & sparam->RAW_BYTES) == 0) {
style |= ES_AUTOHSCROLL;
}

View File

@ -880,7 +880,7 @@ void ParamDialog::AddParam(
bool isFilename = param->get_options() & param->IS_FILENAME;
wxTextCtrl *txtctrl = new wxTextCtrl(context->parent, pstr->id, wxT(""), wxDefaultPosition, isFilename? longTextSize : normalTextSize);
if (description) txtctrl->SetToolTip(wxString(description, wxConvUTF8));
param->sprint(value, 1024, 0);
param->dump_param(value, 1024);
txtctrl->SetValue(wxString(value, wxConvUTF8));
if ((param->get_options() & param->RAW_BYTES) == 0) {
txtctrl->SetMaxLength(param->get_maxsize());

View File

@ -202,31 +202,11 @@ void print_tree(bx_param_c *node, int level, bx_bool xml)
switch (node->get_type()) {
case BXT_PARAM_NUM:
if (((bx_param_num_c*)node)->get_base() == BASE_DEC) {
dbg_printf("" FMT_LL "d", ((bx_param_num_c*)node)->get64());
if (! xml) dbg_printf(" (number)");
} else {
dbg_printf("0x" FMT_LL "x", ((bx_param_num_c*)node)->get64());
if (! xml) dbg_printf(" (hex number)");
}
break;
case BXT_PARAM_BOOL:
dbg_printf("%s", ((bx_param_bool_c*)node)->get()?"true":"false");
if (! xml) dbg_printf(" (boolean)");
break;
case BXT_PARAM_ENUM:
dbg_printf("'%s'", ((bx_param_enum_c*)node)->get_selected());
if (! xml) dbg_printf(" (enum)");
break;
case BXT_PARAM_STRING:
((bx_param_string_c*)node)->sprint(tmpstr, BX_PATHNAME_LEN, 0);
if (((bx_param_string_c*)node)->get_options() & bx_param_string_c::RAW_BYTES) {
dbg_printf("'%s'", tmpstr);
if (! xml) dbg_printf(" (raw byte string)");
} else {
dbg_printf("'%s'", tmpstr);
if (! xml) dbg_printf(" (string)");
}
node->dump_param(tmpstr, BX_PATHNAME_LEN, 1);
dbg_printf("%s", tmpstr);
break;
case BXT_LIST:
{