Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature wish: read_file + read_channel #2559

Closed
vicuna opened this issue Sep 2, 2000 · 4 comments
Closed

feature wish: read_file + read_channel #2559

vicuna opened this issue Sep 2, 2000 · 4 comments

Comments

@vicuna
Copy link

vicuna commented Sep 2, 2000

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

@vicuna
Copy link
Author

vicuna commented Nov 6, 2002

Comment author: administrator

see #2630

@vicuna
Copy link
Author

vicuna commented Dec 30, 2011

Comment author: @damiendoligez

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.

@vicuna
Copy link
Author

vicuna commented Jan 3, 2012

Comment author: @ygrek

NB there is (Std.input_file : ?bin:bool -> string -> string) in extlib

@vicuna
Copy link
Author

vicuna commented Mar 12, 2012

Comment author: @damiendoligez

The official word on this feature:

You should use Jane St Core, or Batteries, or Extlib, which all provide this functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant