add isl_*_list_insert
[isl.git] / isl_printer.c
blob322244a931fb05cbbf27dde5f79966ddb70a0c3b
1 #include <string.h>
2 #include <isl_printer_private.h>
4 static __isl_give isl_printer *file_start_line(__isl_take isl_printer *p)
6 fprintf(p->file, "%*s%s", p->indent, "", p->prefix ? p->prefix : "");
7 return p;
10 static __isl_give isl_printer *file_end_line(__isl_take isl_printer *p)
12 fprintf(p->file, "%s\n", p->suffix ? p->suffix : "");
13 return p;
16 static __isl_give isl_printer *file_flush(__isl_take isl_printer *p)
18 fflush(p->file);
19 return p;
22 static __isl_give isl_printer *file_print_str(__isl_take isl_printer *p,
23 const char *s)
25 fprintf(p->file, "%s", s);
26 return p;
29 static __isl_give isl_printer *file_print_double(__isl_take isl_printer *p,
30 double d)
32 fprintf(p->file, "%g", d);
33 return p;
36 static __isl_give isl_printer *file_print_int(__isl_take isl_printer *p, int i)
38 fprintf(p->file, "%d", i);
39 return p;
42 static __isl_give isl_printer *file_print_isl_int(__isl_take isl_printer *p, isl_int i)
44 isl_int_print(p->file, i, p->width);
45 return p;
48 static int grow_buf(__isl_keep isl_printer *p, int extra)
50 int new_size;
51 char *new_buf;
53 if (p->buf_size == 0)
54 return -1;
56 new_size = ((p->buf_n + extra + 1) * 3) / 2;
57 new_buf = isl_realloc_array(p->ctx, p->buf, char, new_size);
58 if (!new_buf) {
59 p->buf_size = 0;
60 return -1;
62 p->buf = new_buf;
63 p->buf_size = new_size;
65 return 0;
68 static __isl_give isl_printer *str_print(__isl_take isl_printer *p,
69 const char *s, int len)
71 if (p->buf_n + len + 1 >= p->buf_size && grow_buf(p, len))
72 goto error;
73 memcpy(p->buf + p->buf_n, s, len);
74 p->buf_n += len;
76 p->buf[p->buf_n] = '\0';
77 return p;
78 error:
79 isl_printer_free(p);
80 return NULL;
83 static __isl_give isl_printer *str_print_indent(__isl_take isl_printer *p,
84 int indent)
86 int i;
88 if (p->buf_n + indent + 1 >= p->buf_size && grow_buf(p, indent))
89 goto error;
90 for (i = 0; i < indent; ++i)
91 p->buf[p->buf_n++] = ' ';
92 return p;
93 error:
94 isl_printer_free(p);
95 return NULL;
98 static __isl_give isl_printer *str_start_line(__isl_take isl_printer *p)
100 p = str_print_indent(p, p->indent);
101 if (p->prefix)
102 p = str_print(p, p->prefix, strlen(p->prefix));
103 return p;
106 static __isl_give isl_printer *str_end_line(__isl_take isl_printer *p)
108 if (p->suffix)
109 p = str_print(p, p->suffix, strlen(p->suffix));
110 p = str_print(p, "\n", strlen("\n"));
111 return p;
114 static __isl_give isl_printer *str_flush(__isl_take isl_printer *p)
116 p->buf_n = 0;
117 return p;
120 static __isl_give isl_printer *str_print_str(__isl_take isl_printer *p,
121 const char *s)
123 return str_print(p, s, strlen(s));
126 static __isl_give isl_printer *str_print_double(__isl_take isl_printer *p,
127 double d)
129 int left = p->buf_size - p->buf_n;
130 int need = snprintf(p->buf + p->buf_n, left, "%g", d);
131 if (need >= left) {
132 if (grow_buf(p, need))
133 goto error;
134 left = p->buf_size - p->buf_n;
135 need = snprintf(p->buf + p->buf_n, left, "%g", d);
137 p->buf_n += need;
138 return p;
139 error:
140 isl_printer_free(p);
141 return NULL;
144 static __isl_give isl_printer *str_print_int(__isl_take isl_printer *p, int i)
146 int left = p->buf_size - p->buf_n;
147 int need = snprintf(p->buf + p->buf_n, left, "%d", i);
148 if (need >= left) {
149 if (grow_buf(p, need))
150 goto error;
151 left = p->buf_size - p->buf_n;
152 need = snprintf(p->buf + p->buf_n, left, "%d", i);
154 p->buf_n += need;
155 return p;
156 error:
157 isl_printer_free(p);
158 return NULL;
161 static __isl_give isl_printer *str_print_isl_int(__isl_take isl_printer *p,
162 isl_int i)
164 char *s;
165 int len;
166 isl_int_print_gmp_free_t gmp_free;
168 s = isl_int_get_str(i);
169 len = strlen(s);
170 if (len < p->width)
171 p = str_print_indent(p, p->width - len);
172 p = str_print(p, s, len);
173 mp_get_memory_functions(NULL, NULL, &gmp_free);
174 (*gmp_free)(s, len + 1);
175 return p;
178 struct isl_printer_ops {
179 __isl_give isl_printer *(*start_line)(__isl_take isl_printer *p);
180 __isl_give isl_printer *(*end_line)(__isl_take isl_printer *p);
181 __isl_give isl_printer *(*print_double)(__isl_take isl_printer *p,
182 double d);
183 __isl_give isl_printer *(*print_int)(__isl_take isl_printer *p, int i);
184 __isl_give isl_printer *(*print_isl_int)(__isl_take isl_printer *p,
185 isl_int i);
186 __isl_give isl_printer *(*print_str)(__isl_take isl_printer *p,
187 const char *s);
188 __isl_give isl_printer *(*flush)(__isl_take isl_printer *p);
191 static struct isl_printer_ops file_ops = {
192 file_start_line,
193 file_end_line,
194 file_print_double,
195 file_print_int,
196 file_print_isl_int,
197 file_print_str,
198 file_flush
201 static struct isl_printer_ops str_ops = {
202 str_start_line,
203 str_end_line,
204 str_print_double,
205 str_print_int,
206 str_print_isl_int,
207 str_print_str,
208 str_flush
211 __isl_give isl_printer *isl_printer_to_file(isl_ctx *ctx, FILE *file)
213 struct isl_printer *p = isl_alloc_type(ctx, struct isl_printer);
214 if (!p)
215 return NULL;
216 p->ctx = ctx;
217 isl_ctx_ref(p->ctx);
218 p->ops = &file_ops;
219 p->file = file;
220 p->buf = NULL;
221 p->buf_n = 0;
222 p->buf_size = 0;
223 p->indent = 0;
224 p->output_format = ISL_FORMAT_ISL;
225 p->prefix = NULL;
226 p->suffix = NULL;
227 p->width = 0;
229 return p;
232 __isl_give isl_printer *isl_printer_to_str(isl_ctx *ctx)
234 struct isl_printer *p = isl_alloc_type(ctx, struct isl_printer);
235 if (!p)
236 return NULL;
237 p->ctx = ctx;
238 isl_ctx_ref(p->ctx);
239 p->ops = &str_ops;
240 p->file = NULL;
241 p->buf = isl_alloc_array(ctx, char, 256);
242 if (!p->buf)
243 goto error;
244 p->buf_n = 0;
245 p->buf[0] = '\0';
246 p->buf_size = 256;
247 p->indent = 0;
248 p->output_format = ISL_FORMAT_ISL;
249 p->prefix = NULL;
250 p->suffix = NULL;
251 p->width = 0;
253 return p;
254 error:
255 isl_printer_free(p);
256 return NULL;
259 void *isl_printer_free(__isl_take isl_printer *p)
261 if (!p)
262 return NULL;
263 free(p->buf);
264 isl_ctx_deref(p->ctx);
265 free(p);
267 return NULL;
270 isl_ctx *isl_printer_get_ctx(__isl_keep isl_printer *printer)
272 return printer ? printer->ctx : NULL;
275 FILE *isl_printer_get_file(__isl_keep isl_printer *printer)
277 if (!printer)
278 return NULL;
279 if (!printer->file)
280 isl_die(isl_printer_get_ctx(printer), isl_error_invalid,
281 "not a file printer", return NULL);
282 return printer->file;
285 __isl_give isl_printer *isl_printer_set_isl_int_width(__isl_take isl_printer *p,
286 int width)
288 if (!p)
289 return NULL;
291 p->width = width;
293 return p;
296 __isl_give isl_printer *isl_printer_set_indent(__isl_take isl_printer *p,
297 int indent)
299 if (!p)
300 return NULL;
302 p->indent = indent;
304 return p;
307 __isl_give isl_printer *isl_printer_indent(__isl_take isl_printer *p,
308 int indent)
310 if (!p)
311 return NULL;
313 p->indent += indent;
314 if (p->indent < 0)
315 p->indent = 0;
317 return p;
320 __isl_give isl_printer *isl_printer_set_prefix(__isl_take isl_printer *p,
321 const char *prefix)
323 if (!p)
324 return NULL;
326 p->prefix = prefix;
328 return p;
331 __isl_give isl_printer *isl_printer_set_suffix(__isl_take isl_printer *p,
332 const char *suffix)
334 if (!p)
335 return NULL;
337 p->suffix = suffix;
339 return p;
342 __isl_give isl_printer *isl_printer_set_output_format(__isl_take isl_printer *p,
343 int output_format)
345 if (!p)
346 return NULL;
348 p->output_format = output_format;
350 return p;
353 int isl_printer_get_output_format(__isl_keep isl_printer *p)
355 if (!p)
356 return -1;
357 return p->output_format;
360 __isl_give isl_printer *isl_printer_print_str(__isl_take isl_printer *p,
361 const char *s)
363 if (!p)
364 return NULL;
366 return p->ops->print_str(p, s);
369 __isl_give isl_printer *isl_printer_print_double(__isl_take isl_printer *p,
370 double d)
372 if (!p)
373 return NULL;
375 return p->ops->print_double(p, d);
378 __isl_give isl_printer *isl_printer_print_int(__isl_take isl_printer *p, int i)
380 if (!p)
381 return NULL;
383 return p->ops->print_int(p, i);
386 __isl_give isl_printer *isl_printer_print_isl_int(__isl_take isl_printer *p,
387 isl_int i)
389 if (!p)
390 return NULL;
392 return p->ops->print_isl_int(p, i);
395 __isl_give isl_printer *isl_printer_start_line(__isl_take isl_printer *p)
397 if (!p)
398 return NULL;
400 return p->ops->start_line(p);
403 __isl_give isl_printer *isl_printer_end_line(__isl_take isl_printer *p)
405 if (!p)
406 return NULL;
408 return p->ops->end_line(p);
411 char *isl_printer_get_str(__isl_keep isl_printer *printer)
413 if (!printer || !printer->buf)
414 return NULL;
415 return strdup(printer->buf);
418 __isl_give isl_printer *isl_printer_flush(__isl_take isl_printer *p)
420 if (!p)
421 return NULL;
423 return p->ops->flush(p);