From ee968f3972e2f34fc6f2f0dfb6eeb305cbecf4a1 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 24 Oct 2020 13:16:56 +0200 Subject: Added c_timef(fmt, tm) macro - a convenience strftime()-wrapper function to be used in c_printf() calls only! --- stc/cfmt.h | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/stc/cfmt.h b/stc/cfmt.h index 10f23d2d..dfc745be 100644 --- a/stc/cfmt.h +++ b/stc/cfmt.h @@ -32,6 +32,12 @@ STC_API void _cfmt_printf(int s, const char* fmt, ...); STC_API char * _cfmt_conv(int nargs, const char *fmt, ...); +enum {_cfmt_bn=4, _cfmt_sn=64}; +STC_INLINE const char* _cfmt_strftime(char* n, char buf[_cfmt_bn][_cfmt_sn], size_t maxsize, const char *fmt, const struct tm *timeptr) { + if (*n >= _cfmt_bn) return fmt; + strftime(buf[*n], maxsize, fmt, timeptr); + return buf[(*n)++]; +} #ifndef __cplusplus #define _cfmt_fn(x) _Generic ((x), \ @@ -69,6 +75,7 @@ _cfmt_conv(int nargs, const char *fmt, ...); inline auto _cfmt_fn(char*) {return sprintf;} inline auto _cfmt_fn(int s) {return _cfmt_printf;} inline auto _cfmt_fn(cstr_t*) {return cstr_fmt;} + inline auto _cfmt(bool x) {return "d:bool";} inline auto _cfmt(char x) {return "c";} inline auto _cfmt(unsigned char x) {return "hhu";} inline auto _cfmt(short x) {return "hd";} @@ -86,57 +93,59 @@ _cfmt_conv(int nargs, const char *fmt, ...); inline auto _cfmt(const void *x) {return "p";} #endif +#define c_timef(fmt, timeptr) _cfmt_strftime(&_tn, _tm, sizeof _tm[0], fmt, timeptr) + #define c_printf(...) c_MACRO_OVERLOAD(c_printf, __VA_ARGS__) #define c_printf_2(to, fmt) \ do { char *_fm = _cfmt_conv(0, fmt); \ _cfmt_fn(to)(to, _fm); free(_fm); } while (0) #define c_printf_3(to, fmt, c) \ - do { char *_fm = _cfmt_conv(1, fmt, _cfmt(c)); \ + do { char _tm[1][_cfmt_sn], _tn=0, *_fm = _cfmt_conv(1, fmt, _cfmt(c)); \ _cfmt_fn(to)(to, _fm, c); free(_fm); } while (0) #define c_printf_4(to, fmt, c, d) \ - do { char *_fm = _cfmt_conv(2, fmt, _cfmt(c), _cfmt(d)); \ + do { char _tm[2][_cfmt_sn], _tn=0, *_fm = _cfmt_conv(2, fmt, _cfmt(c), _cfmt(d)); \ _cfmt_fn(to)(to, _fm, c, d); free(_fm); } while (0) #define c_printf_5(to, fmt, c, d, e) \ - do { char *_fm = _cfmt_conv(3, fmt, _cfmt(c), _cfmt(d), _cfmt(e)); \ + do { char _tm[3][_cfmt_sn], _tn=0, *_fm = _cfmt_conv(3, fmt, _cfmt(c), _cfmt(d), _cfmt(e)); \ _cfmt_fn(to)(to, _fm, c, d, e); free(_fm); } while (0) #define c_printf_6(to, fmt, c, d, e, f) \ - do { char *_fm = _cfmt_conv(4, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f)); \ + do { char _tm[_cfmt_bn][_cfmt_sn], _tn=0, *_fm = _cfmt_conv(4, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f)); \ _cfmt_fn(to)(to, _fm, c, d, e, f); free(_fm); } while (0) #define c_printf_7(to, fmt, c, d, e, f, g) \ - do { char *_fm = _cfmt_conv(5, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g)); \ + do { char _tm[_cfmt_bn][_cfmt_sn], _tn=0, *_fm = _cfmt_conv(5, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g)); \ _cfmt_fn(to)(to, _fm, c, d, e, f, g); free(_fm); } while (0) #define c_printf_8(to, fmt, c, d, e, f, g, h) \ - do { char *_fm = _cfmt_conv(6, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h)); \ + do { char _tm[_cfmt_bn][_cfmt_sn], _tn=0, *_fm = _cfmt_conv(6, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h)); \ _cfmt_fn(to)(to, _fm, c, d, e, f, g, h); free(_fm); } while (0) #define c_printf_9(to, fmt, c, d, e, f, g, h, i) \ - do { char *_fm = _cfmt_conv(7, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i)); \ + do { char _tm[_cfmt_bn][_cfmt_sn], _tn=0, *_fm = _cfmt_conv(7, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i)); \ _cfmt_fn(to)(to, _fm, c, d, e, f, g, h, i); free(_fm); } while (0) #define c_printf_10(to, fmt, c, d, e, f, g, h, i, j) \ - do { char *_fm = _cfmt_conv(8, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j)); \ + do { char _tm[_cfmt_bn][_cfmt_sn], _tn=0, *_fm = _cfmt_conv(8, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j)); \ _cfmt_fn(to)(to, _fm, c, d, e, f, g, h, i, j); free(_fm); } while (0) #define c_printf_11(to, fmt, c, d, e, f, g, h, i, j, k) \ - do { char *_fm = _cfmt_conv(9, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j), _cfmt(k)); \ + do { char _tm[_cfmt_bn][_cfmt_sn], _tn=0, *_fm = _cfmt_conv(9, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j), _cfmt(k)); \ _cfmt_fn(to)(to, _fm, c, d, e, f, g, h, i, j, k); free(_fm); } while (0) #define c_printf_12(to, fmt, c, d, e, f, g, h, i, j, k, m) \ - do { char *_fm = _cfmt_conv(10, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j), _cfmt(k), _cfmt(m)); \ + do { char _tm[_cfmt_bn][_cfmt_sn], _tn=0, *_fm = _cfmt_conv(10, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j), _cfmt(k), _cfmt(m)); \ _cfmt_fn(to)(to, _fm, c, d, e, f, g, h, i, j, k, m); free(_fm); } while (0) #define c_printf_13(to, fmt, c, d, e, f, g, h, i, j, k, m, n) \ - do { char *_fm = _cfmt_conv(11, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j), _cfmt(k), _cfmt(m), _cfmt(n)); \ + do { char _tm[_cfmt_bn][_cfmt_sn], _tn=0, *_fm = _cfmt_conv(11, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j), _cfmt(k), _cfmt(m), _cfmt(n)); \ _cfmt_fn(to)(to, _fm, c, d, e, f, g, h, i, j, k, m, n); free(_fm); } while (0) #define c_printf_14(to, fmt, c, d, e, f, g, h, i, j, k, m, n, o) \ - do { char *_fm = _cfmt_conv(12, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j), _cfmt(k), _cfmt(m), _cfmt(n), _cfmt(o)); \ + do { char _tm[_cfmt_bn][_cfmt_sn], _tn=0, *_fm = _cfmt_conv(12, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j), _cfmt(k), _cfmt(m), _cfmt(n), _cfmt(o)); \ _cfmt_fn(to)(to, _fm, c, d, e, f, g, h, i, j, k, m, n, o); free(_fm); } while (0) #define c_printf_15(to, fmt, c, d, e, f, g, h, i, j, k, m, n, o, p) \ - do { char *_fm = _cfmt_conv(13, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j), _cfmt(k), _cfmt(m), _cfmt(n), _cfmt(o), _cfmt(p)); \ + do { char _tm[_cfmt_bn][_cfmt_sn], _tn=0, *_fm = _cfmt_conv(13, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j), _cfmt(k), _cfmt(m), _cfmt(n), _cfmt(o), _cfmt(p)); \ _cfmt_fn(to)(to, _fm, c, d, e, f, g, h, i, j, k, m, n, o, p); free(_fm); } while (0) #define c_printf_16(to, fmt, c, d, e, f, g, h, i, j, k, m, n, o, p, q) \ - do { char *_fm = _cfmt_conv(14, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j), _cfmt(k), _cfmt(m), _cfmt(n), _cfmt(o), _cfmt(p), _cfmt(p)); \ + do { char _tm[_cfmt_bn][_cfmt_sn], _tn=0, *_fm = _cfmt_conv(14, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j), _cfmt(k), _cfmt(m), _cfmt(n), _cfmt(o), _cfmt(p), _cfmt(p)); \ _cfmt_fn(to)(to, _fm, c, d, e, f, g, h, i, j, k, m, n, o, p, q); free(_fm); } while (0) #define c_printf_17(to, fmt, c, d, e, f, g, h, i, j, k, m, n, o, p, q, r) \ - do { char *_fm = _cfmt_conv(15, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j), _cfmt(k), _cfmt(m), _cfmt(n), _cfmt(o), _cfmt(p), _cfmt(p), _cfmt(r)); \ + do { char _tm[_cfmt_bn][_cfmt_sn], _tn=0, *_fm = _cfmt_conv(15, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j), _cfmt(k), _cfmt(m), _cfmt(n), _cfmt(o), _cfmt(p), _cfmt(p), _cfmt(r)); \ _cfmt_fn(to)(to, _fm, c, d, e, f, g, h, i, j, k, m, n, o, p, q, r); free(_fm); } while (0) #define c_printf_18(to, fmt, c, d, e, f, g, h, i, j, k, m, n, o, p, q, r, s) \ - do { char *_fm = _cfmt_conv(16, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j), _cfmt(k), _cfmt(m), _cfmt(n), _cfmt(o), _cfmt(p), _cfmt(p), _cfmt(r), _cfmt(s)); \ + do { char _tm[_cfmt_bn][_cfmt_sn], _tn=0, *_fm = _cfmt_conv(16, fmt, _cfmt(c), _cfmt(d), _cfmt(e), _cfmt(f), _cfmt(g), _cfmt(h), _cfmt(i), _cfmt(j), _cfmt(k), _cfmt(m), _cfmt(n), _cfmt(o), _cfmt(p), _cfmt(p), _cfmt(r), _cfmt(s)); \ _cfmt_fn(to)(to, _fm, c, d, e, f, g, h, i, j, k, m, n, o, p, q, r, s); free(_fm); } while (0) #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) -- cgit v1.2.3