123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140(*
* BatFormat - Extended Format module
* Copyright (C) 1996 Pierre Weis
* 2009 David Teller, LIFO, Universite d'Orleans
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version,
* with the special exception on linking described in file LICENSE.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)openBatIOincludeFormat(* internal functions *)letoutput_ofout=funsio->ignore (really_output_substringoutsio)letflush_ofout=BatInnerIO.get_flushoutletnewline_ofout=fun ()->BatInnerIO.writeout'\n'letspaces_ofout=(* Default function to output spaces.
Copied from base format.ml*)letblank_line=Bytes.make80' 'inletrecdisplay_blanks n=ifn>0thenifn<=80thenignore(really_outputoutblank_line0n)elsebeginignore(really_outputoutblank_line080);display_blanks (n-80)endindisplay_blanks(**{6 New functions}*)letformatter_of_outputout=letoutput=output_of outandflush=flush_ofoutinletf=make_formatteroutputflushinBatInnerIO.on_close_outout(fun_->pp_print_flush f());(*Note: we can'tjust use [flush] as [f] contains a cache.*)##V<5##pp_set_all_formatter_output_functionsf##V<5##~out:output##V<5##~flush##V<5##~newline:(newline_ofout)##V<5##~spaces:(spaces_ofout);##V>=5##pp_set_formatter_out_functionsf##V>=5##{out_string=output;##V>=5##out_flush=flush;##V>=5##out_newline=newline_ofout;##V>=5##out_spaces=spaces_ofout;##V>=5##out_indent =spaces_ofout};flet set_formatter_output out=BatInnerIO.on_close_out out(fun_->pp_print_flushFormat.std_formatter());##V<5##set_all_formatter_output_functions##V<5##~out:(output_ofout)##V<5##~flush:(flush_ofout)##V<5##~newline:(newline_ofout)##V<5##~spaces:(spaces_ofout)##V>=5##set_formatter_out_functions{##V>=5##out_string=output_ofout;##V>=5##out_flush=flush_of out;##V>=5##out_newline =newline_ofout;##V>=5##out_spaces=spaces_ofout;##V>=5##out_indent=spaces_ofout}letpp_set_formatter_outputfout=BatInnerIO.on_close_outout(fun_->pp_print_flushf());##V<5##pp_set_all_formatter_output_functionsf##V<5##~out:(output_ofout)##V<5## ~flush:(flush_ofout)##V<5##~newline:(newline_ofout)##V<5##~spaces:(spaces_ofout)##V>=5##pp_set_formatter_out_functions f{##V>=5##out_string=output_ofout;##V>=5##out_flush=flush_ofout;##V>=5##out_newline=newline_ofout;##V>=5##out_spaces=spaces_ofout;##V>=5##out_indent =spaces_of out}(**{6 Old values, new semantics}*)letformatter_of_out_channel=formatter_of_outputletset_formatter_out_channel=set_formatter_outputletpp_set_formatter_out_channel=pp_set_formatter_outputletstd_formatter=formatter_of_outputBatIO.stdoutleterr_formatter=formatter_of_outputBatIO.stderr(* Backward compatibility *)##V<4.02##(* To format a list *)##V<4.02##letrecpp_print_list?(pp_sep=pp_print_cut)pp_vppf=function##V<4.02##|[]->()##V<4.02##|[v]->pp_vppfv##V<4.02##|v::vs->##V<4.02##pp_vppfv;##V<4.02##pp_sepppf();##V<4.02##pp_print_list~pp_seppp_vppfvs##V<4.02####V<4.02##(* To format free-flowing text *)##V<4.02##letpp_print_textppfs=##V<4.02##letlen=String.lengthsin##V<4.02##letleft=ref0in##V<4.02##letright=ref0in##V<4.02##letflush()=##V<4.02##pp_print_stringppf(String.subs!left(!right-!left));##V<4.02##incrright;left:=!right;##V<4.02##in##V<4.02##while(!right<>len)do##V<4.02##matchs.[!right]with##V<4.02##|'\n'->##V<4.02##flush();##V<4.02##pp_force_newlineppf()##V<4.02##|' '->##V<4.02##flush();pp_print_spaceppf()##V<4.02##(* there is no specific support for '\t'
##V<4.02## as it is unclear what a right semantics would be *)##V<4.02##|_->incrright##V<4.02##done;##V<4.02##if!left<>lenthenflush()(**{6 Initialization}*)let()=set_formatter_outputBatIO.stdout;pp_set_formatter_outputFormat.std_formatterstdout;pp_set_formatter_outputFormat.err_formatterstderr