on master help sign in
Add hGetChunk
commitabf20a8ee0691a8eca62ab3d15a050eb92d0593c authorJared Hance <jaredhance@gmail.com> dateSun 8 Apr 2012, 17:44 (15 years ago) parentc13c87a
1 file changed+17
Data/Text/IO.hs+17
Data/Text/IO.hs+17
@@ -26,6 +26,7 @@ module Data.Text.IO
2626 , appendFile
2727 -- * Operations on handles
2828 , hGetContents
29+ , hGetChunk
2930 , hGetLine
3031 , hPutStr
3132 , hPutStrLn
@@ -97,6 +98,22 @@ writeFile p = withFile p WriteMode . flip hPutStr
9798appendFile :: FilePath -> Text -> IO ()
9899appendFile p = withFile p AppendMode . flip hPutStr
99100
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+
100117-- | Read the remaining contents of a 'Handle' as a string. The
101118-- 'Handle' is closed once the contents have been read, or if an
102119-- exception is thrown.