11#if !defined(GOLXZN_OS_FILESYSTEM)
12#define GOLXZN_OS_FILESYSTEM
19#include <unordered_map>
21#if defined(GOLXZN_OS_ALIASES)
22#include <golxzn/os/aliases.hpp>
27#if !defined(GOLXZN_OS_ALIASES)
30using byte = std::byte;
31using usize = std::size_t;
41 using const_pointer =
const T *;
43 template<
class Iterator>
44 constexpr data_view(Iterator begin, Iterator end) noexcept : m_data{ &*begin }, m_length{ end - begin } {}
46 template<
class Container>
47 constexpr data_view(
const Container &container) noexcept
48 : data_view{ std::begin(container), std::end(container) } {}
50 [[nodiscard]]
constexpr const_pointer data() const noexcept{
return m_data; }
51 [[nodiscard]]
constexpr usize size() const noexcept {
return m_length; }
53 [[nodiscard]]
constexpr const_pointer begin() const noexcept{
return m_data; }
54 [[nodiscard]]
constexpr const_pointer end() const noexcept{
return std::next(m_data, m_length); }
57 const_pointer m_data{};
58 const usize m_length{};
73 static constexpr std::wstring_view
none{ L
"" };
74 static constexpr std::wstring::value_type
separator{ L
'/' };
108 operator
bool() const noexcept;
147 static void associate(std::wstring_view protocol, std::wstring &&prefix)
noexcept;
162 [[nodiscard]]
static std::vector<byte>
read_binary(
const std::wstring_view path);
171 [[nodiscard]]
static std::string
read_text(
const std::wstring_view path);
180 template<
class Custom>
181 [[nodiscard]]
static auto read_binary(
const std::wstring_view path)
182 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, Custom>;
191 template<
class Custom>
192 [[nodiscard]]
static auto read_text(
const std::wstring_view path)
193 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, Custom>;
202 template<
class Custom>
204 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, std::shared_ptr<Custom>>;
213 template<
class Custom>
215 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, std::shared_ptr<Custom>>;
224 template<
class Custom>
226 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, std::unique_ptr<Custom>>;
235 template<
class Custom>
237 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, std::unique_ptr<Custom>>;
252 [[nodiscard]]
static error write_binary(
const std::wstring_view path,
const details::data_view<byte> &data);
261 [[nodiscard]]
static error write_binary(
const std::wstring_view path,
const std::initializer_list<byte> data);
270 [[nodiscard]]
static error append_binary(
const std::wstring_view path,
const details::data_view<byte> &data);
279 [[nodiscard]]
static error append_binary(
const std::wstring_view path,
const std::initializer_list<byte> data);
288 [[nodiscard]]
static error write_text(
const std::wstring_view path,
const std::string_view text);
297 [[nodiscard]]
static error append_text(
const std::wstring_view path,
const std::string_view text);
306 [[nodiscard]]
static error write_text(
const std::wstring_view path,
const std::wstring_view text);
315 [[nodiscard]]
static error append_text(
const std::wstring_view path,
const std::wstring_view text);
326 [[nodiscard]]
static std::wstring_view
get_association(
const std::wstring_view protocol)
noexcept;
368 static
void join(std::wstring &left, std::wstring_view right) noexcept;
377 [[nodiscard]] static std::wstring
join(std::wstring_view left, std::wstring_view right) noexcept;
400 [[nodiscard]] static std::wstring
normalize(std::wstring_view path);
412 [[nodiscard]] static
bool exists(const std::wstring_view path) noexcept;
420 [[nodiscard]] static
bool is_file(const std::wstring_view path);
480 [[nodiscard]] static std::vector<std::wstring>
entries(const std::wstring_view path);
488 [[nodiscard]] static std::wstring
to_wide(const std::string_view str) noexcept;
496 [[nodiscard]] static std::
string to_narrow(const std::wstring_view wstr) noexcept;
513 static
void associate(const std::string_view protocol, const std::string_view prefix) noexcept;
516 [[nodiscard]] static std::vector<
byte>
read_binary(const std::string_view path);
519 [[nodiscard]] static std::
string read_text(const std::string_view path);
522 template<class Custom>
523 [[nodiscard]] static auto
read_binary(const std::string_view path)
524 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<
byte>>, Custom>;
527 template<class Custom>
528 [[nodiscard]] static auto
read_text(const std::string_view path)
529 -> std::enable_if_t<std::is_constructible_v<Custom, std::
string>, Custom>;
532 template<class Custom>
534 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<
byte>>, std::shared_ptr<Custom>>;
537 template<class Custom>
539 -> std::enable_if_t<std::is_constructible_v<Custom, std::
string>, std::shared_ptr<Custom>>;
542 template<class Custom>
544 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<
byte>>, std::unique_ptr<Custom>>;
547 template<class Custom>
549 -> std::enable_if_t<std::is_constructible_v<Custom, std::
string>, std::unique_ptr<Custom>>;
552 [[nodiscard]] static
error write_binary(const std::string_view path, const details::data_view<
byte> &data);
555 [[nodiscard]] static
error write_binary(const std::string_view path, const std::initializer_list<
byte> data);
558 [[nodiscard]] static
error append_binary(const std::string_view path, const details::data_view<
byte> &data);
561 [[nodiscard]] static
error append_binary(const std::string_view path, const std::initializer_list<
byte> data);
564 [[nodiscard]] static
error write_text(const std::string_view path, const std::string_view text);
567 [[nodiscard]] static
error append_text(const std::string_view path, const std::string_view text);
570 [[nodiscard]] static
error write_text(const std::string_view path, const std::wstring_view text);
573 [[nodiscard]] static
error append_text(const std::string_view path, const std::wstring_view text);
576 [[nodiscard]] static std::wstring_view
get_association(const std::string_view protocol) noexcept;
579 static
void join(std::
string &left, std::string_view right) noexcept;
582 [[nodiscard]] static std::
string join(std::string_view left, std::string_view right) noexcept;
591 [[nodiscard]] static std::wstring
normalize(const std::string_view path);
594 [[nodiscard]] static
bool exists(const std::string_view path) noexcept;
597 [[nodiscard]] static
bool is_file(const std::string_view path);
615 [[nodiscard]] static std::vector<std::
string>
entries(const std::string_view path);
621 static std::wstring appname;
624 static std::wstring_view get_protocol(const std::wstring_view path) noexcept;
625 static std::wstring replace_association_prefix(std::wstring_view path) noexcept;
626 static std::wstring setup_assets_directories(const std::wstring_view assets_path);
627 static std::wstring setup_user_data_directory();
634template<class Custom>
636 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<
byte>>, Custom> {
639template<
class Custom>
641 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, Custom> {
645template<
class Custom>
647 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, std::shared_ptr<Custom>> {
648 return std::make_shared<Custom>(
read_binary(path));
651template<
class Custom>
653 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, std::shared_ptr<Custom>> {
654 return std::make_shared<Custom>(
read_text(path));
657template<
class Custom>
659 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, std::unique_ptr<Custom>> {
660 return std::make_unique<Custom>(
read_binary(path));
663template<
class Custom>
665 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, std::unique_ptr<Custom>> {
666 return std::make_unique<Custom>(
read_text(path));
670template<
class Custom>
672 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, Custom> {
675template<
class Custom>
677 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, Custom> {
681template<
class Custom>
683 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, std::shared_ptr<Custom>> {
684 return std::make_shared<Custom>(
read_binary(path));
687template<
class Custom>
689 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, std::shared_ptr<Custom>> {
690 return std::make_shared<Custom>(
read_text(path));
693template<
class Custom>
695 -> std::enable_if_t<std::is_constructible_v<Custom, std::vector<byte>>, std::unique_ptr<Custom>> {
696 return std::make_unique<Custom>(
read_binary(path));
699template<
class Custom>
701 -> std::enable_if_t<std::is_constructible_v<Custom, std::string>, std::unique_ptr<Custom>> {
702 return std::make_unique<Custom>(
read_text(path));
709namespace gxzn = golxzn;
Golxzn Resource Manager.
Definition filesystem.hpp:68
static void join(std::wstring &left, std::wstring_view right) noexcept
Join two paths with a slash.
static const associations_type & associations() noexcept
Get the associations.
static std::wstring to_wide(const std::string_view str) noexcept
Convert a string to wide string.
static std::wstring_view get_association(const std::wstring_view protocol) noexcept
Get the association object.
static std::wstring normalize(std::wstring_view path)
Fix the path separators to the '/' and remove the trailing slash.
static constexpr std::wstring::value_type separator
Path separator.
Definition filesystem.hpp:74
static constexpr std::wstring_view none
Empty wide string.
Definition filesystem.hpp:73
static std::string to_narrow(const std::wstring_view wstr) noexcept
Convert a wide string to string.
static constexpr std::string::value_type separator_narrow
Narrow version of golxzn::os::filesystem::separator.
Definition filesystem.hpp:80
static constexpr std::wstring_view protocol_separator
Protocol separator.
Definition filesystem.hpp:77
std::unordered_map< std::wstring, std::wstring > associations_type
Map of the protocol extensions and their prefixes.
Definition filesystem.hpp:71
static constexpr std::wstring_view default_assets_directory_name
Default assets directory name.
Definition filesystem.hpp:76
static std::wstring_view user_data_directory() noexcept
Get the user data directory object.
static constexpr std::string_view none_narrow
Narrow version of golxzn::os::filesystem::none.
Definition filesystem.hpp:79
static std::wstring_view application_name() noexcept
Get the application name.
static void parent_directory(std::wstring &path) noexcept
Remove the last path component.
static std::wstring_view assets_directory() noexcept
Get the assets directories.
static constexpr std::string_view protocol_separator_narrow
Narrow version of golxzn::os::filesystem::protocol_separator.
Definition filesystem.hpp:83
static constexpr std::wstring_view default_application_name
Default application name.
Definition filesystem.hpp:75
static std::vector< std::wstring > entries(const std::wstring_view path)
Get all entries in a directory.
static std::wstring current_directory()
Get the absolute path of this application.
static const error OK
OK struct.
Definition filesystem.hpp:110
static constexpr std::string_view default_assets_directory_name_narrow
Narrow version of golxzn::os::filesystem::default_assets_directory_name.
Definition filesystem.hpp:82
static constexpr std::string_view default_application_name_narrow
Narrow version of golxzn::os::filesystem::default_application_name.
Definition filesystem.hpp:81
static bool is_file(const std::wstring_view path)
Check if an entry is a file.
static error remove_file(const std::wstring_view path)
Remove a file.
static error remove(const std::wstring_view path)
List an entry (file or directory).
static bool is_directory(const std::wstring_view path)
Check if an entry is a directory.
static error remove_directory(const std::wstring_view path)
Remove a directory (recursively).
static error make_directory(const std::wstring_view path)
Create a directory (recursively).
static bool exists(const std::wstring_view path) noexcept
Check if an entry exists.
static error initialize(const std::wstring_view application_name, const std::wstring_view assets_path=default_assets_directory_name)
Initialize the Resource Manager.
static void associate(std::wstring_view protocol, std::wstring &&prefix) noexcept
Add the association between the protocol and the prefix. (ex. L"res://" and L"user://")
static void set_application_name(const std::wstring_view application_name) noexcept
Sets the application name object.
static std::vector< byte > read_binary(const std::wstring_view path)
Read whole binary file.
static auto read_shared_binary(const std::wstring_view path) -> std::enable_if_t< std::is_constructible_v< Custom, std::vector< byte > >, std::shared_ptr< Custom > >
Construct std::shared_ptr<Custom> by binary data from file.
Definition filesystem.hpp:646
static auto read_unique_text(const std::wstring_view path) -> std::enable_if_t< std::is_constructible_v< Custom, std::string >, std::unique_ptr< Custom > >
Construct std::shared_ptr<Custom> by binary data from file.
Definition filesystem.hpp:664
static auto read_unique_binary(const std::wstring_view path) -> std::enable_if_t< std::is_constructible_v< Custom, std::vector< byte > >, std::unique_ptr< Custom > >
Construct std::shared_ptr<Custom> by binary data from file.
Definition filesystem.hpp:658
static std::string read_text(const std::wstring_view path)
Read whole text file.
static auto read_shared_text(const std::wstring_view path) -> std::enable_if_t< std::is_constructible_v< Custom, std::string >, std::shared_ptr< Custom > >
Construct std::shared_ptr<Custom> by binary data from file.
Definition filesystem.hpp:652
static error append_text(const std::wstring_view path, const std::string_view text)
Append text data to a file.
static error append_text(const std::wstring_view path, const std::wstring_view text)
Append text data to a file.
static error write_text(const std::wstring_view path, const std::wstring_view text)
Write text data to a file.
static error append_binary(const std::wstring_view path, const details::data_view< byte > &data)
Append binary data to a file.
static error write_binary(const std::wstring_view path, const std::initializer_list< byte > data)
Write binary data to a file.
static error write_binary(const std::wstring_view path, const details::data_view< byte > &data)
Write binary data to a file.
static error write_text(const std::wstring_view path, const std::string_view text)
Write text data to a file.
static error append_binary(const std::wstring_view path, const std::initializer_list< byte > data)
Append binary data to a file.
Struct returned by some methods to tell if there's an error.
Definition filesystem.hpp:91
bool has_error() const noexcept
Checks if there's an error.
std::wstring message
Error message. If it's golxzn::os::filesystem::none, everything is OK.
Definition filesystem.hpp:92