module Kwio:sig
..end
Function that actually perform I/O on opened files.
Author(s): Keith Waclena
val with_open_in_file : ?iflags:Pervasives.open_flag list ->
?isstdin:(string -> bool) ->
(string -> Pervasives.in_channel -> 'a) -> string -> 'a
Apply f
to the filename
and in_channel
resulting from opening
filename
for input, making sure to close the channel when done;
also handles stdin.
Returns whatever the application of f returns
iflags
: flags to use when opening the input file, as per open_in_gen
; default: [Open_rdonly]
isstdin
: predicate to decide whether a filename refers to
stdin (default: "-" and "" are stdin)f
: f fn chan
function to apply to filename fn
and in_channel chan
resulting from opening filenamefilename
: name of file to openval with_open_tmp_file : ?tmpdir:string ->
?mode:Pervasives.open_flag list ->
?perm:int ->
?prefix:string ->
?suffix:string -> (string -> Pervasives.out_channel -> 'a) -> string * 'a
with_open_tmp_file f
: apply the function f
to the filename and
open output channel of a temp file, making sure to close the
channel afterwards.tmpdir
: directory in which to make tmpfile (default: value of TMPDIR
env var, else /tmp
)mode
: list of open modesperm
: file permissions (default: 0o600
)prefix
: temp filename prefixsuffix
: temp filename suffixf
: f fn chan
function to apply to the filename fn
and out_channel chan
of the temp fileval with_open_out_file : ?oflags:Pervasives.open_flag list ->
?devnull:bool ->
?isstdout:(string -> bool) ->
?tmpdir:string ->
?prefix:string ->
?suffix:'a ->
?perm:int ->
?safely:bool -> (string -> Pervasives.out_channel -> 'b) -> string -> 'b
with_open_out_file f filename
: apply a function to the contents of an output file, making sure to
close the channel afterwards
Apply f
to the filename
and out_channel
resulting from opening
filename
for output, making sure to close the channel when done;
also handles stdout.
Returns whatever the application of f returns
oflags
: flags to use when opening the output file, as per open_out_gen
; default: [Open_creat;Open_wronly;Open_trunc]
devnull
: if true, open "/dev/null"
instead of filename
thus NOT clobbering filename
; default: falseisstdout
: predicate to decide whether a filename refers to stdout (default: "-" and "" are stdout)tmpdir
: directory in which to make tmpfile (default: value of TMPDIR
env var, else /tmp
)prefix
: temp filename prefixsuffix
: temp filename suffixperm
: file permissions (default: 0o666 land (lnot umask)
)safely
: open filename
"safely" so it can be read from inside f
(i.e., use a temp file and rename algorithm) (default: false
)f
: f fn chan
function to apply to the filename fn
and out_channel chan
resulting from opening filenamefilename
: name of file to openval withchan : ('a -> 'b) -> 'c -> 'a -> 'b
with_open_in_file
and with_open_out_file
E.g. with_open_in_file (withchan readlines) filename
Returns function suitable for with_open_in_file
or with_open_out_file
f
: function that take a channelval read : ?size:int -> Pervasives.in_channel -> string
in_channel
size
: input buffer size (only used for non-regular files)chan
: in_channel
to read fromval readfile : ?size:int -> string -> string
size
: input buffer size (only used for non-regular files)filename
: name of file to read frommodule EOL:sig
..end
val foldchars : ?eol:EOL.eol -> ('a -> char -> 'a) -> 'a -> Pervasives.in_channel -> 'a
foldchars f acc chan
: fold_left for a channel as characters.
Tail-recursive.
Returns accumulator
f
: function to apply to each char of input; the first parm is the accumulator and the second is the characc
: initial value for accumulatorchan
: the in_channel to read fromval getline : Pervasives.in_channel -> string option
Returns Some(line)
where line
is the next line read from chan
,
or None if EOF. Suitable for use in tail-recursive I/O functions.
chan
: the in_channel to read fromval readlines : Pervasives.in_channel -> string list
string list
string list
of lines from input channelchan
: the in_channel to read fromval maplines : (int -> string -> 'a) -> Pervasives.in_channel -> 'a list
Return the list that results from mapping f
across all lines of
in_channel chan
. Tail-recursive.
Returns 'a list
of results
f
: function (int -> string -> 'a)
to apply to each line of input; the int
is the line number (1-based) and the string
is the linechan
: the in_channel to read fromval iterlines : (int -> string -> 'a) -> Pervasives.in_channel -> unit
Tail-recursive.
Returns ()
f
: function (int -> string -> ())
to apply to each line of input; the int
is the line number (1-based) and the string
is the linechan
: the in_channel to read fromval foldlines : ('a -> string -> 'a) -> 'a -> Pervasives.in_channel -> 'a
Tail-recursive.
Returns accumulator
f
: function to apply to each line of input; the first parm is the accumulator and the second is the lineacc
: initial value for accumulatorchan
: the in_channel to read fromval writefile : ?oflags:Pervasives.open_flag list ->
?isstdout:(string -> bool) -> string -> string -> unit
writefile ?oflags ?isstdout fn str
: write str
to fn
val clone : ?closeout:bool ->
?bufsize:int -> Pervasives.in_channel -> Pervasives.out_channel -> unit
clone ?closeout ?bufsize inp out
: copy all data from an in_channel to an out_channelcloseout
: close the output channel after cloning (default: true
)bufsize
: size of buffer to use (default: 32K)inp
: in_channel to copy fromout
: out_channel to copy toval copyfile : ?iflags:Pervasives.open_flag list ->
?oflags:Pervasives.open_flag list -> string -> string -> unit
copyfile infile outfile
: copy the input file infile
to the output file outfile
.Failure
if infile and outfile are the sameval tmpstdin : ?isstdin:(string -> bool) -> string array -> string list
Applications that make two passes over their input have a problem
with stdin; if you are going to read it twice, you need to save a
copy of it to a tempfile. The same problem applies to single-pass
applications if you allow the user to refer to stdin multiple times,
typically by using "-"
as a filename.
tmpstdin
takes a list of filenames (probably from the command
line) and if any of them refers to stdin (determined by applying the
isstdin
predicate to each filename), copies the data to a tempfile
and replaces all references to stdin in the list with the name of
the tempfile. The "fixed up" list is returned, and the tempfile is
removed when your application exits.
Example: (tmpstdin ["foo"; "-"; "baz"; "-"])
might return: ["foo"; "/tmp/foo123"; "baz"; "/tmp/foo123"]
No tempfile is created if argv
contains no references to stdin.
Returns fixed-up version of argv
isstdin
: predicate to decide whether a filename refers to stdin (default: "-" and "" are stdin)argv
: array of filenames to fixup (typically (Array.sub Sys.argv 1 (Array.length Sys.argv-1)) or (Kw.argvbut0 ()) which is the same thing)