add isl_printer
[isl.git] / isl_printer.c
blob51e1f3932b4cf837c8e2ab12d37ed32446ca7286
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_print_str(__isl_take isl_printer *p,
17 const char *s)
19 fprintf(p->file, "%s", s);
20 return p;
23 static __isl_give isl_printer *file_print_int(__isl_take isl_printer *p, int i)
25 fprintf(p->file, "%d", i);
26 return p;
29 static __isl_give isl_printer *file_print_isl_int(__isl_take isl_printer *p, isl_int i)
31 isl_int_print(p->file, i, p->width);
32 return p;
35 static int grow_buf(__isl_keep isl_printer *p, int extra)
37 int new_size;
38 char *new_buf;
40 if (p->buf_size == 0)
41 return -1;
43 new_size = ((p->buf_n + extra + 1) * 3) / 2;
44 new_buf = isl_realloc_array(p->ctx, p->buf, char, new_size);
45 if (!new_buf) {
46 p->buf_size = 0;
47 return -1;
49 p->buf = new_buf;
50 p->buf_size = new_size;
52 return 0;
55 static __isl_give isl_printer *str_print(__isl_take isl_printer *p,
56 const char *s, int len)
58 if (p->buf_n + len + 1 >= p->buf_size && grow_buf(p, len))
59 goto error;
60 memcpy(p->buf + p->buf_n, s, len);
61 p->buf_n += len;
63 p->buf[p->buf_n] = '\0';
64 return p;
65 error:
66 isl_printer_free(p);
67 return NULL;
70 static __isl_give isl_printer *str_print_indent(__isl_take isl_printer *p,
71 int indent)
73 int i;
75 if (p->buf_n + indent + 1 >= p->buf_size && grow_buf(p, indent))
76 goto error;
77 for (i = 0; i < indent; ++i)
78 p->buf[p->buf_n++] = ' ';
79 return p;
80 error:
81 isl_printer_free(p);
82 return NULL;
85 static __isl_give isl_printer *str_start_line(__isl_take isl_printer *p)
87 p = str_print_indent(p, p->indent);
88 p = str_print(p, p->prefix, strlen(p->prefix));
89 return p;
92 static __isl_give isl_printer *str_end_line(__isl_take isl_printer *p)
94 p = str_print(p, p->suffix, strlen(p->suffix));
95 p = str_print(p, "\n", strlen("\n"));
96 return p;
99 static __isl_give isl_printer *str_print_str(__isl_take isl_printer *p,
100 const char *s)
102 return str_print(p, s, strlen(s));
105 static __isl_give isl_printer *str_print_int(__isl_take isl_printer *p, int i)
107 int left = p->buf_size - p->buf_n;
108 int need = snprintf(p->buf + p->buf_n, left, "%d", i);
109 if (need >= left) {
110 if (grow_buf(p, need))
111 goto error;
112 left = p->buf_size - p->buf_n;
113 need = snprintf(p->buf + p->buf_n, left, "%d", i);
115 p->buf_n += need;
116 return p;
117 error:
118 isl_printer_free(p);
119 return NULL;
122 static __isl_give isl_printer *str_print_isl_int(__isl_take isl_printer *p,
123 isl_int i)
125 char *s;
126 int len;
127 isl_int_print_gmp_free_t gmp_free;
129 s = mpz_get_str(0, 10, i);
130 len = strlen(s);
131 if (len < p->width)
132 p = str_print_indent(p, p->width - len);
133 p = str_print(p, s, len);
134 mp_get_memory_functions(NULL, NULL, &gmp_free);
135 (*gmp_free)(s, len + 1);
136 return p;
139 struct isl_printer_ops {
140 __isl_give isl_printer *(*start_line)(__isl_take isl_printer *p);
141 __isl_give isl_printer *(*end_line)(__isl_take isl_printer *p);
142 __isl_give isl_printer *(*print_int)(__isl_take isl_printer *p, int i);
143 __isl_give isl_printer *(*print_isl_int)(__isl_take isl_printer *p,
144 isl_int i);
145 __isl_give isl_printer *(*print_str)(__isl_take isl_printer *p,
146 const char *s);
149 static struct isl_printer_ops file_ops = {
150 file_start_line,
151 file_end_line,
152 file_print_int,
153 file_print_isl_int,
154 file_print_str
157 static struct isl_printer_ops str_ops = {
158 str_start_line,
159 str_end_line,
160 str_print_int,
161 str_print_isl_int,
162 str_print_str
165 __isl_give isl_printer *isl_printer_to_file(isl_ctx *ctx, FILE *file)
167 struct isl_printer *p = isl_alloc_type(ctx, struct isl_printer);
168 if (!p)
169 return NULL;
170 p->ctx = ctx;
171 isl_ctx_ref(p->ctx);
172 p->ops = &file_ops;
173 p->file = file;
174 p->buf = NULL;
175 p->buf_n = 0;
176 p->buf_size = 0;
177 p->indent = 0;
178 p->output_format = ISL_FORMAT_ISL;
179 p->prefix = NULL;
180 p->suffix = NULL;
181 p->width = 0;
183 return p;
186 __isl_give isl_printer *isl_printer_to_str(isl_ctx *ctx)
188 struct isl_printer *p = isl_alloc_type(ctx, struct isl_printer);
189 if (!p)
190 return NULL;
191 p->ctx = ctx;
192 isl_ctx_ref(p->ctx);
193 p->ops = &str_ops;
194 p->file = NULL;
195 p->buf = isl_alloc_array(ctx, char, 256);
196 if (!p->buf)
197 goto error;
198 p->buf_n = 0;
199 p->buf_size = 256;
200 p->indent = 0;
201 p->output_format = ISL_FORMAT_ISL;
202 p->prefix = NULL;
203 p->suffix = NULL;
204 p->width = 0;
206 return p;
207 error:
208 isl_printer_free(p);
209 return NULL;
212 void isl_printer_free(__isl_take isl_printer *p)
214 if (!p)
215 return;
216 free(p->buf);
217 isl_ctx_deref(p->ctx);
218 free(p);
221 __isl_give isl_printer *isl_printer_set_isl_int_width(__isl_take isl_printer *p,
222 int width)
224 if (!p)
225 return NULL;
227 p->width = width;
229 return p;
232 __isl_give isl_printer *isl_printer_set_indent(__isl_take isl_printer *p,
233 int indent)
235 if (!p)
236 return NULL;
238 p->indent = indent;
240 return p;
243 __isl_give isl_printer *isl_printer_set_prefix(__isl_take isl_printer *p,
244 const char *prefix)
246 if (!p)
247 return NULL;
249 p->prefix = prefix;
251 return p;
254 __isl_give isl_printer *isl_printer_set_suffix(__isl_take isl_printer *p,
255 const char *suffix)
257 if (!p)
258 return NULL;
260 p->suffix = suffix;
262 return p;
265 __isl_give isl_printer *isl_printer_set_output_format(__isl_take isl_printer *p,
266 int output_format)
268 if (!p)
269 return NULL;
271 p->output_format = output_format;
273 return p;
276 __isl_give isl_printer *isl_printer_print_str(__isl_take isl_printer *p,
277 const char *s)
279 return p->ops->print_str(p, s);
282 __isl_give isl_printer *isl_printer_print_int(__isl_take isl_printer *p, int i)
284 return p->ops->print_int(p, i);
287 __isl_give isl_printer *isl_printer_print_isl_int(__isl_take isl_printer *p,
288 isl_int i)
290 return p->ops->print_isl_int(p, i);
293 __isl_give isl_printer *isl_printer_start_line(__isl_take isl_printer *p)
295 return p->ops->start_line(p);
298 __isl_give isl_printer *isl_printer_end_line(__isl_take isl_printer *p)
300 return p->ops->end_line(p);
303 char *isl_printer_get_str(__isl_keep isl_printer *printer)
305 if (!printer || !printer->buf)
306 return NULL;
307 return strdup(printer->buf);