added macro RESULT_ERROR_FMT and some filesystem functions

This commit is contained in:
2025-08-04 16:03:08 +03:00
parent 961d00fdb0
commit 6d959fe8f5
4 changed files with 28 additions and 3 deletions

View File

@@ -26,9 +26,24 @@ bool file_exists(cstr path){
Result(FILE*) file_open(cstr file_name, cstr fopen_mode){
FILE* f = fopen(file_name, fopen_mode);
if(f == NULL){
char* errmsg = sprintf_malloc(256, "can't open (%s) file '%s': %s",
return RESULT_ERROR_FMT("Can't open (%s) file '%s': %s",
fopen_mode, file_name, strerror(errno));
return RESULT_ERROR(errmsg, true);
}
return RESULT_VALUE(p, f);
}
Result(i64) file_getSize(FILE* f){
i64 r = IFWIN(_ftelli64, ftello64)(f);
if(r < 0){
return RESULT_ERROR(strerror(errno), false);
}
return RESULT_VALUE(i, r);
}
Result(void) file_seek(FILE* f, i64 offset, SeekOrigin origin){
if(IFWIN(_fseeki64, fseeko64)(f, offset, (int)origin) != 0){
return RESULT_ERROR_FMT(
"Can't seek (offset: " IFWIN("%lli", "%li") ", origin: %i) in file: %s",
offset, origin, strerror(errno));
}
}