1226fcc50ea16a3866da34c2b8f1c7496dd9a7f4
[packages/old-time.git] / System / IO.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module : System.IO
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
7 --
8 -- Maintainer : libraries@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
11 --
12 -- The standard IO library.
13 --
14 -----------------------------------------------------------------------------
15
16 module System.IO (
17 Handle, -- abstract, instance of: Eq, Show.
18 HandlePosn(..), -- abstract, instance of: Eq, Show.
19
20 IOMode(ReadMode,WriteMode,AppendMode,ReadWriteMode),
21 BufferMode(NoBuffering,LineBuffering,BlockBuffering),
22 SeekMode(AbsoluteSeek,RelativeSeek,SeekFromEnd),
23
24 stdin, stdout, stderr, -- :: Handle
25
26 openFile, -- :: FilePath -> IOMode -> IO Handle
27 hClose, -- :: Handle -> IO ()
28 hFileSize, -- :: Handle -> IO Integer
29 hIsEOF, -- :: Handle -> IO Bool
30 isEOF, -- :: IO Bool
31
32 hSetBuffering, -- :: Handle -> BufferMode -> IO ()
33 hGetBuffering, -- :: Handle -> IO BufferMode
34 hFlush, -- :: Handle -> IO ()
35 hGetPosn, -- :: Handle -> IO HandlePosn
36 hSetPosn, -- :: HandlePosn -> IO ()
37 hSeek, -- :: Handle -> SeekMode -> Integer -> IO ()
38 hTell, -- :: Handle -> IO Integer
39 hWaitForInput, -- :: Handle -> Int -> IO Bool
40 hReady, -- :: Handle -> IO Bool
41 hGetChar, -- :: Handle -> IO Char
42 hGetLine, -- :: Handle -> IO [Char]
43 hLookAhead, -- :: Handle -> IO Char
44 hGetContents, -- :: Handle -> IO [Char]
45 hPutChar, -- :: Handle -> Char -> IO ()
46 hPutStr, -- :: Handle -> [Char] -> IO ()
47 hPutStrLn, -- :: Handle -> [Char] -> IO ()
48 hPrint, -- :: Show a => Handle -> a -> IO ()
49 hIsOpen, hIsClosed, -- :: Handle -> IO Bool
50 hIsReadable, hIsWritable, -- :: Handle -> IO Bool
51 hIsSeekable, -- :: Handle -> IO Bool
52
53 isAlreadyExistsError, isDoesNotExistError, -- :: IOError -> Bool
54 isAlreadyInUseError, isFullError,
55 isEOFError, isIllegalOperation,
56 isPermissionError, isUserError,
57
58 ioeGetErrorString, -- :: IOError -> String
59 ioeGetHandle, -- :: IOError -> Maybe Handle
60 ioeGetFileName, -- :: IOError -> Maybe FilePath
61
62 try, -- :: IO a -> IO (Either IOError a)
63 bracket, -- :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
64 bracket_, -- :: IO a -> (a -> IO b) -> IO c -> IO c
65
66 -- Non-standard extension (but will hopefully become standard with 1.5) is
67 -- to export the Prelude io functions via IO (in addition to exporting them
68 -- from the prelude...for now.)
69 IO, -- instance MonadFix
70 FilePath, -- :: String
71 IOError,
72 ioError, -- :: IOError -> IO a
73 userError, -- :: String -> IOError
74 catch, -- :: IO a -> (IOError -> IO a) -> IO a
75 interact, -- :: (String -> String) -> IO ()
76
77 putChar, -- :: Char -> IO ()
78 putStr, -- :: String -> IO ()
79 putStrLn, -- :: String -> IO ()
80 print, -- :: Show a => a -> IO ()
81 getChar, -- :: IO Char
82 getLine, -- :: IO String
83 getContents, -- :: IO String
84 readFile, -- :: FilePath -> IO String
85 writeFile, -- :: FilePath -> String -> IO ()
86 appendFile, -- :: FilePath -> String -> IO ()
87 readIO, -- :: Read a => String -> IO a
88 readLn, -- :: Read a => IO a
89
90 #ifndef __HUGS__
91 hPutBuf, -- :: Handle -> Ptr a -> Int -> IO ()
92 hGetBuf, -- :: Handle -> Ptr a -> Int -> IO Int
93 #endif
94
95 fixIO, -- :: (a -> IO a) -> IO a
96
97 #ifndef __HUGS__
98 hSetEcho, -- :: Handle -> Bool -> IO ()
99 hGetEcho, -- :: Handle -> IO Bool
100
101 hIsTerminalDevice, -- :: Handle -> IO Bool
102 #endif
103 ) where
104
105 #ifdef __GLASGOW_HASKELL__
106 import GHC.Base
107 import GHC.IOBase -- Together these four Prelude modules define
108 import GHC.Handle -- all the stuff exported by IO for the GHC version
109 import GHC.IO
110 import GHC.ST ( fixST )
111 import GHC.Exception
112 import GHC.Num
113 import GHC.Read
114 import GHC.Show
115 #endif
116
117 #ifdef __HUGS__
118 import Hugs.IO
119 import Hugs.IOExts
120 #endif
121
122 import System.IO.Error
123
124 -- -----------------------------------------------------------------------------
125 -- Standard IO
126
127 #ifndef __HUGS__
128 putChar :: Char -> IO ()
129 putChar c = hPutChar stdout c
130
131 putStr :: String -> IO ()
132 putStr s = hPutStr stdout s
133
134 putStrLn :: String -> IO ()
135 putStrLn s = do putStr s
136 putChar '\n'
137
138 print :: Show a => a -> IO ()
139 print x = putStrLn (show x)
140
141 getChar :: IO Char
142 getChar = hGetChar stdin
143
144 getLine :: IO String
145 getLine = hGetLine stdin
146
147 getContents :: IO String
148 getContents = hGetContents stdin
149
150 interact :: (String -> String) -> IO ()
151 interact f = do s <- getContents
152 putStr (f s)
153
154 readFile :: FilePath -> IO String
155 readFile name = openFile name ReadMode >>= hGetContents
156
157 writeFile :: FilePath -> String -> IO ()
158 writeFile name str = do
159 hdl <- openFile name WriteMode
160 hPutStr hdl str
161 hClose hdl
162
163 appendFile :: FilePath -> String -> IO ()
164 appendFile name str = do
165 hdl <- openFile name AppendMode
166 hPutStr hdl str
167 hClose hdl
168
169 readLn :: Read a => IO a
170 readLn = do l <- getLine
171 r <- readIO l
172 return r
173
174 -- raises an exception instead of an error
175 readIO :: Read a => String -> IO a
176 readIO s = case (do { (x,t) <- reads s ;
177 ("","") <- lex t ;
178 return x }) of
179 [x] -> return x
180 [] -> ioError (userError "Prelude.readIO: no parse")
181 _ -> ioError (userError "Prelude.readIO: ambiguous parse")
182 #endif /* __HUGS__ */
183
184 hReady :: Handle -> IO Bool
185 hReady h = hWaitForInput h 0
186
187 hPutStrLn :: Handle -> String -> IO ()
188 hPutStrLn hndl str = do
189 hPutStr hndl str
190 hPutChar hndl '\n'
191
192 hPrint :: Show a => Handle -> a -> IO ()
193 hPrint hdl = hPutStrLn hdl . show
194
195 -- ---------------------------------------------------------------------------
196 -- fixIO
197
198 #ifdef __GLASGOW_HASKELL__
199 fixIO :: (a -> IO a) -> IO a
200 fixIO m = stToIO (fixST (ioToST . m))
201 #endif