16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-17 23:39:18 +02:00

Merge pull request #124 from xwizard/refactor/user-config-path

refactor: extract user_config_path() helper
This commit is contained in:
2026-06-30 22:35:53 +02:00
committed by GitHub
8 changed files with 38 additions and 78 deletions

View File

@@ -6,6 +6,7 @@
#include "vehicle/Train.h"
#include "utilities/parser.h"
#include "utilities/Logs.h"
#include "utilities/utilities.h"
#include "simulation/simulationtime.h"
#include "application/application.h"
@@ -192,21 +193,9 @@ uart_input::recall_bindings() {
m_inputbindings.clear();
std::string filePath = "eu07_input-uart.ini";
#ifdef _WIN32
if (const char *appdata = std::getenv("APPDATA"))
{
fs::path appPath = fs::path(appdata) / "MaSzyna" / "eu07_input-uart.ini";
if (fs::exists(appPath))
filePath = appPath.string();
}
#else
if (const char *home = std::getenv("HOME"))
{
fs::path appPath = fs::path(home) / ".config" / "MaSzyna" / "eu07_input-uart.ini";
if (fs::exists(appPath))
filePath = appPath.string();
}
#endif
fs::path appPath = user_config_path("eu07_input-uart.ini");
if (!appPath.empty() && fs::exists(appPath))
filePath = appPath.string();
cParser bindingparser(filePath.c_str(), cParser::buffer_FILE);
if (false == bindingparser.ok())
{

View File

@@ -55,6 +55,22 @@ std::string Now()
#endif
}
std::filesystem::path user_config_path(const std::string &filename)
{
namespace fs = std::filesystem;
#if defined(_WIN32)
if (const char *appdata = std::getenv("APPDATA"))
return fs::path(appdata) / "MaSzyna" / filename;
#elif defined(__APPLE__)
if (const char *home = std::getenv("HOME"))
return fs::path(home) / "Library" / "Application Support" / "MaSzyna" / filename;
#else
if (const char *home = std::getenv("HOME"))
return fs::path(home) / ".config" / "MaSzyna" / filename;
#endif
return {}; // env var missing → caller falls back to CWD-relative filename
}
// zwraca różnicę czasu
// jeśli pierwsza jest aktualna, a druga rozkładowa, to ujemna oznacza opóżnienie
// na dłuższą metę trzeba uwzględnić datę, jakby opóżnienia miały przekraczać 12h (towarowych)

View File

@@ -25,6 +25,10 @@ template <typename T> constexpr T sq(T v)
return v * v;
}
// returns the per-user config path for `filename` (platform-specific dir),
// or an empty path if the home/appdata env var is unavailable
std::filesystem::path user_config_path(const std::string &filename);
// TODO: Shouldn't this be in globals?
namespace paths
{