[604e76d] | 1 | #include <stdarg.h>
|
---|
| 2 | #include <stdio.h>
|
---|
| 3 | #include "c-pair.h"
|
---|
| 4 | #include "c-print.h"
|
---|
| 5 |
|
---|
| 6 | void print_string(FILE* out, const char* x) { fprintf(out, "%s", x); }
|
---|
| 7 |
|
---|
| 8 | void print_bool(FILE* out, _Bool x) { fprintf(out, "%s", x ? "true" : "false"); }
|
---|
| 9 |
|
---|
| 10 | void print_char(FILE* out, char x) {
|
---|
| 11 | if ( 0x20 <= x && x <= 0x7E ) { fprintf(out, "'%c'", x); }
|
---|
| 12 | else { fprintf(out, "'\\%x'", x); }
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 | void print_int(FILE* out, int x) { fprintf(out, "%d", x); }
|
---|
| 16 |
|
---|
| 17 | void print_fmt(FILE* out, char fmt, void* p) {
|
---|
| 18 | switch( fmt ) {
|
---|
| 19 | case 's': print_string(out, (const char*)p); break; /***/
|
---|
| 20 | case 'b': print_bool(out, *(_Bool*)p); break; /***/
|
---|
| 21 | case 'c': print_char(out, *(char*)p); break; /***/
|
---|
| 22 | case 'd': print_int(out, *(int*)p); break; /***/
|
---|
| 23 | }
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | void print(FILE* out, const char* fmt, ...) {
|
---|
| 27 | va_list args;
|
---|
| 28 | va_start(args, fmt);
|
---|
| 29 | for (const char* it = fmt; *it; ++it) {
|
---|
| 30 | switch( *it ) {
|
---|
| 31 | case 's': print_string(out, va_arg(args, const char*)); break; /***/
|
---|
| 32 | case 'b': print_bool(out, va_arg(args, int)); break; /***/
|
---|
| 33 | case 'c': print_char(out, va_arg(args, int)); break; /***/
|
---|
| 34 | case 'd': print_int(out, va_arg(args, int)); break; /***/
|
---|
| 35 | case 'p': {
|
---|
| 36 | const struct pair x = va_arg(args, const struct pair); /***/
|
---|
| 37 | fprintf(out, "[");
|
---|
| 38 | print_fmt(out, *++it, x.first); /***/
|
---|
| 39 | fprintf(out, ", ");
|
---|
| 40 | print_fmt(out, *++it, x.second); /***/
|
---|
| 41 | fprintf(out, "]");
|
---|
| 42 | break;
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 | va_end(args);
|
---|
| 47 | }
|
---|