From: <ssh...@us...> - 2017-04-20 18:26:47
|
Revision: 13200 Author: sshwarts Date: 2017-04-20 18:26:45 +0000 (Thu, 20 Apr 2017) Log Message: ----------- use std::string in param_tree and gain more robust code with automatic garbage collection Modified Paths: -------------- trunk/bochs/bochs.h trunk/bochs/gui/paramtree.cc trunk/bochs/gui/paramtree.h trunk/bochs/gui/textconfig.cc trunk/bochs/gui/win32dialog.cc Modified: trunk/bochs/bochs.h =================================================================== --- trunk/bochs/bochs.h 2017-04-19 21:03:53 UTC (rev 13199) +++ trunk/bochs/bochs.h 2017-04-20 18:26:45 UTC (rev 13200) @@ -30,6 +30,8 @@ #include "config.h" /* generated by configure script from config.h.in */ +using namespace std; + #ifndef __QNXNTO__ extern "C" { #endif @@ -101,6 +103,8 @@ } #endif +#include <string> + #include "osdep.h" /* platform dependent includes and defines */ #include "bx_debug/debug.h" #include "gui/siminterface.h" Modified: trunk/bochs/gui/paramtree.cc =================================================================== --- trunk/bochs/gui/paramtree.cc 2017-04-19 21:03:53 UTC (rev 13199) +++ trunk/bochs/gui/paramtree.cc 2017-04-20 18:26:45 UTC (rev 13200) @@ -37,15 +37,10 @@ bx_param_c::bx_param_c(Bit32u id, const char *param_name, const char *param_desc) : bx_object_c(id), parent(NULL), - description(NULL), - label(NULL), - ask_format(NULL), - group_name(NULL) + name(param_name), + description(param_desc) { set_type(BXT_PARAM); - this->name = new char[strlen(param_name)+1]; - strcpy(this->name, param_name); - set_description(param_desc); this->text_format = default_text_format; this->long_text_format = default_text_format; this->runtime_param = 0; @@ -59,16 +54,11 @@ bx_param_c::bx_param_c(Bit32u id, const char *param_name, const char *param_label, const char *param_desc) : bx_object_c(id), parent(NULL), - description(NULL), - label(NULL), - ask_format(NULL), - group_name(NULL) + name(param_name), + description(param_desc), + label(param_label) { set_type(BXT_PARAM); - this->name = new char[strlen(param_name)+1]; - strcpy(this->name, param_name); - set_description(param_desc); - set_label(param_label); this->text_format = default_text_format; this->long_text_format = default_text_format; this->runtime_param = 0; @@ -81,61 +71,12 @@ bx_param_c::~bx_param_c() { - delete [] name; - delete [] label; - delete [] description; - delete [] ask_format; - delete [] group_name; delete dependent_list; } -void bx_param_c::set_description(const char *text) -{ - delete [] this->description; - if (text) { - this->description = new char[strlen(text)+1]; - strcpy(this->description, text); - } else { - this->description = NULL; - } -} - -void bx_param_c::set_label(const char *text) -{ - delete [] label; - if (text) { - label = new char[strlen(text)+1]; - strcpy(label, text); - } else { - label = NULL; - } -} - -void bx_param_c::set_ask_format(const char *format) -{ - delete [] ask_format; - if (format) { - ask_format = new char[strlen(format)+1]; - strcpy(ask_format, format); - } else { - ask_format = NULL; - } -} - -void bx_param_c::set_group(const char *group) -{ - delete [] group_name; - if (group) { - group_name = new char[strlen(group)+1]; - strcpy(group_name, group); - } else { - group_name = NULL; - } -} - int bx_param_c::get_param_path(char *path_out, int maxlen) { - if ((get_parent() == NULL) || (get_parent() == root_param)) { + if (get_parent() == NULL || get_parent() == root_param) { // Start with an empty string. // Never print the name of the root param. path_out[0] = 0; @@ -145,7 +86,7 @@ strncat(path_out, ".", maxlen); } } - strncat(path_out, name, maxlen); + strncat(path_out, name.c_str(), maxlen); return strlen(path_out); } @@ -1056,18 +997,11 @@ if (list != NULL) { clear(); } - delete [] title; } void bx_list_c::init(const char *list_title) { - if (list_title) { - this->title = new char[strlen(list_title)+1]; - strcpy(this->title, list_title); - } else { - this->title = new char[1]; - this->title[0] = 0; - } + title = string(list_title); this->options = 0; this->choice = 1; } @@ -1090,7 +1024,7 @@ bx_list_c* bx_list_c::clone() { - bx_list_c *newlist = new bx_list_c(NULL, name, title); + bx_list_c *newlist = new bx_list_c(NULL, name.c_str(), title.c_str()); for (int i=0; i<get_size(); i++) newlist->add(get(i)); newlist->set_options(options); Modified: trunk/bochs/gui/paramtree.h =================================================================== --- trunk/bochs/gui/paramtree.h 2017-04-19 21:03:53 UTC (rev 13199) +++ trunk/bochs/gui/paramtree.h 2017-04-20 18:26:45 UTC (rev 13200) @@ -23,6 +23,10 @@ #ifndef BX_PARAM_TREE_H #define BX_PARAM_TREE_H +using namespace std; + +#include <string> + //////////////////////////////////////////////////////////////////// // parameter classes: bx_param_c and family //////////////////////////////////////////////////////////////////// @@ -95,13 +99,13 @@ BOCHSAPI_CYGONLY static const char *default_text_format; protected: bx_list_c *parent; - char *name; - char *description; - char *label; // label string for text menus and gui dialogs + string name; + string description; + string label; // label string for text menus and gui dialogs const char *text_format; // printf format string. %d for ints, %s for strings, etc. const char *long_text_format; // printf format string. %d for ints, %s for strings, etc. - char *ask_format; // format string for asking for a new value - char *group_name; // name of the group the param belongs to + string ask_format; // format string for asking for a new value + string group_name; // name of the group the param belongs to int runtime_param; int enabled; Bit32u options; @@ -124,7 +128,7 @@ virtual void reset() {} - const char *get_name() const { return name; } + const char *get_name() const { return name.c_str(); } bx_param_c *get_parent() { return (bx_param_c *) parent; } int get_param_path(char *path_out, int maxlen); @@ -135,20 +139,20 @@ 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_ask_format(const char *txt) { ask_format = string(txt); } + const char *get_ask_format() const {return ask_format.c_str();} - void set_label(const char *text); - const char *get_label() const {return label;} + void set_label(const char *txt) { label = string(txt); } + const char *get_label() const {return label.c_str();} - void set_description(const char *text); - const char *get_description() const { return description; } + void set_description(const char *desc) { description = string(desc); } + const char *get_description() const { return description.c_str(); } virtual void set_runtime_param(int val) { runtime_param = val; } - int get_runtime_param() { return runtime_param; } + int get_runtime_param() const { return runtime_param; } - void set_group(const char *group); - const char *get_group() const {return group_name;} + void set_group(const char *group) { group_name = string(group); } + const char *get_group() const {return group_name.c_str();} int get_enabled() const { return enabled; } virtual void set_enabled(int enabled) { this->enabled = enabled; } @@ -225,8 +229,8 @@ void set_initial_val(Bit64s initial_val); int get_base() const { return base; } void set_range(Bit64u min, Bit64u max); - Bit64s get_min() { return min; } - Bit64s get_max() { return max; } + Bit64s get_min() const { return min; } + Bit64s get_max() const { return max; } static Bit32u set_default_base(Bit32u val); static Bit32u get_default_base() { return default_base; } #if BX_USE_TEXTCONFIG @@ -486,7 +490,7 @@ // to 1 in the constructor. Bit32u choice; // type Bit32u is compatible with ask_uint // title of the menu or series - char *title; + string title; void init(const char *list_title); // save / restore support void *sr_devptr; @@ -531,7 +535,7 @@ int get_size() const { return size; } int get_choice() const { return choice; } void set_choice(int new_choice) { choice = new_choice; } - char *get_title() { return title; } + const char *get_title() const { return title.c_str(); } void set_parent(bx_param_c *newparent); bx_param_c *get_parent() { return parent; } virtual void reset(); Modified: trunk/bochs/gui/textconfig.cc =================================================================== --- trunk/bochs/gui/textconfig.cc 2017-04-19 21:03:53 UTC (rev 13199) +++ trunk/bochs/gui/textconfig.cc 2017-04-20 18:26:45 UTC (rev 13200) @@ -990,7 +990,7 @@ bx_listitem_t *item; bx_list_c *child; - const char *my_title = title; + const char *my_title = title.c_str(); bx_printf("\n"); int i, imax = strlen(my_title); for (i=0; i<imax; i++) bx_printf("-"); Modified: trunk/bochs/gui/win32dialog.cc =================================================================== --- trunk/bochs/gui/win32dialog.cc 2017-04-19 21:03:53 UTC (rev 13199) +++ trunk/bochs/gui/win32dialog.cc 2017-04-20 18:26:45 UTC (rev 13200) @@ -136,7 +136,7 @@ case WM_INITDIALOG: param = (bx_param_string_c *)lParam; title = param->get_label(); - if ((title == NULL) || (strlen(title) == 0)) { + if (title == NULL || strlen(title) == 0) { title = param->get_name(); } SetWindowText(hDlg, title); @@ -168,7 +168,7 @@ for (level=0; level<N_LOGLEV; level++) { int mod = 0; - int first = SIM->get_log_action (mod, level); + int first = SIM->get_log_action(mod, level); BOOL consensus = true; // now compare all others to first. If all match, then use "first" as // the initial value. |