Changed pathType from string var to enum

This commit is contained in:
Fernando Casas Schössow 2022-11-15 17:40:01 +01:00 committed by trzy
parent df7787f040
commit d11efb8553
4 changed files with 58 additions and 24 deletions

View file

@ -32,8 +32,9 @@
namespace FileSystemPath 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) 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
} }

View file

@ -476,7 +476,7 @@ void Screenshot()
time_t now = std::time(nullptr); time_t now = std::time(nullptr);
tm* ltm = std::localtime(&now); 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; info += file;
puts(info.c_str()); puts(info.c_str());
@ -616,7 +616,7 @@ static void SaveState(IEmulator *Model3)
{ {
CBlockFile SaveState; 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)) if (OKAY != SaveState.Create(file_path, "Supermodel Save State", "Supermodel Version " SUPERMODEL_VERSION))
{ {
ErrorLog("Unable to save state to '%s'.", file_path.c_str()); 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 // Generate file path
if (file_path.empty()) 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 // Open and check to make sure format is correct
if (OKAY != SaveState.Load(file_path)) if (OKAY != SaveState.Load(file_path))
@ -675,7 +675,7 @@ static void SaveNVRAM(IEmulator *Model3)
{ {
CBlockFile NVRAM; 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)) 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()); 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; CBlockFile NVRAM;
// Generate file path // 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 // Open and check to make sure format is correct
if (OKAY != NVRAM.Load(file_path)) if (OKAY != NVRAM.Load(file_path))
@ -1449,10 +1449,10 @@ QuitError:
Entry Point and Command Line Procesing Entry Point and Command Line Procesing
******************************************************************************/ ******************************************************************************/
static const std::string s_analysisPath = Util::Format() << FileSystemPath::GetPath("Analysis"); static const std::string s_analysisPath = Util::Format() << FileSystemPath::GetPath(FileSystemPath::Analysis);
static const std::string s_configFilePath = Util::Format() << FileSystemPath::GetPath("Config") << "Supermodel.ini"; static const std::string s_configFilePath = Util::Format() << FileSystemPath::GetPath(FileSystemPath::Config) << "Supermodel.ini";
static const std::string s_gameXMLFilePath = Util::Format() << FileSystemPath::GetPath("Config") << "Games.xml"; static const std::string s_gameXMLFilePath = Util::Format() << FileSystemPath::GetPath(FileSystemPath::Config) << "Games.xml";
static const std::string s_logFilePath = Util::Format() << FileSystemPath::GetPath("Log") << "Supermodel.log"; static const std::string s_logFilePath = Util::Format() << FileSystemPath::GetPath(FileSystemPath::Log) << "Supermodel.log";
// Create and configure inputs // Create and configure inputs
static bool ConfigureInputs(CInputs *Inputs, Util::Config::Node *fileConfig, Util::Config::Node *runtimeConfig, const Game &game, bool configure) static bool ConfigureInputs(CInputs *Inputs, Util::Config::Node *fileConfig, Util::Config::Node *runtimeConfig, const Game &game, bool configure)

View file

@ -45,12 +45,36 @@ namespace FileSystemPath
} }
// Generates a path to be used by Supermodel files // 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 finalPath = "";
std::string homePath = ""; std::string homePath = "";
std::string strPathType = "";
struct passwd* pwd = getpwuid(getuid()); 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 // Get user's HOME directory
if (pwd) if (pwd)
{ {
@ -65,15 +89,15 @@ namespace FileSystemPath
if (FileSystemPath::PathExists("Config") || homePath.empty()) if (FileSystemPath::PathExists("Config") || homePath.empty())
{ {
// Use current directory // Use current directory
if (pathType == "Screenshots" || pathType == "Log") if (pathType == Screenshots || pathType == Log)
{ {
finalPath = ""; finalPath = "";
} }
else else
{ {
// If directory doesn't exist, create it // If directory doesn't exist, create it
if (!FileSystemPath::PathExists(pathType)) mkdir(pathType.c_str(), 0775); if (!FileSystemPath::PathExists(strPathType)) mkdir(strPathType.c_str(), 0775);
finalPath = pathType; finalPath = strPathType;
} }
} }
@ -81,7 +105,7 @@ namespace FileSystemPath
else if (FileSystemPath::PathExists(Util::Format() << homePath << "/.supermodel")) else if (FileSystemPath::PathExists(Util::Format() << homePath << "/.supermodel"))
{ {
// Use $HOME/.supermodel // Use $HOME/.supermodel
finalPath = Util::Format() << homePath << "/.supermodel/" << pathType; finalPath = Util::Format() << homePath << "/.supermodel/" << strPathType;
// If directory doesn't exist, create it // If directory doesn't exist, create it
if (!FileSystemPath::PathExists(finalPath)) if (!FileSystemPath::PathExists(finalPath))
{ {
@ -92,7 +116,7 @@ namespace FileSystemPath
else else
{ {
// Use $HOME/.config/supermodel or $HOME/.local/share/supermodel depending on the file type // 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"; finalPath = Util::Format() << homePath << "/.config/supermodel";
// If directory doesn't exist, create it // If directory doesn't exist, create it
@ -108,7 +132,7 @@ namespace FileSystemPath
// If directory doesn't exist, create it // If directory doesn't exist, create it
if (!FileSystemPath::PathExists(finalPath)) mkdir(finalPath.c_str(), 0775); if (!FileSystemPath::PathExists(finalPath)) mkdir(finalPath.c_str(), 0775);
// If directory doesn't exist, create it // 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); if (!FileSystemPath::PathExists(finalPath)) mkdir(finalPath.c_str(), 0775);
} }

View file

@ -25,13 +25,22 @@
namespace FileSystemPath namespace FileSystemPath
{ {
// Generates a path to be used by Supermodel files // 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/"; switch (pathType)
if (pathType == "Screenshots") return ""; {
if (pathType == "Saves") return "Saves/"; case Analysis:
if (pathType == "NVRAM") return "NVRAM/"; return "Analysis/";
if (pathType == "Log") return ""; case Config:
if (pathType == "Analysis") return "Analysis/"; return "Config/";
case Log:
return "";
case NVRAM:
return "NVRAM/";
case Saves:
return "Saves/";
case Screenshots:
return "";
}
} }
} }