You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Original bug ID: 184 Reporter: administrator Status: closed (set by @xavierleroy on 2015-12-11T18:04:21Z) Resolution: won't fix Priority: low Severity: feature Category: ~DO NOT USE (was: OCaml general) Parent of:#2630
Bug description
Hello,
would it be possible to add a function like the following to the
Pervasives-library (maybe with another name as required), which reads in a
file completely and returns its contents as string (quite useful)? -
val read_file : string -> string
let read_file name =
let file = open_in name in
let size = in_channel_length file in
let buf = String.create size in
try
unsafe_really_input file buf 0 size;
close_in file;
buf
with exn -> close_in file; raise exn
The following function (read_channel) would also be nice: it reads in a
channel efficiently and completely until EOF is reached (the read buffer
size is optional with a reasonable default).
val read_channel : ?buf_size:int -> in_channel -> string
let rec copy_lst res ofs = function
| [] -> ()
| (str,len)::t ->
let pos = ofs - len in
String.unsafe_blit str 0 res pos len;
copy_lst res pos t
let rec read_channel_aux len lst buf_size ch =
let buf = String.create buf_size in
let n = unsafe_input ch buf 0 buf_size in
if n <> 0 then read_channel_aux (len+n) ((buf,n)::lst) buf_size ch
else begin
let res = String.create (len+n) in
copy_lst res len lst;
String.unsafe_blit buf 0 res len n;
res end
let read_channel ?(buf_size = 4096) = read_channel_aux 0 [] buf_size
This is probably a good idea, but where to put them? read_channel in particular would greatly benefit from using the Buffer module, but that precludes putting it in Pervasives.
Original bug ID: 184
Reporter: administrator
Status: closed (set by @xavierleroy on 2015-12-11T18:04:21Z)
Resolution: won't fix
Priority: low
Severity: feature
Category: ~DO NOT USE (was: OCaml general)
Parent of: #2630
Bug description
Hello,
would it be possible to add a function like the following to the
Pervasives-library (maybe with another name as required), which reads in a
file completely and returns its contents as string (quite useful)? -
val read_file : string -> string
let read_file name =
let file = open_in name in
let size = in_channel_length file in
let buf = String.create size in
try
unsafe_really_input file buf 0 size;
close_in file;
buf
with exn -> close_in file; raise exn
The following function (read_channel) would also be nice: it reads in a
channel efficiently and completely until EOF is reached (the read buffer
size is optional with a reasonable default).
val read_channel : ?buf_size:int -> in_channel -> string
let rec copy_lst res ofs = function
| [] -> ()
| (str,len)::t ->
let pos = ofs - len in
String.unsafe_blit str 0 res pos len;
copy_lst res pos t
let rec read_channel_aux len lst buf_size ch =
let buf = String.create buf_size in
let n = unsafe_input ch buf 0 buf_size in
if n <> 0 then read_channel_aux (len+n) ((buf,n)::lst) buf_size ch
else begin
let res = String.create (len+n) in
copy_lst res len lst;
String.unsafe_blit buf 0 res len n;
res end
let read_channel ?(buf_size = 4096) = read_channel_aux 0 [] buf_size
Best regards,
Markus Mottl
--
Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl
The text was updated successfully, but these errors were encountered: