Re: Re[2]: [Haskell-cafe] `Expect'-like lazy reading/Parsec matching on TCP sockets

2007-04-05 Thread Scott Bell

Bulat,


yes, with both variants. actually, second one should be easier to
implement and understand. you should look into unsafeInterleaveIO
section of http://haskell.org/haskellwiki/IO_inside


This seems to do what I want, and unless I'm overlooking something
it feels very straight-forward:

hGetContentsTimeout :: Handle - Int - IO String
hGetContentsTimeout h t = do
 hSetBuffering stdin NoBuffering
 ready - hWaitForInput h t
 if (not ready) then return []
   else do
 c - hGetChar h
 s - unsafeInterleaveIO (hGetContentsTimeout h t)
 return (c:s)

This is not extensivly tested, but applying my parser to the string
returned by hGetContentsTimeout behaves precisely as I wanted:

It returns a match as soon as it is available, and fails if it is not
seen within t ms.

Thanks for your help!

- Scott
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re[2]: [Haskell-cafe] `Expect'-like lazy reading/Parsec matching on TCP sockets

2007-04-04 Thread Bulat Ziganshin
Hello Scott,

Wednesday, April 4, 2007, 6:39:22 PM, you wrote:

   vGetChar (StreamWithTimeout s t) = do
  timeout t (vGetChar s)
(error Timed out!)

 If possible, I would like to try and use lazy [Char]s -- this would greatly
 simplify my usage of the Parsec parser.

 or, even simple, you can make your own variant of hGetContents which
 adds a timeout checks before each next call to hGetChar or hGetBuf

 Can this be as simple as applying the parser against a string returned
 by the (modified) hGetContents, which will read all that is possible given
 a certain time constraint?

yes, with both variants. actually, second one should be easier to
implement and understand. you should look into unsafeInterleaveIO
section of http://haskell.org/haskellwiki/IO_inside

feel free to ask me if you need more help


-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe