{-# LANGUAGE MagicHash, UnliftedFFITypes #-}
module Data.Array.IO (
IOArray,
IOUArray,
module Data.Array.MArray,
hGetArray,
hPutArray,
) where
import Data.Array.Base
import Data.Array.IO.Internals
import Data.Array.MArray
import System.IO.Error
import Foreign
import Foreign.C
import GHC.Exts (MutableByteArray#, RealWorld)
import GHC.IO.Handle
import GHC.IO.Exception
hGetArray
:: Handle
-> IOUArray Int Word8
-> Int
-> IO Int
hGetArray :: Handle -> IOUArray Int Word8 -> Int -> IO Int
hGetArray Handle
handle (IOUArray (STUArray Int
_l Int
_u Int
n MutableByteArray# RealWorld
ptr)) Int
count
| Int
count Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = Int -> IO Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
0
| Int
count Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
|| Int
count Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
n = Handle -> String -> Int -> IO Int
forall a. Handle -> String -> Int -> IO a
illegalBufferSize Handle
handle String
"hGetArray" Int
count
| Bool
otherwise = do
Int -> (Ptr Any -> IO Int) -> IO Int
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes Int
count ((Ptr Any -> IO Int) -> IO Int) -> (Ptr Any -> IO Int) -> IO Int
forall a b. (a -> b) -> a -> b
$ \Ptr Any
p -> do
Int
r <- Handle -> Ptr Any -> Int -> IO Int
forall a. Handle -> Ptr a -> Int -> IO Int
hGetBuf Handle
handle Ptr Any
p Int
count
Ptr ()
_ <- MutableByteArray# RealWorld -> Ptr Any -> CSize -> IO (Ptr ())
forall a.
MutableByteArray# RealWorld -> Ptr a -> CSize -> IO (Ptr ())
memcpy_ba_ptr MutableByteArray# RealWorld
ptr Ptr Any
p (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
r)
Int -> IO Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
r
foreign import ccall unsafe "memcpy"
memcpy_ba_ptr :: MutableByteArray# RealWorld -> Ptr a -> CSize -> IO (Ptr ())
hPutArray
:: Handle
-> IOUArray Int Word8
-> Int
-> IO ()
hPutArray :: Handle -> IOUArray Int Word8 -> Int -> IO ()
hPutArray Handle
handle (IOUArray (STUArray Int
_l Int
_u Int
n MutableByteArray# RealWorld
raw)) Int
count
| Int
count Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Int
count Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
|| Int
count Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
n = Handle -> String -> Int -> IO ()
forall a. Handle -> String -> Int -> IO a
illegalBufferSize Handle
handle String
"hPutArray" Int
count
| Bool
otherwise = do
Int -> (Ptr Any -> IO ()) -> IO ()
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes Int
count ((Ptr Any -> IO ()) -> IO ()) -> (Ptr Any -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Any
p -> do
Ptr ()
_ <- Ptr Any -> MutableByteArray# RealWorld -> CSize -> IO (Ptr ())
forall a.
Ptr a -> MutableByteArray# RealWorld -> CSize -> IO (Ptr ())
memcpy_ptr_ba Ptr Any
p MutableByteArray# RealWorld
raw (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
count)
Handle -> Ptr Any -> Int -> IO ()
forall a. Handle -> Ptr a -> Int -> IO ()
hPutBuf Handle
handle Ptr Any
p Int
count
foreign import ccall unsafe "memcpy"
memcpy_ptr_ba :: Ptr a -> MutableByteArray# RealWorld -> CSize -> IO (Ptr ())
illegalBufferSize :: Handle -> String -> Int -> IO a
illegalBufferSize :: forall a. Handle -> String -> Int -> IO a
illegalBufferSize Handle
handle String
fn Int
sz =
IOException -> IO a
forall a. IOException -> IO a
ioException (IOException -> String -> IOException
ioeSetErrorString
(IOErrorType
-> String -> Maybe Handle -> Maybe String -> IOException
mkIOError IOErrorType
InvalidArgument String
fn (Handle -> Maybe Handle
forall a. a -> Maybe a
Just Handle
handle) Maybe String
forall a. Maybe a
Nothing)
(String
"illegal buffer size " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> Int -> String -> String
forall a. Show a => Int -> a -> String -> String
showsPrec Int
9 (Int
sz::Int) []))