@K_N thanks to @nojb’s work, these mundane tasks finally get bearable with the upcoming 4.14
Stdlib
.
For example:
let lines file =
let contents = In_channel.with_open_bin file In_channel.input_all in
String.split_on_char '\n' contents
Or to support the widespread tool convention that filename -
means stdin:
let lines file =
let contents = match file with
| "-" -> In_channel.input_all In_channel.stdin
| file -> In_channel.with_open_bin file In_channel.input_all
in
String.split_on_char '\n' contents
And for writing:
let write_lines file lines =
let output_lines oc = output_string oc (String.concat "\n" lines) in
Out_channel.with_open_bin file output_lines
let write_lines file lines =
let output_lines oc = output_string oc (String.concat "\n" lines) in
match file with
| "-" -> output_lines Out_channel.stdout
| file -> Out_channel.with_open_bin file output_lines