* use std::filesystem::u8path to support utf-8

* don't sanitize paths in settings_parser since it removes the colon ':', preventing the usage of absolute paths on Windows like 'C:\aa\bb'
This commit is contained in:
otavepto 2024-04-28 00:45:11 +03:00 committed by otavepto
parent 303cdd2878
commit e2980d100e
5 changed files with 25 additions and 29 deletions

View File

@ -7,15 +7,16 @@
static inline bool remove_file(const std::string &file)
{
if (!std::filesystem::exists(file)) {
const std::filesystem::u8path p_file(std::filesystem::u8path(file));
if (!std::filesystem::exists(p_file)) {
return true;
}
if (std::filesystem::is_directory(file)) {
if (std::filesystem::is_directory(p_file)) {
return false;
}
return std::filesystem::remove(file);
return std::filesystem::remove(p_file);
}
static inline bool remove_file(const std::wstring &file)
@ -32,4 +33,4 @@ static inline bool remove_file(const std::wstring &file)
}
#endif // _TEST_CRASH_PRINTER_HELPER_H
#endif // _TEST_CRASH_PRINTER_HELPER_H

View File

@ -57,10 +57,11 @@ static void copy_file(const std::string &src_filepath, const std::string &dst_fi
try
{
PRINT_DEBUG("copying file '%s' to '%s'", src_filepath.c_str(), dst_filepath.c_str());
const auto src_p = std::filesystem::path(src_filepath);
if (!std::filesystem::exists(src_p) || std::filesystem::is_directory(src_p)) return;
const std::filesystem::path src_p(std::filesystem::u8path(src_filepath));
const auto dst_p = std::filesystem::path(dst_filepath);
if (!common_helpers::file_exist(src_p)) return;
const std::filesystem::path dst_p(std::filesystem::u8path(dst_filepath));
std::filesystem::create_directories(dst_p.parent_path()); // make the folder tree if needed
std::filesystem::copy_file(src_p, dst_p, std::filesystem::copy_options::overwrite_existing);
} catch(...) {}

View File

@ -617,8 +617,8 @@ std::vector<std::string> Local_Storage::get_folders_path(std::string path)
std::vector<std::string> output{};
try
{
const auto path_p = std::filesystem::path(path);
if (!std::filesystem::is_directory(path_p)) return output;
const std::filesystem::path path_p(std::filesystem::u8path(path));
if (!common_helpers::dir_exist(path_p)) return output;
for (const auto &dir_entry :
std::filesystem::directory_iterator(path_p, std::filesystem::directory_options::follow_directory_symlink)) {

View File

@ -198,7 +198,7 @@ static void load_overlay_appearance(class Settings *settings_client, class Setti
PRINT_DEBUG(" Overlay appearance line '%s'='%s'", name.c_str(), value.c_str());
try {
if (name.compare("Font_Override") == 0) {
value = common_helpers::string_strip(Settings::sanitize(value));
value = common_helpers::string_strip(value);
// first try the local settings folder
std::string nfont_override(common_helpers::to_absolute(value, Local_Storage::get_game_settings_path() + "fonts"));
if (!common_helpers::file_exist(nfont_override)) {
@ -513,7 +513,7 @@ static bool parse_local_save(std::string &save_path)
auto ptr = ini.GetValue("user::saves", "local_save_path");
if (!ptr || !ptr[0]) return false;
save_path = common_helpers::to_absolute(common_helpers::string_strip(Settings::sanitize(ptr)), Local_Storage::get_program_path());
save_path = common_helpers::to_absolute(common_helpers::string_strip(ptr), Local_Storage::get_program_path());
if (save_path.size() && save_path.back() != *PATH_SEPARATOR) {
save_path.push_back(*PATH_SEPARATOR);
}
@ -1104,7 +1104,7 @@ static void parse_build_id(class Settings *settings_client, class Settings *sett
// main::general::crash_printer_location
static void parse_crash_printer_location()
{
std::string line(common_helpers::string_strip(Settings::sanitize(ini.GetValue("main::general", "crash_printer_location", ""))));
std::string line(common_helpers::string_strip(ini.GetValue("main::general", "crash_printer_location", "")));
if (line.size()) {
auto crash_path = utf8_decode(common_helpers::to_absolute(line, get_full_program_path()));
if (crash_path.size()) {

View File

@ -21,7 +21,7 @@ static bool create_dir_impl(std::filesystem::path &dirpath)
bool common_helpers::create_dir(const std::string_view &filepath)
{
std::filesystem::path parent(std::filesystem::path(filepath).parent_path());
std::filesystem::path parent(std::filesystem::u8path(filepath).parent_path());
return create_dir_impl(parent);
}
@ -231,17 +231,17 @@ static std::filesystem::path to_absolute_impl(const std::filesystem::path &path,
std::string common_helpers::to_absolute(const std::string_view &path, const std::string_view &base)
{
if (path.empty()) return std::string(path);
if (path.empty()) return {};
auto path_abs = to_absolute_impl(
std::filesystem::path(path),
base.empty() ? std::filesystem::current_path() : std::filesystem::path(base)
std::filesystem::u8path(path),
base.empty() ? std::filesystem::current_path() : std::filesystem::u8path(base)
);
return path_abs.u8string();
}
std::wstring common_helpers::to_absolute(const std::wstring_view &path, const std::wstring_view &base)
{
if (path.empty()) return std::wstring(path);
if (path.empty()) return {};
auto path_abs = to_absolute_impl(
std::filesystem::path(path),
base.empty() ? std::filesystem::current_path() : std::filesystem::path(base)
@ -263,15 +263,13 @@ bool common_helpers::file_exist(const std::filesystem::path &filepath)
bool common_helpers::file_exist(const std::string &filepath)
{
if (filepath.empty()) return false;
std::filesystem::path path(filepath);
return file_exist(path);
return file_exist(std::filesystem::u8path(filepath));
}
bool common_helpers::file_exist(const std::wstring &filepath)
{
if (filepath.empty()) return false;
std::filesystem::path path(filepath);
return file_exist(path);
return file_exist(std::filesystem::path(filepath));
}
bool common_helpers::file_size(const std::filesystem::path &filepath, size_t &size)
@ -285,14 +283,12 @@ bool common_helpers::file_size(const std::filesystem::path &filepath, size_t &si
bool common_helpers::file_size(const std::string &filepath, size_t &size)
{
const auto file_p = std::filesystem::path(filepath);
return file_size(file_p, size);
return file_size(std::filesystem::u8path(filepath), size);
}
bool common_helpers::file_size(const std::wstring &filepath, size_t &size)
{
const auto file_p = std::filesystem::path(filepath);
return file_size(file_p, size);
return file_size(std::filesystem::path(filepath), size);
}
bool common_helpers::dir_exist(const std::filesystem::path &dirpath)
@ -307,13 +303,11 @@ bool common_helpers::dir_exist(const std::filesystem::path &dirpath)
bool common_helpers::dir_exist(const std::string &dirpath)
{
if (dirpath.empty()) return false;
std::filesystem::path path(dirpath);
return dir_exist(path);
return dir_exist(std::filesystem::u8path(dirpath));
}
bool common_helpers::dir_exist(const std::wstring &dirpath)
{
if (dirpath.empty()) return false;
std::filesystem::path path(dirpath);
return dir_exist(path);
return dir_exist(std::filesystem::path(dirpath));
}