Remove coral::fs::access_result

This commit is contained in:
kayomn 2023-02-19 17:16:43 +00:00
parent cba6e26995
commit 8ced8440fc
4 changed files with 32 additions and 66 deletions

View File

@ -13,8 +13,6 @@ import oar;
export namespace app {
struct directory : public coral::fs {
using coral::fs::access_result;
struct rules {
bool can_read;
@ -23,24 +21,22 @@ export namespace app {
directory() : path_buffer{0} {}
access_result read_file(coral::path const & file_path,
void read_file(coral::path const & file_path,
coral::callable<void(coral::readable const &)> const & then) {
if (this->prefix_length == 0) return access_result::not_found;
if (this->prefix_length == 0) return;
if (!this->access_rules.can_read) return access_result::access_denied;
if (!this->access_rules.can_read) return;
::SDL_RWops * rw_ops = this->open_rw(file_path, {.can_read = true});
::SDL_RWops * rw_ops{this->open_rw(file_path, {.can_read = true})};
if (rw_ops == nullptr) return access_result::not_found;
if (rw_ops == nullptr) return;
then([rw_ops](coral::slice<uint8_t> const & buffer) -> size_t {
return ::SDL_RWread(rw_ops, buffer.pointer, sizeof(uint8_t), buffer.length);
});
::SDL_RWclose(rw_ops);
return access_result::ok;
}
void target(coral::slice<char const> const & directory_path, rules const & access_rules) {
@ -57,24 +53,22 @@ export namespace app {
}
}
access_result write_file(coral::path const & file_path,
void write_file(coral::path const & file_path,
coral::callable<void(coral::writable const &)> const & then) {
if (this->prefix_length == 0) return access_result::not_found;
if (this->prefix_length == 0) return;
if (!this->access_rules.can_write) return access_result::access_denied;
if (!this->access_rules.can_write) return;
::SDL_RWops * rw_ops = this->open_rw(file_path, {.can_write = true});
::SDL_RWops * rw_ops{this->open_rw(file_path, {.can_write = true})};
if (rw_ops == nullptr) return access_result::not_found;
if (rw_ops == nullptr) return;
then([rw_ops](coral::slice<uint8_t const> const & buffer) -> size_t {
return ::SDL_RWwrite(rw_ops, buffer.pointer, sizeof(uint8_t), buffer.length);
});
::SDL_RWclose(rw_ops);
return access_result::ok;
}
private:
@ -87,7 +81,7 @@ export namespace app {
coral::u8 path_buffer[path_max];
::SDL_RWops * open_rw(coral::path const & file_path, rules const & file_rules) {
coral::u8 * const path_begin = this->path_buffer + this->prefix_length;
coral::u8 * const path_begin{this->path_buffer + this->prefix_length};
coral::slice const path_remaining =
{path_begin, path_begin + (path_max - this->prefix_length) - 1};
@ -111,12 +105,12 @@ export namespace app {
constexpr directory::rules read_only_rules = {.can_read = true};
{
char * const path = ::SDL_GetBasePath();
char * const path{::SDL_GetBasePath()};
if (path == nullptr) {
this->cwd_directory.target("./", read_only_rules);
} else {
coral::usize path_length = 0;
coral::usize path_length{0};
while (path[path_length] != 0) path_length += 1;
@ -131,10 +125,10 @@ export namespace app {
}
{
char * const path = ::SDL_GetPrefPath("ona", title.begin());
char * const path{::SDL_GetPrefPath("ona", title.begin())};
if (path != nullptr) {
coral::usize path_length = 0;
coral::usize path_length{0};
while (path[path_length] != 0) path_length += 1;
@ -166,8 +160,8 @@ export namespace app {
}
void log(app::log_level level, coral::slice<char const> const & message) {
coral::i32 const length = static_cast<coral::i32>(
coral::min(message.length, static_cast<size_t>(coral::i32_max)));
coral::i32 const length{static_cast<coral::i32>(
coral::min(message.length, static_cast<size_t>(coral::i32_max)))};
::SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,
SDL_LOG_PRIORITY_INFO, "%.*s", length, message.pointer);
@ -215,6 +209,12 @@ export namespace app {
this->retitle(title);
}
void present() {
if (this->sdl_renderer != nullptr) {
::SDL_RenderPresent(this->sdl_renderer);
}
}
void render(canvas & source_canvas) {
if (this->sdl_renderer != nullptr) {
SDL_SetRenderDrawColor(this->sdl_renderer, source_canvas.background_color.to_r8(),
@ -257,12 +257,6 @@ export namespace app {
return show_result::ok;
}
void present() {
if (this->sdl_renderer != nullptr) {
::SDL_RenderPresent(this->sdl_renderer);
}
}
private:
coral::path title;

View File

@ -105,43 +105,22 @@ export namespace coral {
* Platform-generalized file system interface.
*/
struct fs {
/**
* The result from a file access operation.
*
* [file_result::ok] indicates a successful file access operation.
*
* [file_result::not_found] signals that the file was not found.
*
* [file_result::access_denied] warns that the file was found but cannot be opened through
* the file system interface. The most common scenario for this error is a lack of required
* file permissions.
*/
enum class [[nodiscard]] access_result {
ok,
not_found,
access_denied,
};
/**
* Attempts to read the file in the file system located at `file_path` relative, calling
* `then` if it was successfully opened for reading.
*
* The returned [access_result] indicates whether the operation was successful or not.
*
* Once `then` returns, access to the file is closed automatically.
*/
virtual access_result read_file(path const & file_path,
virtual void read_file(path const & file_path,
callable<void(readable const &)> const & then) = 0;
/**
* Attempts to write the file in the file system located at `file_path` relative, calling
* `then` if it was successfully opened for writing.
*
* The returned [access_result] indicates whether the operation was successful or not.
*
* Once `then` returns, access to the file is closed automatically.
*/
virtual access_result write_file(path const & file_path,
virtual void write_file(path const & file_path,
callable<void(writable const &)> const & then) = 0;
};
}

View File

@ -21,22 +21,16 @@ export namespace oar {
};
struct archive : public coral::fs {
using coral::fs::access_result;
archive() {
}
access_result read_file(coral::path const & file_path,
void read_file(coral::path const & file_path,
coral::callable<void(coral::readable const &)> const & then) override {
return access_result::access_denied;
}
virtual access_result write_file(coral::path const & file_path,
void write_file(coral::path const & file_path,
coral::callable<void(coral::writable const &)> const & then) override {
return access_result::access_denied;
}
};
}

View File

@ -15,16 +15,13 @@ extern "C" int main(int argc, char const * const * argv) {
constexpr coral::path config_path{"config.kym"};
bool is_config_loaded{false};
if ((system.res_fs().read_file(config_path, [&](coral::readable const & file) {
system.res_fs().read_file(config_path, [&](coral::readable const & file) {
kym::vm vm{&system.thread_safe_allocator(),
[&system](coral::slice<char const> const & error_message) {
system.log(app::log_level::error, error_message);
}};
if (!vm.init({
.datastack_size = 64,
.callstack_size = 64,
})) {
if (!vm.init({.datastack_size = 64, .callstack_size = 64})) {
system.log(app::log_level::error, "failed to allocate memory for config vm");
return;
@ -65,7 +62,9 @@ extern "C" int main(int argc, char const * const * argv) {
});
});
});
}) != coral::fs::access_result::ok) || (!is_config_loaded)) return coral::u8_max;
});
if (!is_config_loaded) return coral::u8_max;
// app::canvas canvas_2d();