isl_aff.c: extract isl_local_space_get_active
[isl.git] / isl_printer.c
blobae7596d7d093fd9c4202118aa0879644ea6ee51d
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[0] = '\0';
217 p->buf_size = 256;
218 p->indent = 0;
219 p->output_format = ISL_FORMAT_ISL;
220 p->prefix = NULL;
221 p->suffix = NULL;
222 p->width = 0;
224 return p;
225 error:
226 isl_printer_free(p);
227 return NULL;
230 void isl_printer_free(__isl_take isl_printer *p)
232 if (!p)
233 return;
234 free(p->buf);
235 isl_ctx_deref(p->ctx);
236 free(p);
239 isl_ctx *isl_printer_get_ctx(__isl_keep isl_printer *printer)
241 return printer ? printer->ctx : NULL;
244 __isl_give isl_printer *isl_printer_set_isl_int_width(__isl_take isl_printer *p,
245 int width)
247 if (!p)
248 return NULL;
250 p->width = width;
252 return p;
255 __isl_give isl_printer *isl_printer_set_indent(__isl_take isl_printer *p,
256 int indent)
258 if (!p)
259 return NULL;
261 p->indent = indent;
263 return p;
266 __isl_give isl_printer *isl_printer_indent(__isl_take isl_printer *p,
267 int indent)
269 if (!p)
270 return NULL;
272 p->indent += indent;
273 if (p->indent < 0)
274 p->indent = 0;
276 return p;
279 __isl_give isl_printer *isl_printer_set_prefix(__isl_take isl_printer *p,
280 const char *prefix)
282 if (!p)
283 return NULL;
285 p->prefix = prefix;
287 return p;
290 __isl_give isl_printer *isl_printer_set_suffix(__isl_take isl_printer *p,
291 const char *suffix)
293 if (!p)
294 return NULL;
296 p->suffix = suffix;
298 return p;
301 __isl_give isl_printer *isl_printer_set_output_format(__isl_take isl_printer *p,
302 int output_format)
304 if (!p)
305 return NULL;
307 p->output_format = output_format;
309 return p;
312 __isl_give isl_printer *isl_printer_print_str(__isl_take isl_printer *p,
313 const char *s)
315 if (!p)
316 return NULL;
318 return p->ops->print_str(p, s);
321 __isl_give isl_printer *isl_printer_print_int(__isl_take isl_printer *p, int i)
323 if (!p)
324 return NULL;
326 return p->ops->print_int(p, i);
329 __isl_give isl_printer *isl_printer_print_isl_int(__isl_take isl_printer *p,
330 isl_int i)
332 if (!p)
333 return NULL;
335 return p->ops->print_isl_int(p, i);
338 __isl_give isl_printer *isl_printer_start_line(__isl_take isl_printer *p)
340 if (!p)
341 return NULL;
343 return p->ops->start_line(p);
346 __isl_give isl_printer *isl_printer_end_line(__isl_take isl_printer *p)
348 if (!p)
349 return NULL;
351 return p->ops->end_line(p);
354 char *isl_printer_get_str(__isl_keep isl_printer *printer)
356 if (!printer || !printer->buf)
357 return NULL;
358 return strdup(printer->buf);
361 __isl_give isl_printer *isl_printer_flush(__isl_take isl_printer *p)
363 if (!p)
364 return NULL;
366 return p->ops->flush(p);