Module Kwio

module Kwio: sig .. end

I/O Functions

Function that actually perform I/O on opened files.
Author(s): Keith Waclena



Finalizers


val with_open_in_file : ?iflags:Pervasives.open_flag list ->
?isstdin:(string -> bool) ->
(string -> Pervasives.in_channel -> 'a) -> string -> 'a
Apply a function to the contents of an input file, making sure to close the channel afterwards

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 filename
filename : name of file to open
val 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 modes
perm : file permissions (default: 0o600)
prefix : temp filename prefix
suffix : temp filename suffix
f : f fn chan function to apply to the filename fn and out_channel chan of the temp file
val 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: false
isstdout : 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 prefix
suffix : temp filename suffix
perm : 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 filename
filename : name of file to open
val withchan : ('a -> 'b) -> 'c -> 'a -> 'b
convenience combinator to ignore the filename parameter passed by 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 channel

Reading Data


val read : ?size:int -> Pervasives.in_channel -> string
read and return (as string) all data from an in_channel
Returns entire channel contents as a string
size : input buffer size (only used for non-regular files)
chan : in_channel to read from
val readfile : ?size:int -> string -> string
Return entire contents of a file as a string
Returns entire channel contents as a string
size : input buffer size (only used for non-regular files)
filename : name of file to read from
module EOL: sig .. end

Reading Characters


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 char
acc : initial value for accumulator
chan : the in_channel to read from

Reading Lines


val getline : Pervasives.in_channel -> string option
Non-exceptional version of input_line

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 from
val readlines : Pervasives.in_channel -> string list
Return all lines from an in_channel as a string list
Returns string list of lines from input channel
chan : the in_channel to read from
val maplines : (int -> string -> 'a) -> Pervasives.in_channel -> 'a list
Map a function across all lines of an input file

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 line
chan : the in_channel to read from
val iterlines : (int -> string -> 'a) -> Pervasives.in_channel -> unit
Apply a function, for its side-effect, to each line of an input file

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 line
chan : the in_channel to read from
val foldlines : ('a -> string -> 'a) -> 'a -> Pervasives.in_channel -> 'a
fold_left for a channel as lines

Tail-recursive.
Returns accumulator

f : function to apply to each line of input; the first parm is the accumulator and the second is the line
acc : initial value for accumulator
chan : the in_channel to read from

Writing Data


val writefile : ?oflags:Pervasives.open_flag list ->
?isstdout:(string -> bool) -> string -> string -> unit
writefile ?oflags ?isstdout fn str: write str to fn

Cloning Files

Whole-file copying from channel to channel.
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_channel
closeout : close the output channel after cloning (default: true)
bufsize : size of buffer to use (default: 32K)
inp : in_channel to copy from
out : out_channel to copy to
val 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.
Raises Failure if infile and outfile are the same

Processing Standard Input Repeatedly


val tmpstdin : ?isstdin:(string -> bool) -> string array -> string list
Fix up a list of filenames such that stdin can be read repeatedly

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)