From: simonmar Date: Fri, 2 Jun 2000 11:54:53 +0000 (+0000) Subject: [project @ 2000-06-02 11:54:53 by simonmar] X-Git-Tag: Approximately_9120_patches~4330 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=61954ddbf3fc98ac07bcb16e054181cedd1b97ed;p=ghc-hetmet.git [project @ 2000-06-02 11:54:53 by simonmar] Fix bug in new version of hGetLine: it didn't handle unbuffered handles properly. The fix involves re-importing the old code for hGetLine :-( --- diff --git a/ghc/lib/std/PrelIO.lhs b/ghc/lib/std/PrelIO.lhs index ec150bb..dc56a7e 100644 --- a/ghc/lib/std/PrelIO.lhs +++ b/ghc/lib/std/PrelIO.lhs @@ -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}