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