From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id LAA10256; Wed, 17 Mar 2004 11:12:00 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA10621 for ; Wed, 17 Mar 2004 11:11:59 +0100 (MET) Received: from fichte.ai.univie.ac.at (fichte.ai.univie.ac.at [131.130.174.156]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i2HABvHd003059 for ; Wed, 17 Mar 2004 11:11:58 +0100 Received: from fichte.ai.univie.ac.at (markus@localhost [127.0.0.1]) by fichte.ai.univie.ac.at (8.12.3/8.12.3/Debian-6.6) with ESMTP id i2HABvHn018558 for ; Wed, 17 Mar 2004 11:11:57 +0100 Received: (from markus@localhost) by fichte.ai.univie.ac.at (8.12.3/8.12.3/Debian-6.6) id i2HABvUx018557 for caml-list@inria.fr; Wed, 17 Mar 2004 11:11:57 +0100 Date: Wed, 17 Mar 2004 11:11:57 +0100 From: Markus Mottl To: OCaml Subject: Re: [Caml-list] Better option to read a file Message-ID: <20040317101157.GA18178@fichte.ai.univie.ac.at> Mail-Followup-To: OCaml References: <16472.2747.9259.155336@gargle.gargle.HOWL> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <16472.2747.9259.155336@gargle.gargle.HOWL> User-Agent: Mutt/1.4.1i X-Miltered: at concorde by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 unbounded:01 blit:01 unsafe:01 rec:01 rec:01 buf:01 buf:01 mottl:02 mottl:02 bytes:02 exceeded:96 string:03 string:03 argument:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk X-Status: X-Keywords: X-UID: 152 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