diff --git a/include/tlibc/errors.h b/include/tlibc/errors.h index 29dbd4c..1e42296 100755 --- a/include/tlibc/errors.h +++ b/include/tlibc/errors.h @@ -41,6 +41,7 @@ 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_VOID (Result_){ .error = NULL } #define RESULT_VALUE(FIELD, V) (Result_){ .error = NULL, .FIELD = V } diff --git a/include/tlibc/filesystem.h b/include/tlibc/filesystem.h index fc3a1f5..3996000 100644 --- a/include/tlibc/filesystem.h +++ b/include/tlibc/filesystem.h @@ -47,6 +47,15 @@ Result(FILE*) file_open(cstr file_name, cstr fopen_mode); bool file_exists(cstr path); +Result(i64) file_getSize(FILE* f); + +typedef enum SeekOrigin { + SeekOrigin_Start = SEEK_SET, + SeekOrigin_Current = SEEK_CUR, + SeekOrigin_End = SEEK_END, +} SeekOrigin; + +Result(void) file_seek(FILE* f, i64 offset, SeekOrigin origin); bool dir_exists(cstr path); diff --git a/src/filesystem/dir.c b/src/filesystem/dir.c index 6e8c97d..5b5a0e8 100644 --- a/src/filesystem/dir.c +++ b/src/filesystem/dir.c @@ -35,7 +35,7 @@ Result(void) dir_create(cstr path){ if(mkdir(path, 0777) == -1) #endif { - return RESULT_ERROR(sprintf_malloc(512, "can't create dicectory '%s'", path), true); + return RESULT_ERROR_FMT("Can't create dicectory '%s': %s", path, strerror(errno)); } return RESULT_VOID; diff --git a/src/filesystem/file.c b/src/filesystem/file.c index f340cc8..1c62e2c 100644 --- a/src/filesystem/file.c +++ b/src/filesystem/file.c @@ -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)); + } +} \ No newline at end of file