[project @ 2000-06-02 11:54:53 by simonmar]
authorsimonmar <unknown>
Fri, 2 Jun 2000 11:54:53 +0000 (11:54 +0000)
committersimonmar <unknown>
Fri, 2 Jun 2000 11:54:53 +0000 (11:54 +0000)
Fix bug in new version of hGetLine: it didn't handle unbuffered
handles properly.

The fix involves re-importing the old code for hGetLine :-(

ghc/lib/std/PrelIO.lhs

index ec150bb..dc56a7e 100644 (file)
@@ -139,7 +139,14 @@ hGetChar handle = do
 -}
 
 hGetLine :: Handle -> IO String
-hGetLine h = hGetLineBuf' []
+hGetLine h = do
+    buffer_mode <- wantWriteableHandle_ "hGetLine" h
+                       (\ handle_ -> do getBuffer handle_)
+    case buffer_mode of
+       (NoBuffering, _, _)          -> hGetLineUnBuffered h
+       (LineBuffering, buf, bsz)    -> hGetLineBuf' []
+       (BlockBuffering _, buf, bsz) -> hGetLineBuf' []
+
   where hGetLineBuf' xss = do
           (eol, xss) <- catch 
            ( do
@@ -162,6 +169,32 @@ hGetLine h = hGetLineBuf' []
                then return (concat (reverse xss))
                else hGetLineBuf' xss
 
+
+hGetLineUnBuffered :: Handle -> IO String
+hGetLineUnBuffered h = do
+  c <- hGetChar h
+  if c == '\n' then
+     return ""
+   else do
+    l <- getRest
+    return (c:l)
+ where
+  getRest = do
+    c <- 
+      catch 
+        (hGetChar h)
+        (\ err -> do
+          if isEOFError err then
+            return '\n'
+          else
+            ioError err)
+    if c == '\n' then
+       return ""
+     else do
+       s <- getRest
+       return (c:s)
+
+
 readCharOffAddr (A# a) (I# i)
   = IO $ \s -> case readCharOffAddr# a i s of { (# s,x #) -> (# s, C# x #) }
 \end{code}