- copy libcore into libfirm
[libfirm] / ir / ir / irprintf.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   A little printf helper unterstanding firm types
23  * @author  Sebastian Hack
24  * @date    29.11.2004
25  * @version $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #ifdef HAVE_STRING_H
32 #include <string.h>
33 #endif
34 #ifdef HAVE_INTTYPES_H
35 #include <inttypes.h>
36 #endif
37
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <stdarg.h>
41
42 #include <ctype.h>
43
44 #include "firm_config.h"
45 #include "ident.h"
46 #include "irmode_t.h"
47 #include "irnode_t.h"
48 #include "entity_t.h"
49 #include "type_t.h"
50 #include "tv_t.h"
51 #include "irprintf.h"
52 #include "obst.h"
53 #include "pset.h"
54 #include "iterator.h"
55 #include "bitset.h"
56 #include "dbginfo_t.h"
57
58 #define STRNIL "(nil)"
59
60 /**
61  * Init the string.
62  */
63 static void str_init(void *object, size_t n)
64 {
65         (void) n;
66         strcpy(object, "");
67 }
68
69 /**
70  * append a char to a string buffer.
71  */
72 static void str_append_char(void *object, size_t n, char ch)
73 {
74   char buf[2];
75
76   buf[0] = ch;
77   buf[1] = 0;
78
79   strncat(object, buf, n);
80 }
81
82 /**
83  * append a string to a string buffer.
84  */
85 static void str_append_str(void *object, size_t n, const char *str)
86 {
87   strncat(object, str, n);
88 }
89
90
91 /**
92  * Init the file. i.e. do nothing.
93  */
94 static void file_init(void *object, size_t n)
95 {
96         (void) object;
97         (void) n;
98 }
99
100 /**
101  * append a char to a file.
102  */
103 static void file_append_char(void *object, size_t n, char ch)
104 {
105         (void) n;
106         fputc(ch, object);
107 }
108
109 /**
110  * append a string to a file.
111  */
112 static void file_append_str(void *object, size_t n, const char *str)
113 {
114         (void) n;
115         fputs(str, object);
116 }
117
118 /**
119  * Init the obstack. i.e. do nothing.
120  */
121 static void obst_init(void *object, size_t n)
122 {
123         (void) object;
124         (void) n;
125 }
126
127 /**
128  * append a char to a obstack.
129  */
130 static void obst_append_char(void *object, size_t n, char ch)
131 {
132         struct obstack *obst = object;
133         (void) n;
134         obstack_1grow(obst, ch);
135 }
136
137 /**
138  * append a string to a obstack.
139  */
140 static void obst_append_str(void *object, size_t n, const char *str)
141 {
142         struct obstack *obst = object;
143         (void) n;
144         obstack_grow(obst, str, strlen(str));
145 }
146
147
148 /**
149  * the file appender
150  */
151 static const appender_t file_appender = {
152   file_init,
153   file_append_char,
154   file_append_str
155 };
156
157 /**
158  * the string buffer appender
159  */
160 static const appender_t str_appender = {
161   str_init,
162   str_append_char,
163   str_append_str
164 };
165
166 /**
167  * the obstack appender.
168  */
169 static const appender_t obst_appender = {
170   obst_init,
171   obst_append_char,
172   obst_append_str
173 };
174
175 #include "irargs_t.h"
176
177 void ir_printf(const char *fmt, ...)
178 {
179         va_list args;
180
181         va_start(args, fmt);
182         lc_evprintf(firm_get_arg_env(), fmt, args);
183         va_end(args);
184 }
185
186 void ir_fprintf(FILE *f, const char *fmt, ...)
187 {
188         va_list args;
189
190         va_start(args, fmt);
191         lc_evfprintf(firm_get_arg_env(), f, fmt, args);
192         va_end(args);
193 }
194
195 void ir_snprintf(char *buf, size_t n, const char *fmt, ...)
196 {
197         va_list args;
198
199         va_start(args, fmt);
200         lc_evsnprintf(firm_get_arg_env(), buf, n, fmt, args);
201         va_end(args);
202 }
203
204 void ir_vprintf(const char *fmt, va_list args)
205 {
206         lc_evprintf(firm_get_arg_env(), fmt, args);
207 }
208
209 void ir_vfprintf(FILE *f, const char *fmt, va_list args)
210 {
211         lc_evfprintf(firm_get_arg_env(), f, fmt, args);
212 }
213
214 void ir_vsnprintf(char *buf, size_t len, const char *fmt, va_list args)
215 {
216         lc_evsnprintf(firm_get_arg_env(), buf, len, fmt, args);
217 }
218
219 void ir_obst_vprintf(struct obstack *obst, const char *fmt, va_list args)
220 {
221         lc_evoprintf(firm_get_arg_env(), obst, fmt, args);
222 }