feat: error handling, better prelude, double linked lists usage etc

This commit is contained in:
2026-01-02 22:06:56 +01:00
parent a453603b9b
commit 6176d549c1
10 changed files with 148 additions and 65 deletions

View File

@@ -40,7 +40,7 @@ public:
bool Write(const StringView& content)
{
assert(Writable() && "attempt to write to a file in read only mode");
if (fwrite(content.c_str(), sizeof(char), content.size - 1, m_handle) == 0)
if (fwrite(content.c_str(), sizeof(char), content.len(), m_handle) != content.len())
{
return false;
}
@@ -48,29 +48,21 @@ public:
}
public:
StringView ReadAll(bool* status = nullptr)
bool ReadAll(StringBuilder& sb)
{
auto fail = [&]() -> StringView {
if (status) *status = false;
return {};
};
if (status) *status = true;
assert(Readable() && "attempt to read from a file in write only mode");
if (fseek(m_handle, 0, SEEK_END) != 0) return fail();
if (fseek(m_handle, 0, SEEK_END) != 0) return false;
auto size = ftell(m_handle);
if (size == -1 && status) return fail();
if (size == -1) return false;
StringBuilder sb;
sb.ensure_extra(size);
size_t wrote = fread(sb.data, sizeof(char), size, m_handle);
if (wrote == 0) return fail();
if (wrote == 0) return false;
sb.size = wrote;
return sb.view();
return true;
}
private: