From 7a6731df8e6aaf25ea98e78b7cc763347c44d8c7 Mon Sep 17 00:00:00 2001 From: kayomn Date: Fri, 24 Feb 2023 17:23:18 +0000 Subject: [PATCH] Expose way to enumerate over file paths in coral::fs --- source/coral/files.cpp | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/source/coral/files.cpp b/source/coral/files.cpp index 100a35c..dd5ba3c 100644 --- a/source/coral/files.cpp +++ b/source/coral/files.cpp @@ -17,11 +17,11 @@ export namespace coral { */ static char const seperator = '/'; - constexpr path() : buffer{0} { + constexpr path() { this->buffer[max] = max; } - template constexpr path(char const(&text)[text_size]) : path{} { + template constexpr path(char const(&text)[text_size]) { static_assert(text_size <= max); for (usize i = 0; i < text_size; i += 1) this->buffer[i] = text[i]; @@ -53,8 +53,7 @@ export namespace coral { } /** - * Compares the path to `that`, returning the difference between the two paths or `0` if - * they are identical. + * Compares the path to `that`, returning the difference between the two paths or `0` if they are identical. */ constexpr size compare(path const & that) const { return coral::compare(this->as_slice().as_bytes(), that.as_slice().as_bytes()); @@ -68,8 +67,7 @@ export namespace coral { } /** - * Tests the path against `that` for equality, returning `true` if they are identical, - * otherwise `false`. + * Tests the path against `that` for equality, returning `true` if they are identical, otherwise `false`. */ constexpr bool equals(path const & that) const { return coral::equals(this->as_slice().as_bytes(), that.as_slice().as_bytes()); @@ -103,7 +101,7 @@ export namespace coral { } private: - char buffer[max + 1]; + char buffer[max + 1]{0}; }; struct file_reader : public reader { @@ -133,25 +131,29 @@ export namespace coral { virtual ~fs() {}; + /** + * Attempts to read the files in `files_path`, calling `apply` for each of the files encountered with the + * fully-qualified file path. If either no files are found or the file-system does not support the operation, + * `apply` is never caled. + * + * `false` may be returned inside of `apply` to halt the enumeration. + */ + virtual void enumerate_files(path const & files_path, closure)> const & apply) {} + /** * Queries the file-system for its global [access_rules], returning them. */ virtual access_rules query_access() = 0; /** - * Attempts to read the file in the file system located at `file_path` relative, calling - * `then` if it was successfully opened for reading. - * - * Once `then` returns, access to the file is closed automatically. + * Attempts to read the file in `file_path`, calling `then` if it was successfully opened for reading. */ - virtual void read_file(path const & file_path, closure const & then) = 0; + virtual void read_file(path const & file_path, closure const & then) {} /** - * Attempts to write the file in the file system located at `file_path` relative, calling - * `then` if it was successfully opened for writing. - * - * Once `then` returns, access to the file is closed automatically. + * Attempts to write the file in the file system located at `file_path`, calling `then` if it was successfully + * opened for writing. */ - virtual void write_file(path const & file_path, closure const & then) = 0; + virtual void write_file(path const & file_path, closure const & then) {} }; }