123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211(** Internally used modules *)open!Base(** Alternate interface to [Stdio.In_channel]. The [create] and [with_file]
functions don't have a [binary] option, so it is compatible with the
[Record_in_channel.Make] functor. *)moduleIn_channel:sigincludemoduletypeofStdio.In_channelvalcreate:string->tvalwith_file:string->f:(t->'a)->'aend=structincludeStdio.In_channelletcreates=create~binary:falsesletwith_files~f=with_file~binary:falses~fend(** A wrapper of Jane Street's [Stdio.In_channel]. Add's [peek_char] and
[peek_line] functions that work on [fifo]s as well as regular files.
Used internally for bio input channels so that you can pipe directly from
gzip even in channels that need peeking.
Differences from [Stdio.In_channel]
- No binary mode
Some functions are not implemented.
- input
- really_input
- really_input_exn
- input_char
- input_byte
- input_binary_int
- unsafe_input_value
- input_buffer
- seek
- pos
- length
- set_binary_mode_out *)modulePeekable_in_channel:sigtypet[@@derivingequal]valstdin:tvalcreate:string->tvalclose:t->unitvalwith_file:string->f:(t->'a)->'avalinput_all:t->stringvalinput_line:?fix_win_eol:bool->t->stringoptionvalinput_line_exn:?fix_win_eol:bool->t->stringvalfold_lines:?fix_win_eol:bool->t->init:'a->f:('a->string->'a)->'avalinput_lines:?fix_win_eol:bool->t->stringlistvaliter_lines:?fix_win_eol:bool->t->f:(string->unit)->unitvalread_lines:?fix_win_eol:bool->string->stringlistvalread_all:string->string(** Both [peek] functions are safe to call in the context of one of the
iterator functions. *)valpeek_char:?fix_win_eol:bool->t->charoptionvalpeek_line:?fix_win_eol:bool->t->stringoptionend=struct(* TODO let user set newline. *)[@@@coverageoff](* Note that the buf is a line, and the input_line functions remove the
newline, so the buf won't have a newline. *)typet={ic:Stdio.In_channel.t;buf:stringoptionref}[@@derivingequal][@@@coverageon]letstdin={ic=Stdio.In_channel.stdin;buf=refNone}letcreatefname={ic=Stdio.In_channel.createfname;buf=refNone}letclose{ic;buf}=Stdio.In_channel.closeic;buf:=Noneletwith_filefname~f=Exn.protectx(createfname)~f~finally:close(* Input lines don't actually fill the buffer...you only need to fill a buffer
if you're peeking. *)letinput_all{ic;buf}=(* We use 65536 because that is the size of OCaml's IO buffers. *)letchunk_size=65536inletbuffer=Buffer.createchunk_sizeinlet()=match!bufwith|None->()|Someline->Buffer.add_stringbuffer(line^"\n")inletrecloop()=Stdlib.Buffer.add_channelbuffericchunk_size;loop()intryloop()withEnd_of_file->Buffer.contentsbufferletinput_line?(fix_win_eol=true){ic;buf}=match!bufwith|None->Stdio.In_channel.input_line~fix_win_eolic|Someline->buf:=None;Somelineletinput_line_exn?(fix_win_eol=true){ic;buf}=match!bufwith|None->Stdio.In_channel.input_line_exn~fix_win_eolic|Someline->buf:=None;lineletfold_lines?(fix_win_eol=true)t~init~f=letrecloopac=matchinput_line~fix_win_eoltwith|None->ac|Someline->loop(facline)inloopinitletinput_lines?(fix_win_eol=true)t=List.rev(fold_lines~fix_win_eolt~init:[]~f:(funlinesline->line::lines))letiter_lines?(fix_win_eol=true)t~f=fold_lines~fix_win_eolt~init:()~f:(fun()line->fline)letread_lines?(fix_win_eol=true)fname=with_filefname~f:(input_lines~fix_win_eol)letread_allfname=with_filefname~f:input_allletpeek_char?(fix_win_eol=true){ic;buf}=match!bufwith|None->((* No line in the buffer so get a new one. *)matchStdio.In_channel.input_line~fix_win_eolicwith|None->None|Someline->(buf:=Someline;matchlinewith""->Some'\n'|line->Some(String.getline0)))|Some""->Some'\n'|Someline->Some(String.getline0)letpeek_line?(fix_win_eol=true){ic;buf}=match!bufwith|None->((* No line in the buffer so get a new one. *)matchStdio.In_channel.input_line~fix_win_eolicwith|None->None|Someline->buf:=Someline;Someline)|Someline->Somelineend(* Heavily influenced by Jane Street's Stdio.In_channel module. The license for
that code is the following:
The MIT License
Copyright (c) 2016--2021 Jane Street Group, LLC opensource@janestreet.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. *)