idx

C library providing a standard for (non-standard) 64-bit file indexing functions.
git clone git://src.gearsix.net/idx
Log | Files | Refs | Atom | README

commit 22f18d6a6479e1446fee599e668f53d20c48f264
parent ff7b3b19155ee0fbf0d8e7894d62074cb9e9b77b
Author: gearsix <gearsix@tuta.io>
Date:   Sat,  8 Oct 2022 13:43:53 +0100

added: idxtostr, strtoidx; new target: IDX_UKWNC99.

I felt that `strtoidx` was necessary, since strtoull returns an
`unsigned long long` and an `idx_t` isn't guaranteed to be an `ull`.
On top of that I'm not 100% Microsoft support `strtoull` on all their
systems.

`idxtostr` is probably a little superfluous but I added it for
completion-sake.

The IDX_UKWNC99 target now became necessary since `strtoull` is only
available on systems with C99+.

Diffstat:
Midx.h | 6++++--
Midx_posix.c | 10++++++++++
Midx_t.h | 14++++++++++----
Midx_ukwn.c | 19++++++++++++++++++-
Midx_win.c | 10++++++++++
5 files changed, 52 insertions(+), 7 deletions(-)

diff --git a/idx.h b/idx.h @@ -10,8 +10,11 @@ int idxseek(FILE *stream, idx_t offset, int origin); idx_t idxtell(FILE *stream); +idx_t strtoidx(const char *s, char **end, int base); + +int idxtostr(idx_t i, char **buf); #ifdef __cplusplus } #endif -#endif /* IDX */ -\ No newline at end of file +#endif /* IDX */ diff --git a/idx_posix.c b/idx_posix.c @@ -13,4 +13,14 @@ idx_t idxtell(FILE *stream) return (idx_t)ftello(stream); } +idx_t strtoidx(const char *s, char **end, int base) +{ + return (idx_t)strtoull(s, end, base); +} + +int idxtostr(idx_t i, char **buf) +{ + return sprintf(buf, "%llu", i); +} + #endif diff --git a/idx_t.h b/idx_t.h @@ -16,10 +16,16 @@ extern "C" { #define IDX_WIN typedef __int64 idx_t; -/* Unknown system! +/* Unknown system, check if we at least have C99 */ +#elif (__STDC_VERSION__ >= 199901L) + #include <stdint.h> + #define IDX_UKWNC99 + typedef uint_least64_t idx_t; + +/* Unknown system and either non-standard C or pre-C99! This limits the `idx.h` to using the standard - library, rendering it fairly pointless since - `fseek` and `ftell` are limited to `long int`. + library AND being stuck with `unsigned long`, + rendering it fairly pointless. If you would like to use `idx.h` anyway, just remove or comment out the `#error` below. @@ -28,7 +34,7 @@ extern "C" { support that idx.h doesn't know about, please drop an email or submit a patch. */ #else - #error "System not supported. See idx_t.h for details." + #error "System not supported and C std pre-C99." #define IDX_UNKW typedef unsigned long idx_t; diff --git a/idx_ukwn.c b/idx_ukwn.c @@ -1,4 +1,4 @@ -#ifdef IDX_UNKW +#ifdef IDX_UNKW || IDX_UNKWC99 int idxseek(FILE *stream, idx_t offset, int origin) @@ -11,5 +11,22 @@ idx_t idxtell(FILE *stream) return (idx_t)ftell(stream); } +idx_t strtoidx(const char *s, char **end, int base) +{ +#ifdef IDX_UKWNC99 + return (idx_t)strtoull(s, end, base); +#else + return (idx_t)strtoul(s, end, base); +#endif +} + +int idxtostr(idx_t i, char **buf) +{ +#ifdef IDX_UKWNC99 + return sprintf(buf, "%llu", i); +#else + return sprintf(buf, "%lu", i); +#endif +} #endif diff --git a/idx_win.c b/idx_win.c @@ -13,4 +13,14 @@ idx_t idxtell(FILE *stream) return (idx_t)_ftelli64(stream); } +idx_t strtoidx(const char *s, char **end, int base) +{ + return (idx_t)strtoi64(s, end, base); +} + +int idxtostr(idx_t i, char **buf) +{ + return sprintf(buf, "%I64", i); +} + #endif