Commit fd389252 authored by bbr's avatar bbr
Browse files

taking over changes for IO module

see branch gui
parent cd3b0b74
......@@ -3,7 +3,9 @@ module ExternalFunctionsIO where
import Curry
import CurryPrelude
import InstancesIO
import qualified System.IO as SI
import Control.Concurrent
instance ConvertCH C_IOMode SI.IOMode where
toCurry SI.ReadMode = C_ReadMode
......@@ -50,6 +52,22 @@ prim_hSeek = ioFunc3 SI.hSeek
prim_hWaitForInput :: C_Handle -> C_Int -> Result (C_IO C_Bool)
prim_hWaitForInput = ioFunc2 SI.hWaitForInput
selectHandle :: [SI.Handle] -> Int -> IO Int
selectHandle handles t = do
mvar <- newEmptyMVar
threads <- mapM (\ (i,h) -> forkIO (waitOnHandle h i t mvar)) (zip [0..] handles)
res <- readMVar mvar
mapM_ killThread threads
return (maybe (-1) id res)
waitOnHandle :: SI.Handle -> Int -> Int -> MVar (Maybe Int) -> IO ()
waitOnHandle h v t mvar = do
ready <- SI.hWaitForInput h t
putMVar mvar (if ready then Just v else Nothing)
prim_hWaitForInputs :: List C_Handle -> C_Int -> Result (C_IO C_Int)
prim_hWaitForInputs = ioFunc2 selectHandle
prim_hGetChar :: C_Handle -> Result (C_IO C_Char)
prim_hGetChar = ioFunc1 SI.hGetChar
......
......@@ -3,16 +3,17 @@
--- that are not already contained in the prelude.
---
--- @author Michael Hanus, Bernd Braßel
--- @version April 2006
--- @version June 2006
-----------------------------------------------------------------------------
module IO(Handle,IOMode(..),SeekMode(..),stdin,stdout,stderr,
openFile,hClose,hFlush,hIsEOF,isEOF,
hSeek,hWaitForInput,hReady,
hSeek,hWaitForInput,hWaitForInputs,hReady,
hGetChar,hGetLine,hGetContents,getContents,
hPutChar,hPutStr,hPutStrLn,hPrint,
hIsReadable,hIsWritable) where
--- The abstract type of a handle for a stream.
data Handle -- internally defined
--- The modes for opening a file.
......@@ -21,6 +22,7 @@ data IOMode = ReadMode | WriteMode | AppendMode
--- The modes for positioning with <code>hSeek</code> in a file.
data SeekMode = AbsoluteSeek | RelativeSeek | SeekFromEnd
--- Standard input stream.
stdin :: Handle
stdin external
......@@ -35,7 +37,7 @@ stderr external
--- Opens a file in specified mode and returns a handle to it.
openFile :: String -> IOMode -> IO Handle
openFile fileName mode = (prim_openFile $## fileName) $# mode
openFile filename mode = (prim_openFile $## filename) $# mode
prim_openFile :: String -> IOMode -> IO Handle
prim_openFile external
......@@ -65,6 +67,7 @@ prim_hIsEOF external
isEOF :: IO Bool
isEOF = hIsEOF stdin
--- Set the position of a handle to a seekable stream (e.g., a file).
--- If the second argument is <code>AbsoluteSeek</code>,
--- <code>SeekFromEnd</code>, or <code>RelativeSeek</code>,
......@@ -89,6 +92,20 @@ hWaitForInput h i = (prim_hWaitForInput $# h) $# i
prim_hWaitForInput :: Handle -> Int -> IO Bool
prim_hWaitForInput external
--- Waits until input is available on some of the given handles.
--- If no input is available within t milliseconds, it returns -1,
--- otherwise it returns the index of the corresponding handle with the available
--- data.
--- @param handles - a list of handles for input streams
--- @param timeout - milliseconds to wait for input (< 0 : no time out)
--- @return -1 if no input is available within the time out, otherwise i
--- if (handles!!i) has data available
hWaitForInputs :: [Handle] -> Int -> IO Int
hWaitForInputs handles timeout =
(prim_hWaitForInputs $## handles) $# timeout
prim_hWaitForInputs :: [Handle] -> Int -> IO Int
prim_hWaitForInputs external
--- Checks whether an input is available on a given handle.
hReady :: Handle -> IO Bool
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment