From bdcce656c964c6d94d85639e3f064fc8aef0c124 Mon Sep 17 00:00:00 2001 From: Jan Sucan Date: Sun, 12 Nov 2023 15:15:31 +0100 Subject: Refactor the file size and tell operations This was done in order to avoid C++ compiler warnings about comparing types with different signedness. --- src/file.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'src/file.c') diff --git a/src/file.c b/src/file.c index 57a847a..d605ceb 100644 --- a/src/file.c +++ b/src/file.c @@ -31,22 +31,39 @@ #include #include -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 -- cgit v1.2.3