3 #include <isl_printer_private.h>
5 static __isl_give isl_printer
*file_start_line(__isl_take isl_printer
*p
)
7 fprintf(p
->file
, "%*s%s", p
->indent
, "", p
->prefix
? p
->prefix
: "");
11 static __isl_give isl_printer
*file_end_line(__isl_take isl_printer
*p
)
13 fprintf(p
->file
, "%s\n", p
->suffix
? p
->suffix
: "");
17 static __isl_give isl_printer
*file_flush(__isl_take isl_printer
*p
)
23 static __isl_give isl_printer
*file_print_str(__isl_take isl_printer
*p
,
26 fprintf(p
->file
, "%s", s
);
30 static __isl_give isl_printer
*file_print_double(__isl_take isl_printer
*p
,
33 fprintf(p
->file
, "%g", d
);
37 static __isl_give isl_printer
*file_print_int(__isl_take isl_printer
*p
, int i
)
39 fprintf(p
->file
, "%d", i
);
43 static __isl_give isl_printer
*file_print_isl_int(__isl_take isl_printer
*p
, isl_int i
)
45 isl_int_print(p
->file
, i
, p
->width
);
49 static int grow_buf(__isl_keep isl_printer
*p
, int extra
)
57 new_size
= ((p
->buf_n
+ extra
+ 1) * 3) / 2;
58 new_buf
= isl_realloc_array(p
->ctx
, p
->buf
, char, new_size
);
64 p
->buf_size
= new_size
;
69 static __isl_give isl_printer
*str_print(__isl_take isl_printer
*p
,
70 const char *s
, int len
)
72 if (p
->buf_n
+ len
+ 1 >= p
->buf_size
&& grow_buf(p
, len
))
74 memcpy(p
->buf
+ p
->buf_n
, s
, len
);
77 p
->buf
[p
->buf_n
] = '\0';
84 static __isl_give isl_printer
*str_print_indent(__isl_take isl_printer
*p
,
89 if (p
->buf_n
+ indent
+ 1 >= p
->buf_size
&& grow_buf(p
, indent
))
91 for (i
= 0; i
< indent
; ++i
)
92 p
->buf
[p
->buf_n
++] = ' ';
99 static __isl_give isl_printer
*str_start_line(__isl_take isl_printer
*p
)
101 p
= str_print_indent(p
, p
->indent
);
103 p
= str_print(p
, p
->prefix
, strlen(p
->prefix
));
107 static __isl_give isl_printer
*str_end_line(__isl_take isl_printer
*p
)
110 p
= str_print(p
, p
->suffix
, strlen(p
->suffix
));
111 p
= str_print(p
, "\n", strlen("\n"));
115 static __isl_give isl_printer
*str_flush(__isl_take isl_printer
*p
)
121 static __isl_give isl_printer
*str_print_str(__isl_take isl_printer
*p
,
124 return str_print(p
, s
, strlen(s
));
127 static __isl_give isl_printer
*str_print_double(__isl_take isl_printer
*p
,
130 int left
= p
->buf_size
- p
->buf_n
;
131 int need
= snprintf(p
->buf
+ p
->buf_n
, left
, "%g", d
);
133 if (grow_buf(p
, need
))
135 left
= p
->buf_size
- p
->buf_n
;
136 need
= snprintf(p
->buf
+ p
->buf_n
, left
, "%g", d
);
145 static __isl_give isl_printer
*str_print_int(__isl_take isl_printer
*p
, int i
)
147 int left
= p
->buf_size
- p
->buf_n
;
148 int need
= snprintf(p
->buf
+ p
->buf_n
, left
, "%d", i
);
150 if (grow_buf(p
, need
))
152 left
= p
->buf_size
- p
->buf_n
;
153 need
= snprintf(p
->buf
+ p
->buf_n
, left
, "%d", i
);
162 static __isl_give isl_printer
*str_print_isl_int(__isl_take isl_printer
*p
,
168 s
= isl_int_get_str(i
);
171 p
= str_print_indent(p
, p
->width
- len
);
172 p
= str_print(p
, s
, len
);
177 struct isl_printer_ops
{
178 __isl_give isl_printer
*(*start_line
)(__isl_take isl_printer
*p
);
179 __isl_give isl_printer
*(*end_line
)(__isl_take isl_printer
*p
);
180 __isl_give isl_printer
*(*print_double
)(__isl_take isl_printer
*p
,
182 __isl_give isl_printer
*(*print_int
)(__isl_take isl_printer
*p
, int i
);
183 __isl_give isl_printer
*(*print_isl_int
)(__isl_take isl_printer
*p
,
185 __isl_give isl_printer
*(*print_str
)(__isl_take isl_printer
*p
,
187 __isl_give isl_printer
*(*flush
)(__isl_take isl_printer
*p
);
190 static struct isl_printer_ops file_ops
= {
200 static struct isl_printer_ops str_ops
= {
210 __isl_give isl_printer
*isl_printer_to_file(isl_ctx
*ctx
, FILE *file
)
212 struct isl_printer
*p
= isl_alloc_type(ctx
, struct isl_printer
);
223 p
->output_format
= ISL_FORMAT_ISL
;
231 __isl_give isl_printer
*isl_printer_to_str(isl_ctx
*ctx
)
233 struct isl_printer
*p
= isl_alloc_type(ctx
, struct isl_printer
);
240 p
->buf
= isl_alloc_array(ctx
, char, 256);
247 p
->output_format
= ISL_FORMAT_ISL
;
258 void *isl_printer_free(__isl_take isl_printer
*p
)
263 isl_ctx_deref(p
->ctx
);
269 isl_ctx
*isl_printer_get_ctx(__isl_keep isl_printer
*printer
)
271 return printer
? printer
->ctx
: NULL
;
274 FILE *isl_printer_get_file(__isl_keep isl_printer
*printer
)
279 isl_die(isl_printer_get_ctx(printer
), isl_error_invalid
,
280 "not a file printer", return NULL
);
281 return printer
->file
;
284 __isl_give isl_printer
*isl_printer_set_isl_int_width(__isl_take isl_printer
*p
,
295 __isl_give isl_printer
*isl_printer_set_indent(__isl_take isl_printer
*p
,
306 __isl_give isl_printer
*isl_printer_indent(__isl_take isl_printer
*p
,
319 __isl_give isl_printer
*isl_printer_set_prefix(__isl_take isl_printer
*p
,
330 __isl_give isl_printer
*isl_printer_set_suffix(__isl_take isl_printer
*p
,
341 __isl_give isl_printer
*isl_printer_set_output_format(__isl_take isl_printer
*p
,
347 p
->output_format
= output_format
;
352 int isl_printer_get_output_format(__isl_keep isl_printer
*p
)
356 return p
->output_format
;
359 __isl_give isl_printer
*isl_printer_print_str(__isl_take isl_printer
*p
,
365 return isl_printer_free(p
);
367 return p
->ops
->print_str(p
, s
);
370 __isl_give isl_printer
*isl_printer_print_double(__isl_take isl_printer
*p
,
376 return p
->ops
->print_double(p
, d
);
379 __isl_give isl_printer
*isl_printer_print_int(__isl_take isl_printer
*p
, int i
)
384 return p
->ops
->print_int(p
, i
);
387 __isl_give isl_printer
*isl_printer_print_isl_int(__isl_take isl_printer
*p
,
393 return p
->ops
->print_isl_int(p
, i
);
396 __isl_give isl_printer
*isl_printer_start_line(__isl_take isl_printer
*p
)
401 return p
->ops
->start_line(p
);
404 __isl_give isl_printer
*isl_printer_end_line(__isl_take isl_printer
*p
)
409 return p
->ops
->end_line(p
);
412 char *isl_printer_get_str(__isl_keep isl_printer
*printer
)
414 if (!printer
|| !printer
->buf
)
416 return strdup(printer
->buf
);
419 __isl_give isl_printer
*isl_printer_flush(__isl_take isl_printer
*p
)
424 return p
->ops
->flush(p
);