caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Markus Mottl <markus@oefai.at>
To: OCaml <caml-list@inria.fr>
Subject: Re: [Caml-list] Better option to read a file
Date: Wed, 17 Mar 2004 11:11:57 +0100	[thread overview]
Message-ID: <20040317101157.GA18178@fichte.ai.univie.ac.at> (raw)
In-Reply-To: <16472.2747.9259.155336@gargle.gargle.HOWL>

Hi,

I usually use one of the two functions below to read in whole strings.
Function "read_file" does the obvious: read a file as fast as possible
into a string.

Function "read_channel" reads a channel of unbounded size (as long as the
maximum string length is not exceeded, of course).  It also takes the
optional argument "buf_size", which you can set depending on the kind
of channel you read from (the default 4096 bytes are somewhat optimal
when reading from files on Linux).

---------------------------------------------------------------------------
let rec copy_lst res ofs = function
  | [] -> res
  | (str, len) :: t ->
      let pos = ofs - len in
      String.unsafe_blit str 0 res pos len;
      copy_lst res pos t

let read_channel ?(buf_size = 4096) =
  let rec loop len lst ch =
    let buf = String.create buf_size in
    let n = input ch buf 0 buf_size in
    if n <> 0 then loop (len + n) ((buf, n) :: lst) ch
    else copy_lst (String.create len) len lst in
  loop 0 []

let read_file name =
  let file = open_in name in
  let size = in_channel_length file in
  try
    let buf = String.create size in
    really_input file buf 0 size;
    close_in file;
    buf
  with exc ->
    (try close_in file with _ -> ());
    raise exc
---------------------------------------------------------------------------

Regards,
Markus Mottl

-- 
Markus Mottl          http://www.oefai.at/~markus          markus@oefai.at

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2004-03-17 10:12 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-16 21:28 Agustín Valverde
2004-03-17  3:48 ` Pietro Abate
2004-03-17  7:31 ` Christoph Bauer
2004-03-17 16:42   ` Agustin Valverde Ramos
2004-03-17 17:46     ` Markus Mottl
2004-03-17 18:20       ` Agustin Valverde Ramos
2004-03-17 18:54         ` Markus Mottl
2004-03-17  8:22 ` Jean-Christophe Filliatre
2004-03-17 10:11   ` Markus Mottl [this message]
  -- strict thread matches above, loose matches on Subject: below --
2004-03-16 20:38 Agustín Valverde

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040317101157.GA18178@fichte.ai.univie.ac.at \
    --to=markus@oefai.at \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).