From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id C57CDBC48 for ; Sat, 2 Apr 2005 16:44:00 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j32EhxgM027758 for ; Sat, 2 Apr 2005 16:43:59 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id QAA01980 for ; Sat, 2 Apr 2005 16:43:59 +0200 (MET DST) Received: from mail-in-06.arcor-online.net (mail-in-06.arcor-online.net [151.189.21.46]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j32EhwB0027755 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Sat, 2 Apr 2005 16:43:58 +0200 Received: from mail.arcor.de (dialin-212-144-207-018.arcor-ip.net [212.144.207.18]) by mail-in-06.arcor-online.net (Postfix) with ESMTP id 25FB3112280; Sat, 2 Apr 2005 16:43:53 +0200 (CEST) Received: by denkmaschine.unterwegs.local (Postfix, from userid 500) id 923301CF3519; Sun, 3 Apr 2005 16:43:02 +0200 (CEST) To: Eijiro Sumii Cc: caml-list@inria.fr, sumii@saul.cis.upenn.edu Subject: Re: [Caml-list] select (or polling) on in_channel? References: <20050331.182855.07449233.eijiro_sumii@anet.ne.jp> From: Christoph Bauer Date: Sun, 03 Apr 2005 16:43:02 +0200 In-Reply-To: <20050331.182855.07449233.eijiro_sumii@anet.ne.jp> (Eijiro Sumii's message of "Thu, 31 Mar 2005 18:28:55 -0500 (EST)") Message-ID: User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/22.0.50 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Miltered: at nez-perce with ID 424EAFAF.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 424EAFAE.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 polling:01 stdin:01 nonblock:01 stdin:01 buf:01 rec:01 buf:01 blit:01 blit:01 rec:01 acc':01 acc':01 printf:01 printf:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=2.4 required=5.0 tests=DATE_IN_FUTURE_12_24, FORGED_RCVD_HELO autolearn=disabled version=3.0.2 X-Spam-Level: ** Hi, here is a solution that seems to work. The function Stdinbuf.lines returns a list of available lines. It works with or without Unix.select [Unix.stdin] [] [] delay Christoph Bauer (* Stdinbuf.ml *) let () = Unix.set_nonblock Unix.stdin let buf = ref (String.create 256) let pos = ref 0 let rec lines stdin = let len = String.length !buf in let r = try Unix.read stdin !buf !pos (len - !pos) with Unix.Unix_error( Unix.EAGAIN, _ , _ ) -> 0 in pos := r + !pos; if !pos >= len then let s' = String.create (len*2) in String.blit !buf 0 s' 0 len; buf := s'; lines stdin else try let e = String.rindex_from !buf !pos '\n' in let c = String.sub !buf 0 e in String.blit !buf e !buf 0 (!pos-e); pos := !pos - e; StringUtils.split ~sep:'\n' c with Not_found -> [] (* eof *) For completeness: (* stringUtils.ml *) (* ... * ) let split ?(sep = ' ') ?(empty = false) s = let rec loop acc i = let s, idx = try let idx = String.rindex_from s i sep in String.sub s (idx+1) (i-idx), (idx-1) with Not_found -> String.sub s 0 (i+1), ~-1 in let acc' = if empty || s <> "" then s::acc else acc in if idx = ~-1 then acc' else loop acc' idx in loop [] (String.length s -1) -- let () = let rec f a w i j = Printf.printf "%.20f\r" a; let a1 = a *. i /. j in if w then f a1 false (i +. 2.0) j else f a1 true i (j +. 2.0) in f 2.0 false 2.0 1.0