packages feed

MicroHs-0.17.4.0 / lib / System / IO / Internal.hs

module System.IO.Internal(  FilePath,  BFILE,  Handle, HandleState(..),  IOMode(..), ioModeToHMode,  mkHandle, unsafeHandle,  withHandleRd, withHandleWr, withHandleAny,  getHandleState, setHandleState,  killHandle,  addTransducer) whereimport qualified Prelude()import Primitivesimport Control.Applicativeimport Control.Errorimport Control.Monadimport Control.Monad.Failimport Data.Boolimport Data.Charimport Data.Enumimport Data.Eqimport Data.Functionimport Data.IORefimport Data.Listimport Data.Ordimport {-# SOURCE #-} Data.Typeable--import Foreign.ForeignPtr  causes import cycleimport Foreign.Ptrimport System.IO_Handleimport System.IO.Unsafeimport Text.Showinstance Eq Handle where  h == h'  =    unsafePerformIO $    withHandleAny h $ \ p ->    withHandleAny h' $ \ p' ->    pure (p == p')instance Show Handle where  show (Handle _ _ s) = "Handle(" ++ s ++ ")"-----type FilePath = Stringdata IOMode = ReadMode | WriteMode | AppendMode | ReadWriteMode  deriving (Eq, Ord, Enum, Show)ioModeToHMode :: IOMode -> HandleStateioModeToHMode ReadMode = HReadioModeToHMode WriteMode = HWriteioModeToHMode AppendMode = HWriteioModeToHMode ReadWriteMode = HReadWriteunsafeHandle :: ForeignPtr BFILE -> HandleState -> String -> HandleunsafeHandle fp m desc = unsafePerformIO $ do  st <- newIORef m  return (Handle fp st desc)withHandle :: Handle -> (Ptr BFILE -> IO a) -> IO awithHandle (Handle fp _ _) io = do  a <- io (primForeignPtrToPtr fp)  seq fp (return a)  -- hold on to fp so it's not gc():edwithHandleM :: [HandleState] -> Handle -> (Ptr BFILE -> IO a) -> IO awithHandleM ms h@(Handle _ st _) io = do  m <- readIORef st  unless (m `elem` ms) (error "Bad Handle mode")  withHandle h iowithHandleRd :: Handle -> (Ptr BFILE -> IO a) -> IO awithHandleRd = withHandleM [HRead, HReadWrite]withHandleWr :: Handle -> (Ptr BFILE -> IO a) -> IO awithHandleWr = withHandleM [HWrite, HReadWrite]withHandleAny :: Handle -> (Ptr BFILE -> IO a) -> IO awithHandleAny = withHandlegetHandleState :: Handle -> IO HandleStategetHandleState (Handle _ st _) = readIORef stsetHandleState :: Handle -> HandleState -> IO ()setHandleState (Handle _ st _) m = writeIORef st mforeign import ccall "&closeb" c_close :: FunPtr (Ptr BFILE -> IO ()){-primSetDesc :: [Char] -> ForeignPtr BFILE -> IO ()primSetDesc = _primitive "fpstr"-}-- Create a Handle with the appropriate finalizer.mkHandle :: [Char] -> Ptr BFILE -> HandleState -> IO HandlemkHandle desc p mode = do  fp <- primNewForeignPtr p  primAddFinalizer c_close fp{-  primSetDesc desc fp-}  rmode <- newIORef mode  return (Handle fp rmode desc)-- When a handle is closed, we must remove the c_close finalizer.killHandle :: Handle -> IO ()killHandle (Handle fp _ _) =  primAddFinalizer (primIntToFunPtr (0::Int)) fpaddTransducer :: (Ptr BFILE -> IO (Ptr BFILE)) -> Handle -> IO HandleaddTransducer trans h@(Handle _ st desc) =  withHandle h $ \ p -> do        -- unwrap handle    p' <- trans p                 -- add transducer    mode <- readIORef st    killHandle h                  -- old handle should not be finalized,    mkHandle ("trans-" ++ desc) p' mode         --   because the new handle will do that----------------------------------------instance Functor IO where  fmap f ioa   = ioa `primBind` \ a -> primReturn (f a)instance Applicative IO where  pure         = primReturn  (*>)         = primThen  (<*>)        = apinstance Monad IO where  (>>=)        = primBind  (>>)         = primTheninstance MonadFail IO where  fail         = error