isl_stream_next_token: terminate string tokens
[isl.git] / isl_printer.c
blob6011dd6f4bb4e92e979ea0e34348bfc6861666d1
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 if (p->prefix)
89 p = str_print(p, p->prefix, strlen(p->prefix));
90 return p;
93 static __isl_give isl_printer *str_end_line(__isl_take isl_printer *p)
95 if (p->suffix)
96 p = str_print(p, p->suffix, strlen(p->suffix));
97 p = str_print(p, "\n", strlen("\n"));
98 return p;
101 static __isl_give isl_printer *str_print_str(__isl_take isl_printer *p,
102 const char *s)
104 return str_print(p, s, strlen(s));
107 static __isl_give isl_printer *str_print_int(__isl_take isl_printer *p, int i)
109 int left = p->buf_size - p->buf_n;
110 int need = snprintf(p->buf + p->buf_n, left, "%d", i);
111 if (need >= left) {
112 if (grow_buf(p, need))
113 goto error;
114 left = p->buf_size - p->buf_n;
115 need = snprintf(p->buf + p->buf_n, left, "%d", i);
117 p->buf_n += need;
118 return p;
119 error:
120 isl_printer_free(p);
121 return NULL;
124 static __isl_give isl_printer *str_print_isl_int(__isl_take isl_printer *p,
125 isl_int i)
127 char *s;
128 int len;
129 isl_int_print_gmp_free_t gmp_free;
131 s = mpz_get_str(0, 10, i);
132 len = strlen(s);
133 if (len < p->width)
134 p = str_print_indent(p, p->width - len);
135 p = str_print(p, s, len);
136 mp_get_memory_functions(NULL, NULL, &gmp_free);
137 (*gmp_free)(s, len + 1);
138 return p;
141 struct isl_printer_ops {
142 __isl_give isl_printer *(*start_line)(__isl_take isl_printer *p);
143 __isl_give isl_printer *(*end_line)(__isl_take isl_printer *p);
144 __isl_give isl_printer *(*print_int)(__isl_take isl_printer *p, int i);
145 __isl_give isl_printer *(*print_isl_int)(__isl_take isl_printer *p,
146 isl_int i);
147 __isl_give isl_printer *(*print_str)(__isl_take isl_printer *p,
148 const char *s);
151 static struct isl_printer_ops file_ops = {
152 file_start_line,
153 file_end_line,
154 file_print_int,
155 file_print_isl_int,
156 file_print_str
159 static struct isl_printer_ops str_ops = {
160 str_start_line,
161 str_end_line,
162 str_print_int,
163 str_print_isl_int,
164 str_print_str
167 __isl_give isl_printer *isl_printer_to_file(isl_ctx *ctx, FILE *file)
169 struct isl_printer *p = isl_alloc_type(ctx, struct isl_printer);
170 if (!p)
171 return NULL;
172 p->ctx = ctx;
173 isl_ctx_ref(p->ctx);
174 p->ops = &file_ops;
175 p->file = file;
176 p->buf = NULL;
177 p->buf_n = 0;
178 p->buf_size = 0;
179 p->indent = 0;
180 p->output_format = ISL_FORMAT_ISL;
181 p->prefix = NULL;
182 p->suffix = NULL;
183 p->width = 0;
185 return p;
188 __isl_give isl_printer *isl_printer_to_str(isl_ctx *ctx)
190 struct isl_printer *p = isl_alloc_type(ctx, struct isl_printer);
191 if (!p)
192 return NULL;
193 p->ctx = ctx;
194 isl_ctx_ref(p->ctx);
195 p->ops = &str_ops;
196 p->file = NULL;
197 p->buf = isl_alloc_array(ctx, char, 256);
198 if (!p->buf)
199 goto error;
200 p->buf_n = 0;
201 p->buf_size = 256;
202 p->indent = 0;
203 p->output_format = ISL_FORMAT_ISL;
204 p->prefix = NULL;
205 p->suffix = NULL;
206 p->width = 0;
208 return p;
209 error:
210 isl_printer_free(p);
211 return NULL;
214 void isl_printer_free(__isl_take isl_printer *p)
216 if (!p)
217 return;
218 free(p->buf);
219 isl_ctx_deref(p->ctx);
220 free(p);
223 __isl_give isl_printer *isl_printer_set_isl_int_width(__isl_take isl_printer *p,
224 int width)
226 if (!p)
227 return NULL;
229 p->width = width;
231 return p;
234 __isl_give isl_printer *isl_printer_set_indent(__isl_take isl_printer *p,
235 int indent)
237 if (!p)
238 return NULL;
240 p->indent = indent;
242 return p;
245 __isl_give isl_printer *isl_printer_set_prefix(__isl_take isl_printer *p,
246 const char *prefix)
248 if (!p)
249 return NULL;
251 p->prefix = prefix;
253 return p;
256 __isl_give isl_printer *isl_printer_set_suffix(__isl_take isl_printer *p,
257 const char *suffix)
259 if (!p)
260 return NULL;
262 p->suffix = suffix;
264 return p;
267 __isl_give isl_printer *isl_printer_set_output_format(__isl_take isl_printer *p,
268 int output_format)
270 if (!p)
271 return NULL;
273 p->output_format = output_format;
275 return p;
278 __isl_give isl_printer *isl_printer_print_str(__isl_take isl_printer *p,
279 const char *s)
281 return p->ops->print_str(p, s);
284 __isl_give isl_printer *isl_printer_print_int(__isl_take isl_printer *p, int i)
286 return p->ops->print_int(p, i);
289 __isl_give isl_printer *isl_printer_print_isl_int(__isl_take isl_printer *p,
290 isl_int i)
292 return p->ops->print_isl_int(p, i);
295 __isl_give isl_printer *isl_printer_start_line(__isl_take isl_printer *p)
297 return p->ops->start_line(p);
300 __isl_give isl_printer *isl_printer_end_line(__isl_take isl_printer *p)
302 return p->ops->end_line(p);
305 char *isl_printer_get_str(__isl_keep isl_printer *printer)
307 if (!printer || !printer->buf)
308 return NULL;
309 return strdup(printer->buf);