From d3c3cfe0c8d25d5d08a81e08fa5d063bf4d7eaeb Mon Sep 17 00:00:00 2001 From: "Christopher A. Mosher" Date: Thu, 8 Dec 2022 12:18:22 -0500 Subject: [PATCH] fix wxstring handling (it's wchar_t always); refactor --- CMakeLists.txt | 1 + src/E2wxApp.cpp | 27 ++++++++++++++------------- src/E2wxFrame.cpp | 10 +++++----- src/PreferencesDialog.cpp | 30 +++++++++++++++--------------- src/configep2.cpp | 24 ++++++++++++------------ src/e2filesystem.cpp | 6 ++++++ src/e2filesystem.h | 3 +++ src/e2string.cpp | 13 +++++++++++++ src/e2string.h | 24 ++++++++++++++++++++++++ 9 files changed, 93 insertions(+), 45 deletions(-) create mode 100644 src/e2string.cpp create mode 100644 src/e2string.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b587f6e..60c43ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,6 +78,7 @@ diskcontroller.cpp drive.cpp drivemotor.cpp e2filesystem.cpp +e2string.cpp E2wxApp.cpp E2wxFrame.cpp emptyslot.cpp diff --git a/src/E2wxApp.cpp b/src/E2wxApp.cpp index 9284caa..4a3b645 100644 --- a/src/E2wxApp.cpp +++ b/src/E2wxApp.cpp @@ -24,6 +24,7 @@ #include "emulator.h" #include "gui.h" #include "configep2.h" +#include "e2filesystem.h" #include #include #include @@ -81,8 +82,8 @@ void EmuTimer::Notify() { E2wxApp::E2wxApp() : - id(wxSTRINGIZE_T(PROJECT_VENDOR) wxT(".") wxSTRINGIZE_T(PROJECT_NAME)), - version(wxSTRINGIZE_T(PROJECT_VERSION)) { + id(wxSTRINGIZE(PROJECT_VENDOR) "." wxSTRINGIZE(PROJECT_NAME)), + version(wxSTRINGIZE(PROJECT_VERSION)) { } E2wxApp::~E2wxApp() { @@ -93,19 +94,19 @@ E2wxApp::~E2wxApp() { static std::filesystem::path dirCache() { - return std::filesystem::path(wxStandardPaths::Get().GetUserDir(wxStandardPaths::Dir_Cache).fn_str().data()); + return path_from_string(wxStandardPaths::Get().GetUserDir(wxStandardPaths::Dir_Cache)); } static std::filesystem::path dirConfig() { - return std::filesystem::path(wxStandardPaths::Get().GetUserConfigDir().fn_str().data()); + return path_from_string(wxStandardPaths::Get().GetUserConfigDir()); } static std::filesystem::path dirDocuments() { - return std::filesystem::path(wxStandardPaths::Get().GetAppDocumentsDir().fn_str().data()); + return path_from_string(wxStandardPaths::Get().GetAppDocumentsDir()); } static std::filesystem::path dirResources() { - return std::filesystem::path(wxStandardPaths::Get().GetResourcesDir().fn_str().data()); + return path_from_string(wxStandardPaths::Get().GetResourcesDir()); } @@ -137,18 +138,18 @@ bool E2wxApp::OnInit() { - this->confdir = dirConfig() / std::filesystem::path((GetID()+wxT(".d")).fn_str().data()); + this->confdir = dirConfig() / path_from_string(GetID()+".d"); std::filesystem::create_directories(this->confdir); BOOST_LOG_TRIVIAL(info) << "Configuration directory path: " << this->confdir; - this->conffile = dirConfig() / std::filesystem::path(GetID().fn_str().data()); + this->conffile = dirConfig() / path_from_string(GetID()); BOOST_LOG_TRIVIAL(info) << "Configuration file path: " << this->conffile; wxConfigBase::Set(new wxFileConfig("", "", GetID())); - this->docsdir = dirDocuments() / std::filesystem::path(GetID().fn_str().data()); + this->docsdir = dirDocuments() / path_from_string(GetID()); BOOST_LOG_TRIVIAL(info) << "User document directory path: " << this->docsdir; - const std::filesystem::path exe = std::filesystem::path(stdpaths.GetExecutablePath().fn_str().data()); + const std::filesystem::path exe = path_from_string(stdpaths.GetExecutablePath()); std::cout << "Executable file path: " << exe << std::endl; std::filesystem::path res = exe.parent_path(); if (res.filename() == "bin" || res.filename() == "MacOS") { @@ -235,7 +236,7 @@ bool E2wxApp::OnCmdLineParsed(wxCmdLineParser& parser) { if (n <= 0) { std::cout << "no config file specified on the command line; will use config file specified in user-preferences" << std::endl; } else { - this->arg_configfile = std::filesystem::path(parser.GetParam(0).fn_str().data()); + this->arg_configfile = path_from_string(parser.GetParam(0)); std::cout << "using config file specified on the command line: " << this->arg_configfile.c_str() << std::endl; } @@ -277,8 +278,8 @@ const std::filesystem::path E2wxApp::GetDocumentsDir() const { const std::filesystem::path E2wxApp::BuildLogFilePath() const { std::filesystem::path logfile = dirCache() / - std::filesystem::path(GetID().fn_str().data()) / - std::filesystem::path(wxT("log")); + path_from_string(GetID()) / + std::filesystem::path("log"); std::filesystem::create_directories(logfile); logfile = std::filesystem::canonical(logfile); diff --git a/src/E2wxFrame.cpp b/src/E2wxFrame.cpp index ec8b6db..2e21ab6 100644 --- a/src/E2wxFrame.cpp +++ b/src/E2wxFrame.cpp @@ -78,7 +78,7 @@ void E2wxFrame::InitMenuBar() { void E2wxFrame::InitStatusBar() { CreateStatusBar(); - SetStatusText(wxT("Welcome to ")+wxGetApp().GetID()); + SetStatusText("Welcome to "+wxGetApp().GetID()); } @@ -90,14 +90,14 @@ void E2wxFrame::OnExit(wxCommandEvent& event) { void E2wxFrame::OnAbout(wxCommandEvent& event) { wxString msg = ""; - msg += wxGetApp().GetID()+wxT("\n"); + msg += wxGetApp().GetID()+"\n"; - msg += wxT("version: ")+wxGetApp().GetVersion()+wxT("\n"); + msg += "version: "+wxGetApp().GetVersion()+"\n"; - msg += wxT("Current log file:\n"); + msg += "Current log file:\n"; msg += wxGetApp().GetLogFile().c_str(); - wxMessageBox(msg, wxT("About ")+wxGetApp().GetID(), wxOK | wxICON_INFORMATION); + wxMessageBox(msg, "About "+wxGetApp().GetID(), wxOK | wxICON_INFORMATION); } void E2wxFrame::OnPreferences(wxCommandEvent& event) { diff --git a/src/PreferencesDialog.cpp b/src/PreferencesDialog.cpp index 4ec51bd..7a29fb7 100644 --- a/src/PreferencesDialog.cpp +++ b/src/PreferencesDialog.cpp @@ -1,5 +1,6 @@ #include "PreferencesDialog.h" #include "E2wxApp.h" +#include "e2filesystem.h" #include #include #include @@ -70,8 +71,7 @@ public: wxDirTraverseResult OnFile(const wxString& filename) { wxFileName n = wxFileName::FileName(filename); if (n.GetExt() == "conf") { - const std::filesystem::path& full = std::filesystem::path(n.GetFullName().fn_str().data()); - const std::filesystem::path path = m_dir / full; + const std::filesystem::path path = m_dir / path_from_string(n.GetFullName()); m_tree->AppendItem(m_parent, n.GetName(), -1, -1, new TreeItemData(path, m_editable)); } return wxDIR_CONTINUE; @@ -118,13 +118,13 @@ void PreferencesDialog::BuildItemTree() { treItems->DeleteAllItems(); - wxTreeItemId configs = treItems->AddRoot(wxT("configurations"), -1, -1, new EmptyTreeItem()); + wxTreeItemId configs = treItems->AddRoot("configurations", -1, -1, new EmptyTreeItem()); - wxTreeItemId user = treItems->AppendItem(configs, wxT("user"), -1, -1, new EmptyTreeItem()); + wxTreeItemId user = treItems->AppendItem(configs, "user", -1, -1, new EmptyTreeItem()); fillDir(treItems, user, wxGetApp().GetConfigDir(), true); treItems->SortChildren(user); - wxTreeItemId built_in = treItems->AppendItem(configs, wxT("built-in"), -1, -1, new EmptyTreeItem()); + wxTreeItemId built_in = treItems->AppendItem(configs, "built-in", -1, -1, new EmptyTreeItem()); fillDir(treItems, built_in, wxGetApp().GetResDir()); treItems->SortChildren(built_in); @@ -135,10 +135,10 @@ void PreferencesDialog::BuildItemTree() { void PreferencesDialog::OnInit() { wxConfigBase *appconf = wxConfigBase::Get(); - if (!appconf->Read(wxT("/ActivePreferences/name"), &this->active)) { + if (!appconf->Read("/ActivePreferences/name", &this->active)) { // TODO what to do when no config? this->active = "epple2"; - appconf->Write(wxT("/ActivePreferences/name"), this->active); + appconf->Write("/ActivePreferences/name", this->active); appconf->Flush(); } @@ -219,8 +219,8 @@ const std::filesystem::path BuildNewConfFilePath() { std::filesystem::path f = wxGetApp().GetConfigDir(); wxString ts = to_iso_string(boost::posix_time::microsec_clock::universal_time()); - ts.Replace(wxT("."), wxT("_")); - f /= (wxT("Untitled_") + ts + wxT(".conf")).fn_str().data(); + ts.Replace(".", "_"); + f /= path_from_string("Untitled_" + ts + ".conf"); BOOST_LOG_TRIVIAL(info) << "will create file: " << f.c_str(); @@ -236,7 +236,7 @@ void PreferencesDialog::OnActive(wxCommandEvent& evt) { wxString name = wxFileName::FileName(p.c_str()).GetName(); this->active = name; std::cout << "setting current active config file to: " << this->active << std::endl; - wxConfigBase::Get()->Write(wxT("/ActivePreferences/name"), this->active); + wxConfigBase::Get()->Write("/ActivePreferences/name", this->active); BuildItemTree(); // invalidates "data" pointer variable PreSelectUserConfigItemName(p); } @@ -272,8 +272,8 @@ void PreferencesDialog::OnDelete(wxCommandEvent& evt) { if (data->isFile()) { if (data->isEditable()) { if (wxMessageBox( - wxT("Are you sure to want to permanently DELETE this configuration file?"), - wxT("Delete"), wxYES_NO|wxCENTER, this) == wxYES) { + "Are you sure to want to permanently DELETE this configuration file?", + "Delete", wxYES_NO|wxCENTER, this) == wxYES) { std::filesystem::remove(data->path()); BuildItemTree(); treItems->SetFocus(); @@ -294,15 +294,15 @@ void PreferencesDialog::OnRename(wxCommandEvent& evt) { if (data->isEditable()) { Save(data->path()); wxString name = wxFileName::FileName(data->path().c_str()).GetName(); - wxString newname = wxGetTextFromUser(wxT("new name:"), wxT("Rename configuration"), name, this, -1, -1, true); + wxString newname = wxGetTextFromUser("new name:", "Rename configuration", name, this, -1, -1, true); if (!newname.IsEmpty() && newname != name) { wxFileName fn(data->path().c_str()); fn.SetName(newname); // TODO should we check for existence of name in built-in (to prevent override)? if (fn.Exists()) { - wxMessageBox(wxT("That name is already being used."), wxT("File exists"), wxOK|wxCENTER, this); + wxMessageBox("That name is already being used.", "File exists", wxOK|wxCENTER, this); } else { - const std::filesystem::path newpath(fn.GetFullPath().fn_str().data()); + const std::filesystem::path newpath = path_from_string(fn.GetFullPath()); std::filesystem::rename(data->path(), newpath); BuildItemTree(); PreSelectUserConfigItemName(newpath); diff --git a/src/configep2.cpp b/src/configep2.cpp index 9701403..bd66ec4 100644 --- a/src/configep2.cpp +++ b/src/configep2.cpp @@ -107,12 +107,12 @@ void Config::parse(MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revis if (!path.empty()) { - pConfig = new std::ifstream(path.c_str()); + pConfig = new std::ifstream(path); if (!pConfig->is_open()) { // TODO use filename only and look in standard resources std::stringstream ss; - ss << "Cannot open config file " << this->file_path.c_str(); + ss << "Cannot open config file " << this->file_path; throw std::runtime_error(ss.str()); } } @@ -123,20 +123,20 @@ void Config::parse(MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revis { // TODO config file location, how to be backwardly compatible? wxString user_config; - if (!wxConfigBase::Get()->Read(wxT("/ActivePreferences/name"), &user_config)) { + if (!wxConfigBase::Get()->Read("/ActivePreferences/name", &user_config)) { // TODO what to do when no config? - user_config = wxT("epple2"); + user_config = "epple2"; } user_config += ".conf"; - const std::filesystem::path user_path{user_config.fn_str().data()}; + std::filesystem::path user_path{user_config.wc_str()}; path = wxGetApp().GetConfigDir() / user_path; std::cout << "looking for config file: " << path << std::endl; - pConfig = new std::ifstream(path.c_str()); + pConfig = new std::ifstream(path); if (!pConfig->is_open()) { path = wxGetApp().GetResDir() / user_path; std::cout << "looking for config file: " << path << std::endl; - pConfig = new std::ifstream(path.c_str()); + pConfig = new std::ifstream(path); if (!pConfig->is_open()) { path.clear(); } @@ -161,7 +161,7 @@ void Config::parse(MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revis the current directory is /). */ path = "etc/epple2/epple2.conf"; - pConfig = new std::ifstream(path.c_str()); + pConfig = new std::ifstream(path); if (!pConfig->is_open()) path.clear(); } @@ -173,7 +173,7 @@ void Config::parse(MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revis /usr/local/etc/epple2/epple2.conf */ path = ETCDIR "/epple2/epple2.conf"; - pConfig = new std::ifstream(path.c_str()); + pConfig = new std::ifstream(path); if (!pConfig->is_open()) path.clear(); } @@ -183,7 +183,7 @@ void Config::parse(MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revis Try a likely linux location */ path = "/etc/epple2/epple2.conf"; - pConfig = new std::ifstream(path.c_str()); + pConfig = new std::ifstream(path); if (!pConfig->is_open()) path.clear(); } @@ -193,7 +193,7 @@ void Config::parse(MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revis Try another likely linux location */ path = "/etc/epple2.conf"; - pConfig = new std::ifstream(path.c_str()); + pConfig = new std::ifstream(path); if (!pConfig->is_open()) path.clear(); } @@ -203,7 +203,7 @@ void Config::parse(MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revis Last effort to find it. */ path = "epple2.conf"; - pConfig = new std::ifstream(path.c_str()); + pConfig = new std::ifstream(path); if (!pConfig->is_open()) path.clear(); } diff --git a/src/e2filesystem.cpp b/src/e2filesystem.cpp index b470641..89a01c7 100644 --- a/src/e2filesystem.cpp +++ b/src/e2filesystem.cpp @@ -109,3 +109,9 @@ std::filesystem::path valid_input_file(const std::filesystem::path path, const s return p; } + + + +std::filesystem::path path_from_string(const wxString& s) { + return std::filesystem::path{s.wc_str()}; +} diff --git a/src/e2filesystem.h b/src/e2filesystem.h index f5b38c7..82c7fc1 100644 --- a/src/e2filesystem.h +++ b/src/e2filesystem.h @@ -26,8 +26,11 @@ #ifndef E2FILESYSTEM_H #define E2FILESYSTEM_H +#include + #include std::filesystem::path valid_input_file(const std::filesystem::path path, const std::filesystem::path base); +std::filesystem::path path_from_string(const wxString& s); #endif /* E2FILESYSTEM_H */ diff --git a/src/e2string.cpp b/src/e2string.cpp new file mode 100644 index 0000000..3495ef3 --- /dev/null +++ b/src/e2string.cpp @@ -0,0 +1,13 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/cppFiles/file_header.cc to edit this template + */ + +/* + * File: e2string.cpp + * Author: user + * + * Created on December 8, 2022, 9:57 AM + */ + +#include "e2string.h" diff --git a/src/e2string.h b/src/e2string.h new file mode 100644 index 0000000..9d44dfd --- /dev/null +++ b/src/e2string.h @@ -0,0 +1,24 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/cppFiles/file_header.h to edit this template + */ + +/* + * File: e2string.h + * Author: user + * + * Created on December 8, 2022, 9:57 AM + */ + +#ifndef E2STRING_H +#define E2STRING_H + +#include + +// wxWidgets 3.0+ uses wide strings exclusively, which it refers to as "Unicode strings" + +const wchar_t* e2C(const wxString& s) { + return s.wc_str(); +} + +#endif /* E2STRING_H */