2 * This file is part of cparser.
3 * Copyright (C) 2007-2009 Matthias Braun <matze@braunis.de>
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.
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.
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
29 static void print_char_file(const char c)
34 static void print_string_file(const char *str)
39 static void print_vformat_file(const char *format, va_list ap)
41 vfprintf(out, format, ap);
44 void print_to_file(FILE *new_out)
47 print_string = print_string_file;
48 print_vformat = print_vformat_file;
49 print_char = print_char_file;
54 static struct obstack *obst;
56 static void print_char_obstack(const char c)
58 obstack_1grow(obst, c);
61 static void print_string_obstack(const char *str)
63 size_t len = strlen(str);
64 obstack_grow(obst, str, len);
67 static void print_vformat_obstack(const char *format, va_list ap)
69 obstack_vprintf(obst, format, ap);
72 void print_to_obstack(struct obstack *new_obst)
75 print_string = print_string_obstack;
76 print_vformat = print_vformat_obstack;
77 print_char = print_char_obstack;
82 static char *buffer_pos;
83 static char *buffer_end;
85 static void print_char_buffer(const char c)
87 if (buffer_pos == buffer_end)
92 static void print_string_buffer(const char *str)
94 for (const char *c = str; *c != '\0'; ++c) {
95 print_char_buffer(*c);
99 static void print_vformat_buffer(const char *format, va_list ap)
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;
106 void print_to_buffer(char *buffer, size_t buffer_size)
109 buffer_end = buffer + buffer_size - 2;
111 print_string = print_string_buffer;
112 print_vformat = print_vformat_buffer;
113 print_char = print_char_buffer;
116 void finish_print_to_buffer(void)
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;
128 void printer_push(void)
133 void printer_pop(void)