parser: Remove the unused attribute alignment from struct declaration_specifiers_t.
[cparser] / printer.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2012 Matthias Braun <matze@braunis.de>
4  */
5 #include "config.h"
6
7 #include "printer.h"
8
9 #include <stdio.h>
10 #include <stdarg.h>
11
12 static FILE* out;
13
14 static void print_char_file(const char c)
15 {
16         fputc(c, out);
17 }
18
19 static void print_string_file(const char *str)
20 {
21         fputs(str, out);
22 }
23
24 static void print_vformat_file(const char *format, va_list ap)
25 {
26         vfprintf(out, format, ap);
27 }
28
29 void print_to_file(FILE *new_out)
30 {
31         out = new_out;
32         print_string  = print_string_file;
33         print_vformat = print_vformat_file;
34         print_char    = print_char_file;
35 }
36
37
38
39 static struct obstack *obst;
40
41 static void print_char_obstack(const char c)
42 {
43         obstack_1grow(obst, c);
44 }
45
46 static void print_string_obstack(const char *str)
47 {
48         size_t len = strlen(str);
49         obstack_grow(obst, str, len);
50 }
51
52 static void print_vformat_obstack(const char *format, va_list ap)
53 {
54         obstack_vprintf(obst, format, ap);
55 }
56
57 void print_to_obstack(struct obstack *new_obst)
58 {
59         obst = new_obst;
60         print_string  = print_string_obstack;
61         print_vformat = print_vformat_obstack;
62         print_char    = print_char_obstack;
63 }
64
65
66
67 static char *buffer_pos;
68 static char *buffer_end;
69
70 static void print_char_buffer(const char c)
71 {
72         if (buffer_pos == buffer_end)
73                 return;
74         *buffer_pos++ = c;
75 }
76
77 static void print_string_buffer(const char *str)
78 {
79         for (const char *c = str; *c != '\0'; ++c) {
80                 print_char_buffer(*c);
81         }
82 }
83
84 static void print_vformat_buffer(const char *format, va_list ap)
85 {
86         size_t size    = buffer_end - buffer_pos;
87         size_t written = (size_t) vsnprintf(buffer_pos, size, format, ap);
88         buffer_pos    += written < size ? written : size;
89 }
90
91 void print_to_buffer(char *buffer, size_t buffer_size)
92 {
93         buffer_pos = buffer;
94         buffer_end = buffer + buffer_size - 2;
95
96         print_string  = print_string_buffer;
97         print_vformat = print_vformat_buffer;
98         print_char    = print_char_buffer;
99 }
100
101 void finish_print_to_buffer(void)
102 {
103         *buffer_pos = '\0';
104         buffer_pos = NULL;
105         buffer_end = NULL;
106 }
107
108
109 void (*print_string)(const char *str) = print_string_file;
110 void (*print_vformat)(const char *format, va_list ap) = print_vformat_file;
111 void (*print_char)(const char c) = print_char_file;
112
113 void printer_push(void)
114 {
115         /* TODO */
116 }
117
118 void printer_pop(void)
119 {
120         /* TODO */
121 }