home *** CD-ROM | disk | FTP | other *** search
/ PSION CD 2 / PsionCDVol2.iso / Programs / 876 / hugs.sis / IO.hs < prev    next >
Encoding:
Text File  |  2000-09-21  |  5.4 KB  |  156 lines

  1. -----------------------------------------------------------------------------
  2. -- Standard Library: IO operations, beyond those included in the prelude
  3. --
  4. -- WARNING: The names and semantics of functions defined in this module
  5. -- may change as the details of the IO standard are clarified.
  6. --
  7. -- Suitable for use with Hugs 98
  8. -----------------------------------------------------------------------------
  9.  
  10. module IO (
  11.     Handle, HandlePosn,
  12. --  IOMode(ReadMode,WriteMode,AppendMode,ReadWriteMode),
  13.     IOMode(ReadMode,WriteMode,AppendMode),
  14.     BufferMode(NoBuffering,LineBuffering,BlockBuffering),
  15.     SeekMode(AbsoluteSeek,RelativeSeek,SeekFromEnd),
  16.     stdin, stdout, stderr, 
  17.     openFile, hClose, 
  18. --  hFileSize, hIsEOF, isEOF,
  19. --  hSetBuffering, hGetBuffering, hFlush, 
  20.     hFlush, 
  21.     hGetPosn, hSetPosn, 
  22. --  hSeek, hIsSeekable,
  23. --  hReady, hGetChar, hLookAhead, hGetContents, 
  24.     hGetChar, hGetLine, hGetContents, 
  25.     hPutChar, hPutStr, hPutStrLn, hPrint,
  26.     hIsOpen, hIsClosed, hIsReadable, hIsWritable, 
  27.     isAlreadyExistsError, isDoesNotExistError, isAlreadyInUseError, 
  28.     isFullError, isEOFError,
  29.     isIllegalOperation, isPermissionError, isUserError, 
  30.     ioeGetErrorString, ioeGetHandle, ioeGetFileName,
  31.     try, bracket, bracket_,
  32.  
  33.     -- Non-standard extensions 
  34.     hugsIsEOF, hugsHIsEOF,
  35.     hugsIsSearchErr, hugsIsNameErr, hugsIsWriteErr,
  36.  
  37.     -- ... and what the Prelude exports
  38.     IO,
  39.     FilePath, IOError, ioError, userError, catch,
  40.     putChar, putStr, putStrLn, print,
  41.     getChar, getLine, getContents, interact,
  42.     readFile, writeFile, appendFile, readIO, readLn
  43.     ) where
  44.  
  45. import Ix(Ix)
  46.  
  47. data Handle
  48. instance Eq Handle where (==) = primEqHandle
  49. primitive primEqHandle :: Handle -> Handle -> Bool
  50. newtype HandlePosn = HandlePosn Int deriving Eq
  51.  
  52. --data IOMode      =  ReadMode | WriteMode | AppendMode | ReadWriteMode
  53. data IOMode      = ReadMode | WriteMode | AppendMode
  54.                     deriving (Eq, Ord, Ix, Bounded, Enum, Read, Show)
  55. data BufferMode  =  NoBuffering | LineBuffering 
  56.                  |  BlockBuffering (Maybe Int)
  57.                     deriving (Eq, Ord, Read, Show)
  58. data SeekMode    =  AbsoluteSeek | RelativeSeek | SeekFromEnd
  59.                     deriving (Eq, Ord, Ix, Bounded, Enum, Read, Show)
  60.  
  61. primitive stdin       :: Handle
  62. primitive stdout      :: Handle
  63. primitive stderr      :: Handle
  64. primitive openFile    :: FilePath -> IOMode -> IO Handle
  65. primitive hClose      :: Handle -> IO ()
  66. --Not yet implemented:
  67. --hFileSize           :: Handle -> IO Integer
  68. --hIsEOF              :: Handle -> IO Bool
  69. --isEOF               :: IO Bool
  70. --isEOF                = hIsEOF stdin
  71.  
  72. --hSetBuffering       :: Handle  -> BufferMode -> IO ()
  73. --hGetBuffering       :: Handle  -> IO BufferMode
  74. primitive hFlush      :: Handle -> IO ()
  75. primitive hGetPosn    :: Handle -> IO HandlePosn
  76. primitive hSetPosn    :: HandlePosn -> IO () 
  77. --hSeek               :: Handle -> SeekMode -> Integer -> IO () 
  78.  
  79. --hWaitForInput          :: Handle -> Int -> IO Bool
  80. --hReady              :: Handle -> IO Bool 
  81. --hReady h           = hWaitForInput h 0
  82. primitive hGetChar    :: Handle -> IO Char
  83.  
  84. hGetLine              :: Handle -> IO String
  85. hGetLine h             = do c <- hGetChar h
  86.                             if c=='\n' then return ""
  87.                               else do cs <- hGetLine h
  88.                                       return (c:cs)
  89.  
  90. --hLookAhead          :: Handle -> IO Char
  91. primitive hGetContents:: Handle -> IO String
  92. primitive hPutChar    :: Handle -> Char -> IO ()
  93. primitive hPutStr     :: Handle -> String -> IO ()
  94.  
  95. hPutStrLn             :: Handle -> String -> IO ()
  96. hPutStrLn h s          = do { hPutStr h s; hPutChar h '\n' }
  97.  
  98. hPrint                :: Show a => Handle -> a -> IO ()
  99. hPrint h               = hPutStrLn h . show
  100.  
  101. primitive hIsOpen,    
  102.          hIsClosed,  
  103.          hIsReadable,
  104.          hIsWritable :: Handle -> IO Bool
  105. --hIsSeekable         :: Handle -> IO Bool
  106.  
  107. primitive isIllegalOperation, 
  108.       isAlreadyExistsError, 
  109.       isDoesNotExistError, 
  110.           isAlreadyInUseError,   
  111.       isFullError,     
  112.           isEOFError, 
  113.       isPermissionError,
  114.           isUserError        :: IOError -> Bool
  115.  
  116. primitive ioeGetErrorString "primShowIOError" :: IOError -> String
  117. primitive ioeGetHandle      :: IOError -> Maybe Handle
  118. primitive ioeGetFileName    :: IOError -> Maybe FilePath
  119.  
  120. try       :: IO a -> IO (Either IOError a)
  121. try p      = catch (p >>= (return . Right)) (return . Left)
  122.  
  123. bracket        :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
  124. bracket before after m = do
  125.         x  <- before
  126.         rs <- try (m x)
  127.         after x
  128.         case rs of
  129.            Right r -> return r
  130.            Left  e -> ioError e
  131.  
  132. -- variant of the above where middle computation doesn't want x
  133. bracket_        :: IO a -> (a -> IO b) -> IO c -> IO c
  134. bracket_ before after m = do
  135.          x  <- before
  136.          rs <- try m
  137.          after x
  138.          case rs of
  139.             Right r -> return r
  140.             Left  e -> ioError e
  141.  
  142. -----------------------------------------------------------------------------
  143. -- Non-standard extensions 
  144. -- (likely to disappear when IO library is more complete)
  145.  
  146. -- C library style test for EOF (doesn't obey Haskell semantics)
  147. primitive hugsHIsEOF "hIsEOF" :: Handle -> IO Bool
  148. hugsIsEOF             :: IO Bool
  149. hugsIsEOF              = hugsHIsEOF stdin
  150.  
  151. primitive hugsIsSearchErr :: IOError -> Bool
  152. primitive hugsIsNameErr   :: IOError -> Bool
  153. primitive hugsIsWriteErr  :: IOError -> Bool
  154.  
  155. -----------------------------------------------------------------------------
  156.