caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Christoph Bauer <ich@christoph-bauer.net>
To: Eijiro Sumii <eijiro_sumii@anet.ne.jp>
Cc: caml-list@inria.fr, sumii@saul.cis.upenn.edu
Subject: Re: [Caml-list] select (or polling) on in_channel?
Date: Sun, 03 Apr 2005 16:43:02 +0200	[thread overview]
Message-ID: <m3y8bzkjk9.fsf@christoph-bauer.net> (raw)
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)")

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



  parent reply	other threads:[~2005-04-02 14:44 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-31 23:28 Eijiro Sumii
2005-04-01 10:59 ` [Caml-list] " Gerd Stolpmann
2005-04-01 13:37   ` Eijiro Sumii
2005-04-03 14:43 ` Christoph Bauer [this message]
2005-04-04 20:24   ` Eijiro Sumii
2005-04-04 21:29     ` Eijiro Sumii
2005-04-05  5:49     ` Alex Baretta
2005-04-05 13:34       ` Eijiro Sumii
2005-04-05 14:14         ` Alex Baretta
2005-04-05 18:07           ` Eijiro Sumii
2005-04-05 14:30         ` Richard Jones
2005-04-05 18:12           ` Eijiro Sumii
2005-04-05  7:00 sejourne kevin
2005-04-05 13:39 ` Eijiro Sumii

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=m3y8bzkjk9.fsf@christoph-bauer.net \
    --to=ich@christoph-bauer.net \
    --cc=caml-list@inria.fr \
    --cc=eijiro_sumii@anet.ne.jp \
    --cc=sumii@saul.cis.upenn.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).