From 572fc4ed575406d61d6258c804d34fc43a641ace Mon Sep 17 00:00:00 2001 From: Bryce Denney Date: Sun, 1 Sep 2002 15:27:33 +0000 Subject: [PATCH] - I had been misunderstanding how you're supposed to convert wxStrings into normal C strings. After asking about it on wx-users, I understand it better now. Example of unsafe code: char *filename = dlg.GetFilename().c_str (); printf ("file name is %s\n", filename); The problem is that dlg.GetFilename() returns a temporary wxString that goes out of scope at the end of that line of code. The "filename" string is unstable if you write it this way. Example of safe code: char filename[1024]; wxString fn (dlg.GetFilename ()); strncpy (filename, fn.c_str (), sizeof(filename)); printf ("file name is %s\n", name); Now we have a stable copy of the wxString in "fn" which is usable as long as fn is in scope. - also now we use wxStrings (almost) all the time in the interface to the wxdialogs. Any conversion from char* to wxString and back is done in wxmain.cc now. --- bochs/gui/wxdialog.cc | 49 +++++++++++++++++++++---------------------- bochs/gui/wxdialog.h | 35 ++++++++++++++++--------------- bochs/gui/wxmain.cc | 27 +++++++++++++++--------- 3 files changed, 59 insertions(+), 52 deletions(-) diff --git a/bochs/gui/wxdialog.cc b/bochs/gui/wxdialog.cc index a25788c39..a60ec3c29 100644 --- a/bochs/gui/wxdialog.cc +++ b/bochs/gui/wxdialog.cc @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////// -// $Id: wxdialog.cc,v 1.13 2002-08-30 22:52:32 bdenney Exp $ +// $Id: wxdialog.cc,v 1.14 2002-09-01 15:27:32 bdenney Exp $ ///////////////////////////////////////////////////////////////// // // misc/wxdialog.cc @@ -92,15 +92,15 @@ LogMsgAskDialog::LogMsgAskDialog( // so that caller has time to configure the dialog. } -void LogMsgAskDialog::SetContext (char *s) { +void LogMsgAskDialog::SetContext (wxString s) { wxString text; - text.Printf (LOG_MSG_CONTEXT, s); + text.Printf (LOG_MSG_CONTEXT, s.c_str ()); ChangeStaticText (vertSizer, context, text); } -void LogMsgAskDialog::SetMessage (char *s) { +void LogMsgAskDialog::SetMessage (wxString s) { wxString text; - text.Printf (LOG_MSG_MSG, s); + text.Printf (LOG_MSG_MSG, s.c_str ()); ChangeStaticText (vertSizer, message, text); } @@ -229,7 +229,9 @@ FloppyConfigDialog::FloppyConfigDialog( // AddRadio(). The diskImageSizer will be added last, in Init(). } -void FloppyConfigDialog::AddRadio (char *description, char *filename) +void FloppyConfigDialog::AddRadio ( + const wxString& description, + const wxString& filename) { if (n_rbtns >= FLOPPY_MAX_RBTNS) { wxLogError ("AddRadio failed: increase FLOPPY_MAX_RBTNS in wxdialog.h"); @@ -241,12 +243,11 @@ void FloppyConfigDialog::AddRadio (char *description, char *filename) n_rbtns++; } -void FloppyConfigDialog::SetDriveName (const char *name) -{ +void FloppyConfigDialog::SetDriveName (wxString name) { wxString text; - text.Printf (FLOPPY_CONFIG_TITLE, name); + text.Printf (FLOPPY_CONFIG_TITLE, name.c_str ()); SetTitle (text); - text.Printf (FLOPPY_CONFIG_INSTRS, name); + text.Printf (FLOPPY_CONFIG_INSTRS, name.c_str ()); ChangeStaticText (vertSizer, instr, text); } @@ -298,11 +299,11 @@ FloppyConfigDialog::SetRadio (int n) { } } -void FloppyConfigDialog::SetFilename (const char *f) { +void FloppyConfigDialog::SetFilename (wxString f) { // search equivalentFilename[] for matches. if it matches, select the // radio button instead. for (int i=0; iSetValue (TRUE); return; // leaving filename text field unchanged } @@ -311,14 +312,14 @@ void FloppyConfigDialog::SetFilename (const char *f) { diskImageRadioBtn->SetValue (TRUE); } -char * +wxString FloppyConfigDialog::GetFilename () { int n = GetRadio (); if (n < n_rbtns) { return equivalentFilename[n]; } else { - return (char *)filename->GetValue ().c_str (); + return filename->GetValue ().c_str (); } } @@ -474,10 +475,9 @@ HDConfigDialog::HDConfigDialog( Center (); } -void HDConfigDialog::SetDriveName (const char *name) -{ +void HDConfigDialog::SetDriveName (wxString name) { wxString text; - text.Printf (HD_CONFIG_TITLE, name); + text.Printf (HD_CONFIG_TITLE, name.c_str ()); SetTitle (text); } @@ -682,10 +682,9 @@ void CdromConfigDialog::Init() Center (); } -void CdromConfigDialog::SetDriveName (const char *name) -{ +void CdromConfigDialog::SetDriveName (wxString name) { wxString text; - text.Printf (CDROM_CONFIG_TITLE, name); + text.Printf (CDROM_CONFIG_TITLE, name.c_str ()); SetTitle (text); } @@ -700,9 +699,9 @@ void CdromConfigDialog::EnableChanged () diskImageRadioBtn->Enable (en); } -void CdromConfigDialog::SetFilename (const char *f) { +void CdromConfigDialog::SetFilename (wxString f) { for (int i=0; iSetValue (TRUE); return; } @@ -710,7 +709,7 @@ void CdromConfigDialog::SetFilename (const char *f) { filename->SetValue (wxString (f)); } -char * +wxString CdromConfigDialog::GetFilename () { if (enable->GetValue ()) { @@ -723,11 +722,11 @@ CdromConfigDialog::GetFilename () // be the last one. That's what radio buttons do! wxASSERT (diskImageRadioBtn->GetValue ()); } - return (char *)filename->GetValue().c_str (); + return filename->GetValue(); } void -CdromConfigDialog::AddRadio (const char *description, char *filename) +CdromConfigDialog::AddRadio (const wxString& description, const wxString& filename) { if (n_rbtns >= CDROM_MAX_RBTNS) { wxLogError ("AddRadio failed: increase CDROM_MAX_RBTNS in wxdialog.h"); diff --git a/bochs/gui/wxdialog.h b/bochs/gui/wxdialog.h index 67c9338d5..63afe9e66 100644 --- a/bochs/gui/wxdialog.h +++ b/bochs/gui/wxdialog.h @@ -1,5 +1,5 @@ //////////////////////////////////////////////////////////////////// -// $Id: wxdialog.h,v 1.13 2002-08-31 00:35:25 bdenney Exp $ +// $Id: wxdialog.h,v 1.14 2002-09-01 15:27:33 bdenney Exp $ //////////////////////////////////////////////////////////////////// // // wxWindows dialogs for Bochs @@ -63,8 +63,8 @@ public: wxWindowID id, const wxString& title); void EnableButton (button_t btn, bool en) { enabled[(int)btn] = en; } - void SetContext (char *s); - void SetMessage (char *s); + void SetContext (wxString s); + void SetMessage (wxString s); bool GetDontAsk () { return dontAsk->GetValue (); } void OnEvent (wxCommandEvent& event); int ShowModal() { Init(); return wxDialog::ShowModal(); } @@ -150,7 +150,7 @@ private: wxStaticText *instr; #define FLOPPY_MAX_RBTNS 4 wxRadioButton *rbtn[FLOPPY_MAX_RBTNS]; - char *equivalentFilename[FLOPPY_MAX_RBTNS]; + wxString equivalentFilename[FLOPPY_MAX_RBTNS]; int n_rbtns; wxRadioButton *diskImageRadioBtn; wxTextCtrl *filename; @@ -164,15 +164,17 @@ public: void OnTextEvent (wxCommandEvent& event); int ShowModal() { Init(); return wxDialog::ShowModal(); } void SetRadio (int val); - void SetFilename (const char *f); + void SetFilename (wxString f); + // Use char* instead of wxString because the array we use is already + // expressed as a char *[]. void SetCapacityChoices (int n, char *choices[]); void SetCapacity (int cap) { capacity->SetSelection (cap); } int GetRadio (); int GetCapacity () { return capacity->GetSelection (); } - char *GetFilename (); - void SetDriveName (const char *name); + wxString GetFilename (); + void SetDriveName (wxString name); void SetValidateFunc (validateFunc_t v) { validate = v; } - void AddRadio (char *description, char *filename); + void AddRadio (const wxString& description, const wxString& filename); DECLARE_EVENT_TABLE() }; @@ -225,9 +227,9 @@ public: HDConfigDialog(wxWindow* parent, wxWindowID id); void OnEvent (wxCommandEvent& event); int ShowModal() { Init(); return wxDialog::ShowModal(); } - void SetFilename (const char *f) { filename->SetValue (wxString (f)); } - char *GetFilename () { return (char *)filename->GetValue().c_str (); } - void SetDriveName (const char *name); + void SetFilename (wxString f) { filename->SetValue (f); } + wxString GetFilename () { return filename->GetValue(); } + void SetDriveName (wxString n); void SetGeom (int n, int value); int GetGeom (int n) { return geom[n]->GetValue (); } void SetGeomRange (int n, int min, int max) { geom[n]->SetRange (min, max); } @@ -295,19 +297,19 @@ private: wxRadioButton *diskImageRadioBtn; #define CDROM_MAX_RBTNS 2 wxRadioButton *rbtn[CDROM_MAX_RBTNS]; - char *equivalentFilename[CDROM_MAX_RBTNS]; + wxString equivalentFilename[CDROM_MAX_RBTNS]; int n_rbtns; public: CdromConfigDialog(wxWindow* parent, wxWindowID id); void OnEvent (wxCommandEvent& event); int ShowModal() { Init(); return wxDialog::ShowModal(); } - void SetFilename (const char *f); - char *GetFilename (); - void SetDriveName (const char *name); + void SetFilename (wxString f); + wxString GetFilename (); + void SetDriveName (wxString f); void EnableChanged (); void SetEnable (bool val) { enable->SetValue (val); EnableChanged (); } bool GetEnable () { return enable->GetValue (); } - void AddRadio (const char *descr, char *path); + void AddRadio (const wxString& descr, const wxString& path); // rbtn[0] will always be the "ejected" button void SetEjected (bool val) { if (val) rbtn[0]->SetValue (TRUE); } bool GetEjected () { return rbtn[0]->GetValue (); } @@ -486,7 +488,6 @@ disk c, or cdrom. The boot selection could be as simple as | [ ] Floppy A | | [X] Hard Disk C | | [ ] CD-ROM | -| [ ] CD-ROM | | [ Help ] [ Cancel ] [ Ok ] | +-------------------------------------------+ or fancier with icons for the device types, and Edit buttons that diff --git a/bochs/gui/wxmain.cc b/bochs/gui/wxmain.cc index 2c80fdc23..8333bc46b 100644 --- a/bochs/gui/wxmain.cc +++ b/bochs/gui/wxmain.cc @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////// -// $Id: wxmain.cc,v 1.19 2002-08-31 04:58:24 bdenney Exp $ +// $Id: wxmain.cc,v 1.20 2002-09-01 15:27:33 bdenney Exp $ ///////////////////////////////////////////////////////////////// // // wxmain.cc implements the wxWindows frame, toolbar, menus, and dialogs. @@ -703,7 +703,7 @@ MyFrame::editFloppyValidate (FloppyConfigDialog *dialog) void MyFrame::editFloppyConfig (int drive) { FloppyConfigDialog dlg (this, -1); - dlg.SetDriveName (drive==0? BX_FLOPPY0_NAME : BX_FLOPPY1_NAME); + dlg.SetDriveName (wxString (drive==0? BX_FLOPPY0_NAME : BX_FLOPPY1_NAME)); dlg.SetCapacityChoices (n_floppy_type_names, floppy_type_names); bx_list_c *list = (bx_list_c*) SIM->get_param ((drive==0)? BXP_FLOPPYA : BXP_FLOPPYB); if (!list) { wxLogError ("floppy object param is null"); return; } @@ -733,9 +733,12 @@ void MyFrame::editFloppyConfig (int drive) int n = dlg.ShowModal (); printf ("floppy config returned %d\n", n); if (n==0) { - printf ("filename is '%s'\n", dlg.GetFilename ()); + char filename[1024]; + wxString fn (dlg.GetFilename ()); + strncpy (filename, fn.c_str (), sizeof(filename)); + printf ("filename is '%s'\n", filename); printf ("capacity = %d (%s)\n", dlg.GetCapacity(), floppy_type_names[dlg.GetCapacity ()]); - fname->set (dlg.GetFilename ()); + fname->set (filename); disktype->set (disktype->get_min () + dlg.GetCapacity ()); if (dlg.GetRadio () == 0) disktype->set (BX_FLOPPY_NONE); @@ -770,8 +773,11 @@ void MyFrame::editHDConfig (int drive) int n = dlg.ShowModal (); printf ("HD config returned %d\n", n); if (n==0) { - printf ("filename is '%s'\n", dlg.GetFilename ()); - fname->set (dlg.GetFilename ()); + char filename[1024]; + wxString fn (dlg.GetFilename ()); + strncpy (filename, fn.c_str (), sizeof (filename)); + printf ("filename is '%s'\n", filename); + fname->set (filename); cyl->set (dlg.GetGeom (0)); heads->set (dlg.GetGeom (1)); spt->set (dlg.GetGeom (2)); @@ -812,12 +818,13 @@ void MyFrame::editCdromConfig () int n = dlg.ShowModal (); printf ("cdrom config returned %d\n", n); if (n==0) { - char buffer[1024]; - strncpy (buffer, dlg.GetFilename (), sizeof(buffer)); - fname->set (buffer); ///// doing something illegal? + char filename[1024]; + wxString fn (dlg.GetFilename ()); + strncpy (filename, fn.c_str (), sizeof(filename)); + fname->set (filename); present->set (dlg.GetEnable ()); status->set (dlg.GetEjected () ? BX_EJECTED : BX_INSERTED); - printf ("filename is '%s'\n", dlg.GetFilename ()); + printf ("filename is '%s'\n", filename); printf ("enabled=%d ejected=%d\n", present->get(), status->get()); // cdrom and hard disk D cannot both be enabled. if (present->get ()) {