From d11efb85535ef3244b7d4fde8d0fe49af25c4210 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Casas=20Sch=C3=B6ssow?= Date: Tue, 15 Nov 2022 17:40:01 +0100 Subject: [PATCH] Changed pathType from string var to enum --- Src/OSD/FileSystemPath.h | 3 ++- Src/OSD/SDL/Main.cpp | 18 +++++++------- Src/OSD/Unix/FileSystemPath.cpp | 38 ++++++++++++++++++++++++------ Src/OSD/Windows/FileSystemPath.cpp | 23 ++++++++++++------ 4 files changed, 58 insertions(+), 24 deletions(-) diff --git a/Src/OSD/FileSystemPath.h b/Src/OSD/FileSystemPath.h index b23a92f..b192f2b 100644 --- a/Src/OSD/FileSystemPath.h +++ b/Src/OSD/FileSystemPath.h @@ -32,8 +32,9 @@ namespace FileSystemPath { + enum fsPathType { Analysis, Config, Log, NVRAM, Saves, Screenshots }; // Filesystem path types bool PathExists(std::string fileSystemPath); // Checks if a directory exists (returns true if exists, false if it doesn't) - std::string GetPath(std::string pathType); // Generates a path to be used by Supermodel files + std::string GetPath(fsPathType pathType); // Generates a path to be used by Supermodel files } diff --git a/Src/OSD/SDL/Main.cpp b/Src/OSD/SDL/Main.cpp index 19fa4f4..c98d5d9 100644 --- a/Src/OSD/SDL/Main.cpp +++ b/Src/OSD/SDL/Main.cpp @@ -476,7 +476,7 @@ void Screenshot() time_t now = std::time(nullptr); tm* ltm = std::localtime(&now); - sprintf(file, "%sScreenshot %.4d-%.2d-%.2d (%.2d-%.2d-%.2d).bmp", FileSystemPath::GetPath("Screenshots").c_str(), 1900 + ltm->tm_year, 1 + ltm->tm_mon, ltm->tm_mday, ltm->tm_hour, ltm->tm_min, ltm->tm_sec); + sprintf(file, "%sScreenshot %.4d-%.2d-%.2d (%.2d-%.2d-%.2d).bmp", FileSystemPath::GetPath(FileSystemPath::Screenshots).c_str(), 1900 + ltm->tm_year, 1 + ltm->tm_mon, ltm->tm_mday, ltm->tm_hour, ltm->tm_min, ltm->tm_sec); info += file; puts(info.c_str()); @@ -616,7 +616,7 @@ static void SaveState(IEmulator *Model3) { CBlockFile SaveState; - std::string file_path = Util::Format() << FileSystemPath::GetPath("Saves") << Model3->GetGame().name << ".st" << s_saveSlot; + std::string file_path = Util::Format() << FileSystemPath::GetPath(FileSystemPath::Saves) << Model3->GetGame().name << ".st" << s_saveSlot; if (OKAY != SaveState.Create(file_path, "Supermodel Save State", "Supermodel Version " SUPERMODEL_VERSION)) { ErrorLog("Unable to save state to '%s'.", file_path.c_str()); @@ -641,7 +641,7 @@ static void LoadState(IEmulator *Model3, std::string file_path = std::string()) // Generate file path if (file_path.empty()) - file_path = Util::Format() << FileSystemPath::GetPath("Saves") << Model3->GetGame().name << ".st" << s_saveSlot; + file_path = Util::Format() << FileSystemPath::GetPath(FileSystemPath::Saves) << Model3->GetGame().name << ".st" << s_saveSlot; // Open and check to make sure format is correct if (OKAY != SaveState.Load(file_path)) @@ -675,7 +675,7 @@ static void SaveNVRAM(IEmulator *Model3) { CBlockFile NVRAM; - std::string file_path = Util::Format() << FileSystemPath::GetPath("NVRAM") << Model3->GetGame().name << ".nv"; + std::string file_path = Util::Format() << FileSystemPath::GetPath(FileSystemPath::NVRAM) << Model3->GetGame().name << ".nv"; if (OKAY != NVRAM.Create(file_path, "Supermodel NVRAM State", "Supermodel Version " SUPERMODEL_VERSION)) { ErrorLog("Unable to save NVRAM to '%s'. Make sure directory exists!", file_path.c_str()); @@ -698,7 +698,7 @@ static void LoadNVRAM(IEmulator *Model3) CBlockFile NVRAM; // Generate file path - std::string file_path = Util::Format() << FileSystemPath::GetPath("NVRAM") << Model3->GetGame().name << ".nv"; + std::string file_path = Util::Format() << FileSystemPath::GetPath(FileSystemPath::NVRAM) << Model3->GetGame().name << ".nv"; // Open and check to make sure format is correct if (OKAY != NVRAM.Load(file_path)) @@ -1449,10 +1449,10 @@ QuitError: Entry Point and Command Line Procesing ******************************************************************************/ -static const std::string s_analysisPath = Util::Format() << FileSystemPath::GetPath("Analysis"); -static const std::string s_configFilePath = Util::Format() << FileSystemPath::GetPath("Config") << "Supermodel.ini"; -static const std::string s_gameXMLFilePath = Util::Format() << FileSystemPath::GetPath("Config") << "Games.xml"; -static const std::string s_logFilePath = Util::Format() << FileSystemPath::GetPath("Log") << "Supermodel.log"; +static const std::string s_analysisPath = Util::Format() << FileSystemPath::GetPath(FileSystemPath::Analysis); +static const std::string s_configFilePath = Util::Format() << FileSystemPath::GetPath(FileSystemPath::Config) << "Supermodel.ini"; +static const std::string s_gameXMLFilePath = Util::Format() << FileSystemPath::GetPath(FileSystemPath::Config) << "Games.xml"; +static const std::string s_logFilePath = Util::Format() << FileSystemPath::GetPath(FileSystemPath::Log) << "Supermodel.log"; // Create and configure inputs static bool ConfigureInputs(CInputs *Inputs, Util::Config::Node *fileConfig, Util::Config::Node *runtimeConfig, const Game &game, bool configure) diff --git a/Src/OSD/Unix/FileSystemPath.cpp b/Src/OSD/Unix/FileSystemPath.cpp index b7a8e8c..ce899fe 100644 --- a/Src/OSD/Unix/FileSystemPath.cpp +++ b/Src/OSD/Unix/FileSystemPath.cpp @@ -45,12 +45,36 @@ namespace FileSystemPath } // Generates a path to be used by Supermodel files - std::string GetPath(std::string pathType) + std::string GetPath(fsPathType pathType) { std::string finalPath = ""; std::string homePath = ""; + std::string strPathType = ""; struct passwd* pwd = getpwuid(getuid()); + // Resolve pathType to string for later use + switch (pathType) + { + case Analysis: + strPathType = "Analysis"; + break; + case Config: + strPathType = "Config"; + break; + case Log: + strPathType = "Log"; + break; + case NVRAM: + strPathType = "NVRAM"; + break; + case Saves: + strPathType = "Saves"; + break; + case Screenshots: + strPathType = "Screenshots"; + break; + } + // Get user's HOME directory if (pwd) { @@ -65,15 +89,15 @@ namespace FileSystemPath if (FileSystemPath::PathExists("Config") || homePath.empty()) { // Use current directory - if (pathType == "Screenshots" || pathType == "Log") + if (pathType == Screenshots || pathType == Log) { finalPath = ""; } else { // If directory doesn't exist, create it - if (!FileSystemPath::PathExists(pathType)) mkdir(pathType.c_str(), 0775); - finalPath = pathType; + if (!FileSystemPath::PathExists(strPathType)) mkdir(strPathType.c_str(), 0775); + finalPath = strPathType; } } @@ -81,7 +105,7 @@ namespace FileSystemPath else if (FileSystemPath::PathExists(Util::Format() << homePath << "/.supermodel")) { // Use $HOME/.supermodel - finalPath = Util::Format() << homePath << "/.supermodel/" << pathType; + finalPath = Util::Format() << homePath << "/.supermodel/" << strPathType; // If directory doesn't exist, create it if (!FileSystemPath::PathExists(finalPath)) { @@ -92,7 +116,7 @@ namespace FileSystemPath else { // Use $HOME/.config/supermodel or $HOME/.local/share/supermodel depending on the file type - if (pathType == "Config") + if (pathType == Config) { finalPath = Util::Format() << homePath << "/.config/supermodel"; // If directory doesn't exist, create it @@ -108,7 +132,7 @@ namespace FileSystemPath // If directory doesn't exist, create it if (!FileSystemPath::PathExists(finalPath)) mkdir(finalPath.c_str(), 0775); // If directory doesn't exist, create it - finalPath = Util::Format() << homePath << "/.local/share/supermodel/" << pathType; + finalPath = Util::Format() << homePath << "/.local/share/supermodel/" << strPathType; if (!FileSystemPath::PathExists(finalPath)) mkdir(finalPath.c_str(), 0775); } diff --git a/Src/OSD/Windows/FileSystemPath.cpp b/Src/OSD/Windows/FileSystemPath.cpp index 66f9126..4390730 100644 --- a/Src/OSD/Windows/FileSystemPath.cpp +++ b/Src/OSD/Windows/FileSystemPath.cpp @@ -25,13 +25,22 @@ namespace FileSystemPath { // Generates a path to be used by Supermodel files - std::string GetPath(std::string pathType) + std::string GetPath(fsPathType pathType) { - if (pathType == "Config") return "Config/"; - if (pathType == "Screenshots") return ""; - if (pathType == "Saves") return "Saves/"; - if (pathType == "NVRAM") return "NVRAM/"; - if (pathType == "Log") return ""; - if (pathType == "Analysis") return "Analysis/"; + switch (pathType) + { + case Analysis: + return "Analysis/"; + case Config: + return "Config/"; + case Log: + return ""; + case NVRAM: + return "NVRAM/"; + case Saves: + return "Saves/"; + case Screenshots: + return ""; + } } }