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%s", p
->indent_prefix
? p
->indent_prefix
: "",
8 p
->indent
, "", p
->prefix
? p
->prefix
: "");
12 static __isl_give isl_printer
*file_end_line(__isl_take isl_printer
*p
)
14 fprintf(p
->file
, "%s\n", p
->suffix
? p
->suffix
: "");
18 static __isl_give isl_printer
*file_flush(__isl_take isl_printer
*p
)
24 static __isl_give isl_printer
*file_print_str(__isl_take isl_printer
*p
,
27 fprintf(p
->file
, "%s", s
);
31 static __isl_give isl_printer
*file_print_double(__isl_take isl_printer
*p
,
34 fprintf(p
->file
, "%g", d
);
38 static __isl_give isl_printer
*file_print_int(__isl_take isl_printer
*p
, int i
)
40 fprintf(p
->file
, "%d", i
);
44 static __isl_give isl_printer
*file_print_isl_int(__isl_take isl_printer
*p
, isl_int i
)
46 isl_int_print(p
->file
, i
, p
->width
);
50 static int grow_buf(__isl_keep isl_printer
*p
, int extra
)
58 new_size
= ((p
->buf_n
+ extra
+ 1) * 3) / 2;
59 new_buf
= isl_realloc_array(p
->ctx
, p
->buf
, char, new_size
);
65 p
->buf_size
= new_size
;
70 static __isl_give isl_printer
*str_print(__isl_take isl_printer
*p
,
71 const char *s
, int len
)
73 if (p
->buf_n
+ len
+ 1 >= p
->buf_size
&& grow_buf(p
, len
))
75 memcpy(p
->buf
+ p
->buf_n
, s
, len
);
78 p
->buf
[p
->buf_n
] = '\0';
85 static __isl_give isl_printer
*str_print_indent(__isl_take isl_printer
*p
,
90 if (p
->buf_n
+ indent
+ 1 >= p
->buf_size
&& grow_buf(p
, indent
))
92 for (i
= 0; i
< indent
; ++i
)
93 p
->buf
[p
->buf_n
++] = ' ';
100 static __isl_give isl_printer
*str_start_line(__isl_take isl_printer
*p
)
102 if (p
->indent_prefix
)
103 p
= str_print(p
, p
->indent_prefix
, strlen(p
->indent_prefix
));
104 p
= str_print_indent(p
, p
->indent
);
106 p
= str_print(p
, p
->prefix
, strlen(p
->prefix
));
110 static __isl_give isl_printer
*str_end_line(__isl_take isl_printer
*p
)
113 p
= str_print(p
, p
->suffix
, strlen(p
->suffix
));
114 p
= str_print(p
, "\n", strlen("\n"));
118 static __isl_give isl_printer
*str_flush(__isl_take isl_printer
*p
)
121 p
->buf
[p
->buf_n
] = '\0';
125 static __isl_give isl_printer
*str_print_str(__isl_take isl_printer
*p
,
128 return str_print(p
, s
, strlen(s
));
131 static __isl_give isl_printer
*str_print_double(__isl_take isl_printer
*p
,
134 int left
= p
->buf_size
- p
->buf_n
;
135 int need
= snprintf(p
->buf
+ p
->buf_n
, left
, "%g", d
);
137 if (grow_buf(p
, need
))
139 left
= p
->buf_size
- p
->buf_n
;
140 need
= snprintf(p
->buf
+ p
->buf_n
, left
, "%g", d
);
149 static __isl_give isl_printer
*str_print_int(__isl_take isl_printer
*p
, int i
)
151 int left
= p
->buf_size
- p
->buf_n
;
152 int need
= snprintf(p
->buf
+ p
->buf_n
, left
, "%d", i
);
154 if (grow_buf(p
, need
))
156 left
= p
->buf_size
- p
->buf_n
;
157 need
= snprintf(p
->buf
+ p
->buf_n
, left
, "%d", i
);
166 static __isl_give isl_printer
*str_print_isl_int(__isl_take isl_printer
*p
,
172 s
= isl_int_get_str(i
);
175 p
= str_print_indent(p
, p
->width
- len
);
176 p
= str_print(p
, s
, len
);
181 struct isl_printer_ops
{
182 __isl_give isl_printer
*(*start_line
)(__isl_take isl_printer
*p
);
183 __isl_give isl_printer
*(*end_line
)(__isl_take isl_printer
*p
);
184 __isl_give isl_printer
*(*print_double
)(__isl_take isl_printer
*p
,
186 __isl_give isl_printer
*(*print_int
)(__isl_take isl_printer
*p
, int i
);
187 __isl_give isl_printer
*(*print_isl_int
)(__isl_take isl_printer
*p
,
189 __isl_give isl_printer
*(*print_str
)(__isl_take isl_printer
*p
,
191 __isl_give isl_printer
*(*flush
)(__isl_take isl_printer
*p
);
194 static struct isl_printer_ops file_ops
= {
204 static struct isl_printer_ops str_ops
= {
214 __isl_give isl_printer
*isl_printer_to_file(isl_ctx
*ctx
, FILE *file
)
216 struct isl_printer
*p
= isl_alloc_type(ctx
, struct isl_printer
);
227 p
->output_format
= ISL_FORMAT_ISL
;
228 p
->indent_prefix
= NULL
;
236 __isl_give isl_printer
*isl_printer_to_str(isl_ctx
*ctx
)
238 struct isl_printer
*p
= isl_calloc_type(ctx
, struct isl_printer
);
245 p
->buf
= isl_alloc_array(ctx
, char, 256);
252 p
->output_format
= ISL_FORMAT_ISL
;
253 p
->indent_prefix
= NULL
;
264 __isl_null isl_printer
*isl_printer_free(__isl_take isl_printer
*p
)
269 free(p
->indent_prefix
);
272 isl_ctx_deref(p
->ctx
);
278 isl_ctx
*isl_printer_get_ctx(__isl_keep isl_printer
*printer
)
280 return printer
? printer
->ctx
: NULL
;
283 FILE *isl_printer_get_file(__isl_keep isl_printer
*printer
)
288 isl_die(isl_printer_get_ctx(printer
), isl_error_invalid
,
289 "not a file printer", return NULL
);
290 return printer
->file
;
293 __isl_give isl_printer
*isl_printer_set_isl_int_width(__isl_take isl_printer
*p
,
304 __isl_give isl_printer
*isl_printer_set_indent(__isl_take isl_printer
*p
,
315 __isl_give isl_printer
*isl_printer_indent(__isl_take isl_printer
*p
,
328 /* Replace the indent prefix of "p" by "prefix".
330 __isl_give isl_printer
*isl_printer_set_indent_prefix(__isl_take isl_printer
*p
,
336 free(p
->indent_prefix
);
337 p
->indent_prefix
= prefix
? strdup(prefix
) : NULL
;
342 __isl_give isl_printer
*isl_printer_set_prefix(__isl_take isl_printer
*p
,
349 p
->prefix
= prefix
? strdup(prefix
) : NULL
;
354 __isl_give isl_printer
*isl_printer_set_suffix(__isl_take isl_printer
*p
,
361 p
->suffix
= suffix
? strdup(suffix
) : NULL
;
366 __isl_give isl_printer
*isl_printer_set_output_format(__isl_take isl_printer
*p
,
372 p
->output_format
= output_format
;
377 int isl_printer_get_output_format(__isl_keep isl_printer
*p
)
381 return p
->output_format
;
384 __isl_give isl_printer
*isl_printer_print_str(__isl_take isl_printer
*p
,
390 return isl_printer_free(p
);
392 return p
->ops
->print_str(p
, s
);
395 __isl_give isl_printer
*isl_printer_print_double(__isl_take isl_printer
*p
,
401 return p
->ops
->print_double(p
, d
);
404 __isl_give isl_printer
*isl_printer_print_int(__isl_take isl_printer
*p
, int i
)
409 return p
->ops
->print_int(p
, i
);
412 __isl_give isl_printer
*isl_printer_print_isl_int(__isl_take isl_printer
*p
,
418 return p
->ops
->print_isl_int(p
, i
);
421 __isl_give isl_printer
*isl_printer_start_line(__isl_take isl_printer
*p
)
426 return p
->ops
->start_line(p
);
429 __isl_give isl_printer
*isl_printer_end_line(__isl_take isl_printer
*p
)
434 return p
->ops
->end_line(p
);
437 char *isl_printer_get_str(__isl_keep isl_printer
*printer
)
439 if (!printer
|| !printer
->buf
)
441 return strdup(printer
->buf
);
444 __isl_give isl_printer
*isl_printer_flush(__isl_take isl_printer
*p
)
449 return p
->ops
->flush(p
);