summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-10-21 08:50:05 +0200
committerTyge Løvset <[email protected]>2020-10-21 08:50:05 +0200
commit8014ab4e48e7cf41377634742830f1a7ca5b7b7d (patch)
tree8e6276eee4bfe45b91b0655cbbd7fa9249bde5ab
parent1b05a8aa813c45bf3fb8a1232c0234b6fb8a2f47 (diff)
downloadSTC-modified-8014ab4e48e7cf41377634742830f1a7ca5b7b7d.tar.gz
STC-modified-8014ab4e48e7cf41377634742830f1a7ca5b7b7d.zip
Added c++ support for cfmt.h
-rw-r--r--stc/cfmt.h54
1 files changed, 39 insertions, 15 deletions
diff --git a/stc/cfmt.h b/stc/cfmt.h
index 2e82ca6a..9b3d8999 100644
--- a/stc/cfmt.h
+++ b/stc/cfmt.h
@@ -28,10 +28,16 @@
// https://gist.github.com/alexameen/6550d892338626964a870e408d1e8912
// https://gist.github.com/alexameen/4440e4bcad557a464dcc9ff884763049
+STC_API void
+_cfmt_printf(int s, const char* fmt, ...);
+STC_API char *
+_cfmt_conv(const char *fmt, ...);
+
+#ifndef __cplusplus
#define _cfmt_fn(x) _Generic ((x), \
FILE*: fprintf, \
- int: _cfmt_printf, \
char *: sprintf, \
+ int: _cfmt_printf, \
cstr_t *: cstr_fmt)
#define _cfmt(x) _Generic ((x), \
@@ -40,17 +46,37 @@
short: "hd", \
unsigned short: "hu", \
int: "d", \
- unsigned int: "u", \
- long int: "ld", \
- unsigned long int: "lu", \
- long long int: "lld", \
- unsigned long long int: "llu", \
+ unsigned: "u", \
+ long: "ld", \
+ unsigned long: "lu", \
+ long long: "lld", \
+ unsigned long long: "llu", \
float: "g", \
double: "g", \
long double: "Lg", \
char *: "s", \
void *: "p")
-
+#else
+ inline auto _cfmt_fn(FILE*) {return fprintf;}
+ 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(char x) {return "c";}
+ inline auto _cfmt(unsigned char x) {return "hhu";}
+ inline auto _cfmt(short x) {return "hd";}
+ inline auto _cfmt(unsigned short x) {return "hu";}
+ inline auto _cfmt(int x) {return "d";}
+ inline auto _cfmt(unsigned x) {return "u";}
+ inline auto _cfmt(long x) {return "ld";}
+ inline auto _cfmt(unsigned long x) {return "lu";}
+ inline auto _cfmt(long long x) {return "lld";}
+ inline auto _cfmt(unsigned long long x) {return "llu";}
+ inline auto _cfmt(float x) {return "g";}
+ inline auto _cfmt(double x) {return "g";}
+ inline auto _cfmt(long double x) {return "Lg";}
+ inline auto _cfmt(const char *x) {return "s";}
+ inline auto _cfmt(const void *x) {return "p";}
+#endif
#define c_print(...) c_MACRO_OVERLOAD(c_print, __VA_ARGS__)
#define c_print_3(to, fmt, c) \
@@ -102,9 +128,11 @@
do { char *_fm = _cfmt_conv(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)
-STC_API char *
-_cfmt_conv(const char *fmt, ...);
-STC_INLINE void
+#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
+
+#include <stdarg.h>
+
+STC_DEF void
_cfmt_printf(int s, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
@@ -112,13 +140,9 @@ _cfmt_printf(int s, const char* fmt, ...) {
va_end(args);
}
-#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
-
-#include <stdarg.h>
-
STC_DEF char *
_cfmt_conv(const char *fmt, ...) {
- char *fmt2 = strcpy((char *) malloc(strlen(fmt)*2 + 1), fmt), *p = fmt2, *f, ch;
+ char *fmt2 = (char *) malloc(strlen(fmt)*2 + 1), *p = fmt2, *f, ch;
va_list args;
va_start(args, fmt);
do {