source: doc/papers/general/evaluation/c-print.c@ 3ca912a

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 3ca912a was ac4dad2, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

shorten experimental code

  • Property mode set to 100644
File size: 1.4 KB
Line 
1#include <stdarg.h>
2#include <stdio.h>
3#include "c-pair.h"
4#include "c-print.h"
5
6void print_string( FILE * out, const char * x ) { fprintf( out, "%s", x ); }
7
8void print_bool( FILE * out, _Bool x ) { fprintf( out, "%s", x ? "true" : "false" ); }
9
10void print_char( FILE * out, char x ) {
11 if ( 0x20 <= x && x <= 0x7E ) { fprintf( out, "'%c'", x ); }
12 else { fprintf( out, "'\\%x'", x ); }
13}
14
15void print_int( FILE * out, int x ) { fprintf( out, "%d", x ); }
16
17void 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
26void 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}
Note: See TracBrowser for help on using the repository browser.