|
|
|
@ -19,10 +19,7 @@
@@ -19,10 +19,7 @@
|
|
|
|
|
|
|
|
|
|
namespace vfs { |
|
|
|
|
|
|
|
|
|
struct error { |
|
|
|
|
std::string message; |
|
|
|
|
error(std::string message) : message(message) { spdlog::error(message); } |
|
|
|
|
}; |
|
|
|
|
typedef unsigned char ftype; |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A null-terminated path object. |
|
|
|
@ -46,6 +43,8 @@ public:
@@ -46,6 +43,8 @@ public:
|
|
|
|
|
Expects(data.back() == '\0'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
zpath_view() : storage({0}), view(storage) {} |
|
|
|
|
|
|
|
|
|
zpath_view(zpath_view &&o) : storage(std::move(o.storage)), view(o.view) {} |
|
|
|
|
|
|
|
|
|
zpath_view( |
|
|
|
@ -55,6 +54,17 @@ public:
@@ -55,6 +54,17 @@ public:
|
|
|
|
|
: storage(other.storage), |
|
|
|
|
view(storage.size() > 0 ? storage : other.view) {} |
|
|
|
|
|
|
|
|
|
bool operator==(const zpath_view &o) const { |
|
|
|
|
if (size() != o.size()) |
|
|
|
|
return false; |
|
|
|
|
for (int i = 0; i < size(); i++) { |
|
|
|
|
if (view[i] != o.view[i]) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
zpath_view &operator=(zpath_view &&o) { |
|
|
|
|
storage = std::move(o.storage); |
|
|
|
|
view = o.view; |
|
|
|
@ -102,11 +112,6 @@ public:
@@ -102,11 +112,6 @@ public:
|
|
|
|
|
auto data() { return view.data(); } |
|
|
|
|
auto empty() { return view.empty(); } |
|
|
|
|
|
|
|
|
|
zpath_view operator/(zpath_view other) { return join_into(other); } |
|
|
|
|
zpath_view operator/(auto other) { return join_into(zpath_view(other)); } |
|
|
|
|
zpath_view operator/(zpath_view other) const { return join(other); } |
|
|
|
|
zpath_view operator/(auto other) const { return join(zpath_view(other)); } |
|
|
|
|
|
|
|
|
|
static void merge_paths(std::vector<char> &first_inout, |
|
|
|
|
const std::span<const char> other) { |
|
|
|
|
Expects(other.back() == '\0'); |
|
|
|
@ -139,8 +144,42 @@ public:
@@ -139,8 +144,42 @@ public:
|
|
|
|
|
merge_paths(inner, other.get_span()); |
|
|
|
|
return zpath_view(inner); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
zpath_view directory() const { |
|
|
|
|
auto last_slash = std::string_view(view.begin(), view.end() - 1).rfind('/'); |
|
|
|
|
return zpath_view(std::span(view.begin(), last_slash)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const zpath_view filename() const { |
|
|
|
|
auto dot = str().rfind('/'); |
|
|
|
|
if (dot != std::string::npos) { |
|
|
|
|
return zpath_view(std::span(view.begin() + dot + 1, view.end())); |
|
|
|
|
} |
|
|
|
|
return *this; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const zpath_view ext() const { |
|
|
|
|
auto dot = str().rfind('.'); |
|
|
|
|
if (dot != std::string::npos) { |
|
|
|
|
return zpath_view(std::span(view.begin() + dot + 1, view.end())); |
|
|
|
|
} |
|
|
|
|
return zpath_view(std::span(view.end() - 1, view.end())); |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
zpath_view operator/(zpath_view &&self, const auto other) { |
|
|
|
|
// mutate and return LHS if it is an rvalue reference
|
|
|
|
|
spdlog::info("Move ({}) {} {}", (void *)self.get_span().data(), self.str(), |
|
|
|
|
zpath_view(other).str()); |
|
|
|
|
return std::move(self.join_into(zpath_view(other).get_span())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
zpath_view operator/(const zpath_view &self, const auto other) { |
|
|
|
|
spdlog::info("Copy ({}) {} {}", (void *)self.get_span().data(), self.str(), |
|
|
|
|
zpath_view(other).str()); |
|
|
|
|
return self.join(zpath_view(other).get_span()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename T> class file_node { |
|
|
|
|
private: |
|
|
|
|
typedef std::vector<T> vfs_buffer; |
|
|
|
@ -301,14 +340,23 @@ public:
@@ -301,14 +340,23 @@ public:
|
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
template <typename T> struct vfmount_sdlfile { |
|
|
|
|
template <typename T> class mount { |
|
|
|
|
public: |
|
|
|
|
virtual expected<std::unique_ptr<file_node<T>>, error> |
|
|
|
|
open_read(zpath_view path) = 0; |
|
|
|
|
|
|
|
|
|
virtual expected<std::unique_ptr<file_node<T>>, error> |
|
|
|
|
open_write(zpath_view path) = 0; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
template <typename T> struct mount_sdlfile : mount<T> { |
|
|
|
|
private: |
|
|
|
|
typedef std::unique_ptr<file_node<T>> file_ptr; |
|
|
|
|
|
|
|
|
|
public: |
|
|
|
|
const zpath_view location; |
|
|
|
|
|
|
|
|
|
vfmount_sdlfile(zpath_view path) : location(path) {} |
|
|
|
|
mount_sdlfile(zpath_view path) : location(path) {} |
|
|
|
|
|
|
|
|
|
expected<file_ptr, error> open_with_mode(zpath_view path, const char *mode) { |
|
|
|
|
auto npath = location / path; |
|
|
|
@ -327,9 +375,11 @@ public:
@@ -327,9 +375,11 @@ public:
|
|
|
|
|
return open_with_mode(path, "r"); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
expected<file_ptr, error> open_readwrite(zpath_view path) { |
|
|
|
|
expected<file_ptr, error> open_write(zpath_view path) { |
|
|
|
|
return open_with_mode(path, "w"); |
|
|
|
|
}; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
typedef mount<ftype> fsmount; |
|
|
|
|
|
|
|
|
|
}; // namespace vfs
|
|
|
|
|