filesystem

This commit is contained in:
2022-11-05 13:10:02 +06:00
parent 451358ad6c
commit 7f62334a4a
7 changed files with 228 additions and 22 deletions

74
src/Filesystem/file.h Normal file
View File

@@ -0,0 +1,74 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../base/base.h"
#include "../Array/Array.h"
#include "../String/string.h"
typedef char* FilePath;
Array_declare(FilePath);
typedef FILE File;
ktid_declare(File);
typedef enum FileOpenMode{
// open a file for reading
FileOpenMode_Read=1,
// (re)create a file for writing
FileOpenMode_Write=2,
// opens file for writing additional data to the end / creates new file
FileOpenMode_Append=4,
// (re)creates file for reading/writing
FileOpenMode_ReadWrite=FileOpenMode_Read|FileOpenMode_Write,
// opens file for readng/writing additional data to the end / creates new file
FileOpenMode_ReadAppend=FileOpenMode_Read|FileOpenMode_Append
} FileOpenMode;
/// @brief opens file
/// @param path path to file
/// @param mode Read/Write/Append/ReadWrite/ReadAppend
/// @return Maybe<File*>
Maybe file_open(FilePath path, FileOpenMode mode);
/// @brief closes file descriptor
/// @return Maybe<void>
Maybe file_close(File* file);
/// @brief closes file descriptor
/// @param byte byte to write
/// @return Maybe<void>
Maybe file_writeChar(File* file, char byte);
/// @brief closes file descriptor
/// @param buffer bytes to write
/// @param length buffer length
/// @return Maybe<void>
Maybe file_writeBuffer(File* file, char* buffer, uint64 length);
/// @brief writes all cstring array content to file
/// @param cptr zero-terminated cstring
/// @return Maybe<void>
Maybe file_writeCptr(File* file, char* cptr);
/// @brief reads single byte from file
/// @return Maybe<char>
Maybe file_readChar(File* file);
/// @brief reads byte array of specofied length
/// @param buffer buffer that will be filled with file bytes
/// @param length buffer length
/// @return Maybe<uint64> total number of successfully read bytes (<=length)
Maybe file_readBuffer(File* file, char* buffer, uint64 length);
/// @brief reads all bytes from file
/// @param allBytes ptr to the file's content will be pushed there
/// @return Maybe<uint64> total number of successfully read bytes
Maybe file_readAll(File* file, char** allBytes);
#if __cplusplus
}
#endif