fix wxstring handling (it's wchar_t always); refactor

This commit is contained in:
Christopher A. Mosher 2022-12-08 12:18:22 -05:00
parent 65369d5758
commit d3c3cfe0c8
9 changed files with 93 additions and 45 deletions

View File

@ -78,6 +78,7 @@ diskcontroller.cpp
drive.cpp drive.cpp
drivemotor.cpp drivemotor.cpp
e2filesystem.cpp e2filesystem.cpp
e2string.cpp
E2wxApp.cpp E2wxApp.cpp
E2wxFrame.cpp E2wxFrame.cpp
emptyslot.cpp emptyslot.cpp

View File

@ -24,6 +24,7 @@
#include "emulator.h" #include "emulator.h"
#include "gui.h" #include "gui.h"
#include "configep2.h" #include "configep2.h"
#include "e2filesystem.h"
#include <wx/app.h> #include <wx/app.h>
#include <wx/xrc/xmlres.h> #include <wx/xrc/xmlres.h>
#include <wx/fileconf.h> #include <wx/fileconf.h>
@ -81,8 +82,8 @@ void EmuTimer::Notify() {
E2wxApp::E2wxApp() : E2wxApp::E2wxApp() :
id(wxSTRINGIZE_T(PROJECT_VENDOR) wxT(".") wxSTRINGIZE_T(PROJECT_NAME)), id(wxSTRINGIZE(PROJECT_VENDOR) "." wxSTRINGIZE(PROJECT_NAME)),
version(wxSTRINGIZE_T(PROJECT_VERSION)) { version(wxSTRINGIZE(PROJECT_VERSION)) {
} }
E2wxApp::~E2wxApp() { E2wxApp::~E2wxApp() {
@ -93,19 +94,19 @@ E2wxApp::~E2wxApp() {
static std::filesystem::path dirCache() { 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() { 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() { 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() { 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); std::filesystem::create_directories(this->confdir);
BOOST_LOG_TRIVIAL(info) << "Configuration directory path: " << 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; BOOST_LOG_TRIVIAL(info) << "Configuration file path: " << this->conffile;
wxConfigBase::Set(new wxFileConfig("", "", GetID())); 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; 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::cout << "Executable file path: " << exe << std::endl;
std::filesystem::path res = exe.parent_path(); std::filesystem::path res = exe.parent_path();
if (res.filename() == "bin" || res.filename() == "MacOS") { if (res.filename() == "bin" || res.filename() == "MacOS") {
@ -235,7 +236,7 @@ bool E2wxApp::OnCmdLineParsed(wxCmdLineParser& parser) {
if (n <= 0) { if (n <= 0) {
std::cout << "no config file specified on the command line; will use config file specified in user-preferences" << std::endl; std::cout << "no config file specified on the command line; will use config file specified in user-preferences" << std::endl;
} else { } 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; 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 { const std::filesystem::path E2wxApp::BuildLogFilePath() const {
std::filesystem::path logfile = std::filesystem::path logfile =
dirCache() / dirCache() /
std::filesystem::path(GetID().fn_str().data()) / path_from_string(GetID()) /
std::filesystem::path(wxT("log")); std::filesystem::path("log");
std::filesystem::create_directories(logfile); std::filesystem::create_directories(logfile);
logfile = std::filesystem::canonical(logfile); logfile = std::filesystem::canonical(logfile);

View File

@ -78,7 +78,7 @@ void E2wxFrame::InitMenuBar() {
void E2wxFrame::InitStatusBar() { void E2wxFrame::InitStatusBar() {
CreateStatusBar(); 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) { void E2wxFrame::OnAbout(wxCommandEvent& event) {
wxString msg = ""; 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(); 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) { void E2wxFrame::OnPreferences(wxCommandEvent& event) {

View File

@ -1,5 +1,6 @@
#include "PreferencesDialog.h" #include "PreferencesDialog.h"
#include "E2wxApp.h" #include "E2wxApp.h"
#include "e2filesystem.h"
#include <wx/msgdlg.h> #include <wx/msgdlg.h>
#include <wx/treectrl.h> #include <wx/treectrl.h>
#include <wx/textctrl.h> #include <wx/textctrl.h>
@ -70,8 +71,7 @@ public:
wxDirTraverseResult OnFile(const wxString& filename) { wxDirTraverseResult OnFile(const wxString& filename) {
wxFileName n = wxFileName::FileName(filename); wxFileName n = wxFileName::FileName(filename);
if (n.GetExt() == "conf") { if (n.GetExt() == "conf") {
const std::filesystem::path& full = std::filesystem::path(n.GetFullName().fn_str().data()); const std::filesystem::path path = m_dir / path_from_string(n.GetFullName());
const std::filesystem::path path = m_dir / full;
m_tree->AppendItem(m_parent, n.GetName(), -1, -1, new TreeItemData(path, m_editable)); m_tree->AppendItem(m_parent, n.GetName(), -1, -1, new TreeItemData(path, m_editable));
} }
return wxDIR_CONTINUE; return wxDIR_CONTINUE;
@ -118,13 +118,13 @@ void PreferencesDialog::BuildItemTree() {
treItems->DeleteAllItems(); 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); fillDir(treItems, user, wxGetApp().GetConfigDir(), true);
treItems->SortChildren(user); 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()); fillDir(treItems, built_in, wxGetApp().GetResDir());
treItems->SortChildren(built_in); treItems->SortChildren(built_in);
@ -135,10 +135,10 @@ void PreferencesDialog::BuildItemTree() {
void PreferencesDialog::OnInit() { void PreferencesDialog::OnInit() {
wxConfigBase *appconf = wxConfigBase::Get(); 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? // TODO what to do when no config?
this->active = "epple2"; this->active = "epple2";
appconf->Write(wxT("/ActivePreferences/name"), this->active); appconf->Write("/ActivePreferences/name", this->active);
appconf->Flush(); appconf->Flush();
} }
@ -219,8 +219,8 @@ const std::filesystem::path BuildNewConfFilePath() {
std::filesystem::path f = wxGetApp().GetConfigDir(); std::filesystem::path f = wxGetApp().GetConfigDir();
wxString ts = to_iso_string(boost::posix_time::microsec_clock::universal_time()); wxString ts = to_iso_string(boost::posix_time::microsec_clock::universal_time());
ts.Replace(wxT("."), wxT("_")); ts.Replace(".", "_");
f /= (wxT("Untitled_") + ts + wxT(".conf")).fn_str().data(); f /= path_from_string("Untitled_" + ts + ".conf");
BOOST_LOG_TRIVIAL(info) << "will create file: " << f.c_str(); 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(); wxString name = wxFileName::FileName(p.c_str()).GetName();
this->active = name; this->active = name;
std::cout << "setting current active config file to: " << this->active << std::endl; 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 BuildItemTree(); // invalidates "data" pointer variable
PreSelectUserConfigItemName(p); PreSelectUserConfigItemName(p);
} }
@ -272,8 +272,8 @@ void PreferencesDialog::OnDelete(wxCommandEvent& evt) {
if (data->isFile()) { if (data->isFile()) {
if (data->isEditable()) { if (data->isEditable()) {
if (wxMessageBox( if (wxMessageBox(
wxT("Are you sure to want to permanently DELETE this configuration file?"), "Are you sure to want to permanently DELETE this configuration file?",
wxT("Delete"), wxYES_NO|wxCENTER, this) == wxYES) { "Delete", wxYES_NO|wxCENTER, this) == wxYES) {
std::filesystem::remove(data->path()); std::filesystem::remove(data->path());
BuildItemTree(); BuildItemTree();
treItems->SetFocus(); treItems->SetFocus();
@ -294,15 +294,15 @@ void PreferencesDialog::OnRename(wxCommandEvent& evt) {
if (data->isEditable()) { if (data->isEditable()) {
Save(data->path()); Save(data->path());
wxString name = wxFileName::FileName(data->path().c_str()).GetName(); 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) { if (!newname.IsEmpty() && newname != name) {
wxFileName fn(data->path().c_str()); wxFileName fn(data->path().c_str());
fn.SetName(newname); fn.SetName(newname);
// TODO should we check for existence of name in built-in (to prevent override)? // TODO should we check for existence of name in built-in (to prevent override)?
if (fn.Exists()) { 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 { } 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); std::filesystem::rename(data->path(), newpath);
BuildItemTree(); BuildItemTree();
PreSelectUserConfigItemName(newpath); PreSelectUserConfigItemName(newpath);

View File

@ -107,12 +107,12 @@ void Config::parse(MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revis
if (!path.empty()) if (!path.empty())
{ {
pConfig = new std::ifstream(path.c_str()); pConfig = new std::ifstream(path);
if (!pConfig->is_open()) if (!pConfig->is_open())
{ {
// TODO use filename only and look in standard resources // TODO use filename only and look in standard resources
std::stringstream ss; 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()); 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? // TODO config file location, how to be backwardly compatible?
wxString user_config; 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? // TODO what to do when no config?
user_config = wxT("epple2"); user_config = "epple2";
} }
user_config += ".conf"; 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; path = wxGetApp().GetConfigDir() / user_path;
std::cout << "looking for config file: " << path << std::endl; 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()) { if (!pConfig->is_open()) {
path = wxGetApp().GetResDir() / user_path; path = wxGetApp().GetResDir() / user_path;
std::cout << "looking for config file: " << path << std::endl; 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()) { if (!pConfig->is_open()) {
path.clear(); path.clear();
} }
@ -161,7 +161,7 @@ void Config::parse(MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revis
the current directory is /). the current directory is /).
*/ */
path = "etc/epple2/epple2.conf"; path = "etc/epple2/epple2.conf";
pConfig = new std::ifstream(path.c_str()); pConfig = new std::ifstream(path);
if (!pConfig->is_open()) if (!pConfig->is_open())
path.clear(); path.clear();
} }
@ -173,7 +173,7 @@ void Config::parse(MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revis
/usr/local/etc/epple2/epple2.conf /usr/local/etc/epple2/epple2.conf
*/ */
path = ETCDIR "/epple2/epple2.conf"; path = ETCDIR "/epple2/epple2.conf";
pConfig = new std::ifstream(path.c_str()); pConfig = new std::ifstream(path);
if (!pConfig->is_open()) if (!pConfig->is_open())
path.clear(); path.clear();
} }
@ -183,7 +183,7 @@ void Config::parse(MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revis
Try a likely linux location Try a likely linux location
*/ */
path = "/etc/epple2/epple2.conf"; path = "/etc/epple2/epple2.conf";
pConfig = new std::ifstream(path.c_str()); pConfig = new std::ifstream(path);
if (!pConfig->is_open()) if (!pConfig->is_open())
path.clear(); path.clear();
} }
@ -193,7 +193,7 @@ void Config::parse(MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revis
Try another likely linux location Try another likely linux location
*/ */
path = "/etc/epple2.conf"; path = "/etc/epple2.conf";
pConfig = new std::ifstream(path.c_str()); pConfig = new std::ifstream(path);
if (!pConfig->is_open()) if (!pConfig->is_open())
path.clear(); path.clear();
} }
@ -203,7 +203,7 @@ void Config::parse(MemoryRandomAccess& ram, Memory& rom, Slots& slts, int& revis
Last effort to find it. Last effort to find it.
*/ */
path = "epple2.conf"; path = "epple2.conf";
pConfig = new std::ifstream(path.c_str()); pConfig = new std::ifstream(path);
if (!pConfig->is_open()) if (!pConfig->is_open())
path.clear(); path.clear();
} }

View File

@ -109,3 +109,9 @@ std::filesystem::path valid_input_file(const std::filesystem::path path, const s
return p; return p;
} }
std::filesystem::path path_from_string(const wxString& s) {
return std::filesystem::path{s.wc_str()};
}

View File

@ -26,8 +26,11 @@
#ifndef E2FILESYSTEM_H #ifndef E2FILESYSTEM_H
#define E2FILESYSTEM_H #define E2FILESYSTEM_H
#include <wx/string.h>
#include <filesystem> #include <filesystem>
std::filesystem::path valid_input_file(const std::filesystem::path path, const std::filesystem::path base); 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 */ #endif /* E2FILESYSTEM_H */

13
src/e2string.cpp Normal file
View File

@ -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"

24
src/e2string.h Normal file
View File

@ -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 <wx/string.h>
// 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 */