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