type: Add missing space for printing complex types.
[cparser] / printer.c
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 #include "config.h"
21
22 #include "printer.h"
23
24 #include <stdio.h>
25 #include <stdarg.h>
26
27 static FILE* out;
28
29 static void print_char_file(const char c)
30 {
31         fputc(c, out);
32 }
33
34 static void print_string_file(const char *str)
35 {
36         fputs(str, out);
37 }
38
39 static void print_vformat_file(const char *format, va_list ap)
40 {
41         vfprintf(out, format, ap);
42 }
43
44 void print_to_file(FILE *new_out)
45 {
46         out = new_out;
47         print_string  = print_string_file;
48         print_vformat = print_vformat_file;
49         print_char    = print_char_file;
50 }
51
52
53
54 static struct obstack *obst;
55
56 static void print_char_obstack(const char c)
57 {
58         obstack_1grow(obst, c);
59 }
60
61 static void print_string_obstack(const char *str)
62 {
63         size_t len = strlen(str);
64         obstack_grow(obst, str, len);
65 }
66
67 static void print_vformat_obstack(const char *format, va_list ap)
68 {
69         obstack_vprintf(obst, format, ap);
70 }
71
72 void print_to_obstack(struct obstack *new_obst)
73 {
74         obst = new_obst;
75         print_string  = print_string_obstack;
76         print_vformat = print_vformat_obstack;
77         print_char    = print_char_obstack;
78 }
79
80
81
82 static char *buffer_pos;
83 static char *buffer_end;
84
85 static void print_char_buffer(const char c)
86 {
87         if (buffer_pos == buffer_end)
88                 return;
89         *buffer_pos++ = c;
90 }
91
92 static void print_string_buffer(const char *str)
93 {
94         for (const char *c = str; *c != '\0'; ++c) {
95                 print_char_buffer(*c);
96         }
97 }
98
99 static void print_vformat_buffer(const char *format, va_list ap)
100 {
101         size_t size    = buffer_end - buffer_pos;
102         size_t written = (size_t) vsnprintf(buffer_pos, size, format, ap);
103         buffer_pos    += written < size ? written : size;
104 }
105
106 void print_to_buffer(char *buffer, size_t buffer_size)
107 {
108         buffer_pos = buffer;
109         buffer_end = buffer + buffer_size - 2;
110
111         print_string  = print_string_buffer;
112         print_vformat = print_vformat_buffer;
113         print_char    = print_char_buffer;
114 }
115
116 void finish_print_to_buffer(void)
117 {
118         *buffer_pos = '\0';
119         buffer_pos = NULL;
120         buffer_end = NULL;
121 }
122
123
124 void (*print_string)(const char *str) = print_string_file;
125 void (*print_vformat)(const char *format, va_list ap) = print_vformat_file;
126 void (*print_char)(const char c) = print_char_file;
127
128 void printer_push(void)
129 {
130         /* TODO */
131 }
132
133 void printer_pop(void)
134 {
135         /* TODO */
136 }