[project @ 2002-10-14 10:06:28 by ross]
[ghc-base.git] / System / IO / Error.hs
index 357d50c..9ac35a0 100644 (file)
@@ -4,7 +4,7 @@
 -- |
 -- Module      :  System.IO.Error
 -- Copyright   :  (c) The University of Glasgow 2001
--- License     :  BSD-style (see the file libraries/core/LICENSE)
+-- License     :  BSD-style (see the file libraries/base/LICENSE)
 -- 
 -- Maintainer  :  libraries@haskell.org
 -- Stability   :  provisional
 
 module System.IO.Error (
     IOError,                   -- abstract
+#ifdef __GLASGOW_HASKELL__
     IOErrorType,               -- abstract
+#endif
 
     ioError,                   -- :: IOError -> IO a
     userError,                 -- :: String  -> IOError
 
+#ifdef __GLASGOW_HASKELL__
     mkIOError,                 -- :: IOErrorType -> String -> Maybe Handle
                                --    -> Maybe FilePath -> IOError
 
@@ -41,6 +44,7 @@ module System.IO.Error (
     isIllegalOperationErrorType, 
     isPermissionErrorType,
     isUserErrorType, 
+#endif  /* __GLASGOW_HASKELL__ */
 
     isAlreadyExistsError,      -- :: IOError -> Bool
     isDoesNotExistError,
@@ -51,7 +55,9 @@ module System.IO.Error (
     isPermissionError,
     isUserError,
 
+#ifdef __GLASGOW_HASKELL__
     ioeGetErrorType,           -- :: IOError -> IOErrorType
+#endif
     ioeGetErrorString,         -- :: IOError -> String
     ioeGetHandle,              -- :: IOError -> Maybe Handle
     ioeGetFileName,            -- :: IOError -> Maybe FilePath
@@ -66,6 +72,32 @@ import GHC.IOBase
 import Text.Show
 #endif
 
+#ifdef __HUGS__
+import Hugs.IO
+#endif
+
+#ifdef __NHC__
+import IO
+  ( IOError ()
+  , ioError
+  , userError
+  , isAlreadyExistsError       -- :: IOError -> Bool
+  , isDoesNotExistError
+  , isAlreadyInUseError
+  , isFullError
+  , isEOFError
+  , isIllegalOperation
+  , isPermissionError
+  , isUserError
+  , ioeGetErrorString           -- :: IOError -> String
+  , ioeGetHandle                -- :: IOError -> Maybe Handle
+  , ioeGetFileName              -- :: IOError -> Maybe FilePath
+  )
+--import Data.Maybe (fromJust)
+--import Control.Monad (MonadPlus(mplus))
+#endif
+
+#ifdef __GLASGOW_HASKELL__
 -- -----------------------------------------------------------------------------
 -- Constructing an IOError
 
@@ -77,6 +109,21 @@ mkIOError t location maybe_hdl maybe_filename =
                        ioe_handle = maybe_hdl, 
                        ioe_filename = maybe_filename
                        }
+#ifdef __NHC__
+mkIOError EOF       location maybe_hdl maybe_filename =
+    EOFError location (fromJust maybe_hdl)
+mkIOError UserError location maybe_hdl maybe_filename =
+    UserError location ""
+mkIOError t         location maybe_hdl maybe_filename =
+    NHC.FFI.mkIOError location maybe_filename maybe_handle (ioeTypeToInt t)
+  where
+    ioeTypeToInt AlreadyExists     = fromEnum EEXIST
+    ioeTypeToInt NoSuchThing       = fromEnum ENOENT
+    ioeTypeToInt ResourceBusy      = fromEnum EBUSY
+    ioeTypeToInt ResourceExhausted = fromEnum ENOSPC
+    ioeTypeToInt IllegalOperation  = fromEnum EPERM
+    ioeTypeToInt PermissionDenied  = fromEnum EACCES
+#endif
 
 -- -----------------------------------------------------------------------------
 -- IOErrorType
@@ -93,10 +140,17 @@ isEOFError           = isEOFErrorType              . ioeGetErrorType
 isIllegalOperation   = isIllegalOperationErrorType . ioeGetErrorType
 isPermissionError    = isPermissionErrorType       . ioeGetErrorType
 isUserError          = isUserErrorType             . ioeGetErrorType
+#endif
 
 -- -----------------------------------------------------------------------------
 -- IOErrorTypes
 
+#ifdef __NHC__
+data IOErrorType = AlreadyExists | NoSuchThing | ResourceBusy
+                | ResourceExhausted | EOF | IllegalOperation
+                | PermissionDenied | UserError
+#endif
+
 #ifdef __GLASGOW_HASKELL__
 alreadyExistsErrorType, doesNotExistErrorType, alreadyInUseErrorType,
  fullErrorType, eofErrorType, illegalOperationErrorType,
@@ -115,9 +169,11 @@ userErrorType                = UserError
 -- -----------------------------------------------------------------------------
 -- IOErrorType predicates
 
+#ifdef __GLASGOW_HASKELL__
 isAlreadyExistsErrorType, isDoesNotExistErrorType, isAlreadyInUseErrorType,
   isFullErrorType, isEOFErrorType, isIllegalOperationErrorType, 
   isPermissionErrorType, isUserErrorType :: IOErrorType -> Bool
+#endif
 
 #ifdef __GLASGOW_HASKELL__
 isAlreadyExistsErrorType AlreadyExists = True
@@ -155,13 +211,13 @@ ioeGetErrorString     :: IOError -> String
 ioeGetFileName        :: IOError -> Maybe FilePath
 
 ioeGetErrorType (IOException ioe) = ioe_type ioe
-ioeGetErrorType _ = error "System.IO.Error.ioeGetHandle: not an IO error"
+ioeGetErrorType _ = error "System.IO.Error.ioeGetErrorType: not an IO error"
 
 ioeGetHandle (IOException ioe) = ioe_handle ioe
 ioeGetHandle _ = error "System.IO.Error.ioeGetHandle: not an IO error"
 
 ioeGetErrorString (IOException ioe) 
-   | isUserErrorType (ioe_type ioe) = show (ioe_descr ioe)
+   | isUserErrorType (ioe_type ioe) = ioe_descr ioe
    | otherwise                      = show (ioe_type ioe)
 ioeGetErrorString _ = error "System.IO.Error.ioeGetErrorString: not an IO error"
 
@@ -186,3 +242,14 @@ annotateIOError (IOException (IOError hdl errTy _ str path)) loc opath ohdl =
 annotateIOError exc _ _ _ = 
   exc
 #endif
+
+#ifdef 0 /*__NHC__*/
+annotateIOError (IOError msg file hdl code) msg' file' hdl' =
+    IOError (msg++'\n':msg') (file`mplus`file') (hdl`mplus`hdl') code
+annotateIOError (EOFError msg hdl) msg' file' hdl' =
+    EOFError (msg++'\n':msg') (hdl`mplus`hdl')
+annotateIOError (UserError loc msg) msg' file' hdl' =
+    UserError loc (msg++'\n':msg')
+annotateIOError (PatternError loc) msg' file' hdl' =
+    PatternError (loc++'\n':msg')
+#endif