From: ross Date: Mon, 19 May 2003 16:48:20 +0000 (+0000) Subject: [project @ 2003-05-19 16:48:18 by ross] X-Git-Tag: nhc98-1-18-release~646 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=0c9d467775a0a14560dbb133046eb3734fb12a60;p=ghc-base.git [project @ 2003-05-19 16:48:18 by ross] non-GHC (and non-NHC): simplistic implementation of byte array I/O. --- diff --git a/Data/Array/IO.hs b/Data/Array/IO.hs index 5e001d1..b43573d 100644 --- a/Data/Array/IO.hs +++ b/Data/Array/IO.hs @@ -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 @@ -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 @@ -222,4 +224,37 @@ 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 + | count < 0 || count > rangeSize (bounds arr) + = illegalBufferSize handle "hGetArray" count + | otherwise = 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 + | count < 0 || count > rangeSize (bounds arr) + = illegalBufferSize handle "hPutArray" count + | otherwise = 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__ */ diff --git a/Data/PackedString.hs b/Data/PackedString.hs index 6ab3e94..de4f392 100644 --- a/Data/PackedString.hs +++ b/Data/PackedString.hs @@ -23,7 +23,7 @@ module Data.PackedString ( packString, -- :: String -> PackedString unpackPS, -- :: PackedString -> String -#ifdef __GLASGOW_HASKELL__ +#ifndef __NHC__ -- * I\/O with @PackedString@s hPutPS, -- :: Handle -> PackedString -> IO () hGetPS, -- :: Handle -> Int -> IO PackedString @@ -260,11 +260,10 @@ first_pos_that_satisfies pred ps len n = substrPS :: PackedString -> Int -> Int -> PackedString substrPS (PS ps) begin end = packString [ ps ! i | i <- [begin..end] ] -#ifdef __GLASGOW_HASKELL__ -- ----------------------------------------------------------------------------- -- hPutPS --- | Outputs a 'PackedString' to the specified 'Handle' (GHC only). +-- | Outputs a 'PackedString' to the specified 'Handle'. -- -- NOTE: the representation of the 'PackedString' in the file is assumed to -- be in the ISO-8859-1 encoding. In other words, only the least signficant @@ -279,7 +278,7 @@ hPutPS h (PS ps) = do -- ----------------------------------------------------------------------------- -- hGetPS --- | Read a 'PackedString' directly from the specified 'Handle' (GHC only). +-- | Read a 'PackedString' directly from the specified 'Handle'. -- This is far more efficient than reading the characters into a 'String' -- and then using 'packString'. -- @@ -291,7 +290,6 @@ hGetPS h i = do l <- hGetArray h arr i chars <- mapM (\i -> readArray arr i >>= return.chr.fromIntegral) [0..l-1] return (packString chars) -#endif /* __GLASGOW_HASKELL__ */ #else /* __NHC__ */