caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dave Benjamin <dave@ramenlabs.com>
To: Eric Cooper <ecc@cmu.edu>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] commands.getoutput () in ocaml?
Date: Wed, 22 Aug 2007 23:03:42 -0500 (CDT)	[thread overview]
Message-ID: <Pine.LNX.4.63.0708222301001.24186@tenhost.net> (raw)
In-Reply-To: <20070822230418.GC10505@stratocaster.home>

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


  parent reply	other threads:[~2007-08-23  4:31 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-08-22 19:56 Luca de Alfaro
2007-08-22 20:05 ` [Caml-list] " malc
2007-08-22 20:10   ` malc
2007-08-22 20:11 ` Dave Benjamin
2007-08-22 21:32   ` Luca de Alfaro
2007-08-22 22:11     ` Yitzhak Mandelbaum
2007-08-22 22:13     ` Daniel Bünzli
2007-08-22 22:35   ` Olivier Andrieu
2007-08-22 23:04     ` Eric Cooper
2007-08-22 22:52       ` Dave Benjamin
2007-08-23  4:03       ` Dave Benjamin [this message]
2007-08-23 17:07         ` Eric Cooper
2007-08-23 15:24   `  Dr. Axel Poigné 
2007-08-22 20:17 ` Brian Hurt

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=Pine.LNX.4.63.0708222301001.24186@tenhost.net \
    --to=dave@ramenlabs.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=ecc@cmu.edu \
    /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).