summaryrefslogtreecommitdiff
path: root/ghc/lib/std/IO.lhs
diff options
context:
space:
mode:
authorsof <unknown>1999-05-10 16:55:43 +0000
committersof <unknown>1999-05-10 16:55:43 +0000
commitd993f5b07a82df12aaba033f42925693ac524752 (patch)
treec5099f8ddb0129b984a1fc1be8faeff322a2508e /ghc/lib/std/IO.lhs
parent1c6ce98a2e27b2f3abf5ae5199d9357f592f7b44 (diff)
downloadhaskell-d993f5b07a82df12aaba033f42925693ac524752.tar.gz
[project @ 1999-05-10 16:55:43 by sof]
The implementation of hGetLine, as given in the report, doesn't handle partial lines on unbuffered handles that well (not at all, as it turns out). Fixed.
Diffstat (limited to 'ghc/lib/std/IO.lhs')
-rw-r--r--ghc/lib/std/IO.lhs28
1 files changed, 24 insertions, 4 deletions
diff --git a/ghc/lib/std/IO.lhs b/ghc/lib/std/IO.lhs
index 6670ff3172..5b5aef6b8c 100644
--- a/ghc/lib/std/IO.lhs
+++ b/ghc/lib/std/IO.lhs
@@ -216,14 +216,34 @@ hGetChar handle =
then return (chr intc)
else constructErrorAndFail "hGetChar"
+{-
+ If EOF is reached before EOL is encountered, ignore the
+ EOF and return the partial line. Next attempt at calling
+ hGetLine on the handle will yield an EOF IO exception though.
+-}
hGetLine :: Handle -> IO String
hGetLine h = do
c <- hGetChar h
- if c == '\n'
- then return ""
+ if c == '\n' then
+ return ""
else do
- s <- hGetLine h
- return (c:s)
+ 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)
\end{code}