isl_ast_codegen.c: create_node_scaled: inline call to isl_ast_graft_alloc_level
[isl.git] / isl_printer.c
blobd5407fce6add3d88cc89938beaf56e0859174509
1 #include <string.h>
2 #include <isl_int.h>
3 #include <isl_printer_private.h>
5 static __isl_give isl_printer *file_start_line(__isl_take isl_printer *p)
7 fprintf(p->file, "%s%*s%s", p->indent_prefix ? p->indent_prefix : "",
8 p->indent, "", p->prefix ? p->prefix : "");
9 return p;
12 static __isl_give isl_printer *file_end_line(__isl_take isl_printer *p)
14 fprintf(p->file, "%s\n", p->suffix ? p->suffix : "");
15 return p;
18 static __isl_give isl_printer *file_flush(__isl_take isl_printer *p)
20 fflush(p->file);
21 return p;
24 static __isl_give isl_printer *file_print_str(__isl_take isl_printer *p,
25 const char *s)
27 fprintf(p->file, "%s", s);
28 return p;
31 static __isl_give isl_printer *file_print_double(__isl_take isl_printer *p,
32 double d)
34 fprintf(p->file, "%g", d);
35 return p;
38 static __isl_give isl_printer *file_print_int(__isl_take isl_printer *p, int i)
40 fprintf(p->file, "%d", i);
41 return p;
44 static __isl_give isl_printer *file_print_isl_int(__isl_take isl_printer *p, isl_int i)
46 isl_int_print(p->file, i, p->width);
47 return p;
50 static int grow_buf(__isl_keep isl_printer *p, int extra)
52 int new_size;
53 char *new_buf;
55 if (p->buf_size == 0)
56 return -1;
58 new_size = ((p->buf_n + extra + 1) * 3) / 2;
59 new_buf = isl_realloc_array(p->ctx, p->buf, char, new_size);
60 if (!new_buf) {
61 p->buf_size = 0;
62 return -1;
64 p->buf = new_buf;
65 p->buf_size = new_size;
67 return 0;
70 static __isl_give isl_printer *str_print(__isl_take isl_printer *p,
71 const char *s, int len)
73 if (p->buf_n + len + 1 >= p->buf_size && grow_buf(p, len))
74 goto error;
75 memcpy(p->buf + p->buf_n, s, len);
76 p->buf_n += len;
78 p->buf[p->buf_n] = '\0';
79 return p;
80 error:
81 isl_printer_free(p);
82 return NULL;
85 static __isl_give isl_printer *str_print_indent(__isl_take isl_printer *p,
86 int indent)
88 int i;
90 if (p->buf_n + indent + 1 >= p->buf_size && grow_buf(p, indent))
91 goto error;
92 for (i = 0; i < indent; ++i)
93 p->buf[p->buf_n++] = ' ';
94 return p;
95 error:
96 isl_printer_free(p);
97 return NULL;
100 static __isl_give isl_printer *str_start_line(__isl_take isl_printer *p)
102 if (p->indent_prefix)
103 p = str_print(p, p->indent_prefix, strlen(p->indent_prefix));
104 p = str_print_indent(p, p->indent);
105 if (p->prefix)
106 p = str_print(p, p->prefix, strlen(p->prefix));
107 return p;
110 static __isl_give isl_printer *str_end_line(__isl_take isl_printer *p)
112 if (p->suffix)
113 p = str_print(p, p->suffix, strlen(p->suffix));
114 p = str_print(p, "\n", strlen("\n"));
115 return p;
118 static __isl_give isl_printer *str_flush(__isl_take isl_printer *p)
120 p->buf_n = 0;
121 return p;
124 static __isl_give isl_printer *str_print_str(__isl_take isl_printer *p,
125 const char *s)
127 return str_print(p, s, strlen(s));
130 static __isl_give isl_printer *str_print_double(__isl_take isl_printer *p,
131 double d)
133 int left = p->buf_size - p->buf_n;
134 int need = snprintf(p->buf + p->buf_n, left, "%g", d);
135 if (need >= left) {
136 if (grow_buf(p, need))
137 goto error;
138 left = p->buf_size - p->buf_n;
139 need = snprintf(p->buf + p->buf_n, left, "%g", d);
141 p->buf_n += need;
142 return p;
143 error:
144 isl_printer_free(p);
145 return NULL;
148 static __isl_give isl_printer *str_print_int(__isl_take isl_printer *p, int i)
150 int left = p->buf_size - p->buf_n;
151 int need = snprintf(p->buf + p->buf_n, left, "%d", i);
152 if (need >= left) {
153 if (grow_buf(p, need))
154 goto error;
155 left = p->buf_size - p->buf_n;
156 need = snprintf(p->buf + p->buf_n, left, "%d", i);
158 p->buf_n += need;
159 return p;
160 error:
161 isl_printer_free(p);
162 return NULL;
165 static __isl_give isl_printer *str_print_isl_int(__isl_take isl_printer *p,
166 isl_int i)
168 char *s;
169 int len;
171 s = isl_int_get_str(i);
172 len = strlen(s);
173 if (len < p->width)
174 p = str_print_indent(p, p->width - len);
175 p = str_print(p, s, len);
176 isl_int_free_str(s);
177 return p;
180 struct isl_printer_ops {
181 __isl_give isl_printer *(*start_line)(__isl_take isl_printer *p);
182 __isl_give isl_printer *(*end_line)(__isl_take isl_printer *p);
183 __isl_give isl_printer *(*print_double)(__isl_take isl_printer *p,
184 double d);
185 __isl_give isl_printer *(*print_int)(__isl_take isl_printer *p, int i);
186 __isl_give isl_printer *(*print_isl_int)(__isl_take isl_printer *p,
187 isl_int i);
188 __isl_give isl_printer *(*print_str)(__isl_take isl_printer *p,
189 const char *s);
190 __isl_give isl_printer *(*flush)(__isl_take isl_printer *p);
193 static struct isl_printer_ops file_ops = {
194 file_start_line,
195 file_end_line,
196 file_print_double,
197 file_print_int,
198 file_print_isl_int,
199 file_print_str,
200 file_flush
203 static struct isl_printer_ops str_ops = {
204 str_start_line,
205 str_end_line,
206 str_print_double,
207 str_print_int,
208 str_print_isl_int,
209 str_print_str,
210 str_flush
213 __isl_give isl_printer *isl_printer_to_file(isl_ctx *ctx, FILE *file)
215 struct isl_printer *p = isl_alloc_type(ctx, struct isl_printer);
216 if (!p)
217 return NULL;
218 p->ctx = ctx;
219 isl_ctx_ref(p->ctx);
220 p->ops = &file_ops;
221 p->file = file;
222 p->buf = NULL;
223 p->buf_n = 0;
224 p->buf_size = 0;
225 p->indent = 0;
226 p->output_format = ISL_FORMAT_ISL;
227 p->indent_prefix = NULL;
228 p->prefix = NULL;
229 p->suffix = NULL;
230 p->width = 0;
232 return p;
235 __isl_give isl_printer *isl_printer_to_str(isl_ctx *ctx)
237 struct isl_printer *p = isl_calloc_type(ctx, struct isl_printer);
238 if (!p)
239 return NULL;
240 p->ctx = ctx;
241 isl_ctx_ref(p->ctx);
242 p->ops = &str_ops;
243 p->file = NULL;
244 p->buf = isl_alloc_array(ctx, char, 256);
245 if (!p->buf)
246 goto error;
247 p->buf_n = 0;
248 p->buf[0] = '\0';
249 p->buf_size = 256;
250 p->indent = 0;
251 p->output_format = ISL_FORMAT_ISL;
252 p->indent_prefix = NULL;
253 p->prefix = NULL;
254 p->suffix = NULL;
255 p->width = 0;
257 return p;
258 error:
259 isl_printer_free(p);
260 return NULL;
263 __isl_null isl_printer *isl_printer_free(__isl_take isl_printer *p)
265 if (!p)
266 return NULL;
267 free(p->buf);
268 free(p->indent_prefix);
269 free(p->prefix);
270 free(p->suffix);
271 isl_ctx_deref(p->ctx);
272 free(p);
274 return NULL;
277 isl_ctx *isl_printer_get_ctx(__isl_keep isl_printer *printer)
279 return printer ? printer->ctx : NULL;
282 FILE *isl_printer_get_file(__isl_keep isl_printer *printer)
284 if (!printer)
285 return NULL;
286 if (!printer->file)
287 isl_die(isl_printer_get_ctx(printer), isl_error_invalid,
288 "not a file printer", return NULL);
289 return printer->file;
292 __isl_give isl_printer *isl_printer_set_isl_int_width(__isl_take isl_printer *p,
293 int width)
295 if (!p)
296 return NULL;
298 p->width = width;
300 return p;
303 __isl_give isl_printer *isl_printer_set_indent(__isl_take isl_printer *p,
304 int indent)
306 if (!p)
307 return NULL;
309 p->indent = indent;
311 return p;
314 __isl_give isl_printer *isl_printer_indent(__isl_take isl_printer *p,
315 int indent)
317 if (!p)
318 return NULL;
320 p->indent += indent;
321 if (p->indent < 0)
322 p->indent = 0;
324 return p;
327 /* Replace the indent prefix of "p" by "prefix".
329 __isl_give isl_printer *isl_printer_set_indent_prefix(__isl_take isl_printer *p,
330 const char *prefix)
332 if (!p)
333 return NULL;
335 free(p->indent_prefix);
336 p->indent_prefix = prefix ? strdup(prefix) : NULL;
338 return p;
341 __isl_give isl_printer *isl_printer_set_prefix(__isl_take isl_printer *p,
342 const char *prefix)
344 if (!p)
345 return NULL;
347 free(p->prefix);
348 p->prefix = prefix ? strdup(prefix) : NULL;
350 return p;
353 __isl_give isl_printer *isl_printer_set_suffix(__isl_take isl_printer *p,
354 const char *suffix)
356 if (!p)
357 return NULL;
359 free(p->suffix);
360 p->suffix = suffix ? strdup(suffix) : NULL;
362 return p;
365 __isl_give isl_printer *isl_printer_set_output_format(__isl_take isl_printer *p,
366 int output_format)
368 if (!p)
369 return NULL;
371 p->output_format = output_format;
373 return p;
376 int isl_printer_get_output_format(__isl_keep isl_printer *p)
378 if (!p)
379 return -1;
380 return p->output_format;
383 __isl_give isl_printer *isl_printer_print_str(__isl_take isl_printer *p,
384 const char *s)
386 if (!p)
387 return NULL;
388 if (!s)
389 return isl_printer_free(p);
391 return p->ops->print_str(p, s);
394 __isl_give isl_printer *isl_printer_print_double(__isl_take isl_printer *p,
395 double d)
397 if (!p)
398 return NULL;
400 return p->ops->print_double(p, d);
403 __isl_give isl_printer *isl_printer_print_int(__isl_take isl_printer *p, int i)
405 if (!p)
406 return NULL;
408 return p->ops->print_int(p, i);
411 __isl_give isl_printer *isl_printer_print_isl_int(__isl_take isl_printer *p,
412 isl_int i)
414 if (!p)
415 return NULL;
417 return p->ops->print_isl_int(p, i);
420 __isl_give isl_printer *isl_printer_start_line(__isl_take isl_printer *p)
422 if (!p)
423 return NULL;
425 return p->ops->start_line(p);
428 __isl_give isl_printer *isl_printer_end_line(__isl_take isl_printer *p)
430 if (!p)
431 return NULL;
433 return p->ops->end_line(p);
436 char *isl_printer_get_str(__isl_keep isl_printer *printer)
438 if (!printer || !printer->buf)
439 return NULL;
440 return strdup(printer->buf);
443 __isl_give isl_printer *isl_printer_flush(__isl_take isl_printer *p)
445 if (!p)
446 return NULL;
448 return p->ops->flush(p);