Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0000184OCamlOCaml generalpublic2000-09-02 18:502012-03-12 15:39
Reporteradministrator 
Assigned To 
PrioritylowSeverityfeatureReproducibilityalways
StatusresolvedResolutionwon't fix 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0000184: feature wish: read_file + read_channel
DescriptionHello,

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 [^]

TagsNo tags attached.
Attached Files

- Relationships
parent of 0000239resolved feature wish: read_file + read_channel 

-  Notes
(0000102)
administrator (administrator)
2002-11-06 17:07

see PR#239
(0006564)
doligez (administrator)
2011-12-30 16:46

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.
(0006580)
ygrek (reporter)
2012-01-03 10:10

NB there is (Std.input_file : ?bin:bool -> string -> string) in extlib
(0007045)
doligez (administrator)
2012-03-12 15:39

The official word on this feature:

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

- Issue History
Date Modified Username Field Change
2005-11-18 10:13 administrator New Issue
2008-01-22 13:37 doligez Relationship added related to 0000239
2008-01-22 13:37 doligez Relationship deleted related to 0000239
2008-01-22 13:37 doligez Relationship added parent of 0000239
2011-12-30 16:46 doligez Note Added: 0006564
2012-01-03 10:10 ygrek Note Added: 0006580
2012-01-20 14:44 doligez Priority normal => low
2012-01-20 14:44 doligez Description Updated View Revisions
2012-03-12 15:39 doligez Note Added: 0007045
2012-03-12 15:39 doligez Status acknowledged => resolved
2012-03-12 15:39 doligez Resolution open => won't fix


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker