implemented file read/write functions

This commit is contained in:
2025-08-04 18:23:46 +03:00
parent ab55f85160
commit e1dc972b24
5 changed files with 129 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
#include "tlibc/filesystem.h"
#include "io_includes.h"
#include "internal.h"
bool dir_exists(cstr path){
if(path[0]=='.'){
@@ -8,7 +8,7 @@ bool dir_exists(cstr path){
// else if(path[1]=='.' && path[2]==path_sep)
//TODO path_resolve because windows doesnt recognize .\ pattern
}
#if KFS_USE_WINDOWS_H
#if TLIBC_FS_USE_WINDOWS_H
DWORD dwAttrib = GetFileAttributes(path);
return (bool)(
(dwAttrib != INVALID_FILE_ATTRIBUTES) && // file exists
@@ -29,7 +29,7 @@ Result(void) dir_create(cstr path){
char* parentDir= str_copy(path_dirname(str_from_cstr((void*)path))).data;
try_void(dir_create(parentDir), free(parentDir));
free(parentDir);
#if KFS_USE_WINDOWS_H
#if TLIBC_FS_USE_WINDOWS_H
if(!CreateDirectory(path, NULL))
#else
if(mkdir(path, 0777) == -1)

View File

@@ -1,5 +1,5 @@
#include "tlibc/filesystem.h"
#include "io_includes.h"
#include "internal.h"
bool file_exists(cstr path){
if(path[0]=='.'){
@@ -9,7 +9,7 @@ bool file_exists(cstr path){
//TODO path_resolve because windows doesnt recognize .\ pattern
}
#if KFS_USE_WINDOWS_H
#if TLIBC_FS_USE_WINDOWS_H
DWORD dwAttrib = GetFileAttributes(path);
return (bool)(
(dwAttrib != INVALID_FILE_ATTRIBUTES) && // file exists
@@ -35,7 +35,7 @@ Result(FILE*) file_open(cstr file_name, cstr fopen_mode){
Result(i64) file_getSize(FILE* f){
i64 r = IFWIN(_ftelli64, ftello64)(f);
if(r < 0){
return RESULT_ERROR(strerror(errno), false);
return RESULT_ERROR_ERRNO();
}
return RESULT_VALUE(i, r);
}
@@ -47,4 +47,53 @@ Result(void) file_seek(FILE* f, i64 offset, SeekOrigin origin){
offset, origin, strerror(errno));
}
return RESULT_VOID;
}
}
Result(void) file_writeStructs(FILE* f, const void* src, u64 struct_size, u64 count){
u64 r = fwrite(src, struct_size, count, f);
if(r != count){
return RESULT_ERROR_ERRNO();
}
return RESULT_VOID;
}
Result(void) file_writeByte(FILE* f, u8 b){
if(fputc(b, f) != 0){
return RESULT_ERROR_ERRNO();
}
return RESULT_VOID;
}
Result(bool) file_readByte(FILE* f, u8* out_byte){
int r = fgetc(f);
if(ferror(f)){
return RESULT_ERROR_ERRNO();
}
if(feof(f)){
return RESULT_VALUE(u, false);
}
*out_byte = r;
return RESULT_VALUE(u, true);
}
Result(u64) file_readStructs(FILE* f, void* dst, u64 struct_size, u64 max_count){
u64 r = fread(dst, struct_size, max_count, f);
if(ferror(f)){
return RESULT_ERROR_ERRNO();
}
return RESULT_VALUE(u, r);
}
Result(void) file_readStructsExactly(FILE* f, void* dst, u64 struct_size, u64 exact_count){
u64 r = fread(dst, struct_size, exact_count, f);
if(ferror(f)){
return RESULT_ERROR_ERRNO();
}
if(r != exact_count){
return RESULT_ERROR_FMT(
"read " IFWIN("%llu", "%lu") " structures out of " IFWIN("%llu", "%lu"),
r, exact_count);
}
return RESULT_VOID;
}

View File

@@ -1,6 +1,7 @@
#pragma once
#if KFS_USE_WINDOWS_H
#include "tlibc/filesystem.h"
#if TLIBC_FS_USE_WINDOWS_H
#include <windows.h>
#else
#include <sys/types.h>