diff options
| author | Jan Sucan <jan@jansucan.com> | 2023-11-12 15:15:31 +0100 |
|---|---|---|
| committer | Jan Sucan <jan@jansucan.com> | 2023-11-12 15:15:38 +0100 |
| commit | bdcce656c964c6d94d85639e3f064fc8aef0c124 (patch) | |
| tree | 226ee7572421645d6308eb405ccbb254ad9a5837 /src/file.c | |
| parent | 28ec9477c63342f05c6d7b0d39581c6611f76ab4 (diff) | |
Refactor the file size and tell operations
This was done in order to avoid C++ compiler warnings about comparing types with
different signedness.
Diffstat (limited to 'src/file.c')
| -rw-r--r-- | src/file.c | 27 |
1 files changed, 22 insertions, 5 deletions
@@ -31,22 +31,39 @@ #include <errno.h> #include <string.h> -long -file_size(FILE *const file) +size_t +file_size(FILE *const file, bool *const return_is_ok) { fpos_t p; if ((fgetpos(file, &p) != 0) || (fseek(file, 0L, SEEK_END) != 0)) { - return -1; + *return_is_ok = false; + return 0; } const long size = ftell(file); if (fsetpos(file, &p) != 0) { - return -1; + *return_is_ok = false; + return 0; } - return size; + *return_is_ok = true; + return (size_t)size; +} + +size_t +file_tell(FILE *const file, bool *const return_is_ok) +{ + const long pos = ftell(file); + + if (pos < 0) { + *return_is_ok = false; + return 0; + } else { + *return_is_ok = true; + return (size_t)pos; + } } size_t |
