[project @ 2003-03-04 11:09:15 by simonmar]
[ghc-base.git] / Data / Array / IO.hs
1 {-# OPTIONS -#include "HsBase.h" #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  Data.Array.IO
5 -- Copyright   :  (c) The University of Glasgow 2001
6 -- License     :  BSD-style (see the file libraries/base/LICENSE)
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  experimental
10 -- Portability :  non-portable
11 --
12 -- Mutable boxed and unboxed arrays in the IO monad.
13 --
14 -----------------------------------------------------------------------------
15
16 module Data.Array.IO (
17    -- * @IO@ arrays with boxed elements
18    IOArray,             -- instance of: Eq, Typeable
19
20    -- * @IO@ arrays with unboxed elements
21    IOUArray,            -- instance of: Eq, Typeable
22    castIOUArray,        -- :: IOUArray i a -> IO (IOUArray i b)
23
24    -- * Overloaded mutable array interface
25    module Data.Array.MArray,
26
27 #ifdef __GLASGOW_HASKELL__
28    -- * Doing I\/O with @IOUArray@s
29    hGetArray,           -- :: Handle -> IOUArray Int Word8 -> Int -> IO Int
30    hPutArray,           -- :: Handle -> IOUArray Int Word8 -> Int -> IO ()
31 #endif
32  ) where
33
34 import Prelude
35
36 import Data.Array.IO.Internals
37 import Data.Array               ( Array )
38 import Data.Array.MArray
39 import Data.Int
40 import Data.Word
41
42 #ifdef __GLASGOW_HASKELL__
43 import Foreign
44 import Foreign.C
45 import Data.Array.Base
46
47 import GHC.Arr
48 import GHC.ST           ( ST(..) )
49 import GHC.IOBase
50 import GHC.Handle
51 #endif
52
53 #ifdef __GLASGOW_HASKELL__
54 -----------------------------------------------------------------------------
55 -- Freezing
56
57 freezeIOArray :: Ix ix => IOArray ix e -> IO (Array ix e)
58 freezeIOArray (IOArray marr) = stToIO (freezeSTArray marr)
59
60 freezeIOUArray :: Ix ix => IOUArray ix e -> IO (UArray ix e)
61 freezeIOUArray (IOUArray marr) = stToIO (freezeSTUArray marr)
62
63 {-# RULES
64 "freeze/IOArray"  freeze = freezeIOArray
65 "freeze/IOUArray" freeze = freezeIOUArray
66     #-}
67
68 {-# INLINE unsafeFreezeIOArray #-}
69 unsafeFreezeIOArray :: Ix ix => IOArray ix e -> IO (Array ix e)
70 unsafeFreezeIOArray (IOArray marr) = stToIO (unsafeFreezeSTArray marr)
71
72 {-# INLINE unsafeFreezeIOUArray #-}
73 unsafeFreezeIOUArray :: Ix ix => IOUArray ix e -> IO (UArray ix e)
74 unsafeFreezeIOUArray (IOUArray marr) = stToIO (unsafeFreezeSTUArray marr)
75
76 {-# RULES
77 "unsafeFreeze/IOArray"  unsafeFreeze = unsafeFreezeIOArray
78 "unsafeFreeze/IOUArray" unsafeFreeze = unsafeFreezeIOUArray
79     #-}
80
81 -----------------------------------------------------------------------------
82 -- Thawing
83
84 thawIOArray :: Ix ix => Array ix e -> IO (IOArray ix e)
85 thawIOArray arr = stToIO $ do
86     marr <- thawSTArray arr
87     return (IOArray marr)
88
89 thawIOUArray :: Ix ix => UArray ix e -> IO (IOUArray ix e)
90 thawIOUArray arr = stToIO $ do
91     marr <- thawSTUArray arr
92     return (IOUArray marr)
93
94 {-# RULES
95 "thaw/IOArray"  thaw = thawIOArray
96 "thaw/IOUArray" thaw = thawIOUArray
97     #-}
98
99 {-# INLINE unsafeThawIOArray #-}
100 unsafeThawIOArray :: Ix ix => Array ix e -> IO (IOArray ix e)
101 unsafeThawIOArray arr = stToIO $ do
102     marr <- unsafeThawSTArray arr
103     return (IOArray marr)
104
105 {-# INLINE unsafeThawIOUArray #-}
106 unsafeThawIOUArray :: Ix ix => UArray ix e -> IO (IOUArray ix e)
107 unsafeThawIOUArray arr = stToIO $ do
108     marr <- unsafeThawSTUArray arr
109     return (IOUArray marr)
110
111 {-# RULES
112 "unsafeThaw/IOArray"  unsafeThaw = unsafeThawIOArray
113 "unsafeThaw/IOUArray" unsafeThaw = unsafeThawIOUArray
114     #-}
115
116 castSTUArray :: STUArray s ix a -> ST s (STUArray s ix b)
117 castSTUArray (STUArray l u marr#) = return (STUArray l u marr#)
118
119 -- | Casts an 'IOUArray' with one element type into one with a
120 -- different element type.  All the elements of the resulting array
121 -- are undefined (unless you know what you\'re doing...).
122 castIOUArray :: IOUArray ix a -> IO (IOUArray ix b)
123 castIOUArray (IOUArray marr) = stToIO $ do
124     marr' <- castSTUArray marr
125     return (IOUArray marr')
126
127 -- ---------------------------------------------------------------------------
128 -- hGetArray
129
130 -- | Reads a number of 'Word8's from the specified 'Handle' directly
131 -- into an array.
132 hGetArray
133         :: Handle               -- ^ Handle to read from
134         -> IOUArray Int Word8   -- ^ Array in which to place the values
135         -> Int                  -- ^ Number of 'Word8's to read
136         -> IO Int
137                 -- ^ Returns: the number of 'Word8's actually 
138                 -- read, which might be smaller than the number requested
139                 -- if the end of file was reached.
140
141 hGetArray handle (IOUArray (STUArray l u ptr)) count
142   | count <= 0 || count > rangeSize (l,u)
143   = illegalBufferSize handle "hGetArray" count
144   | otherwise = do
145       wantReadableHandle "hGetArray" handle $ 
146         \ handle_@Handle__{ haFD=fd, haBuffer=ref, haIsStream=is_stream } -> do
147         buf@Buffer{ bufBuf=raw, bufWPtr=w, bufRPtr=r } <- readIORef ref
148         if bufferEmpty buf
149            then readChunk fd is_stream ptr 0 count
150            else do 
151                 let avail = w - r
152                 copied <- if (count >= avail)
153                             then do 
154                                 memcpy_ba_baoff ptr raw r (fromIntegral avail)
155                                 writeIORef ref buf{ bufWPtr=0, bufRPtr=0 }
156                                 return avail
157                             else do 
158                                 memcpy_ba_baoff ptr raw r (fromIntegral count)
159                                 writeIORef ref buf{ bufRPtr = r + count }
160                                 return count
161
162                 let remaining = count - copied
163                 if remaining > 0 
164                    then do rest <- readChunk fd is_stream ptr copied remaining
165                            return (rest + count)
166                    else return count
167
168 readChunk :: FD -> Bool -> RawBuffer -> Int -> Int -> IO Int
169 readChunk fd is_stream ptr init_off bytes = loop init_off bytes 
170  where
171   loop :: Int -> Int -> IO Int
172   loop off bytes | bytes <= 0 = return (off - init_off)
173   loop off bytes = do
174     r' <- readRawBuffer "readChunk" (fromIntegral fd) is_stream ptr
175                                     (fromIntegral off) (fromIntegral bytes)
176     let r = fromIntegral r'
177     if r == 0
178         then return (off - init_off)
179         else loop (off + r) (bytes - r)
180
181 -- ---------------------------------------------------------------------------
182 -- hPutArray
183
184 -- | Writes an array of 'Word8' to the specified 'Handle'.
185 hPutArray
186         :: Handle                       -- ^ Handle to write to
187         -> IOUArray Int Word8           -- ^ Array to write from
188         -> Int                          -- ^ Number of 'Word8's to write
189         -> IO ()
190
191 hPutArray handle (IOUArray (STUArray l u raw)) count
192   | count <= 0 || count > rangeSize (l,u)
193   = illegalBufferSize handle "hPutArray" count
194   | otherwise
195    = do wantWritableHandle "hPutArray" handle $ 
196           \ handle_@Handle__{ haFD=fd, haBuffer=ref, haIsStream=stream } -> do
197
198           old_buf@Buffer{ bufBuf=old_raw, bufRPtr=r, bufWPtr=w, bufSize=size }
199             <- readIORef ref
200
201           -- enough room in handle buffer?
202           if (size - w > count)
203                 -- There's enough room in the buffer:
204                 -- just copy the data in and update bufWPtr.
205             then do memcpy_baoff_ba old_raw w raw (fromIntegral count)
206                     writeIORef ref old_buf{ bufWPtr = w + count }
207                     return ()
208
209                 -- else, we have to flush
210             else do flushed_buf <- flushWriteBuffer fd stream old_buf
211                     writeIORef ref flushed_buf
212                     let this_buf = 
213                             Buffer{ bufBuf=raw, bufState=WriteBuffer, 
214                                     bufRPtr=0, bufWPtr=count, bufSize=count }
215                     flushWriteBuffer fd stream this_buf
216                     return ()
217
218 -- ---------------------------------------------------------------------------
219 -- Internal Utils
220
221 foreign import ccall unsafe "__hscore_memcpy_dst_off"
222    memcpy_baoff_ba :: RawBuffer -> Int -> RawBuffer -> CSize -> IO (Ptr ())
223 foreign import ccall unsafe "__hscore_memcpy_src_off"
224    memcpy_ba_baoff :: RawBuffer -> RawBuffer -> Int -> CSize -> IO (Ptr ())
225
226 illegalBufferSize :: Handle -> String -> Int -> IO a
227 illegalBufferSize handle fn sz = 
228         ioException (IOError (Just handle)
229                             InvalidArgument  fn
230                             ("illegal buffer size " ++ showsPrec 9 (sz::Int) [])
231                             Nothing)
232
233 #endif /* __GLASGOW_HASKELL__ */