isl_access_info_add_source: plug memory leak on error path
[isl.git] / isl_printer.c
blob5bfaac9092fa5ce6693d7666c07e7f2d12bb8366
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 FILE *isl_printer_get_file(__isl_keep isl_printer *printer)
246 if (!printer)
247 return NULL;
248 if (!printer->file)
249 isl_die(isl_printer_get_ctx(printer), isl_error_invalid,
250 "not a file printer", return NULL);
251 return printer->file;
254 __isl_give isl_printer *isl_printer_set_isl_int_width(__isl_take isl_printer *p,
255 int width)
257 if (!p)
258 return NULL;
260 p->width = width;
262 return p;
265 __isl_give isl_printer *isl_printer_set_indent(__isl_take isl_printer *p,
266 int indent)
268 if (!p)
269 return NULL;
271 p->indent = indent;
273 return p;
276 __isl_give isl_printer *isl_printer_indent(__isl_take isl_printer *p,
277 int indent)
279 if (!p)
280 return NULL;
282 p->indent += indent;
283 if (p->indent < 0)
284 p->indent = 0;
286 return p;
289 __isl_give isl_printer *isl_printer_set_prefix(__isl_take isl_printer *p,
290 const char *prefix)
292 if (!p)
293 return NULL;
295 p->prefix = prefix;
297 return p;
300 __isl_give isl_printer *isl_printer_set_suffix(__isl_take isl_printer *p,
301 const char *suffix)
303 if (!p)
304 return NULL;
306 p->suffix = suffix;
308 return p;
311 __isl_give isl_printer *isl_printer_set_output_format(__isl_take isl_printer *p,
312 int output_format)
314 if (!p)
315 return NULL;
317 p->output_format = output_format;
319 return p;
322 int isl_printer_get_output_format(__isl_keep isl_printer *p)
324 if (!p)
325 return -1;
326 return p->output_format;
329 __isl_give isl_printer *isl_printer_print_str(__isl_take isl_printer *p,
330 const char *s)
332 if (!p)
333 return NULL;
335 return p->ops->print_str(p, s);
338 __isl_give isl_printer *isl_printer_print_int(__isl_take isl_printer *p, int i)
340 if (!p)
341 return NULL;
343 return p->ops->print_int(p, i);
346 __isl_give isl_printer *isl_printer_print_isl_int(__isl_take isl_printer *p,
347 isl_int i)
349 if (!p)
350 return NULL;
352 return p->ops->print_isl_int(p, i);
355 __isl_give isl_printer *isl_printer_start_line(__isl_take isl_printer *p)
357 if (!p)
358 return NULL;
360 return p->ops->start_line(p);
363 __isl_give isl_printer *isl_printer_end_line(__isl_take isl_printer *p)
365 if (!p)
366 return NULL;
368 return p->ops->end_line(p);
371 char *isl_printer_get_str(__isl_keep isl_printer *printer)
373 if (!printer || !printer->buf)
374 return NULL;
375 return strdup(printer->buf);
378 __isl_give isl_printer *isl_printer_flush(__isl_take isl_printer *p)
380 if (!p)
381 return NULL;
383 return p->ops->flush(p);