add isl_union_pw_aff_val_on_domain
[isl.git] / isl_printer.c
blobb7afba5265513f48655b952832581bf8d4255506
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 p->buf[p->buf_n] = '\0';
122 return p;
125 static __isl_give isl_printer *str_print_str(__isl_take isl_printer *p,
126 const char *s)
128 return str_print(p, s, strlen(s));
131 static __isl_give isl_printer *str_print_double(__isl_take isl_printer *p,
132 double d)
134 int left = p->buf_size - p->buf_n;
135 int need = snprintf(p->buf + p->buf_n, left, "%g", d);
136 if (need >= left) {
137 if (grow_buf(p, need))
138 goto error;
139 left = p->buf_size - p->buf_n;
140 need = snprintf(p->buf + p->buf_n, left, "%g", d);
142 p->buf_n += need;
143 return p;
144 error:
145 isl_printer_free(p);
146 return NULL;
149 static __isl_give isl_printer *str_print_int(__isl_take isl_printer *p, int i)
151 int left = p->buf_size - p->buf_n;
152 int need = snprintf(p->buf + p->buf_n, left, "%d", i);
153 if (need >= left) {
154 if (grow_buf(p, need))
155 goto error;
156 left = p->buf_size - p->buf_n;
157 need = snprintf(p->buf + p->buf_n, left, "%d", i);
159 p->buf_n += need;
160 return p;
161 error:
162 isl_printer_free(p);
163 return NULL;
166 static __isl_give isl_printer *str_print_isl_int(__isl_take isl_printer *p,
167 isl_int i)
169 char *s;
170 int len;
172 s = isl_int_get_str(i);
173 len = strlen(s);
174 if (len < p->width)
175 p = str_print_indent(p, p->width - len);
176 p = str_print(p, s, len);
177 isl_int_free_str(s);
178 return p;
181 struct isl_printer_ops {
182 __isl_give isl_printer *(*start_line)(__isl_take isl_printer *p);
183 __isl_give isl_printer *(*end_line)(__isl_take isl_printer *p);
184 __isl_give isl_printer *(*print_double)(__isl_take isl_printer *p,
185 double d);
186 __isl_give isl_printer *(*print_int)(__isl_take isl_printer *p, int i);
187 __isl_give isl_printer *(*print_isl_int)(__isl_take isl_printer *p,
188 isl_int i);
189 __isl_give isl_printer *(*print_str)(__isl_take isl_printer *p,
190 const char *s);
191 __isl_give isl_printer *(*flush)(__isl_take isl_printer *p);
194 static struct isl_printer_ops file_ops = {
195 file_start_line,
196 file_end_line,
197 file_print_double,
198 file_print_int,
199 file_print_isl_int,
200 file_print_str,
201 file_flush
204 static struct isl_printer_ops str_ops = {
205 str_start_line,
206 str_end_line,
207 str_print_double,
208 str_print_int,
209 str_print_isl_int,
210 str_print_str,
211 str_flush
214 __isl_give isl_printer *isl_printer_to_file(isl_ctx *ctx, FILE *file)
216 struct isl_printer *p = isl_alloc_type(ctx, struct isl_printer);
217 if (!p)
218 return NULL;
219 p->ctx = ctx;
220 isl_ctx_ref(p->ctx);
221 p->ops = &file_ops;
222 p->file = file;
223 p->buf = NULL;
224 p->buf_n = 0;
225 p->buf_size = 0;
226 p->indent = 0;
227 p->output_format = ISL_FORMAT_ISL;
228 p->indent_prefix = NULL;
229 p->prefix = NULL;
230 p->suffix = NULL;
231 p->width = 0;
233 return p;
236 __isl_give isl_printer *isl_printer_to_str(isl_ctx *ctx)
238 struct isl_printer *p = isl_calloc_type(ctx, struct isl_printer);
239 if (!p)
240 return NULL;
241 p->ctx = ctx;
242 isl_ctx_ref(p->ctx);
243 p->ops = &str_ops;
244 p->file = NULL;
245 p->buf = isl_alloc_array(ctx, char, 256);
246 if (!p->buf)
247 goto error;
248 p->buf_n = 0;
249 p->buf[0] = '\0';
250 p->buf_size = 256;
251 p->indent = 0;
252 p->output_format = ISL_FORMAT_ISL;
253 p->indent_prefix = NULL;
254 p->prefix = NULL;
255 p->suffix = NULL;
256 p->width = 0;
258 return p;
259 error:
260 isl_printer_free(p);
261 return NULL;
264 __isl_null isl_printer *isl_printer_free(__isl_take isl_printer *p)
266 if (!p)
267 return NULL;
268 free(p->buf);
269 free(p->indent_prefix);
270 free(p->prefix);
271 free(p->suffix);
272 isl_ctx_deref(p->ctx);
273 free(p);
275 return NULL;
278 isl_ctx *isl_printer_get_ctx(__isl_keep isl_printer *printer)
280 return printer ? printer->ctx : NULL;
283 FILE *isl_printer_get_file(__isl_keep isl_printer *printer)
285 if (!printer)
286 return NULL;
287 if (!printer->file)
288 isl_die(isl_printer_get_ctx(printer), isl_error_invalid,
289 "not a file printer", return NULL);
290 return printer->file;
293 __isl_give isl_printer *isl_printer_set_isl_int_width(__isl_take isl_printer *p,
294 int width)
296 if (!p)
297 return NULL;
299 p->width = width;
301 return p;
304 __isl_give isl_printer *isl_printer_set_indent(__isl_take isl_printer *p,
305 int indent)
307 if (!p)
308 return NULL;
310 p->indent = indent;
312 return p;
315 __isl_give isl_printer *isl_printer_indent(__isl_take isl_printer *p,
316 int indent)
318 if (!p)
319 return NULL;
321 p->indent += indent;
322 if (p->indent < 0)
323 p->indent = 0;
325 return p;
328 /* Replace the indent prefix of "p" by "prefix".
330 __isl_give isl_printer *isl_printer_set_indent_prefix(__isl_take isl_printer *p,
331 const char *prefix)
333 if (!p)
334 return NULL;
336 free(p->indent_prefix);
337 p->indent_prefix = prefix ? strdup(prefix) : NULL;
339 return p;
342 __isl_give isl_printer *isl_printer_set_prefix(__isl_take isl_printer *p,
343 const char *prefix)
345 if (!p)
346 return NULL;
348 free(p->prefix);
349 p->prefix = prefix ? strdup(prefix) : NULL;
351 return p;
354 __isl_give isl_printer *isl_printer_set_suffix(__isl_take isl_printer *p,
355 const char *suffix)
357 if (!p)
358 return NULL;
360 free(p->suffix);
361 p->suffix = suffix ? strdup(suffix) : NULL;
363 return p;
366 __isl_give isl_printer *isl_printer_set_output_format(__isl_take isl_printer *p,
367 int output_format)
369 if (!p)
370 return NULL;
372 p->output_format = output_format;
374 return p;
377 int isl_printer_get_output_format(__isl_keep isl_printer *p)
379 if (!p)
380 return -1;
381 return p->output_format;
384 __isl_give isl_printer *isl_printer_print_str(__isl_take isl_printer *p,
385 const char *s)
387 if (!p)
388 return NULL;
389 if (!s)
390 return isl_printer_free(p);
392 return p->ops->print_str(p, s);
395 __isl_give isl_printer *isl_printer_print_double(__isl_take isl_printer *p,
396 double d)
398 if (!p)
399 return NULL;
401 return p->ops->print_double(p, d);
404 __isl_give isl_printer *isl_printer_print_int(__isl_take isl_printer *p, int i)
406 if (!p)
407 return NULL;
409 return p->ops->print_int(p, i);
412 __isl_give isl_printer *isl_printer_print_isl_int(__isl_take isl_printer *p,
413 isl_int i)
415 if (!p)
416 return NULL;
418 return p->ops->print_isl_int(p, i);
421 __isl_give isl_printer *isl_printer_start_line(__isl_take isl_printer *p)
423 if (!p)
424 return NULL;
426 return p->ops->start_line(p);
429 __isl_give isl_printer *isl_printer_end_line(__isl_take isl_printer *p)
431 if (!p)
432 return NULL;
434 return p->ops->end_line(p);
437 char *isl_printer_get_str(__isl_keep isl_printer *printer)
439 if (!printer || !printer->buf)
440 return NULL;
441 return strdup(printer->buf);
444 __isl_give isl_printer *isl_printer_flush(__isl_take isl_printer *p)
446 if (!p)
447 return NULL;
449 return p->ops->flush(p);