-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
52 lines (41 loc) · 1.38 KB
/
example.c
File metadata and controls
52 lines (41 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#define CFMT_FORMAT_IMPLEMENTATION // declare implementation
// define custom formatters before include
#define CFMT_CUSTOM_PRINT_TYPES(X) \
X(Vec3, vec3_formatter)
#include "format.h"
#include <stdarg.h>
#include <math.h>
// custom type that can be formatted
typedef struct {
float x, y, z;
} Vec3;
// custom formatter setup
// should behave exactly like snprintf
int vec3_formatter(char *buf, size_t size, ...) {
va_list ap;
va_start(ap, size);
Vec3 v = va_arg(ap, Vec3);
va_end(ap);
return snprintf(buf, size, "{%g, %g, %g}", v.x, v.y, v.z);
}
int main() {
Vec3 v = {1, 2.5, 3};
// regular printing
print("%; %; %; %; %\n", 1+2, "Hello world", (void*)0xbeefbabe, v, sin(M_PI_4));
// -> "3; Hello world; 0xbeefbabe; {1, 2.5, 3}; 0.707107"
// missing arguments
print("% + % = %\n", 1, 2);
// -> "1 + 2 = %!MISSING"
// extra arguments
print("hi", 1+2, "Hello world", (void*)0xbeefbabe, v, sin(M_PI_4));
// -> "hi%!(EXTRA int=3, char*=Hello world, void*=0xbeefbabe, Vec3={1, 2.5, 3}, double=0.707107)"
// unknown type compile error
struct foo {} f;
(void)f;
// print("%", f);
// -> error: controlling expression type 'struct foo' not compatible with any generic association type
puts("\n\n");
char buf[16];
snprint(buf, 16, "hi", 1+2, "Hello world", (void*)0xbeefbabe, v, sin(M_PI_4));
puts(buf);
}