add a printing abstraction layer so we can print type/ast to files and memory buffers
[cparser] / printer.h
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20
21 /**
22  * @file
23  * @brief  Abstracts away printing strings to a stream so we can reuse our
24  *         printing routines for printing to files or into memory
25  */
26 #ifndef PRINTER_H
27 #define PRINTER_H
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include "adt/obst.h"
33 #include "string_rep.h"
34
35 /** print a string into current output */
36 extern void (*print_string)(const char *str);
37 extern void (*print_vformat)(const char *format, va_list ap);
38 /** print a single unicode character to current output (encoded as UTF-8) */
39 extern void (*print_char)(wchar_rep_t c);
40
41 /** print a printf style format string to current output */
42 static inline void __attribute__((format(printf,1,2))) print_format(const char *format, ...)
43 {
44         va_list ap;
45         va_start(ap, format);
46         print_vformat(format, ap);
47         va_end(ap);
48 }
49
50 /** Set current output to be a FILE* stream */
51 void print_to_file(FILE *out);
52
53 /** Set current output to an obstack (grows an object on the obstack) */
54 void print_to_obstack(struct obstack *obst);
55
56 /** Set current output to be a buffer with limited size */
57 void print_to_buffer(char *buffer, size_t buffer_size);
58
59 /** Assures that the string in the buffer is 0 terminated */
60 void finish_print_to_buffer(void);
61
62 /** push current printer output to the (printer output) stack */
63 void printer_push(void);
64
65 /** pop a printer output from the stack */
66 void printer_pop(void);
67
68 #endif