MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
file_sys.hpp
Go to the documentation of this file.
1/*
2
3Copyright 2025 Matthew Tolman
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17*/
18
19#pragma once
20
21#ifndef MTCORE_IO_HPP
22#define MTCORE_IO_HPP
23
25#include "mtcore/core.hpp"
26#include "mtcore/random.hpp"
27
32
34namespace mtcore::io {
39 using FileError = int;
40
57 Result<FILE *, FileError> (*file_open)(void *state, const char *path, const char *mode);
58
64 Result<void, FileError> (*file_close)(void *state, FILE *fp);
65
74 size_t (*file_read)(void *state, void *buffer, size_t size, size_t count, FILE *stream);
75
84 size_t (*file_write)(void *state, const void *ptr, size_t size, size_t nmemb, FILE *stream);
91 Result<void, FileError> (*file_rename)(void *state, const char *oldFileName, const char *newFileName);
92
98 Result<void, FileError> (*file_remove)(void *state, const char *path);
99
105 Result<void, FileError> (*file_sync)(void *state, FILE *fp);
106
112 size_t (*file_size)(void *state, FILE *file);
113
117 bool (*file_eof)(void *state, FILE *file);
118
122 FileError (*file_error)(void *state, FILE *file);
123 };
124
139
143 const SyncFileSystemVTable *vtable = nullptr;
144 void *state = nullptr;
145
159 Prng &prng) const;
160
170
182 [[nodiscard]] Result<Slice<u8>, FileError> read_file_into(const char *path, Slice<u8> out) const;
183
195 [[nodiscard]] Result<Slice<char>, FileError> read_file_str_into(const char *path, const Slice<char> out) const;
196
208 [[nodiscard]] Result<void, FileError> read_file(Allocator &alloc, const char *path, Slice<u8> &out) const;
209
221 [[nodiscard]] Result<void, FileError> read_file_str(Allocator &alloc, const char *path, Slice<char> &out) const;
222
232 [[nodiscard]] Result<Slice<u8>, FileError> read_chunk_into(FILE *file, const Slice<u8> out) const;
233
244 [[nodiscard]] Result<Slice<char>, FileError> read_chunk_str_into(FILE *file, const Slice<char> out) const;
245
254 [[nodiscard]] Result<void, FileError> write_chunk(FILE *file, const Slice<const u8> buff) const;
255
264 [[nodiscard]] Result<void, FileError> write_chunk_str(FILE *file, const Slice<const char> buff) const;
265
274 [[nodiscard]] Result<FILE *, FileError> open(const char *path, const char *mode) const;
275
282 [[nodiscard]] Result<void, FileError> sync(FILE *file) const;
283
290 [[nodiscard]] Result<void, FileError> close(FILE *file) const;
291
299 [[nodiscard]] Result<void, FileError> rename(const char *oldPath, const char *newPath) const;
300
307 [[nodiscard]] Result<void, FileError> remove(const char *path) const;
308 };
309
317} // namespace mtcore
318
319#endif // MTCORE_IO_HPP
SyncFileSystem c_filesys()
Creates a sync file system based on the C API Uses the 64-bit options where possible (enables >2GB fi...
int FileError
File error.
Definition file_sys.hpp:39
Represents a memory allocator Exact behavior depends on the underlying VTable used Should use the a_*...
Represents a Pseudo Random Number Generator.
Definition random.hpp:100
Represents a Result that may have an error (error code) or a success value A type of "void" means the...
Definition result.hpp:170
A Slice which is just a pointer + length Accessing elements through the array operator will do bounds...
VTable for synchronous file system.
Definition file_sys.hpp:48
Result< void, FileError >(* file_remove)(void *state, const char *path)
Function pointer for removing a file.
Definition file_sys.hpp:98
Result< void, FileError >(* file_rename)(void *state, const char *oldFileName, const char *newFileName)
Function pointer for renaming a file.
Definition file_sys.hpp:91
FileError(* file_error)(void *state, FILE *file)
Checks if a file stream has errors.
Definition file_sys.hpp:122
size_t(* file_read)(void *state, void *buffer, size_t size, size_t count, FILE *stream)
Function pointer for reading a file.
Definition file_sys.hpp:74
size_t(* file_write)(void *state, const void *ptr, size_t size, size_t nmemb, FILE *stream)
Function pointer for writing a file.
Definition file_sys.hpp:84
bool(* file_eof)(void *state, FILE *file)
Checks if file handle is at end of file.
Definition file_sys.hpp:117
Result< void, FileError >(* file_sync)(void *state, FILE *fp)
Function pointer for syncing a file to disk.
Definition file_sys.hpp:105
Result< void, FileError >(* file_close)(void *state, FILE *fp)
Function pointer for closing a file.
Definition file_sys.hpp:64
Result< FILE *, FileError >(* file_open)(void *state, const char *path, const char *mode)
Function pointer for opening a file.
Definition file_sys.hpp:57
size_t(* file_size)(void *state, FILE *file)
Function pointer for getting a file's size.
Definition file_sys.hpp:112
Synchronous File System interface for handling I/O calls This is encapsulated as a struct of file poi...
Definition file_sys.hpp:138
Result< Slice< char >, FileError > read_chunk_str_into(FILE *file, const Slice< char > out) const
Reads a chunk of characters from a file handle (without closing) into a buffer Uses a buffer of chars...
Result< void, FileError > write_file_safe(Allocator &tmpAlloc, const char *path, Slice< const u8 > data, Prng &prng) const
Overwrites a file's contents by first creating a temporary file and then doing a rename Will sync fil...
Result< FILE *, FileError > open(const char *path, const char *mode) const
Opens a file handle On failure, returns NULL.
Result< Slice< char >, FileError > read_file_str_into(const char *path, const Slice< char > out) const
Reads a file into a static buffer of chars (for text data).
Result< void, FileError > write_chunk(FILE *file, const Slice< const u8 > buff) const
Writes a chunk of data into a file handle (without closing) from a buffer If the entire buffer could ...
Result< void, FileError > remove(const char *path) const
Removes a file.
Result< void, FileError > read_file(Allocator &alloc, const char *path, Slice< u8 > &out) const
Reads a file into a dynamically allocated buffer and then sets out to point to that buffer.
Result< void, FileError > sync(FILE *file) const
Syncs a file to disk.
Result< void, FileError > write_file_direct(const char *path, Slice< const u8 > data) const
Overwrites a file's contents directly Will sync file to disk.
Result< void, FileError > rename(const char *oldPath, const char *newPath) const
Renames a file.
Result< void, FileError > write_chunk_str(FILE *file, const Slice< const char > buff) const
Writes a chunk of data into a file handle (without closing) from a buffer of chars (text) If the enti...
Result< void, FileError > close(FILE *file) const
Closes a file handle.
Result< Slice< u8 >, FileError > read_chunk_into(FILE *file, const Slice< u8 > out) const
Reads a chunk of characters from a file handle (without closing) into a buffer On success,...
Result< void, FileError > read_file_str(Allocator &alloc, const char *path, Slice< char > &out) const
Reads a file into a dynamically allocated buffer and then sets out to point to that buffer The buffer...
const SyncFileSystemVTable * vtable
Underlying vtable for file operations.
Definition file_sys.hpp:143
Result< Slice< u8 >, FileError > read_file_into(const char *path, Slice< u8 > out) const
Reads a file into a static buffer.