Export app::readable_file for API use
This commit is contained in:
parent
685e09412b
commit
37b2586eaa
138
source/app.cpp
138
source/app.cpp
|
@ -11,90 +11,92 @@ import coral.math;
|
|||
|
||||
import oar;
|
||||
|
||||
struct file_reader : public coral::file_reader {
|
||||
enum class [[nodiscard]] close_result {
|
||||
ok,
|
||||
io_unavailable,
|
||||
};
|
||||
namespace app {
|
||||
struct file_reader : public coral::file_reader {
|
||||
enum class [[nodiscard]] close_result {
|
||||
ok,
|
||||
io_unavailable,
|
||||
};
|
||||
|
||||
enum class [[nodiscard]] open_result {
|
||||
ok,
|
||||
io_unavailable,
|
||||
access_denied,
|
||||
not_found,
|
||||
};
|
||||
enum class [[nodiscard]] open_result {
|
||||
ok,
|
||||
io_unavailable,
|
||||
access_denied,
|
||||
not_found,
|
||||
};
|
||||
|
||||
file_reader(coral::fs * fs) : rw_ops{nullptr} {
|
||||
this->fs = fs;
|
||||
}
|
||||
|
||||
close_result close() {
|
||||
if (::SDL_RWclose(this->rw_ops) != 0) return close_result::io_unavailable;
|
||||
|
||||
this->rw_ops = nullptr;
|
||||
|
||||
return close_result::ok;
|
||||
}
|
||||
|
||||
bool is_open() const {
|
||||
return this->rw_ops != nullptr;
|
||||
}
|
||||
|
||||
open_result open(coral::path const & file_path) {
|
||||
if (this->is_open()) switch (this->close()) {
|
||||
case close_result::ok: break;
|
||||
case close_result::io_unavailable: return open_result::io_unavailable;
|
||||
default: coral::unreachable();
|
||||
file_reader(coral::fs * fs) : rw_ops{nullptr} {
|
||||
this->fs = fs;
|
||||
}
|
||||
|
||||
this->rw_ops = ::SDL_RWFromFile(reinterpret_cast<char const *>(this->path_buffer), "r");
|
||||
close_result close() {
|
||||
if (::SDL_RWclose(this->rw_ops) != 0) return close_result::io_unavailable;
|
||||
|
||||
if (this->rw_ops == nullptr) return open_result::not_found;
|
||||
this->rw_ops = nullptr;
|
||||
|
||||
return open_result::ok;
|
||||
}
|
||||
return close_result::ok;
|
||||
}
|
||||
|
||||
coral::expected<coral::usize, coral::io_error> read(coral::slice<coral::u8> const & buffer) override {
|
||||
if (!this->is_open()) return coral::io_error::unavailable;
|
||||
bool is_open() const {
|
||||
return this->rw_ops != nullptr;
|
||||
}
|
||||
|
||||
coral::usize const bytes_read{::SDL_RWread(this->rw_ops, buffer.pointer, sizeof(uint8_t), buffer.length)};
|
||||
open_result open(coral::path const & file_path) {
|
||||
if (this->is_open()) switch (this->close()) {
|
||||
case close_result::ok: break;
|
||||
case close_result::io_unavailable: return open_result::io_unavailable;
|
||||
default: coral::unreachable();
|
||||
}
|
||||
|
||||
if ((bytes_read == 0) && (::SDL_GetError() != nullptr)) return coral::io_error::unavailable;
|
||||
this->rw_ops = ::SDL_RWFromFile(reinterpret_cast<char const *>(this->path_buffer), "r");
|
||||
|
||||
return bytes_read;
|
||||
}
|
||||
if (this->rw_ops == nullptr) return open_result::not_found;
|
||||
|
||||
coral::expected<coral::u64, coral::io_error> seek(coral::u64 offset) override {
|
||||
if (!this->is_open()) return coral::io_error::unavailable;
|
||||
return open_result::ok;
|
||||
}
|
||||
|
||||
// TODO: Fix cast.
|
||||
coral::i64 const byte_position{
|
||||
::SDL_RWseek(this->rw_ops, static_cast<coral::i64>(offset), RW_SEEK_SET)};
|
||||
coral::expected<coral::usize, coral::io_error> read(coral::slice<coral::u8> const & buffer) override {
|
||||
if (!this->is_open()) return coral::io_error::unavailable;
|
||||
|
||||
if (byte_position == -1) return coral::io_error::unavailable;
|
||||
coral::usize const bytes_read{::SDL_RWread(this->rw_ops, buffer.pointer, sizeof(uint8_t), buffer.length)};
|
||||
|
||||
return static_cast<coral::u64>(byte_position);
|
||||
}
|
||||
if ((bytes_read == 0) && (::SDL_GetError() != nullptr)) return coral::io_error::unavailable;
|
||||
|
||||
coral::expected<coral::u64, coral::io_error> tell() override {
|
||||
if (!this->is_open()) return coral::io_error::unavailable;
|
||||
return bytes_read;
|
||||
}
|
||||
|
||||
coral::i64 const byte_position{::SDL_RWseek(this->rw_ops, 0, RW_SEEK_SET)};
|
||||
coral::expected<coral::u64, coral::io_error> seek(coral::u64 offset) override {
|
||||
if (!this->is_open()) return coral::io_error::unavailable;
|
||||
|
||||
if (byte_position == -1) return coral::io_error::unavailable;
|
||||
// TODO: Fix cast.
|
||||
coral::i64 const byte_position{
|
||||
::SDL_RWseek(this->rw_ops, static_cast<coral::i64>(offset), RW_SEEK_SET)};
|
||||
|
||||
return static_cast<coral::u64>(byte_position);
|
||||
}
|
||||
if (byte_position == -1) return coral::io_error::unavailable;
|
||||
|
||||
private:
|
||||
static constexpr coral::usize path_max{4096};
|
||||
return static_cast<coral::u64>(byte_position);
|
||||
}
|
||||
|
||||
coral::u8 path_buffer[path_max];
|
||||
coral::expected<coral::u64, coral::io_error> tell() override {
|
||||
if (!this->is_open()) return coral::io_error::unavailable;
|
||||
|
||||
coral::fs * fs;
|
||||
coral::i64 const byte_position{::SDL_RWseek(this->rw_ops, 0, RW_SEEK_SET)};
|
||||
|
||||
::SDL_RWops * rw_ops;
|
||||
};
|
||||
if (byte_position == -1) return coral::io_error::unavailable;
|
||||
|
||||
return static_cast<coral::u64>(byte_position);
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr coral::usize path_max{4096};
|
||||
|
||||
coral::u8 path_buffer[path_max];
|
||||
|
||||
coral::fs * fs;
|
||||
|
||||
::SDL_RWops * rw_ops;
|
||||
};
|
||||
}
|
||||
|
||||
struct base_directory : public coral::fs {
|
||||
base_directory() : directory_path{} {
|
||||
|
@ -122,13 +124,13 @@ struct base_directory : public coral::fs {
|
|||
void read_file(coral::path const & file_path, coral::callable<void(coral::file_reader &)> const & then) override {
|
||||
if (this->directory_path.length == 0) return;
|
||||
|
||||
file_reader reader{this};
|
||||
app::file_reader reader{this};
|
||||
|
||||
if (reader.open(file_path) != file_reader::open_result::ok) return;
|
||||
if (reader.open(file_path) != app::file_reader::open_result::ok) return;
|
||||
|
||||
then(reader);
|
||||
|
||||
if (reader.close() != file_reader::close_result::ok) return;
|
||||
if (reader.close() != app::file_reader::close_result::ok) return;
|
||||
}
|
||||
|
||||
void write_file(coral::path const & file_path, coral::callable<void(coral::file_writer &)> const & then) override {
|
||||
|
@ -165,13 +167,13 @@ struct user_directory : public coral::fs {
|
|||
void read_file(coral::path const & file_path, coral::callable<void(coral::file_reader &)> const & then) override {
|
||||
if (this->directory_path.length == 0) return;
|
||||
|
||||
file_reader reader{this};
|
||||
app::file_reader reader{this};
|
||||
|
||||
if (reader.open(file_path) != file_reader::open_result::ok) return;
|
||||
if (reader.open(file_path) != app::file_reader::open_result::ok) return;
|
||||
|
||||
then(reader);
|
||||
|
||||
if (reader.close() != file_reader::close_result::ok) return;
|
||||
if (reader.close() != app::file_reader::close_result::ok) return;
|
||||
}
|
||||
|
||||
void write_file(coral::path const & file_path, coral::callable<void(coral::file_writer &)> const & then) override {
|
||||
|
|
Loading…
Reference in New Issue