2 * Convert to standard format.
4 * ----------------------------------------------------------------
6 * Copyright (C) 2000-2005 Mojave Group, Caltech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation,
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * Additional permission is given to link this library with the
23 * OpenSSL project's "OpenSSL" library, and with the OCaml runtime,
24 * and you may distribute the linked executables. See the file
25 * LICENSE.libmojave for more details.
27 * Author: Jason Hickey
35 (************************************************************************
37 ************************************************************************)
40 * Multiple formatted output.
43 { mutable form_buffer
: buffer
;
44 mutable form_out_string
: (string -> int -> int -> unit);
45 mutable form_out_flush
: (unit -> unit);
46 mutable form_out_newline
: (unit -> unit);
47 mutable form_out_space
: (int -> unit);
48 mutable form_max_boxes
: int;
49 mutable form_max_indent
: int;
50 mutable form_ellipsis
: string;
51 mutable form_margin
: int;
52 mutable form_divert
: (buffer
-> unit) option
55 (************************************************************************
56 * STANDARD OPERATIONS *
57 ************************************************************************)
59 let default_max_boxes = max_int
60 let default_max_indent = max_int
61 let default_ellipsis = "..."
62 let default_margin = 80
64 let output_substring = output
70 let { form_buffer
= buf
;
71 form_out_flush
= flush
;
72 form_out_string
= out_string
;
73 form_out_newline
= out_newline
;
74 form_out_space
= out_space
;
83 form
.form_buffer
<- new_buffer
()
86 { raw_print_string
= out_string
;
87 raw_print_newline
= out_newline
;
88 raw_print_spaces
= out_space
;
89 raw_print_flush
= flush
92 let printer = make_text_printer
raw_printer in
94 print_to_printer buf margin
printer;
96 form
.form_buffer
<- new_buffer
()
101 let pp_open_hbox form
() =
102 format_lzone form
.form_buffer
104 let pp_open_vbox form tab
=
105 format_hzone form
.form_buffer
;
106 format_pushm form
.form_buffer tab
108 let pp_open_hvbox form tab
=
109 format_hzone form
.form_buffer
;
110 format_pushm form
.form_buffer tab
112 let pp_open_hovbox form tab
=
113 format_hzone form
.form_buffer
;
114 format_pushm form
.form_buffer tab
116 let pp_open_box = pp_open_hovbox
118 let pp_close_box form
() =
119 format_flush_popm form
.form_buffer
;
120 format_ezone form
.form_buffer
122 let pp_print_string form s
=
123 format_string form
.form_buffer s
125 let pp_print_as form i s
=
126 let len = String.length s
in
129 format_string form
.form_buffer
(String.sub s
0 i
);
130 format_izone form
.form_buffer
;
131 format_string form
.form_buffer
(String.sub s i
(len - i
));
132 format_ezone form
.form_buffer
136 format_string form
.form_buffer s
;
138 format_string form
.form_buffer
(String.make
(i
- len) ' '
)
141 let pp_print_int form i
=
142 format_int form
.form_buffer i
144 let pp_print_float form x
=
145 format_string form
.form_buffer
(string_of_float x
)
147 let pp_print_char form c
=
148 format_char form
.form_buffer c
150 let pp_print_bool form x
=
151 format_string form
.form_buffer
(if x
then "true" else "false")
153 let pp_print_break form nspaces offset
=
154 format_sbreak form
.form_buffer
(String.make nspaces ' '
) (String.make offset ' '
)
156 let pp_print_cut form
() =
157 format_sbreak form
.form_buffer
"" ""
159 let pp_print_space form
() =
160 format_hspace form
.form_buffer
162 let pp_print_rbuffer form buffer
=
163 format_buffer form
.form_buffer buffer
;
166 let pp_force_newline form
() =
167 format_newline form
.form_buffer
169 let pp_print_flush form
() =
172 let pp_print_newline form
() =
173 format_newline form
.form_buffer
;
176 let pp_print_if_newline _form
() =
179 let pp_open_tbox _form
() =
182 let pp_close_tbox _form
() =
185 let pp_print_tbreak = pp_print_break
187 let pp_set_tab _form
() =
190 let pp_print_tab _form
() =
193 let pp_set_margin form margin
=
194 form
.form_margin
<- margin
196 let pp_get_margin form
() =
199 let pp_set_max_indent form indent
=
200 form
.form_max_indent
<- indent
202 let pp_get_max_indent form
() =
205 let pp_set_max_boxes form max
=
206 form
.form_max_boxes
<- max
208 let pp_get_max_boxes form
() =
211 let pp_over_max_boxes form
() =
212 format_depth form
.form_buffer
> form
.form_max_boxes
214 let pp_set_ellipsis_text form text
=
215 form
.form_ellipsis
<- text
217 let pp_get_ellipsis_text form
() =
220 let pp_set_formatter_out_channel form outx
=
221 form
.form_out_string
<- output_substring outx
;
222 form
.form_out_flush
<- (fun () -> flush outx
);
223 form
.form_out_newline
<- (fun () -> output_char outx '
\n'
);
224 form
.form_out_space
<- (fun i
->
225 for j
= 0 to pred i
do
229 let pp_set_formatter_output_functions form outx flush
=
230 form
.form_out_string
<- outx
;
231 form
.form_out_flush
<- flush
233 let pp_get_formatter_output_functions form
() =
234 form
.form_out_string
, form
.form_out_flush
236 let pp_set_all_formatter_output_functions form outx flush outnewline outspace
=
237 form
.form_out_string
<- outx
;
238 form
.form_out_flush
<- flush
;
239 form
.form_out_newline
<- outnewline
;
240 form
.form_out_space
<- outspace
242 let pp_get_all_formatter_output_functions form
() =
243 form
.form_out_string
,
245 form
.form_out_newline
,
248 (************************************************************************
250 ************************************************************************)
252 let formatter_of_out_channel outx
=
253 { form_buffer
= new_buffer
();
254 form_out_string
= output_substring outx
;
255 form_out_flush
= (fun () -> flush outx
);
256 form_out_newline
= (fun () -> output_char outx '
\n'
);
257 form_out_space
= (fun i
->
261 form_max_boxes
= default_max_boxes;
262 form_max_indent
= default_max_indent;
263 form_ellipsis
= default_ellipsis;
264 form_margin
= default_margin;
268 let std_formatter = formatter_of_out_channel stdout
269 let err_formatter = formatter_of_out_channel stderr
271 let formatter_of_buffer buf
=
272 { form_buffer
= new_buffer
();
273 form_out_string
= Buffer.add_substring buf
;
274 form_out_flush
= (fun () -> ());
275 form_out_newline
= (fun () -> Buffer.add_char buf '
\n'
);
279 Buffer.add_char buf ' '
281 form_max_boxes
= default_max_boxes;
282 form_max_indent
= default_max_indent;
283 form_ellipsis
= default_ellipsis;
284 form_margin
= default_margin;
288 let stdbuf = Buffer.create
19
290 let str_formatter = formatter_of_buffer stdbuf
292 let flush_str_formatter () =
293 flush_form str_formatter;
294 let s = Buffer.contents
stdbuf in
298 let make_formatter outx flush
=
299 { form_buffer
= new_buffer
();
300 form_out_string
= outx
;
301 form_out_flush
= flush
;
302 form_out_newline
= (fun () -> outx
"\n" 0 1);
303 form_out_space
= (fun i
->
307 form_max_boxes
= default_max_boxes;
308 form_max_indent
= default_max_indent;
309 form_ellipsis
= default_ellipsis;
310 form_margin
= default_margin;
317 let open_box = pp_open_box std_formatter
318 let open_vbox = pp_open_vbox std_formatter
319 let open_hbox = pp_open_hbox std_formatter
320 let open_hvbox = pp_open_hvbox std_formatter
321 let open_hovbox = pp_open_hovbox std_formatter
322 let close_box = pp_close_box std_formatter
325 * Formatting functions.
327 let print_string = pp_print_string std_formatter
328 let print_as = pp_print_as std_formatter
329 let print_int = pp_print_int std_formatter
330 let print_float = pp_print_float std_formatter
331 let print_char = pp_print_char std_formatter
332 let print_bool = pp_print_bool std_formatter
337 let print_space = pp_print_space std_formatter
338 let print_cut = pp_print_cut std_formatter
339 let print_break = pp_print_break std_formatter
340 let print_flush = pp_print_flush std_formatter
341 let print_newline = pp_print_newline std_formatter
342 let force_newline = pp_force_newline std_formatter
343 let print_if_newline = pp_print_if_newline std_formatter
348 let set_margin = pp_set_margin std_formatter
349 let get_margin = pp_get_margin std_formatter
354 let set_max_indent = pp_set_max_indent std_formatter
355 let get_max_indent = pp_get_max_indent std_formatter
360 let set_max_boxes = pp_set_max_boxes std_formatter
361 let get_max_boxes = pp_get_max_boxes std_formatter
362 let over_max_boxes = pp_over_max_boxes std_formatter
367 let open_tbox = pp_open_tbox std_formatter
368 let close_tbox = pp_close_tbox std_formatter
369 let print_tbreak = pp_print_tbreak std_formatter
370 let set_tab = pp_set_tab std_formatter
371 let print_tab = pp_print_tab std_formatter
376 let set_ellipsis_text = pp_set_ellipsis_text std_formatter
377 let get_ellipsis_text = pp_get_ellipsis_text std_formatter
380 * Redirecting formatter output.
382 let set_formatter_out_channel = pp_set_formatter_out_channel std_formatter
383 let set_formatter_output_functions = pp_set_formatter_output_functions std_formatter
384 let get_formatter_output_functions = pp_get_formatter_output_functions std_formatter
385 let set_all_formatter_output_functions = pp_set_all_formatter_output_functions std_formatter
386 let get_all_formatter_output_functions = pp_get_all_formatter_output_functions std_formatter
388 let divert formatter
divert =
389 formatter
.form_divert
<- divert
391 (************************************************************************
393 ************************************************************************)
403 let print_char = pp_print_char
404 let print_string = pp_print_string
406 let open_box = pp_open_box
407 let open_hbox fmt
= pp_open_hbox fmt
()
408 let open_vbox = pp_open_vbox
409 let open_hvbox = pp_open_hvbox
410 let open_hovbox = pp_open_hovbox
411 let close_box fmt
= pp_close_box fmt
()
413 let print_cut fmt
= pp_print_cut fmt
()
414 let print_space fmt
= pp_print_space fmt
()
415 let force_newline fmt
= pp_force_newline fmt
()
416 let print_break = pp_print_break
417 let print_flush fmt
= pp_print_flush fmt
()
418 let print_newline fmt
= pp_print_newline fmt
()
428 let print_char () = pp_print_char str_formatter
429 let print_string () = pp_print_string str_formatter
431 let open_box () = pp_open_box str_formatter
432 let open_hbox () = pp_open_hbox str_formatter ()
433 let open_vbox () = pp_open_vbox str_formatter
434 let open_hvbox () = pp_open_hvbox str_formatter
435 let open_hovbox () = pp_open_hovbox str_formatter
436 let close_box () = pp_close_box str_formatter ()
438 let print_cut () = pp_print_cut str_formatter ()
439 let print_space () = pp_print_space str_formatter ()
440 let force_newline () = pp_force_newline str_formatter ()
441 let print_break () = pp_print_break str_formatter
442 let print_flush () = pp_print_flush str_formatter ()
443 let print_newline () = pp_print_newline str_formatter ()
445 let exit = flush_str_formatter
448 module FPrintf
= MakePrintf
(Args
)
449 module SPrintf
= MakePrintf
(StringArgs
)
451 let fprintf = FPrintf.fprintf
457 fprintf std_formatter s
460 fprintf err_formatter s
463 fprintf (formatter_of_buffer buf
) s
468 * Caml-master: "compile"