update io::Device::write
This commit is contained in:
@@ -8,27 +8,56 @@
|
||||
#include "../path.hpp"
|
||||
|
||||
namespace io {
|
||||
|
||||
/// @brief Device interface for file system operations
|
||||
class Device {
|
||||
public:
|
||||
virtual ~Device() = default;
|
||||
|
||||
/// @brief Resolve path to the filesystem path
|
||||
virtual std::filesystem::path resolve(std::string_view path) = 0;
|
||||
|
||||
virtual void write(std::string_view path, const void* data, size_t size) = 0;
|
||||
/// @brief Open file for writing
|
||||
/// @throw std::runtime_error if file cannot be opened
|
||||
virtual std::unique_ptr<std::ostream> write(std::string_view path) = 0;
|
||||
|
||||
/// @brief Open file for reading
|
||||
/// @throw std::runtime_error if file cannot be opened
|
||||
virtual std::unique_ptr<std::istream> read(std::string_view path) = 0;
|
||||
|
||||
/// @brief Get file size in bytes
|
||||
virtual size_t size(std::string_view path) = 0;
|
||||
|
||||
/// @brief Check if file or directory exists
|
||||
virtual bool exists(std::string_view path) = 0;
|
||||
|
||||
/// @brief Check if path is a directory
|
||||
virtual bool isdir(std::string_view path) = 0;
|
||||
|
||||
/// @brief Check if path is a regular file
|
||||
virtual bool isfile(std::string_view path) = 0;
|
||||
|
||||
/// @brief Create directory
|
||||
/// @return true if directory was created
|
||||
virtual bool mkdir(std::string_view path) = 0;
|
||||
|
||||
/// @brief Create directories recursively
|
||||
/// @return true if directory was created
|
||||
virtual bool mkdirs(std::string_view path) = 0;
|
||||
|
||||
/// @brief Remove file or empty directory
|
||||
/// @return true if file or directory was removed
|
||||
virtual bool remove(std::string_view path) = 0;
|
||||
|
||||
/// @brief Remove all files and directories in the folder recursively
|
||||
/// @return number of removed files and directories
|
||||
virtual uint64_t removeAll(std::string_view path) = 0;
|
||||
|
||||
/// @brief List directory contents
|
||||
virtual std::unique_ptr<PathsGenerator> list(std::string_view path) = 0;
|
||||
};
|
||||
|
||||
/// @brief Subdevice is a wrapper around another device limited to a directory
|
||||
class SubDevice : public Device {
|
||||
public:
|
||||
SubDevice(
|
||||
@@ -41,8 +70,8 @@ namespace io {
|
||||
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);
|
||||
std::unique_ptr<std::ostream> write(std::string_view path) override {
|
||||
return parent->write((root / path).pathPart());
|
||||
}
|
||||
|
||||
std::unique_ptr<std::istream> read(std::string_view path) override {
|
||||
|
||||
@@ -26,13 +26,13 @@ fs::path StdfsDevice::resolve(std::string_view path) {
|
||||
return root / fs::u8path(io::path(std::string(path)).normalized().string());
|
||||
}
|
||||
|
||||
void StdfsDevice::write(std::string_view path, const void* data, size_t size) {
|
||||
std::unique_ptr<std::ostream> StdfsDevice::write(std::string_view path) {
|
||||
auto resolved = resolve(path);
|
||||
std::ofstream output(resolved, std::ios::binary);
|
||||
if (!output.is_open()) {
|
||||
auto output = std::make_unique<std::ofstream>(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);
|
||||
return output;
|
||||
}
|
||||
|
||||
std::unique_ptr<std::istream> StdfsDevice::read(std::string_view path) {
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
#include "Device.hpp"
|
||||
|
||||
namespace io {
|
||||
/// @brief Device based on the standard filesystem
|
||||
class StdfsDevice : public Device {
|
||||
public:
|
||||
StdfsDevice(std::filesystem::path root, bool createDirectory = true);
|
||||
|
||||
std::filesystem::path resolve(std::string_view path) override;
|
||||
void write(std::string_view path, const void* data, size_t size) override;
|
||||
std::unique_ptr<std::ostream> write(std::string_view path) override;
|
||||
std::unique_ptr<std::istream> read(std::string_view path) override;
|
||||
size_t size(std::string_view path) override;
|
||||
bool exists(std::string_view path) override;
|
||||
|
||||
Reference in New Issue
Block a user