Use utils::optional instead of std::optional in filesystem

This commit is contained in:
Celtic Minstrel 2024-07-23 09:05:53 -04:00
parent 62d9ebecc2
commit ffde1cb21c
2 changed files with 25 additions and 25 deletions

View file

@ -664,11 +664,11 @@ static void setup_user_data_dir()
/**
* @return the path to the My Games directory on success or an empty string on failure
*/
static std::optional<std::string> get_games_path()
static utils::optional<std::string> get_games_path()
{
PWSTR docs_path = nullptr;
HRESULT res = SHGetKnownFolderPath(FOLDERID_Documents, KF_FLAG_CREATE, nullptr, &docs_path);
std::optional<std::string> path = std::nullopt;
utils::optional<std::string> path = std::nullopt;
if(res == S_OK) {
bfs::path games_path = bfs::path(docs_path) / "My Games";
@ -725,7 +725,7 @@ void set_user_data_dir(std::string newprefdir)
bfs::path dir;
if(newprefdir[0] == '~') {
#ifdef _WIN32
std::optional<std::string> games_path = get_games_path();
utils::optional<std::string> games_path = get_games_path();
if(games_path) {
create_directory_if_missing(*games_path);
dir = *games_path;
@ -1551,7 +1551,7 @@ const std::vector<std::string>& get_binary_paths(const std::string& type)
return res;
}
std::optional<std::string> get_binary_file_location(const std::string& type, const std::string& filename)
utils::optional<std::string> get_binary_file_location(const std::string& type, const std::string& filename)
{
// We define ".." as "remove everything before" this is needed because
// on the one hand allowing ".." would be a security risk but
@ -1567,7 +1567,7 @@ std::optional<std::string> get_binary_file_location(const std::string& type, con
}
if(!is_legal_file(filename)) {
return std::nullopt;
return utils::nullopt;
}
std::string result;
@ -1594,16 +1594,16 @@ std::optional<std::string> get_binary_file_location(const std::string& type, con
if(result.empty()) {
DBG_FS << " not found";
return std::nullopt;
return utils::nullopt;
} else {
return result;
}
}
std::optional<std::string> get_binary_dir_location(const std::string& type, const std::string& filename)
utils::optional<std::string> get_binary_dir_location(const std::string& type, const std::string& filename)
{
if(!is_legal_file(filename)) {
return std::nullopt;
return utils::nullopt;
}
for(const std::string& bp : get_binary_paths(type)) {
@ -1617,13 +1617,13 @@ std::optional<std::string> get_binary_dir_location(const std::string& type, cons
}
DBG_FS << " not found";
return std::nullopt;
return utils::nullopt;
}
std::optional<std::string> get_wml_location(const std::string& filename, const std::string& current_dir)
utils::optional<std::string> get_wml_location(const std::string& filename, const std::string& current_dir)
{
if(!is_legal_file(filename)) {
return std::nullopt;
return utils::nullopt;
}
assert(game_config::path.empty() == false);
@ -1648,7 +1648,7 @@ std::optional<std::string> get_wml_location(const std::string& filename, const s
if(result.empty() || !file_exists(result)) {
DBG_FS << " not found";
return std::nullopt;
return utils::nullopt;
} else {
DBG_FS << " found: '" << result.string() << "'";
return result.string();
@ -1691,11 +1691,11 @@ std::string get_short_wml_path(const std::string& filename)
return filename;
}
std::optional<std::string> get_independent_binary_file_path(const std::string& type, const std::string& filename)
utils::optional<std::string> get_independent_binary_file_path(const std::string& type, const std::string& filename)
{
auto bp = get_binary_file_location(type, filename);
if(!bp) {
return std::nullopt;
return utils::nullopt;
}
bfs::path full_path{bp.value()};
@ -1739,7 +1739,7 @@ std::string sanitize_path(const std::string& path)
// Return path to localized counterpart of the given file, if any, or empty string.
// Localized counterpart may also be requested to have a suffix to base name.
std::optional<std::string> get_localized_path(const std::string& file, const std::string& suff)
utils::optional<std::string> get_localized_path(const std::string& file, const std::string& suff)
{
std::string dir = filesystem::directory_name(file);
std::string base = filesystem::base_name(file);
@ -1776,10 +1776,10 @@ std::optional<std::string> get_localized_path(const std::string& file, const std
}
}
return std::nullopt;
return utils::nullopt;
}
std::optional<std::string> get_addon_id_from_path(const std::string& location)
utils::optional<std::string> get_addon_id_from_path(const std::string& location)
{
std::string full_path = normalize_path(location, true);
std::string addons_path = normalize_path(get_addons_dir(), true);
@ -1791,7 +1791,7 @@ std::optional<std::string> get_addon_id_from_path(const std::string& location)
}
}
return std::nullopt;
return utils::nullopt;
}
} // namespace filesystem

View file

@ -24,13 +24,13 @@
#include <fstream>
#include <iosfwd>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "exceptions.hpp"
#include "game_version.hpp"
#include "global.hpp"
#include "utils/optional_fwd.hpp"
namespace game_config {
extern std::string path;
@ -463,17 +463,17 @@ NOT_DANGLING const std::vector<std::string>& get_binary_paths(const std::string&
/**
* Returns a complete path to the actual file of a given @a type, if it exists.
*/
std::optional<std::string> get_binary_file_location(const std::string& type, const std::string& filename);
utils::optional<std::string> get_binary_file_location(const std::string& type, const std::string& filename);
/**
* Returns a complete path to the actual directory of a given @a type, if it exists.
*/
std::optional<std::string> get_binary_dir_location(const std::string &type, const std::string &filename);
utils::optional<std::string> get_binary_dir_location(const std::string &type, const std::string &filename);
/**
* Returns a complete path to the actual WML file or directory, if either exists.
*/
std::optional<std::string> get_wml_location(const std::string &filename,
utils::optional<std::string> get_wml_location(const std::string &filename,
const std::string &current_dir = std::string());
/**
@ -488,7 +488,7 @@ std::string get_short_wml_path(const std::string &filename);
* images, units/konrad-fighter.png ->
* data/campaigns/Heir_To_The_Throne/images/units/konrad-fighter.png
*/
std::optional<std::string> get_independent_binary_file_path(const std::string& type, const std::string &filename);
utils::optional<std::string> get_independent_binary_file_path(const std::string& type, const std::string &filename);
/**
* Returns the appropriate invocation for a Wesnoth-related binary, assuming
@ -502,12 +502,12 @@ std::string get_program_invocation(const std::string &program_name);
/**
* Returns the localized version of the given filename, if it exists.
*/
std::optional<std::string> get_localized_path(const std::string& file, const std::string& suff = "");
utils::optional<std::string> get_localized_path(const std::string& file, const std::string& suff = "");
/**
* Returns the add-on ID from a path.
* aka the directory directly following the "add-ons" folder, or an empty string if none is found.
*/
std::optional<std::string> get_addon_id_from_path(const std::string& location);
utils::optional<std::string> get_addon_id_from_path(const std::string& location);
}