on master help sign in
Add hGetChunk
abf20a8Jared Hance
Data/Text/IO.hs+17
@@ -26,6 +26,7 @@ module Data.Text.IO
26 , appendFile
26 , appendFile
27 -- * Operations on handles
27 -- * Operations on handles
28 , hGetContents
28 , hGetContents
29+ , hGetChunk
29 , hGetLine
30 , hGetLine
30 , hPutStr
31 , hPutStr
31 , hPutStrLn
32 , hPutStrLn
@@ -97,6 +98,22 @@ writeFile p = withFile p WriteMode . flip hPutStr
97appendFile :: FilePath -> Text -> IO ()
98appendFile :: FilePath -> Text -> IO ()
98appendFile p = withFile p AppendMode . flip hPutStr
99appendFile p = withFile p AppendMode . flip hPutStr
99
100
101+-- | Read a single chunk of strict text from a 'Handle'.
102+hGetChunk :: Handle -> IO Text
103+hGetChunk h = wantReadableHandle "hGetChunk" h readSingleChunk
104+ where
105+ readSingleChunk hh@Handle__{..} = do
106+ let catchError e
107+ | isEOFError e = do
108+ buf <- readIORef haCharBuffer
109+ return $ if isEmptyBuffer buf
110+ then T.empty
111+ else T.singleton '\r'
112+ | otherwise = throwIO (augmentIOError e "hGetChunk" h)
113+ buf <- readIORef haCharBuffer
114+ t <- readChunk hh buf `catch` catchError
115+ return (hh, t)
116+
100-- | Read the remaining contents of a 'Handle' as a string. The
117-- | Read the remaining contents of a 'Handle' as a string. The
101-- 'Handle' is closed once the contents have been read, or if an
118-- 'Handle' is closed once the contents have been read, or if an
102-- exception is thrown.
119-- exception is thrown.