Browse Source

vfs::path uses move semantics more correct

master
alistair 11 months ago
parent
commit
b7e3c0cb2f
  1. 36
      source/tests.cpp
  2. 74
      source/vfs.hpp

36
source/tests.cpp

@ -74,13 +74,20 @@ TEST_CASE("zpath join string") { @@ -74,13 +74,20 @@ TEST_CASE("zpath join string") {
SUBCASE("basic") {
vfs::zpath_view p{"hello"};
auto n = p / "world";
CHECK(strcmp(p.c_str(), "hello/world") == 0);
CHECK(strcmp(p.c_str(), "hello") == 0);
INFO(n.c_str());
CHECK(strcmp(n.c_str(), "hello/world") == 0);
CHECK(!buffers_differ(n.get_span(),
vfs::zpath_view("hello/world").get_span()));
CHECK_EQ(n.str(), std::string("hello/world"));
CHECK_EQ(p.str(), std::string("hello/world"));
CHECK_EQ(p.str(), std::string("hello"));
}
SUBCASE("span copy") {
vfs::zpath_view p{"hello"};
vfs::zpath_view q{p.get_span()};
vfs::zpath_view r{std::span(p.get_span().begin(), p.get_span().end())};
CHECK_EQ(p.str(), q.str());
}
SUBCASE("const zpath") {
const vfs::zpath_view p{"hello"};
@ -100,9 +107,30 @@ TEST_CASE("zpath join string") { @@ -100,9 +107,30 @@ TEST_CASE("zpath join string") {
CHECK_EQ(p.str(), std::string("root/home/user/beans"));
vfs::zpath_view q = p / "Documents";
CHECK_EQ(p.str(), std::string("root/home/user/beans"));
CHECK_EQ(q.str(), std::string("root/home/user/beans/Documents"));
}
SUBCASE("divop nonconst") {
vfs::zpath_view p = vfs::zpath_view("root") / "home" / "user" / "beans";
CHECK_EQ(p.str(), std::string("root/home/user/beans"));
vfs::zpath_view q = p / "Documents";
CHECK_EQ(p.str(), std::string("root/home/user/beans"));
CHECK_EQ(q.str(), std::string("root/home/user/beans/Documents"));
}
SUBCASE("substr") {
const vfs::zpath_view p =
vfs::zpath_view("root") / "home" / "user" / "beans.exe";
CHECK_EQ(p.ext().str(), std::string("exe"));
CHECK_EQ(p.filename().str(), std::string("beans.exe"));
const vfs::zpath_view q;
CHECK_EQ(q.filename().str(), std::string(""));
const vfs::zpath_view r{"beans"};
CHECK_EQ(r.filename().str(), std::string("beans"));
CHECK_EQ(r.ext().str(), std::string(""));
}
}
TEST_CASE("merge method") {
@ -124,7 +152,7 @@ TEST_CASE("merge method") { @@ -124,7 +152,7 @@ TEST_CASE("merge method") {
}
TEST_CASE("vfs") {
vfs::vfmount_sdlfile<char> test{"test/vfs"};
vfs::mount_sdlfile<char> test{"test/vfs"};
auto fl = test.open_read("testfile");
@ -141,7 +169,7 @@ TEST_CASE("vfs") { @@ -141,7 +169,7 @@ TEST_CASE("vfs") {
REQUIRE(res->size() != 0);
REQUIRE((*fl)->close() == false);
auto flw = test.open_readwrite("testfilew");
auto flw = test.open_write("testfilew");
REQUIRE(flw.has_value());
auto testw = std::string("alskdahldha");

74
source/vfs.hpp

@ -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

Loading…
Cancel
Save