fix: normalize paths

This commit is contained in:
2025-12-17 02:58:23 +03:00
parent 1c2486d476
commit ef0854aa39
8 changed files with 38 additions and 26 deletions

View File

@@ -35,13 +35,8 @@ Result<void> config::initialize(std::unique_ptr<clrsync::core::io::file> 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()

View File

@@ -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<void> toml_file::parse()

View File

@@ -19,7 +19,7 @@ template <typename FileType> 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 <typename FileType> 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<FileType> 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<FileType> pal_file(file_path.string());
pal_file.save_palette(pal);
}

View File

@@ -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<void> theme_template::load_template()

View File

@@ -1,5 +1,6 @@
#include "utils.hpp"
#include <iostream>
#include <filesystem>
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

View File

@@ -2,6 +2,7 @@
#define CLRSYNC_CORE_UTILS_HPP
#include <string>
#include <filesystem>
#include <core/palette/color_keys.hpp>
@@ -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

View File

@@ -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();

View File

@@ -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 <filesystem>
#include <fstream>
@@ -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();