Remove Control.Parallel*, now in package parallel
[haskell-directory.git] / Data / Array / IO.hs
index 5e001d1..1231683 100644 (file)
@@ -1,4 +1,4 @@
-{-# OPTIONS -#include "HsBase.h" #-}
+{-# OPTIONS_GHC -#include "HsBase.h" #-}
 -----------------------------------------------------------------------------
 -- |
 -- Module      :  Data.Array.IO
@@ -7,7 +7,7 @@
 -- 
 -- Maintainer  :  libraries@haskell.org
 -- Stability   :  experimental
--- Portability :  non-portable
+-- Portability :  non-portable (uses Data.Array.MArray)
 --
 -- Mutable boxed and unboxed arrays in the IO monad.
 --
@@ -24,15 +24,14 @@ module Data.Array.IO (
    -- * Overloaded mutable array interface
    module Data.Array.MArray,
 
-#ifdef __GLASGOW_HASKELL__
    -- * Doing I\/O with @IOUArray@s
    hGetArray,          -- :: Handle -> IOUArray Int Word8 -> Int -> IO Int
    hPutArray,          -- :: Handle -> IOUArray Int Word8 -> Int -> IO ()
-#endif
  ) where
 
 import Prelude
 
+import Data.Array.Base
 import Data.Array.IO.Internals
 import Data.Array              ( Array )
 import Data.Array.MArray
@@ -42,11 +41,14 @@ import Data.Word
 #ifdef __GLASGOW_HASKELL__
 import Foreign
 import Foreign.C
-import Data.Array.Base
 
 import GHC.Arr
 import GHC.IOBase
 import GHC.Handle
+#else
+import Data.Char
+import System.IO
+import System.IO.Error
 #endif
 
 #ifdef __GLASGOW_HASKELL__
@@ -116,7 +118,7 @@ unsafeThawIOUArray arr = stToIO $ do
 -- hGetArray
 
 -- | Reads a number of 'Word8's from the specified 'Handle' directly
--- into an array (GHC only).
+-- into an array.
 hGetArray
        :: Handle               -- ^ Handle to read from
        -> IOUArray Int Word8   -- ^ Array in which to place the values
@@ -141,11 +143,11 @@ hGetArray handle (IOUArray (STUArray l u ptr)) count
                let avail = w - r
                copied <- if (count >= avail)
                            then do 
-                               memcpy_ba_baoff ptr raw r (fromIntegral avail)
+                               memcpy_ba_baoff ptr raw (fromIntegral r) (fromIntegral avail)
                                writeIORef ref buf{ bufWPtr=0, bufRPtr=0 }
                                return avail
                            else do 
-                               memcpy_ba_baoff ptr raw r (fromIntegral count)
+                               memcpy_ba_baoff ptr raw (fromIntegral r) (fromIntegral count)
                                writeIORef ref buf{ bufRPtr = r + count }
                                return count
 
@@ -171,7 +173,7 @@ readChunk fd is_stream ptr init_off bytes = loop init_off bytes
 -- ---------------------------------------------------------------------------
 -- hPutArray
 
--- | Writes an array of 'Word8' to the specified 'Handle' (GHC only).
+-- | Writes an array of 'Word8' to the specified 'Handle'.
 hPutArray
        :: Handle                       -- ^ Handle to write to
        -> IOUArray Int Word8           -- ^ Array to write from
@@ -194,7 +196,7 @@ hPutArray handle (IOUArray (STUArray l u raw)) count
           if (size - w > count)
                -- There's enough room in the buffer:
                -- just copy the data in and update bufWPtr.
-           then do memcpy_baoff_ba old_raw w raw (fromIntegral count)
+           then do memcpy_baoff_ba old_raw (fromIntegral w) raw (fromIntegral count)
                    writeIORef ref old_buf{ bufWPtr = w + count }
                    return ()
 
@@ -211,9 +213,9 @@ hPutArray handle (IOUArray (STUArray l u raw)) count
 -- Internal Utils
 
 foreign import ccall unsafe "__hscore_memcpy_dst_off"
-   memcpy_baoff_ba :: RawBuffer -> Int -> RawBuffer -> CSize -> IO (Ptr ())
+   memcpy_baoff_ba :: RawBuffer -> CInt -> RawBuffer -> CSize -> IO (Ptr ())
 foreign import ccall unsafe "__hscore_memcpy_src_off"
-   memcpy_ba_baoff :: RawBuffer -> RawBuffer -> Int -> CSize -> IO (Ptr ())
+   memcpy_ba_baoff :: RawBuffer -> RawBuffer -> CInt -> CSize -> IO (Ptr ())
 
 illegalBufferSize :: Handle -> String -> Int -> IO a
 illegalBufferSize handle fn sz = 
@@ -222,4 +224,39 @@ illegalBufferSize handle fn sz =
                            ("illegal buffer size " ++ showsPrec 9 (sz::Int) [])
                            Nothing)
 
-#endif /* __GLASGOW_HASKELL__ */
+#else /* !__GLASGOW_HASKELL__ */
+hGetArray :: Handle -> IOUArray Int Word8 -> Int -> IO Int
+hGetArray handle arr count = do
+       bds <- getBounds arr
+       if count < 0 || count > rangeSize bds
+          then illegalBufferSize handle "hGetArray" count
+          else get 0
+ where
+  get i | i == count = return i
+       | otherwise = do
+               error_or_c <- try (hGetChar handle)
+               case error_or_c of
+                   Left ex
+                       | isEOFError ex -> return i
+                       | otherwise -> ioError ex
+                   Right c -> do
+                       unsafeWrite arr i (fromIntegral (ord c))
+                       get (i+1)
+
+hPutArray :: Handle -> IOUArray Int Word8 -> Int -> IO ()
+hPutArray handle arr count = do
+       bds <- getBounds arr
+       if count < 0 || count > rangeSize bds
+          then illegalBufferSize handle "hPutArray" count
+          else put 0
+ where
+  put i | i == count = return ()
+       | otherwise = do
+               w <- unsafeRead arr i
+               hPutChar handle (chr (fromIntegral w))
+               put (i+1)
+
+illegalBufferSize :: Handle -> String -> Int -> IO a
+illegalBufferSize _ fn sz = ioError $
+       userError (fn ++ ": illegal buffer size " ++ showsPrec 9 (sz::Int) [])
+#endif /* !__GLASGOW_HASKELL__ */