[project @ 2002-02-06 15:54:23 by simonpj]
[ghc-hetmet.git] / ghc / compiler / utils / StringBuffer.lhs
index f49449e..291fee4 100644 (file)
@@ -69,31 +69,32 @@ module StringBuffer
 
 #include "HsVersions.h"
 
-import GlaExts
+
 #if __GLASGOW_HASKELL__ < 411
 import PrelAddr        ( Addr(..) )
+import Panic           ( panic )
 #else
 import Addr            ( Addr(..) )
+import Ptr             ( Ptr(..) )
 #endif
-import Foreign
+
+#if __GLASGOW_HASKELL__ >= 501
+import PrelIO          ( hGetcBuffered )
+#else
 import Char            ( chr )
-import Panic           ( panic )
+#endif
+
+import GlaExts
+import Foreign
 
 import IO              ( openFile  )
 import IOExts          ( slurpFile )
 import PrelIOBase
 import PrelHandle
 import Addr
-#if __GLASGOW_HASKELL__ >= 411
-import Ptr             ( Ptr(..) )
-#endif
 
 import PrelPack                ( unpackCStringBA )
 
-#if __GLASGOW_HASKELL__ >= 501
-import PrelIO          ( hGetcBuffered )
-#endif
-
 import Exception       ( bracket )
 import PrimPacked
 import FastString
@@ -198,9 +199,14 @@ slurpFileExpandTabs fname = do
          then ioError (userError "slurpFile: file too big")
           else do
            let sz_i = fromInteger sz
-               sz_i' = (sz_i * 12) `div` 10            -- add 20% for tabs
-           chunk <- allocMem sz_i'
-           trySlurp handle sz_i' chunk
+            if sz_i == 0
+                       -- empty file: just allocate a buffer containing '\0'
+               then do chunk <- allocMem 1
+                       writeCharOffAddr chunk 0 '\0'
+                       return (chunk, 0)
+               else do let sz_i' = (sz_i * 12) `div` 10 -- add 20% for tabs
+                       chunk <- allocMem sz_i'
+                       trySlurp handle sz_i' chunk
    )
 
 trySlurp :: Handle -> Int -> Addr -> IO (Addr, Int)
@@ -239,7 +245,12 @@ trySlurp handle sz_i chunk =
                buf <- readIORef ref
                ch <- (if not (bufferEmpty buf)
                      then hGetcBuffered fd ref buf
-                     else do new_buf <- fillReadBuffer fd True buf
+                     else do 
+#if __GLASGOW_HASKELL__ >= 503
+                             new_buf <- fillReadBuffer fd True False buf
+#else
+                             new_buf <- fillReadBuffer fd True buf
+#endif
                              hGetcBuffered fd ref new_buf)
                    `catch` \e -> if isEOFError e
                        then return '\xFFFF'