From ad0cfd6bd94f9fe44ca86a8019cdf6fea72a6c8b Mon Sep 17 00:00:00 2001 From: lionsoul2014 Date: Tue, 27 Jan 2026 18:56:04 +0800 Subject: [PATCH] pack the ftell & fseek for later large file supports --- binding/c/xdb_api.h | 9 +++++++++ binding/c/xdb_util.c | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/binding/c/xdb_api.h b/binding/c/xdb_api.h index b58e6a2..7c785d1 100644 --- a/binding/c/xdb_api.h +++ b/binding/c/xdb_api.h @@ -9,6 +9,10 @@ #ifndef C_IP2REGION_XDB_H #define C_IP2REGION_XDB_H +// @Note: +// this define must be put before any header include +// force the LFS for ftell +#define _FILE_OFFSET_BITS 64 #include #include #include @@ -201,6 +205,11 @@ XDB_PUBLIC(int) xdb_v6_ip_to_string(const bytes_ip_t *, char *, size_t); // returns: -1 if ip1 < ip2, 1 if ip1 > ip2 or 0 XDB_PUBLIC(int) xdb_ip_sub_compare(const bytes_ip_t *, int, const char *, int); +// large file seek and tell +XDB_PUBLIC(int) xdb_fseek(FILE *, long long, int); + +XDB_PUBLIC(long long) xdb_ftell(FILE *); + // --- END xdb utils diff --git a/binding/c/xdb_util.c b/binding/c/xdb_util.c index 14bd5c0..05ec2ad 100644 --- a/binding/c/xdb_util.c +++ b/binding/c/xdb_util.c @@ -228,7 +228,7 @@ XDB_PUBLIC(void) xdb_free_content(void *ptr) { } XDB_PUBLIC(int) xdb_verify_from_header(FILE *handle, xdb_header_t *header) { - int runtime_ptr_bytes = 0; // runtime ptr bytes + unsigned int runtime_ptr_bytes = 0; // runtime ptr bytes if (header->version == xdb_structure_20) { runtime_ptr_bytes = 4; } else if (header->version == xdb_structure_30) { @@ -244,9 +244,9 @@ XDB_PUBLIC(int) xdb_verify_from_header(FILE *handle, xdb_header_t *header) { return 3; } - long int fileBytes = ftell(handle); - long int maxFilePtr = (1L << (runtime_ptr_bytes * 8)) - 1; - // printf("fileBytes: %ld, maxFilePtr: %ld\n", fileBytes, maxFilePtr); + long long fileBytes = xdb_ftell(handle); + long long maxFilePtr = (1LL << (runtime_ptr_bytes * 8)) - 1; + // printf("fileBytes: %lld, maxFilePtr: %lld\n", fileBytes, maxFilePtr); if (fileBytes > maxFilePtr) { return 4; } @@ -496,3 +496,30 @@ XDB_PUBLIC(int) xdb_ip_sub_compare(const bytes_ip_t *ip1, int bytes, const char } return 0; } + +XDB_PUBLIC(int) xdb_fseek(FILE *handle, long long offset, int whence) { + // we may have to use the large file solution later + // #if defined(XDB_LINUX) + // return fseeko(handle, (off_t) offset, whence); + // #elif defined(XDB_WINDOWS) + // return _fseeki64(handle, (__int64) offset, whence) + // #else + // return fseek(handle, (long) offset, whence); + // #endif + + return fseek(handle, (long) offset, whence); +} + +XDB_PUBLIC(long long) xdb_ftell(FILE *handle) { + // we may have to use the large file solution later + // #if defined(XDB_LINUX) + // return (long long) ftello(handle); + // #elif defined(XDB_WINDOWS) + // return (long long) _ftelli64(handle); + // #else + // // report error ? + // return (long long) ftell(handle); + // #endif + + return (long long) ftell(handle); +} \ No newline at end of file