From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 3A5D7BC69 for ; Thu, 23 Aug 2007 06:31:07 +0200 (CEST) Received: from tenhost.net (tenhost.net [64.246.28.52]) by concorde.inria.fr (8.13.6/8.13.6) with SMTP id l7N4V5b4002266 for ; Thu, 23 Aug 2007 06:31:06 +0200 Received: (qmail 24523 invoked by uid 502); 22 Aug 2007 23:03:42 -0500 Received: from localhost (sendmail-bs@127.0.0.1) by localhost with SMTP; 22 Aug 2007 23:03:42 -0500 Date: Wed, 22 Aug 2007 23:03:42 -0500 (CDT) From: Dave Benjamin X-X-Sender: ramen@tenhost.net To: Eric Cooper Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] commands.getoutput () in ocaml? In-Reply-To: <20070822230418.GC10505@stratocaster.home> Message-ID: References: <28fa90930708221256t497e8356k84861abd6b2e91b8@mail.gmail.com> <95513600708221535t7508e900h192554da73d37fcf@mail.gmail.com> <20070822230418.GC10505@stratocaster.home> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Miltered: at concorde with ID 46CD0D89.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 0200,:01 andrieu:01 buffer:01 buffer:01 2048:01 2007,:98 23,:98 fires:98 wrote:01 wrote:01 unix:01 unix:01 caml-list:01 benjamin:01 On Wed, 22 Aug 2007, Eric Cooper wrote: > On Thu, Aug 23, 2007 at 12:35:14AM +0200, Olivier Andrieu wrote: >> you could use Buffer.add_substring instead of add_string, that will >> avoid an intermediate copy of the string > > And you can use Buffer.add_channel to avoid intermediate strings > altogether (until the final Buffer.to_string). I'm not quite sure how to do this, actually. Here's what I've got so far: let read_process command = let buffer_size = 2048 in let buffer = Buffer.create buffer_size in let in_channel = Unix.open_process_in command in begin try while true do Buffer.add_channel buffer in_channel buffer_size; done with | End_of_file -> () | e -> ignore (Unix.close_process_in in_channel); raise e; end; ignore (Unix.close_process_in in_channel); Buffer.contents buffer However, this doesn't work unless I set buffer_size to 1. Otherwise, the End_of_file fires before all the data is read in, so I either get nothing or an incomplete result. Any advice? Thanks, Dave