migrate from std::filesystem::path to io::path (WIP)

This commit is contained in:
MihailRis
2025-01-30 22:23:13 +03:00
parent 1e22882284
commit e0314803c0
72 changed files with 1189 additions and 791 deletions
+76
View File
@@ -0,0 +1,76 @@
#pragma once
#include <string>
#include <filesystem>
#include "../path.hpp"
namespace io {
class Device {
public:
virtual ~Device() = default;
virtual std::filesystem::path resolve(std::string_view path) = 0;
virtual void write(std::string_view path, const void* data, size_t size) = 0;
virtual void read(std::string_view path, void* data, size_t size) = 0;
virtual size_t size(std::string_view path) = 0;
virtual bool exists(std::string_view path) = 0;
virtual bool isdir(std::string_view path) = 0;
virtual bool isfile(std::string_view path) = 0;
virtual void mkdirs(std::string_view path) = 0;
virtual bool remove(std::string_view path) = 0;
virtual uint64_t removeAll(std::string_view path) = 0;
};
class SubDevice : public Device {
public:
SubDevice(std::shared_ptr<Device> parent, const std::string& path)
: parent(std::move(parent)), root(path) {}
std::filesystem::path resolve(std::string_view path) override {
return parent->resolve((root / path).pathPart());
}
void write(std::string_view path, const void* data, size_t size) override {
parent->write((root / path).pathPart(), data, size);
}
void read(std::string_view path, void* data, size_t size) override {
parent->read((root / path).pathPart(), data, size);
}
size_t size(std::string_view path) override {
return parent->size((root / path).pathPart());
}
bool exists(std::string_view path) override {
return parent->exists((root / path).pathPart());
}
bool isdir(std::string_view path) override {
return parent->isdir((root / path).pathPart());
}
bool isfile(std::string_view path) override {
return parent->isfile((root / path).pathPart());
}
void mkdirs(std::string_view path) override {
parent->mkdirs((root / path).pathPart());
}
bool remove(std::string_view path) override {
return parent->remove((root / path).pathPart());
}
uint64_t removeAll(std::string_view path) override {
return parent->removeAll((root / path).pathPart());
}
private:
std::shared_ptr<Device> parent;
path root;
};
}
+63
View File
@@ -0,0 +1,63 @@
#include "StdfsDevice.hpp"
#include <fstream>
#include <filesystem>
using namespace io;
std::filesystem::path StdfsDevice::resolve(std::string_view path) {
return root / std::filesystem::u8path(path);
}
void StdfsDevice::write(std::string_view path, const void* data, size_t size) {
auto resolved = resolve(path);
std::ofstream output(resolved, std::ios::binary);
if (!output.is_open()) {
throw std::runtime_error("could not to open file " + resolved.u8string());
}
output.write((const char*)data, size);
}
void StdfsDevice::read(std::string_view path, void* data, size_t size) {
auto resolved = resolve(path);
std::ifstream input(resolved, std::ios::binary);
if (!input.is_open()) {
throw std::runtime_error("could not to open file " + resolved.u8string());
}
input.read((char*)data, size);
}
size_t StdfsDevice::size(std::string_view path) {
auto resolved = resolve(path);
return std::filesystem::file_size(resolved);
}
bool StdfsDevice::exists(std::string_view path) {
auto resolved = resolve(path);
return std::filesystem::exists(resolved);
}
bool StdfsDevice::isdir(std::string_view path) {
auto resolved = resolve(path);
return std::filesystem::is_directory(resolved);
}
bool StdfsDevice::isfile(std::string_view path) {
auto resolved = resolve(path);
return std::filesystem::is_regular_file(resolved);
}
void StdfsDevice::mkdirs(std::string_view path) {
auto resolved = resolve(path);
std::filesystem::create_directories(resolved);
}
bool StdfsDevice::remove(std::string_view path) {
auto resolved = resolve(path);
return std::filesystem::remove(resolved);
}
uint64_t StdfsDevice::removeAll(std::string_view path) {
auto resolved = resolve(path);
return std::filesystem::remove_all(resolved);
}
+21
View File
@@ -0,0 +1,21 @@
#include "Device.hpp"
namespace io {
class StdfsDevice : public Device {
public:
StdfsDevice(std::filesystem::path root) : root(std::move(root)) {}
std::filesystem::path resolve(std::string_view path) override;
void write(std::string_view path, const void* data, size_t size) override;
void read(std::string_view path, void* data, size_t size) override;
size_t size(std::string_view path) override;
bool exists(std::string_view path) override;
bool isdir(std::string_view path) override;
bool isfile(std::string_view path) override;
void mkdirs(std::string_view path) override;
bool remove(std::string_view path) override;
uint64_t removeAll(std::string_view path) override;
private:
std::filesystem::path root;
};
}