implemented file read/write functions
This commit is contained in:
@@ -41,7 +41,8 @@ typedef struct Result_ {
|
||||
|
||||
|
||||
#define RESULT_ERROR(MSG, IS_MSG_ON_HEAP) (Result_){ .error = Error_create(MSG, IS_MSG_ON_HEAP, ErrorCallPos_here()) }
|
||||
#define RESULT_ERROR_FMT(FORMAT, ARGS...) RESULT_ERROR(sprintf_malloc(4096, FORMAT, ARGS), true)
|
||||
#define RESULT_ERROR_FMT(FORMAT, ARGS...) RESULT_ERROR(sprintf_malloc(4096, FORMAT ,##ARGS), true)
|
||||
#define RESULT_ERROR_ERRNO() RESULT_ERROR(strerror(errno), false)
|
||||
#define RESULT_VOID (Result_){ .error = NULL }
|
||||
#define RESULT_VALUE(FIELD, V) (Result_){ .error = NULL, .FIELD = V }
|
||||
|
||||
|
||||
@@ -1,17 +1,24 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
#include "std.h"
|
||||
#include "errors.h"
|
||||
#include "string/str.h"
|
||||
#include "collections/Array.h"
|
||||
|
||||
#if !defined(KFS_USE_WINDOWS_H)
|
||||
#if !defined(TLIBC_FS_USE_WINDOWS_H)
|
||||
#if defined(_WIN64) || defined(_WIN32)
|
||||
#define KFS_USE_WINDOWS_H 1
|
||||
#define TLIBC_FS_USE_WINDOWS_H 1
|
||||
#else
|
||||
#define KFS_USE_WINDOWS_H 0
|
||||
#define TLIBC_FS_USE_WINDOWS_H 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if KFS_USE_WINDOWS_H
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// PATH //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#if defined(_WIN64) || defined(_WIN32)
|
||||
#define path_sep '\\'
|
||||
#define path_seps "\\"
|
||||
#define path_notsep '/'
|
||||
@@ -32,6 +39,10 @@ str path_dirname(str path);
|
||||
str path_basename(str path, bool remove_ext);
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// FILE //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// open a file for reading
|
||||
#define FO_Read "rb"
|
||||
/// (re)create a file for writing
|
||||
@@ -45,10 +56,11 @@ str path_basename(str path, bool remove_ext);
|
||||
|
||||
Result(FILE*) file_open(cstr file_name, cstr fopen_mode);
|
||||
|
||||
bool file_exists(cstr path);
|
||||
|
||||
bool file_exists(cstr path);
|
||||
Result(i64) file_getSize(FILE* f);
|
||||
|
||||
|
||||
typedef enum SeekOrigin {
|
||||
SeekOrigin_Start = SEEK_SET,
|
||||
SeekOrigin_Current = SEEK_CUR,
|
||||
@@ -58,6 +70,58 @@ typedef enum SeekOrigin {
|
||||
Result(void) file_seek(FILE* f, i64 offset, SeekOrigin origin);
|
||||
|
||||
|
||||
Result(void) file_writeByte(FILE* f, u8 b);
|
||||
Result(void) file_writeStructs(FILE* f, const void* src, u64 struct_size, u64 count);
|
||||
|
||||
static inline Result(void) file_writeBytes(FILE* f, const void* src, u64 size){
|
||||
return file_writeStructs(f, src, size, 1);
|
||||
}
|
||||
|
||||
static inline Result(void) file_writeBytesArray(FILE* f, Array(u8) src){
|
||||
return file_writeStructs(f, src.data, src.size, 1);
|
||||
}
|
||||
|
||||
|
||||
/// @param out_byte is not set on error or end of file
|
||||
/// @return true if byte was read, false if end of file was reached
|
||||
Result(bool) file_readByte(FILE* f, u8* out_byte);
|
||||
|
||||
|
||||
/// @param max_count maximum number of structs to read
|
||||
/// @return number of structs that were read (<=max_count)
|
||||
Result(u64) file_readStructs(FILE* f, void* dst, u64 struct_size, u64 max_count);
|
||||
|
||||
/// @param max_count maximum number of bytes to read
|
||||
/// @return number of bytes that were read (<=max_count)
|
||||
static inline Result(u64) file_readBytes(FILE* f, void* dst, u64 max_count){
|
||||
return file_readStructs(f, dst, 1, max_count);
|
||||
}
|
||||
|
||||
/// @param dst array where .size is the maximum number of bytes to read
|
||||
/// @return number of bytes that were read (<=max_count)
|
||||
static inline Result(u64) file_readBytesArray(FILE* f, Array(u8) dst){
|
||||
return file_readStructs(f, dst.data, 1, dst.size);
|
||||
}
|
||||
|
||||
|
||||
/// @param max_count exact number of structs to read
|
||||
Result(void) file_readStructsExactly(FILE* f, void* dst, u64 struct_size, u64 exact_count);
|
||||
|
||||
/// @param exact_count exact number of bytes to read
|
||||
static inline Result(void) file_readBytesExactly(FILE* f, void* dst, u64 exact_count){
|
||||
return file_readStructsExactly(f, dst, 1, exact_count);
|
||||
}
|
||||
|
||||
/// @param dst array where .size is the exact number of bytes to read
|
||||
static inline Result(void) file_readBytesArrayExactly(FILE* f, Array(u8) dst){
|
||||
return file_readStructsExactly(f, dst.data, 1, dst.size);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// DIRECTORY //
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
bool dir_exists(cstr path);
|
||||
|
||||
/// @brief creates directories specified in path recursively if they don't exist
|
||||
Result(void) dir_create(cstr path);
|
||||
|
||||
Reference in New Issue
Block a user