ona/source/coral/files.cpp

91 lines
3.1 KiB
C++
Raw Normal View History

2023-02-19 16:50:29 +00:00
export module coral.files;
2023-02-19 16:50:29 +00:00
import coral;
2023-02-19 16:50:29 +00:00
export namespace coral {
2023-02-26 01:16:53 +00:00
/**
* [reader] that has a known range of data and may attempt to traverse it freely.
*/
struct file_reader : public reader {
2023-02-26 01:16:53 +00:00
/**
* Attempts to seek to the position in the file defined by `offset`, returning the literal absolute position
* that was actually sought to or a [io_error] if the operation failed for whatever reason.
*/
virtual expected<u64, io_error> seek(u64 offset) = 0;
2023-02-26 01:16:53 +00:00
/**
* Attempts to get the current cursor position in the file, returning the literal absolute position of it or a
* [io_error] if the operation failed for whatever reason.
*/
virtual expected<u64, io_error> tell() = 0;
};
/**
2023-03-02 20:00:46 +00:00
* Enumerates a file tree recursively, returning the absolute path of each file within.
*/
2023-03-02 20:00:46 +00:00
struct file_walker : public enumerator<expected<slice<char const>, io_error>> {
virtual ~file_walker() {}
/**
* See [enumerator::enumerate].
*/
virtual bool enumerate() = 0;
/**
* Returns the last-enumerated path as a sequence of `char`s or an [io_error] if the last enumeration did not
* successfully complete.
*/
virtual expected<slice<char const>, io_error> value() = 0;
};
2023-02-26 01:16:53 +00:00
/**
* [writer] that has a known range of data and may attempt to traverse it freely.
*/
struct file_writer : public writer {
2023-02-26 01:16:53 +00:00
/**
* Attempts to seek to the position in the file defined by `offset`, returning the literal absolute position
* that was actually sought to or a [io_error] if the operation failed for whatever reason.
*/
virtual expected<u64, io_error> seek(u64 offset) = 0;
2023-02-26 01:16:53 +00:00
/**
* Attempts to get the current cursor position in the file, returning the literal absolute position of it or a
* [io_error] if the operation failed for whatever reason.
*/
virtual expected<u64, io_error> tell() = 0;
};
/**
* Platform-generalized file system interface.
*/
2023-03-02 20:00:46 +00:00
struct file_system {
virtual ~file_system() {};
/**
* Attempts to read the file in `target_path`, calling `then` if it was successfully opened for reading and
* passing the [file_reader] context along.
*
* See [file_reader] for more information on how to read from the file.
*/
2023-03-02 20:00:46 +00:00
virtual void read_file(slice<char const> const & target_path, closure<void(file_reader &)> const & then) {}
/**
* Attempts to walk the file tree from `target_path`, calling `then` if it was successfully opened for walking
* and passing the [file_walker] context along.
*
* See [file_walker] for more information on how to traverse the file tree.
*/
2023-03-02 20:00:46 +00:00
virtual void walk_files(slice<char const> const & target_path, closure<void(file_walker &)> const & then) {}
/**
* Attempts to write a file in the file system located at `target_path`, calling `then` if it was successfully
* created and / or opened for writing and passing the [file_writer] context along.
*
* See [file_writer] for more information on how to write to the file.
*
* *Note*: Any file already existing at `target_path` will be overwritten to create a new file for writing.
*/
2023-03-02 20:00:46 +00:00
virtual void write_file(slice<char const> const & target_path, closure<void(file_writer &)> const & then) {}
};
}