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
: "");
10 static __isl_give isl_printer
*file_end_line(__isl_take isl_printer
*p
)
12 fprintf(p
->file
, "%s\n", p
->suffix
? p
->suffix
: "");
16 static __isl_give isl_printer
*file_flush(__isl_take isl_printer
*p
)
22 static __isl_give isl_printer
*file_print_str(__isl_take isl_printer
*p
,
25 fprintf(p
->file
, "%s", s
);
29 static __isl_give isl_printer
*file_print_double(__isl_take isl_printer
*p
,
32 fprintf(p
->file
, "%g", d
);
36 static __isl_give isl_printer
*file_print_int(__isl_take isl_printer
*p
, int i
)
38 fprintf(p
->file
, "%d", i
);
42 static __isl_give isl_printer
*file_print_isl_int(__isl_take isl_printer
*p
, isl_int i
)
44 isl_int_print(p
->file
, i
, p
->width
);
48 static int grow_buf(__isl_keep isl_printer
*p
, int extra
)
56 new_size
= ((p
->buf_n
+ extra
+ 1) * 3) / 2;
57 new_buf
= isl_realloc_array(p
->ctx
, p
->buf
, char, new_size
);
63 p
->buf_size
= new_size
;
68 static __isl_give isl_printer
*str_print(__isl_take isl_printer
*p
,
69 const char *s
, int len
)
71 if (p
->buf_n
+ len
+ 1 >= p
->buf_size
&& grow_buf(p
, len
))
73 memcpy(p
->buf
+ p
->buf_n
, s
, len
);
76 p
->buf
[p
->buf_n
] = '\0';
83 static __isl_give isl_printer
*str_print_indent(__isl_take isl_printer
*p
,
88 if (p
->buf_n
+ indent
+ 1 >= p
->buf_size
&& grow_buf(p
, indent
))
90 for (i
= 0; i
< indent
; ++i
)
91 p
->buf
[p
->buf_n
++] = ' ';
98 static __isl_give isl_printer
*str_start_line(__isl_take isl_printer
*p
)
100 p
= str_print_indent(p
, p
->indent
);
102 p
= str_print(p
, p
->prefix
, strlen(p
->prefix
));
106 static __isl_give isl_printer
*str_end_line(__isl_take isl_printer
*p
)
109 p
= str_print(p
, p
->suffix
, strlen(p
->suffix
));
110 p
= str_print(p
, "\n", strlen("\n"));
114 static __isl_give isl_printer
*str_flush(__isl_take isl_printer
*p
)
120 static __isl_give isl_printer
*str_print_str(__isl_take isl_printer
*p
,
123 return str_print(p
, s
, strlen(s
));
126 static __isl_give isl_printer
*str_print_double(__isl_take isl_printer
*p
,
129 int left
= p
->buf_size
- p
->buf_n
;
130 int need
= snprintf(p
->buf
+ p
->buf_n
, left
, "%g", d
);
132 if (grow_buf(p
, need
))
134 left
= p
->buf_size
- p
->buf_n
;
135 need
= snprintf(p
->buf
+ p
->buf_n
, left
, "%g", d
);
144 static __isl_give isl_printer
*str_print_int(__isl_take isl_printer
*p
, int i
)
146 int left
= p
->buf_size
- p
->buf_n
;
147 int need
= snprintf(p
->buf
+ p
->buf_n
, left
, "%d", i
);
149 if (grow_buf(p
, need
))
151 left
= p
->buf_size
- p
->buf_n
;
152 need
= snprintf(p
->buf
+ p
->buf_n
, left
, "%d", i
);
161 static __isl_give isl_printer
*str_print_isl_int(__isl_take isl_printer
*p
,
166 isl_int_print_gmp_free_t gmp_free
;
168 s
= isl_int_get_str(i
);
171 p
= str_print_indent(p
, p
->width
- len
);
172 p
= str_print(p
, s
, len
);
173 mp_get_memory_functions(NULL
, NULL
, &gmp_free
);
174 (*gmp_free
)(s
, len
+ 1);
178 struct isl_printer_ops
{
179 __isl_give isl_printer
*(*start_line
)(__isl_take isl_printer
*p
);
180 __isl_give isl_printer
*(*end_line
)(__isl_take isl_printer
*p
);
181 __isl_give isl_printer
*(*print_double
)(__isl_take isl_printer
*p
,
183 __isl_give isl_printer
*(*print_int
)(__isl_take isl_printer
*p
, int i
);
184 __isl_give isl_printer
*(*print_isl_int
)(__isl_take isl_printer
*p
,
186 __isl_give isl_printer
*(*print_str
)(__isl_take isl_printer
*p
,
188 __isl_give isl_printer
*(*flush
)(__isl_take isl_printer
*p
);
191 static struct isl_printer_ops file_ops
= {
201 static struct isl_printer_ops str_ops
= {
211 __isl_give isl_printer
*isl_printer_to_file(isl_ctx
*ctx
, FILE *file
)
213 struct isl_printer
*p
= isl_alloc_type(ctx
, struct isl_printer
);
224 p
->output_format
= ISL_FORMAT_ISL
;
232 __isl_give isl_printer
*isl_printer_to_str(isl_ctx
*ctx
)
234 struct isl_printer
*p
= isl_alloc_type(ctx
, struct isl_printer
);
241 p
->buf
= isl_alloc_array(ctx
, char, 256);
248 p
->output_format
= ISL_FORMAT_ISL
;
259 void *isl_printer_free(__isl_take isl_printer
*p
)
264 isl_ctx_deref(p
->ctx
);
270 isl_ctx
*isl_printer_get_ctx(__isl_keep isl_printer
*printer
)
272 return printer
? printer
->ctx
: NULL
;
275 FILE *isl_printer_get_file(__isl_keep isl_printer
*printer
)
280 isl_die(isl_printer_get_ctx(printer
), isl_error_invalid
,
281 "not a file printer", return NULL
);
282 return printer
->file
;
285 __isl_give isl_printer
*isl_printer_set_isl_int_width(__isl_take isl_printer
*p
,
296 __isl_give isl_printer
*isl_printer_set_indent(__isl_take isl_printer
*p
,
307 __isl_give isl_printer
*isl_printer_indent(__isl_take isl_printer
*p
,
320 __isl_give isl_printer
*isl_printer_set_prefix(__isl_take isl_printer
*p
,
331 __isl_give isl_printer
*isl_printer_set_suffix(__isl_take isl_printer
*p
,
342 __isl_give isl_printer
*isl_printer_set_output_format(__isl_take isl_printer
*p
,
348 p
->output_format
= output_format
;
353 int isl_printer_get_output_format(__isl_keep isl_printer
*p
)
357 return p
->output_format
;
360 __isl_give isl_printer
*isl_printer_print_str(__isl_take isl_printer
*p
,
366 return isl_printer_free(p
);
368 return p
->ops
->print_str(p
, s
);
371 __isl_give isl_printer
*isl_printer_print_double(__isl_take isl_printer
*p
,
377 return p
->ops
->print_double(p
, d
);
380 __isl_give isl_printer
*isl_printer_print_int(__isl_take isl_printer
*p
, int i
)
385 return p
->ops
->print_int(p
, i
);
388 __isl_give isl_printer
*isl_printer_print_isl_int(__isl_take isl_printer
*p
,
394 return p
->ops
->print_isl_int(p
, i
);
397 __isl_give isl_printer
*isl_printer_start_line(__isl_take isl_printer
*p
)
402 return p
->ops
->start_line(p
);
405 __isl_give isl_printer
*isl_printer_end_line(__isl_take isl_printer
*p
)
410 return p
->ops
->end_line(p
);
413 char *isl_printer_get_str(__isl_keep isl_printer
*printer
)
415 if (!printer
|| !printer
->buf
)
417 return strdup(printer
->buf
);
420 __isl_give isl_printer
*isl_printer_flush(__isl_take isl_printer
*p
)
425 return p
->ops
->flush(p
);