[project @ 2002-07-16 16:08:58 by ross]
[ghc-base.git] / System / IO / Error.hs
1 {-# OPTIONS -fno-implicit-prelude #-}
2
3 -----------------------------------------------------------------------------
4 -- |
5 -- Module      :  System.IO.Error
6 -- Copyright   :  (c) The University of Glasgow 2001
7 -- License     :  BSD-style (see the file libraries/base/LICENSE)
8 -- 
9 -- Maintainer  :  libraries@haskell.org
10 -- Stability   :  provisional
11 -- Portability :  portable
12 --
13 -- Standard IO Errors.
14 --
15 -----------------------------------------------------------------------------
16
17 module System.IO.Error (
18     IOError,                    -- abstract
19 #ifndef __HUGS__
20     IOErrorType,                -- abstract
21 #endif
22
23     ioError,                    -- :: IOError -> IO a
24     userError,                  -- :: String  -> IOError
25
26 #ifndef __HUGS__
27     mkIOError,                  -- :: IOErrorType -> String -> Maybe Handle
28                                 --    -> Maybe FilePath -> IOError
29
30     alreadyExistsErrorType,     -- :: IOErrorType
31     doesNotExistErrorType,
32     alreadyInUseErrorType,
33     fullErrorType,
34     eofErrorType,
35     illegalOperationErrorType, 
36     permissionErrorType,
37     userErrorType,
38
39     isAlreadyExistsErrorType,   -- :: IOErrorType -> Bool
40     isDoesNotExistErrorType,
41     isAlreadyInUseErrorType,
42     isFullErrorType, 
43     isEOFErrorType,
44     isIllegalOperationErrorType, 
45     isPermissionErrorType,
46     isUserErrorType, 
47 #endif  /* __HUGS__ */
48
49     isAlreadyExistsError,       -- :: IOError -> Bool
50     isDoesNotExistError,
51     isAlreadyInUseError,
52     isFullError, 
53     isEOFError,
54     isIllegalOperation, 
55     isPermissionError,
56     isUserError,
57
58 #ifndef __HUGS__
59     ioeGetErrorType,            -- :: IOError -> IOErrorType
60 #endif
61     ioeGetErrorString,          -- :: IOError -> String
62     ioeGetHandle,               -- :: IOError -> Maybe Handle
63     ioeGetFileName,             -- :: IOError -> Maybe FilePath
64
65   ) where
66
67
68 #ifdef __GLASGOW_HASKELL__
69 import GHC.Base
70 import Data.Maybe
71 import GHC.IOBase
72 import Text.Show
73 #endif
74
75 #ifdef __HUGS__
76 import Hugs.IO
77 #endif
78
79 #ifndef __HUGS__
80 -- -----------------------------------------------------------------------------
81 -- Constructing an IOError
82
83 mkIOError :: IOErrorType -> String -> Maybe Handle -> Maybe FilePath -> IOError
84 mkIOError t location maybe_hdl maybe_filename =
85    IOException IOError{ ioe_type = t, 
86                         ioe_location = location,
87                         ioe_descr = "",
88                         ioe_handle = maybe_hdl, 
89                         ioe_filename = maybe_filename
90                         }
91
92 -- -----------------------------------------------------------------------------
93 -- IOErrorType
94
95 isAlreadyExistsError, isDoesNotExistError, isAlreadyInUseError,
96  isFullError, isEOFError, isIllegalOperation, isPermissionError,
97  isUserError :: IOError -> Bool
98
99 isAlreadyExistsError = isAlreadyExistsErrorType    . ioeGetErrorType
100 isDoesNotExistError  = isDoesNotExistErrorType     . ioeGetErrorType
101 isAlreadyInUseError  = isAlreadyInUseErrorType     . ioeGetErrorType
102 isFullError          = isFullErrorType             . ioeGetErrorType
103 isEOFError           = isEOFErrorType              . ioeGetErrorType
104 isIllegalOperation   = isIllegalOperationErrorType . ioeGetErrorType
105 isPermissionError    = isPermissionErrorType       . ioeGetErrorType
106 isUserError          = isUserErrorType             . ioeGetErrorType
107 #endif
108
109 -- -----------------------------------------------------------------------------
110 -- IOErrorTypes
111
112 #ifdef __GLASGOW_HASKELL__
113 alreadyExistsErrorType, doesNotExistErrorType, alreadyInUseErrorType,
114  fullErrorType, eofErrorType, illegalOperationErrorType,
115  permissionErrorType, userErrorType :: IOErrorType
116
117 alreadyExistsErrorType    = AlreadyExists
118 doesNotExistErrorType     = NoSuchThing
119 alreadyInUseErrorType     = ResourceBusy
120 fullErrorType             = ResourceExhausted
121 eofErrorType              = EOF
122 illegalOperationErrorType = IllegalOperation
123 permissionErrorType       = PermissionDenied
124 userErrorType             = UserError
125 #endif
126
127 -- -----------------------------------------------------------------------------
128 -- IOErrorType predicates
129
130 #ifndef __HUGS__
131 isAlreadyExistsErrorType, isDoesNotExistErrorType, isAlreadyInUseErrorType,
132   isFullErrorType, isEOFErrorType, isIllegalOperationErrorType, 
133   isPermissionErrorType, isUserErrorType :: IOErrorType -> Bool
134 #endif
135
136 #ifdef __GLASGOW_HASKELL__
137 isAlreadyExistsErrorType AlreadyExists = True
138 isAlreadyExistsErrorType _ = False
139
140 isDoesNotExistErrorType NoSuchThing = True
141 isDoesNotExistErrorType _ = False
142
143 isAlreadyInUseErrorType ResourceBusy = True
144 isAlreadyInUseErrorType _ = False
145
146 isFullErrorType ResourceExhausted = True
147 isFullErrorType _ = False
148
149 isEOFErrorType EOF = True
150 isEOFErrorType _ = False
151
152 isIllegalOperationErrorType IllegalOperation = True
153 isIllegalOperationErrorType _ = False
154
155 isPermissionErrorType PermissionDenied = True
156 isPermissionErrorType _ = False
157
158 isUserErrorType UserError = True
159 isUserErrorType _ = False
160 #endif
161
162 -- -----------------------------------------------------------------------------
163 -- Miscellaneous
164
165 #ifdef __GLASGOW_HASKELL__
166 ioeGetErrorType       :: IOError -> IOErrorType
167 ioeGetHandle          :: IOError -> Maybe Handle
168 ioeGetErrorString     :: IOError -> String
169 ioeGetFileName        :: IOError -> Maybe FilePath
170
171 ioeGetErrorType (IOException ioe) = ioe_type ioe
172 ioeGetErrorType _ = error "System.IO.Error.ioeGetHandle: not an IO error"
173
174 ioeGetHandle (IOException ioe) = ioe_handle ioe
175 ioeGetHandle _ = error "System.IO.Error.ioeGetHandle: not an IO error"
176
177 ioeGetErrorString (IOException ioe) 
178    | isUserErrorType (ioe_type ioe) = show (ioe_descr ioe)
179    | otherwise                      = show (ioe_type ioe)
180 ioeGetErrorString _ = error "System.IO.Error.ioeGetErrorString: not an IO error"
181
182 ioeGetFileName (IOException ioe) = ioe_filename ioe
183 ioeGetFileName _ = error "System.IO.Error.ioeGetFileName: not an IO error"
184 #endif
185
186 -- -----------------------------------------------------------------------------
187 -- annotating an IOError
188
189 #ifdef __GLASGOW_HASKELL__
190 annotateIOError :: IOError 
191               -> String 
192               -> Maybe FilePath 
193               -> Maybe Handle 
194               -> IOError 
195 annotateIOError (IOException (IOError hdl errTy _ str path)) loc opath ohdl = 
196   IOException (IOError (hdl `mplus` ohdl) errTy loc str (path `mplus` opath))
197   where
198     Nothing `mplus` ys = ys
199     xs      `mplus` _  = xs
200 annotateIOError exc _ _ _ = 
201   exc
202 #endif