| 1 |
// Formatting library for C++ - the C API |
| 2 |
// |
| 3 |
// Copyright (c) 2012 - present, Victor Zverovich and {fmt} contributors |
| 4 |
// All rights reserved. |
| 5 |
// |
| 6 |
// For the license information refer to format.h. |
| 7 |
|
| 8 |
#ifndef FMT_C_H_ |
| 9 |
#define FMT_C_H_ |
| 10 |
|
| 11 |
#include <stdbool.h> // bool |
| 12 |
#include <stddef.h> // size_t |
| 13 |
#include <stdio.h> // FILE |
| 14 |
|
| 15 |
#ifdef __cplusplus |
| 16 |
extern "C" { |
| 17 |
#endif |
| 18 |
|
| 19 |
typedef enum { |
| 20 |
fmt_int = 1, |
| 21 |
fmt_uint, |
| 22 |
fmt_bool = 7, |
| 23 |
fmt_char, |
| 24 |
fmt_float, |
| 25 |
fmt_double, |
| 26 |
fmt_long_double, |
| 27 |
fmt_cstring, |
| 28 |
fmt_pointer = 14 |
| 29 |
} fmt_type; |
| 30 |
|
| 31 |
typedef struct { |
| 32 |
fmt_type type; |
| 33 |
union { |
| 34 |
long long int_value; |
| 35 |
unsigned long long uint_value; // Used for FMT_PTR and custom data |
| 36 |
bool bool_value; |
| 37 |
char char_value; |
| 38 |
float float_value; |
| 39 |
double double_value; |
| 40 |
long double long_double_value; |
| 41 |
const char* cstring; |
| 42 |
const void* pointer; |
| 43 |
} value; |
| 44 |
} fmt_arg; |
| 45 |
|
| 46 |
enum { fmt_error = -1, fmt_error_invalid_arg = -2 }; |
| 47 |
|
| 48 |
int fmt_vformat(char* buffer, size_t size, const char* fmt, const fmt_arg* args, |
| 49 |
size_t num_args); |
| 50 |
int fmt_vprint(FILE* stream, const char* fmt, const fmt_arg* args, |
| 51 |
size_t num_args); |
| 52 |
|
| 53 |
#ifdef __cplusplus |
| 54 |
} |
| 55 |
#endif |
| 56 |
|
| 57 |
#ifndef __cplusplus |
| 58 |
|
| 59 |
static inline fmt_arg fmt_from_int(long long x) { |
| 60 |
return (fmt_arg){.type = fmt_int, .value.int_value = x}; |
| 61 |
} |
| 62 |
|
| 63 |
static inline fmt_arg fmt_from_uint(unsigned long long x) { |
| 64 |
return (fmt_arg){.type = fmt_uint, .value.uint_value = x}; |
| 65 |
} |
| 66 |
|
| 67 |
static inline fmt_arg fmt_from_bool(bool x) { |
| 68 |
return (fmt_arg){.type = fmt_bool, .value.bool_value = x}; |
| 69 |
} |
| 70 |
|
| 71 |
static inline fmt_arg fmt_from_char(char x) { |
| 72 |
return (fmt_arg){.type = fmt_char, .value.char_value = x}; |
| 73 |
} |
| 74 |
|
| 75 |
static inline fmt_arg fmt_from_float(float x) { |
| 76 |
return (fmt_arg){.type = fmt_float, .value.float_value = x}; |
| 77 |
} |
| 78 |
|
| 79 |
static inline fmt_arg fmt_from_double(double x) { |
| 80 |
return (fmt_arg){.type = fmt_double, .value.double_value = x}; |
| 81 |
} |
| 82 |
|
| 83 |
static inline fmt_arg fmt_from_long_double(long double x) { |
| 84 |
return (fmt_arg){.type = fmt_long_double, .value.long_double_value = x}; |
| 85 |
} |
| 86 |
|
| 87 |
static inline fmt_arg fmt_from_str(const char* x) { |
| 88 |
return (fmt_arg){.type = fmt_cstring, .value.cstring = x}; |
| 89 |
} |
| 90 |
|
| 91 |
static inline fmt_arg fmt_from_ptr(const void* x) { |
| 92 |
return (fmt_arg){.type = fmt_pointer, .value.pointer = x}; |
| 93 |
} |
| 94 |
|
| 95 |
void fmt_unsupported_type(void); |
| 96 |
|
| 97 |
# if !defined(_MSC_VER) || defined(__clang__) |
| 98 |
typedef signed char fmt_signed_char; |
| 99 |
# else |
| 100 |
typedef enum {} fmt_signed_char; |
| 101 |
# endif |
| 102 |
|
| 103 |
// Require modern MSVC with conformant preprocessor. |
| 104 |
# if defined(_MSC_VER) && !defined(__clang__) && \ |
| 105 |
(!defined(_MSVC_TRADITIONAL) || _MSVC_TRADITIONAL) |
| 106 |
# error "C API requires MSVC 2019+ with /Zc:preprocessor flag." |
| 107 |
# endif |
| 108 |
|
| 109 |
# define FMT_MAKE_ARG(x) \ |
| 110 |
_Generic((x), \ |
| 111 |
fmt_signed_char: fmt_from_int, \ |
| 112 |
unsigned char: fmt_from_uint, \ |
| 113 |
short: fmt_from_int, \ |
| 114 |
unsigned short: fmt_from_uint, \ |
| 115 |
int: fmt_from_int, \ |
| 116 |
unsigned int: fmt_from_uint, \ |
| 117 |
long: fmt_from_int, \ |
| 118 |
unsigned long: fmt_from_uint, \ |
| 119 |
long long: fmt_from_int, \ |
| 120 |
unsigned long long: fmt_from_uint, \ |
| 121 |
bool: fmt_from_bool, \ |
| 122 |
char: fmt_from_char, \ |
| 123 |
float: fmt_from_float, \ |
| 124 |
double: fmt_from_double, \ |
| 125 |
long double: fmt_from_long_double, \ |
| 126 |
char*: fmt_from_str, \ |
| 127 |
const char*: fmt_from_str, \ |
| 128 |
void*: fmt_from_ptr, \ |
| 129 |
const void*: fmt_from_ptr, \ |
| 130 |
default: fmt_unsupported_type)(x) |
| 131 |
|
| 132 |
# define FMT_CAT(a, b) FMT_CAT_(a, b) |
| 133 |
# define FMT_CAT_(a, b) a##b |
| 134 |
|
| 135 |
# define FMT_NARG_(_unused, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, \ |
| 136 |
_12, _13, _14, _15, _16, N, ...) \ |
| 137 |
N |
| 138 |
# define FMT_NARG(_unused, ...) \ |
| 139 |
FMT_NARG_(, ##__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, \ |
| 140 |
3, 2, 1, 0) |
| 141 |
|
| 142 |
# define FMT_MAP_0(...) |
| 143 |
# define FMT_MAP_1(f, a) f(a) |
| 144 |
# define FMT_MAP_2(f, a, b) f(a), f(b) |
| 145 |
# define FMT_MAP_3(f, a, b, c) f(a), f(b), f(c) |
| 146 |
# define FMT_MAP_4(f, a, b, c, d) f(a), f(b), f(c), f(d) |
| 147 |
# define FMT_MAP_5(f, a, b, c, d, e) f(a), f(b), f(c), f(d), f(e) |
| 148 |
# define FMT_MAP_6(f, a, b, c, d, e, g) f(a), f(b), f(c), f(d), f(e), f(g) |
| 149 |
# define FMT_MAP_7(f, a, b, c, d, e, g, h) \ |
| 150 |
f(a), f(b), f(c), f(d), f(e), f(g), f(h) |
| 151 |
# define FMT_MAP_8(f, a, b, c, d, e, g, h, i) \ |
| 152 |
f(a), f(b), f(c), f(d), f(e), f(g), f(h), f(i) |
| 153 |
# define FMT_MAP_9(f, a, b, c, d, e, g, h, i, j) \ |
| 154 |
f(a), f(b), f(c), f(d), f(e), f(g), f(h), f(i), f(j) |
| 155 |
# define FMT_MAP_10(f, a, b, c, d, e, g, h, i, j, k) \ |
| 156 |
f(a), f(b), f(c), f(d), f(e), f(g), f(h), f(i), f(j), f(k) |
| 157 |
# define FMT_MAP_11(f, a, b, c, d, e, g, h, i, j, k, l) \ |
| 158 |
f(a), f(b), f(c), f(d), f(e), f(g), f(h), f(i), f(j), f(k), f(l) |
| 159 |
# define FMT_MAP_12(f, a, b, c, d, e, g, h, i, j, k, l, m) \ |
| 160 |
f(a), f(b), f(c), f(d), f(e), f(g), f(h), f(i), f(j), f(k), f(l), f(m) |
| 161 |
# define FMT_MAP_13(f, a, b, c, d, e, g, h, i, j, k, l, m, n) \ |
| 162 |
f(a), f(b), f(c), f(d), f(e), f(g), f(h), f(i), f(j), f(k), f(l), f(m), f(n) |
| 163 |
# define FMT_MAP_14(f, a, b, c, d, e, g, h, i, j, k, l, m, n, o) \ |
| 164 |
f(a), f(b), f(c), f(d), f(e), f(g), f(h), f(i), f(j), f(k), f(l), f(m), \ |
| 165 |
f(n), f(o) |
| 166 |
# define FMT_MAP_15(f, a, b, c, d, e, g, h, i, j, k, l, m, n, o, p) \ |
| 167 |
f(a), f(b), f(c), f(d), f(e), f(g), f(h), f(i), f(j), f(k), f(l), f(m), \ |
| 168 |
f(n), f(o), f(p) |
| 169 |
# define FMT_MAP_16(f, a, b, c, d, e, g, h, i, j, k, l, m, n, o, p, q) \ |
| 170 |
f(a), f(b), f(c), f(d), f(e), f(g), f(h), f(i), f(j), f(k), f(l), f(m), \ |
| 171 |
f(n), f(o), f(p), f(q) |
| 172 |
|
| 173 |
# define FMT_MAP(f, ...) \ |
| 174 |
FMT_CAT(FMT_MAP_, FMT_NARG(, ##__VA_ARGS__))(f, ##__VA_ARGS__) |
| 175 |
|
| 176 |
// select between two expressions depending on whether __VA_ARGS__ is empty |
| 177 |
// expands to e if __VA_ARGS__ is empty and n otherwise |
| 178 |
# define FMT_VA_SELECT(e, n, ...) \ |
| 179 |
FMT_NARG_(, ##__VA_ARGS__, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, \ |
| 180 |
e) |
| 181 |
|
| 182 |
# define FMT_MAKE_NULL(...) NULL |
| 183 |
# define FMT_MAKE_ARGLIST(...) \ |
| 184 |
(fmt_arg[]) { FMT_MAP(FMT_MAKE_ARG, ##__VA_ARGS__) } |
| 185 |
# define FMT_EXPAND(v) v |
| 186 |
|
| 187 |
# define FMT_FORMAT_ARGS(fmt, ...) \ |
| 188 |
(fmt), \ |
| 189 |
FMT_EXPAND(FMT_VA_SELECT(FMT_MAKE_NULL, FMT_MAKE_ARGLIST, \ |
| 190 |
##__VA_ARGS__)(__VA_ARGS__)), \ |
| 191 |
FMT_NARG(, ##__VA_ARGS__) |
| 192 |
|
| 193 |
# define fmt_format(buffer, size, fmt, ...) \ |
| 194 |
fmt_vformat((buffer), (size), FMT_FORMAT_ARGS((fmt), ##__VA_ARGS__)) |
| 195 |
|
| 196 |
# define fmt_print(stream, fmt, ...) \ |
| 197 |
fmt_vprint((stream), FMT_FORMAT_ARGS((fmt), ##__VA_ARGS__)) |
| 198 |
|
| 199 |
#endif // __cplusplus |
| 200 |
|
| 201 |
#endif // FMT_C_H_ |