add isl_union_map_is_bijective
[isl.git] / isl_printer.c
blob42130c12fb244f05bab02f9948c06bed932b85f1
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_int(__isl_take isl_printer *p, int i)
31 fprintf(p->file, "%d", i);
32 return p;
35 static __isl_give isl_printer *file_print_isl_int(__isl_take isl_printer *p, isl_int i)
37 isl_int_print(p->file, i, p->width);
38 return p;
41 static int grow_buf(__isl_keep isl_printer *p, int extra)
43 int new_size;
44 char *new_buf;
46 if (p->buf_size == 0)
47 return -1;
49 new_size = ((p->buf_n + extra + 1) * 3) / 2;
50 new_buf = isl_realloc_array(p->ctx, p->buf, char, new_size);
51 if (!new_buf) {
52 p->buf_size = 0;
53 return -1;
55 p->buf = new_buf;
56 p->buf_size = new_size;
58 return 0;
61 static __isl_give isl_printer *str_print(__isl_take isl_printer *p,
62 const char *s, int len)
64 if (p->buf_n + len + 1 >= p->buf_size && grow_buf(p, len))
65 goto error;
66 memcpy(p->buf + p->buf_n, s, len);
67 p->buf_n += len;
69 p->buf[p->buf_n] = '\0';
70 return p;
71 error:
72 isl_printer_free(p);
73 return NULL;
76 static __isl_give isl_printer *str_print_indent(__isl_take isl_printer *p,
77 int indent)
79 int i;
81 if (p->buf_n + indent + 1 >= p->buf_size && grow_buf(p, indent))
82 goto error;
83 for (i = 0; i < indent; ++i)
84 p->buf[p->buf_n++] = ' ';
85 return p;
86 error:
87 isl_printer_free(p);
88 return NULL;
91 static __isl_give isl_printer *str_start_line(__isl_take isl_printer *p)
93 p = str_print_indent(p, p->indent);
94 if (p->prefix)
95 p = str_print(p, p->prefix, strlen(p->prefix));
96 return p;
99 static __isl_give isl_printer *str_end_line(__isl_take isl_printer *p)
101 if (p->suffix)
102 p = str_print(p, p->suffix, strlen(p->suffix));
103 p = str_print(p, "\n", strlen("\n"));
104 return p;
107 static __isl_give isl_printer *str_flush(__isl_take isl_printer *p)
109 p->buf_n = 0;
110 return p;
113 static __isl_give isl_printer *str_print_str(__isl_take isl_printer *p,
114 const char *s)
116 return str_print(p, s, strlen(s));
119 static __isl_give isl_printer *str_print_int(__isl_take isl_printer *p, int i)
121 int left = p->buf_size - p->buf_n;
122 int need = snprintf(p->buf + p->buf_n, left, "%d", i);
123 if (need >= left) {
124 if (grow_buf(p, need))
125 goto error;
126 left = p->buf_size - p->buf_n;
127 need = snprintf(p->buf + p->buf_n, left, "%d", i);
129 p->buf_n += need;
130 return p;
131 error:
132 isl_printer_free(p);
133 return NULL;
136 static __isl_give isl_printer *str_print_isl_int(__isl_take isl_printer *p,
137 isl_int i)
139 char *s;
140 int len;
141 isl_int_print_gmp_free_t gmp_free;
143 s = isl_int_get_str(i);
144 len = strlen(s);
145 if (len < p->width)
146 p = str_print_indent(p, p->width - len);
147 p = str_print(p, s, len);
148 mp_get_memory_functions(NULL, NULL, &gmp_free);
149 (*gmp_free)(s, len + 1);
150 return p;
153 struct isl_printer_ops {
154 __isl_give isl_printer *(*start_line)(__isl_take isl_printer *p);
155 __isl_give isl_printer *(*end_line)(__isl_take isl_printer *p);
156 __isl_give isl_printer *(*print_int)(__isl_take isl_printer *p, int i);
157 __isl_give isl_printer *(*print_isl_int)(__isl_take isl_printer *p,
158 isl_int i);
159 __isl_give isl_printer *(*print_str)(__isl_take isl_printer *p,
160 const char *s);
161 __isl_give isl_printer *(*flush)(__isl_take isl_printer *p);
164 static struct isl_printer_ops file_ops = {
165 file_start_line,
166 file_end_line,
167 file_print_int,
168 file_print_isl_int,
169 file_print_str,
170 file_flush
173 static struct isl_printer_ops str_ops = {
174 str_start_line,
175 str_end_line,
176 str_print_int,
177 str_print_isl_int,
178 str_print_str,
179 str_flush
182 __isl_give isl_printer *isl_printer_to_file(isl_ctx *ctx, FILE *file)
184 struct isl_printer *p = isl_alloc_type(ctx, struct isl_printer);
185 if (!p)
186 return NULL;
187 p->ctx = ctx;
188 isl_ctx_ref(p->ctx);
189 p->ops = &file_ops;
190 p->file = file;
191 p->buf = NULL;
192 p->buf_n = 0;
193 p->buf_size = 0;
194 p->indent = 0;
195 p->output_format = ISL_FORMAT_ISL;
196 p->prefix = NULL;
197 p->suffix = NULL;
198 p->width = 0;
200 return p;
203 __isl_give isl_printer *isl_printer_to_str(isl_ctx *ctx)
205 struct isl_printer *p = isl_alloc_type(ctx, struct isl_printer);
206 if (!p)
207 return NULL;
208 p->ctx = ctx;
209 isl_ctx_ref(p->ctx);
210 p->ops = &str_ops;
211 p->file = NULL;
212 p->buf = isl_alloc_array(ctx, char, 256);
213 if (!p->buf)
214 goto error;
215 p->buf_n = 0;
216 p->buf_size = 256;
217 p->indent = 0;
218 p->output_format = ISL_FORMAT_ISL;
219 p->prefix = NULL;
220 p->suffix = NULL;
221 p->width = 0;
223 return p;
224 error:
225 isl_printer_free(p);
226 return NULL;
229 void isl_printer_free(__isl_take isl_printer *p)
231 if (!p)
232 return;
233 free(p->buf);
234 isl_ctx_deref(p->ctx);
235 free(p);
238 __isl_give isl_printer *isl_printer_set_isl_int_width(__isl_take isl_printer *p,
239 int width)
241 if (!p)
242 return NULL;
244 p->width = width;
246 return p;
249 __isl_give isl_printer *isl_printer_set_indent(__isl_take isl_printer *p,
250 int indent)
252 if (!p)
253 return NULL;
255 p->indent = indent;
257 return p;
260 __isl_give isl_printer *isl_printer_set_prefix(__isl_take isl_printer *p,
261 const char *prefix)
263 if (!p)
264 return NULL;
266 p->prefix = prefix;
268 return p;
271 __isl_give isl_printer *isl_printer_set_suffix(__isl_take isl_printer *p,
272 const char *suffix)
274 if (!p)
275 return NULL;
277 p->suffix = suffix;
279 return p;
282 __isl_give isl_printer *isl_printer_set_output_format(__isl_take isl_printer *p,
283 int output_format)
285 if (!p)
286 return NULL;
288 p->output_format = output_format;
290 return p;
293 __isl_give isl_printer *isl_printer_print_str(__isl_take isl_printer *p,
294 const char *s)
296 if (!p)
297 return NULL;
299 return p->ops->print_str(p, s);
302 __isl_give isl_printer *isl_printer_print_int(__isl_take isl_printer *p, int i)
304 if (!p)
305 return NULL;
307 return p->ops->print_int(p, i);
310 __isl_give isl_printer *isl_printer_print_isl_int(__isl_take isl_printer *p,
311 isl_int i)
313 if (!p)
314 return NULL;
316 return p->ops->print_isl_int(p, i);
319 __isl_give isl_printer *isl_printer_start_line(__isl_take isl_printer *p)
321 if (!p)
322 return NULL;
324 return p->ops->start_line(p);
327 __isl_give isl_printer *isl_printer_end_line(__isl_take isl_printer *p)
329 if (!p)
330 return NULL;
332 return p->ops->end_line(p);
335 char *isl_printer_get_str(__isl_keep isl_printer *printer)
337 if (!printer || !printer->buf)
338 return NULL;
339 return strdup(printer->buf);
342 __isl_give isl_printer *isl_printer_flush(__isl_take isl_printer *p)
344 if (!p)
345 return NULL;
347 return p->ops->flush(p);