123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216(*
* File - File manipulation
* Copyright (C) 2008 David Teller
*
* 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
*)(* this code is purposedly before any module open directive *)letcount_lines(fn:string):int=letcount=ref0inletinput=open_in fnin(trywhiletruedolet_line=input_lineinputinincrcountdonewith End_of_file ->close_ininput);!count(*$T count_lines
(Sys.file_exists __FILE__) && (count_lines __FILE__ > 0)
*)openBatIOopenListLabelsopenUnixletfinally =BatInnerPervasives.finally(* Permissions *)typepermission=int(**Internally, permissions are represented in Unix-style
octal.*)letdefault_permission=0o000letuser_read =0o400letuser_write=0o200letuser_exec=0o100letgroup_read=0o040letgroup_write=0o020letgroup_exec =0o010letother_read=0o004letother_write=0o002letother_exec =0o001letperml=fold_leftl~init:default_permission~f:(funaccx->acclorx)letunix_permi=if0<=i&&i<=511thenielsePrintf.ksprintfinvalid_arg"File.unix_perm: Unix permission %o"i(* Opening *)typeopen_in_flag=[`create|`excl(**Fail if the file exists and [`create] is set *)|`text(**Open in ascii mode -- if this flag is not specified or if the
operating system does not perform conversions, the file is
opened in binary mode. *)|`nonblock(**Open in non-blocking mode *)|`mmap(**Open in memory-mapped mode (experimental)*)]typeopen_out_flag=[`append(**Start writing at the end of the file rather than the start *)|`create(**Create the file if it does not exist *)|`trunc(**Empty the file if it already exists (on by default) *)|`excl(**Fail if the file exists and [`create] is set *)|`text(**Open in ascii mode -- if this flag is not specified or if the
operating system does not perform conversions, the file is
opened in binary mode. *)|`nonblock(**Open in non-blocking mode *)](**
Convert a [open_in_flag list] into a low-level [open_flag list]
*)letin_chan_mode?modebinary=letmode_to_open_flagl=letrecauxaccis_binary=function|[]->ifis_binarythenOpen_binary::accelseOpen_text::acc|`create::t->aux(Open_creat::acc)is_binaryt|`excl::t->aux(Open_excl::acc)is_binaryt|`text::t->auxaccfalset|`nonblock::t->aux(Open_nonblock::acc)is_binaryt|_::t->auxaccis_binaryt(*Allow for futureextensions*)inaux[]binarylinmatchmodewith|None->[Open_rdonly;Open_binary]|Somel->mode_to_open_flagl(**
Convert a[open_out_flag list] into a low-level [open_flag list]
*)letout_chan_mode?modebinary=letmode_to_open_flagl=letrecauxaccis_binary=function|[]->letacc'=ifList.memOpen_appendaccthenaccelseOpen_trunc::accinifis_binarythenOpen_binary::acc'elseOpen_text::acc'|`append::t->aux(Open_append::acc)is_binaryt|`trunc::t->aux(Open_trunc::acc)is_binaryt|`create::t->aux(Open_creat::acc)is_binaryt|`excl::t->aux(Open_excl::acc)is_binaryt|`text::t->auxaccfalset|`nonblock::t->aux(Open_nonblock::acc)is_binaryt|_::t->auxaccis_binaryt(*Allow for futureextensions*)inaux[]binarylinmatchmodewith|None->[Open_wronly;Open_binary;Open_creat;Open_trunc]|Somel->Open_wronly::(mode_to_open_flagl)letopen_out?mode ?(perm=0o666)name=(* Printf.eprintf "Opening out\n%!";*)output_channel~cleanup:true(open_out_gen(out_chan_mode?modetrue)permname)open BatBigarrayletopen_in?mode?(perm=default_permission)name=letunix_mode=in_chan_mode?modetrueinmatchmodewith|SomelwhenList.mem`mmapl->letdesc =Unix.openfilename[O_RDONLY]0inletarray=Array1.map_filedesccharc_layout(*shared*)false (-1)inletpos=ref0andlen=Array1.dimarrayincreate_in~read:(fun()->if!pos>=lenthenraiseNo_more_inputelseArray1.getarray(BatRef.post_incrpos))~input:(funsout_pl->if!pos>=lenthenraiseNo_more_input;letn=(if!pos+l>lenthenlen-!poselsel)infor i=0ton-1doBytes.setsout(!pos+i)(Array1.getarrayi)done;(* String.unsafe_blit s (post pos ( (+) n ) ) sout p n;*)pos:=!pos+n;n)~close:(fun()->Unix.closedesc)|_->input_channel ~cleanup:true~autoclose:false(open_in_genunix_modepermname)letwith_do openercloserxf=letfile =opener xinfinally(fun()->closerfile)ffileletwith_file_in ?mode?permx=with_do (open_in?mode?perm)close_inxlet with_file_out?mode?permx=with_do (open_out?mode?perm)close_outxletlines_offile=BatIO.lines_of(open_in file)letwrite_linesfilelines=letmode=[`trunc;`create]inwith_file_out~modefile(funoc->BatEnum.iter(BatIO.write_lineoc)lines)(**
{6 Temporary files}
*)typeopen_temporary_out_flag=[open_out_flag|`delete_on_exit(**Should the file be deleted when program ends?*)]letopen_temporary_out?mode?(prefix="ocaml")?(suffix="tmp")?temp_dir():(_output *string)=letchan_mode =out_chan_mode?modetrueinlet(name,cout)=Filename.open_temp_file?temp_dir~mode:chan_modeprefixsuffixinletout=output_channel~cleanup:truecoutin(matchmodewith|Somelwhen List.mem`delete_on_exitl->at_exit(fun()->tryBatIO.close_outout;Sys.removenamewith_->())|_->());(out,name)letwith_temporary_out?mode?prefix?suffix?temp_dir f=let(file,name)=open_temporary_out?mode?prefix?suffix?temp_dir ()infinally(fun()->close_outfile)(fun(file,name)->ffilename)(file,name)(**
{6 File manipulation}
*)openUnixletsize_ofs=(stat s).st_sizeletsize_of_bigs=(LargeFile.stats).LargeFile.st_sizeletchmod=Unix.chmodletset_permissions=chmod