1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
-----------------------------------------------------------------------------
-- |
-- Module : System.IO.Strict
-- Copyright : (c) Don Stewart 2007
-- License : BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer : dons@galois.com
-- Stability : stable
-- Portability : portable
--
-- The standard IO input functions using strict IO.
--
-----------------------------------------------------------------------------
module System.IO.Strict (
-- * Strict Handle IO
hGetContents, -- :: Handle -> IO [Char]
-- * Strict String IO wrappers
getContents, -- :: IO String
readFile, -- :: FilePath -> IO String
interact -- :: (String -> String) -> IO ()
) where
import Prelude ( String, (>>=), seq, return, (.), (=<<), FilePath, length)
import System.IO (IO)
import qualified System.IO as IO
-- -----------------------------------------------------------------------------
-- Strict hGetContents
-- | Computation 'hGetContents' @hdl@ returns the list of characters
-- corresponding to the unread portion of the channel or file managed
-- by @hdl@, which is immediate closed.
--
-- Items are read strictly from the input Handle.
--
-- This operation may fail with:
--
-- * 'isEOFError' if the end of file has been reached.
hGetContents :: IO.Handle -> IO.IO String
hGetContents h = IO.hGetContents h >>= \s -> length s `seq` return s
-- -----------------------------------------------------------------------------
-- Standard IO
-- | The 'getContents' operation returns all user input as a single string,
-- which is read stirctly (same as 'hGetContents' 'stdin').
getContents :: IO String
getContents = hGetContents IO.stdin
{-# INLINE getContents #-}
-- | The 'interact' function takes a function of type @String->String@
-- as its argument. The entire input from the standard input device is
-- passed to this function as its argument, and the resulting string is
-- output on the standard output device.
interact :: (String -> String) -> IO ()
interact f = IO.putStr . f =<< getContents
{-# INLINE interact #-}
-- | The 'readFile' function reads a file and
-- returns the contents of the file as a string.
-- The file is read strictly, as with 'getContents'.
readFile :: FilePath -> IO String
readFile name = IO.openFile name IO.ReadMode >>= hGetContents
{-# INLINE readFile #-}
|