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_print_str(__isl_take isl_printer
*p
,
19 fprintf(p
->file
, "%s", s
);
23 static __isl_give isl_printer
*file_print_int(__isl_take isl_printer
*p
, int i
)
25 fprintf(p
->file
, "%d", i
);
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
);
35 static int grow_buf(__isl_keep isl_printer
*p
, int extra
)
43 new_size
= ((p
->buf_n
+ extra
+ 1) * 3) / 2;
44 new_buf
= isl_realloc_array(p
->ctx
, p
->buf
, char, new_size
);
50 p
->buf_size
= new_size
;
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
))
60 memcpy(p
->buf
+ p
->buf_n
, s
, len
);
63 p
->buf
[p
->buf_n
] = '\0';
70 static __isl_give isl_printer
*str_print_indent(__isl_take isl_printer
*p
,
75 if (p
->buf_n
+ indent
+ 1 >= p
->buf_size
&& grow_buf(p
, indent
))
77 for (i
= 0; i
< indent
; ++i
)
78 p
->buf
[p
->buf_n
++] = ' ';
85 static __isl_give isl_printer
*str_start_line(__isl_take isl_printer
*p
)
87 p
= str_print_indent(p
, p
->indent
);
89 p
= str_print(p
, p
->prefix
, strlen(p
->prefix
));
93 static __isl_give isl_printer
*str_end_line(__isl_take isl_printer
*p
)
96 p
= str_print(p
, p
->suffix
, strlen(p
->suffix
));
97 p
= str_print(p
, "\n", strlen("\n"));
101 static __isl_give isl_printer
*str_print_str(__isl_take isl_printer
*p
,
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
);
112 if (grow_buf(p
, need
))
114 left
= p
->buf_size
- p
->buf_n
;
115 need
= snprintf(p
->buf
+ p
->buf_n
, left
, "%d", i
);
124 static __isl_give isl_printer
*str_print_isl_int(__isl_take isl_printer
*p
,
129 isl_int_print_gmp_free_t gmp_free
;
131 s
= mpz_get_str(0, 10, i
);
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);
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
,
147 __isl_give isl_printer
*(*print_str
)(__isl_take isl_printer
*p
,
151 static struct isl_printer_ops file_ops
= {
159 static struct isl_printer_ops str_ops
= {
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
);
180 p
->output_format
= ISL_FORMAT_ISL
;
188 __isl_give isl_printer
*isl_printer_to_str(isl_ctx
*ctx
)
190 struct isl_printer
*p
= isl_alloc_type(ctx
, struct isl_printer
);
197 p
->buf
= isl_alloc_array(ctx
, char, 256);
203 p
->output_format
= ISL_FORMAT_ISL
;
214 void isl_printer_free(__isl_take isl_printer
*p
)
219 isl_ctx_deref(p
->ctx
);
223 __isl_give isl_printer
*isl_printer_set_isl_int_width(__isl_take isl_printer
*p
,
234 __isl_give isl_printer
*isl_printer_set_indent(__isl_take isl_printer
*p
,
245 __isl_give isl_printer
*isl_printer_set_prefix(__isl_take isl_printer
*p
,
256 __isl_give isl_printer
*isl_printer_set_suffix(__isl_take isl_printer
*p
,
267 __isl_give isl_printer
*isl_printer_set_output_format(__isl_take isl_printer
*p
,
273 p
->output_format
= output_format
;
278 __isl_give isl_printer
*isl_printer_print_str(__isl_take isl_printer
*p
,
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
,
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
)
309 return strdup(printer
->buf
);