From ef0854aa39dd261fc0f26a2ac78dcde035da5c07 Mon Sep 17 00:00:00 2001 From: Daniel Dada Date: Wed, 17 Dec 2025 02:58:23 +0300 Subject: [PATCH] fix: normalize paths --- src/core/config/config.cpp | 9 ++------- src/core/io/toml_file.cpp | 2 +- src/core/palette/palette_manager.hpp | 7 ++++--- src/core/theme/theme_template.cpp | 8 ++++---- src/core/utils.cpp | 30 +++++++++++++++++++--------- src/core/utils.hpp | 2 ++ src/gui/palette_controller.cpp | 3 ++- src/gui/template_editor.cpp | 3 ++- 8 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/core/config/config.cpp b/src/core/config/config.cpp index 0743bb7..b440e9e 100644 --- a/src/core/config/config.cpp +++ b/src/core/config/config.cpp @@ -35,13 +35,8 @@ Result config::initialize(std::unique_ptr file) std::filesystem::path config::get_user_config_dir() { - auto home = expand_user("~"); -#ifdef _WIN32 - return home + "\\.config\\clrsync"; - -#else - return home + "/.config/clrsync"; -#endif + std::filesystem::path home = normalize_path("~"); + return home / ".config" / "clrsync"; } std::filesystem::path config::get_data_dir() diff --git a/src/core/io/toml_file.cpp b/src/core/io/toml_file.cpp index fa41bc3..c4f7e08 100644 --- a/src/core/io/toml_file.cpp +++ b/src/core/io/toml_file.cpp @@ -8,7 +8,7 @@ namespace clrsync::core::io { toml_file::toml_file(std::string path) { - m_path = expand_user(path); + m_path = normalize_path(path).string(); } Result toml_file::parse() diff --git a/src/core/palette/palette_manager.hpp b/src/core/palette/palette_manager.hpp index 5df0e7c..666e71c 100644 --- a/src/core/palette/palette_manager.hpp +++ b/src/core/palette/palette_manager.hpp @@ -19,7 +19,7 @@ template class palette_manager palette_manager() = default; void load_palettes_from_directory(const std::string &directory_path) { - auto directory_path_expanded = expand_user(directory_path); + std::filesystem::path directory_path_expanded = normalize_path(directory_path); if (!std::filesystem::exists(directory_path_expanded)) return; for (const auto &entry : std::filesystem::directory_iterator(directory_path_expanded)) @@ -34,8 +34,9 @@ template class palette_manager } void save_palette_to_file(const palette &pal, const std::string &directory_path) const { - std::string file_path = directory_path + "/" + pal.name() + ".toml"; - palette_file pal_file(file_path); + std::filesystem::path dir_path = normalize_path(directory_path); + std::filesystem::path file_path = dir_path / (pal.name() + ".toml"); + palette_file pal_file(file_path.string()); pal_file.save_palette(pal); } diff --git a/src/core/theme/theme_template.cpp b/src/core/theme/theme_template.cpp index ac18aca..e0290ba 100644 --- a/src/core/theme/theme_template.cpp +++ b/src/core/theme/theme_template.cpp @@ -8,8 +8,8 @@ namespace clrsync::core { theme_template::theme_template(const std::string &name, const std::string &template_path, const std::string &out_path) - : m_name(name), m_template_path(expand_user(template_path)), - m_output_path(expand_user(out_path)) + : m_name(name), m_template_path(normalize_path(template_path).string()), + m_output_path(normalize_path(out_path).string()) { } @@ -30,7 +30,7 @@ const std::string &theme_template::template_path() const void theme_template::set_template_path(const std::string &path) { - m_template_path = expand_user(path); + m_template_path = normalize_path(path).string(); } const std::string &theme_template::output_path() const @@ -40,7 +40,7 @@ const std::string &theme_template::output_path() const void theme_template::set_output_path(const std::string &path) { - m_output_path = expand_user(path); + m_output_path = normalize_path(path).string(); } Result theme_template::load_template() diff --git a/src/core/utils.cpp b/src/core/utils.cpp index 0501387..a43a6b8 100644 --- a/src/core/utils.cpp +++ b/src/core/utils.cpp @@ -1,5 +1,6 @@ #include "utils.hpp" #include +#include namespace clrsync::core { @@ -15,20 +16,19 @@ std::string get_default_config_path() { const char* env_path = std::getenv("CLRSYNC_CONFIG_PATH"); if (env_path && env_path[0] != '\0') - return expand_user(env_path); + return normalize_path(env_path).string(); - auto home = expand_user("~"); -#ifdef _WIN32 - return home + "\\.config\\clrsync\\config.toml"; - -#else - return home + "/.config/clrsync/config.toml"; -#endif + std::filesystem::path home = normalize_path("~"); + std::filesystem::path config_path = home / ".config" / "clrsync" / "config.toml"; + return config_path.string(); } std::string expand_user(const std::string &path) { - if (!path.empty() && path[0] == '~') + if (path.empty() || path[0] != '~') + return path; + + if (path.length() == 1 || path[1] == '/' || path[1] == '\\') { #ifdef _WIN32 const char *home = std::getenv("USERPROFILE"); @@ -37,9 +37,21 @@ std::string expand_user(const std::string &path) #endif if (!home) return path; + + if (path.length() == 1) + return std::string(home); + return std::string(home) + path.substr(1); } return path; } +std::filesystem::path normalize_path(const std::string &path) +{ + std::string expanded = expand_user(path); + std::filesystem::path fs_path(expanded); + // lexically_normal() resolves . and .. and normalizes separators + return fs_path.lexically_normal(); +} + } // namespace clrsync::core diff --git a/src/core/utils.hpp b/src/core/utils.hpp index e51b415..3e49da9 100644 --- a/src/core/utils.hpp +++ b/src/core/utils.hpp @@ -2,6 +2,7 @@ #define CLRSYNC_CORE_UTILS_HPP #include +#include #include @@ -10,5 +11,6 @@ namespace clrsync::core void print_color_keys(); std::string get_default_config_path(); std::string expand_user(const std::string &path); +std::filesystem::path normalize_path(const std::string &path); } // namespace clrsync::core #endif // CLRSYNC_CORE_UTILS_HPP \ No newline at end of file diff --git a/src/gui/palette_controller.cpp b/src/gui/palette_controller.cpp index de27253..0a10a73 100644 --- a/src/gui/palette_controller.cpp +++ b/src/gui/palette_controller.cpp @@ -30,7 +30,8 @@ void palette_controller::select_palette(const std::string& name) void palette_controller::create_palette(const std::string& name) { - clrsync::core::palette new_palette = m_palette_manager.load_palette_from_file(clrsync::core::config::get_data_dir().string() + "/palettes/cursed.toml"); + std::filesystem::path template_path = clrsync::core::config::get_data_dir() / "palettes" / "cursed.toml"; + clrsync::core::palette new_palette = m_palette_manager.load_palette_from_file(template_path.string()); new_palette.set_name(name); auto dir = clrsync::core::config::instance().palettes_path(); diff --git a/src/gui/template_editor.cpp b/src/gui/template_editor.cpp index 80d67a8..06aeee6 100644 --- a/src/gui/template_editor.cpp +++ b/src/gui/template_editor.cpp @@ -1,6 +1,7 @@ #include "template_editor.hpp" #include "core/config/config.hpp" #include "core/theme/theme_template.hpp" +#include "core/utils.hpp" #include "imgui.h" #include #include @@ -420,7 +421,7 @@ void template_editor::save_template() auto &cfg = clrsync::core::config::instance(); - std::filesystem::path template_file = trimmed_input_path; + std::filesystem::path template_file = clrsync::core::normalize_path(trimmed_input_path); // Ensure the parent directory exists auto parent_dir = template_file.parent_path();