{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE CPP
, NoImplicitPrelude
, RecordWildCards
, BangPatterns
, NondecreasingIndentation
, MagicHash
#-}
{-# OPTIONS_GHC -Wno-unused-matches #-}
{-# OPTIONS_HADDOCK not-home #-}
module GHC.IO.Handle.Text (
hWaitForInput, hGetChar, hGetLine, hGetContents, hPutChar, hPutStr,
commitBuffer',
hGetBuf, hGetBufSome, hGetBufNonBlocking, hPutBuf, hPutBufNonBlocking,
memcpy, hPutStrLn, hGetContents',
) where
import GHC.IO
import GHC.IO.Buffer
import qualified GHC.IO.BufferedIO as Buffered
import GHC.IO.Exception
import GHC.Exception
import GHC.IO.Handle.Types
import GHC.IO.Handle.Internals
import qualified GHC.IO.Device as IODevice
import qualified GHC.IO.Device as RawIO
import Foreign
import Foreign.C
import qualified Control.Exception as Exception
import System.IO.Error
import Data.Either (Either(..))
import Data.Maybe
import GHC.IORef
import GHC.Base
import GHC.Real
import GHC.Num
import GHC.Show
import GHC.List
hWaitForInput :: Handle -> Int -> IO Bool
hWaitForInput :: Handle -> Int -> IO Bool
hWaitForInput Handle
h Int
msecs =
forall a. String -> Handle -> (Handle__ -> IO a) -> IO a
wantReadableHandle_ String
"hWaitForInput" Handle
h forall a b. (a -> b) -> a -> b
$ \ handle_ :: Handle__
handle_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
..} -> do
Buffer Char
cbuf <- forall a. IORef a -> IO a
readIORef IORef (Buffer Char)
haCharBuffer
if Bool -> Bool
not (forall e. Buffer e -> Bool
isEmptyBuffer Buffer Char
cbuf) then forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True else do
if Int
msecs forall a. Ord a => a -> a -> Bool
< Int
0
then do Buffer Char
cbuf' <- Handle__ -> Buffer Char -> IO (Buffer Char)
readTextDevice Handle__
handle_ Buffer Char
cbuf
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Char)
haCharBuffer Buffer Char
cbuf'
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
else do
Buffer Char
cbuf' <- Handle__ -> Buffer Char -> IO (Buffer Char)
decodeByteBuf Handle__
handle_ Buffer Char
cbuf
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Char)
haCharBuffer Buffer Char
cbuf'
if Bool -> Bool
not (forall e. Buffer e -> Bool
isEmptyBuffer Buffer Char
cbuf') then forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True else do
Bool
r <- forall a. IODevice a => a -> Bool -> Int -> IO Bool
IODevice.ready dev
haDevice Bool
False Int
msecs
if Bool
r then do
Char
_ <- Handle__ -> IO Char
hLookAhead_ Handle__
handle_
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
else forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
hGetChar :: Handle -> IO Char
hGetChar :: Handle -> IO Char
hGetChar Handle
handle =
forall a. String -> Handle -> (Handle__ -> IO a) -> IO a
wantReadableHandle_ String
"hGetChar" Handle
handle forall a b. (a -> b) -> a -> b
$ \handle_ :: Handle__
handle_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..} -> do
Buffer Char
buf0 <- forall a. IORef a -> IO a
readIORef IORef (Buffer Char)
haCharBuffer
Buffer Char
buf1 <- if forall e. Buffer e -> Bool
isEmptyBuffer Buffer Char
buf0
then Handle__ -> Buffer Char -> IO (Buffer Char)
readTextDevice Handle__
handle_ Buffer Char
buf0
else forall (m :: * -> *) a. Monad m => a -> m a
return Buffer Char
buf0
(Char
c1,Int
i) <- RawCharBuffer -> Int -> IO (Char, Int)
readCharBuf (forall e. Buffer e -> RawBuffer e
bufRaw Buffer Char
buf1) (forall e. Buffer e -> Int
bufL Buffer Char
buf1)
let buf2 :: Buffer Char
buf2 = forall e. Int -> Buffer e -> Buffer e
bufferAdjustL Int
i Buffer Char
buf1
if Newline
haInputNL forall a. Eq a => a -> a -> Bool
== Newline
CRLF Bool -> Bool -> Bool
&& Char
c1 forall a. Eq a => a -> a -> Bool
== Char
'\r'
then do
Maybe (Buffer Char)
mbuf3 <- if forall e. Buffer e -> Bool
isEmptyBuffer Buffer Char
buf2
then Handle__ -> Buffer Char -> IO (Maybe (Buffer Char))
maybeFillReadBuffer Handle__
handle_ Buffer Char
buf2
else forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just Buffer Char
buf2)
case Maybe (Buffer Char)
mbuf3 of
Maybe (Buffer Char)
Nothing -> do
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Char)
haCharBuffer Buffer Char
buf2
forall (m :: * -> *) a. Monad m => a -> m a
return Char
'\r'
Just Buffer Char
buf3 -> do
(Char
c2,Int
i2) <- RawCharBuffer -> Int -> IO (Char, Int)
readCharBuf (forall e. Buffer e -> RawBuffer e
bufRaw Buffer Char
buf2) (forall e. Buffer e -> Int
bufL Buffer Char
buf2)
if Char
c2 forall a. Eq a => a -> a -> Bool
== Char
'\n'
then do
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Char)
haCharBuffer (forall e. Int -> Buffer e -> Buffer e
bufferAdjustL Int
i2 Buffer Char
buf3)
forall (m :: * -> *) a. Monad m => a -> m a
return Char
'\n'
else do
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Char)
haCharBuffer Buffer Char
buf3
forall (m :: * -> *) a. Monad m => a -> m a
return Char
'\r'
else do
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Char)
haCharBuffer Buffer Char
buf2
forall (m :: * -> *) a. Monad m => a -> m a
return Char
c1
hGetLine :: Handle -> IO String
hGetLine :: Handle -> IO String
hGetLine Handle
h =
forall a. String -> Handle -> (Handle__ -> IO a) -> IO a
wantReadableHandle_ String
"hGetLine" Handle
h forall a b. (a -> b) -> a -> b
$ \ Handle__
handle_ ->
Handle__ -> IO String
hGetLineBuffered Handle__
handle_
hGetLineBuffered :: Handle__ -> IO String
hGetLineBuffered :: Handle__ -> IO String
hGetLineBuffered handle_ :: Handle__
handle_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..} = do
Buffer Char
buf <- forall a. IORef a -> IO a
readIORef IORef (Buffer Char)
haCharBuffer
Handle__ -> Buffer Char -> [String] -> IO String
hGetLineBufferedLoop Handle__
handle_ Buffer Char
buf []
hGetLineBufferedLoop :: Handle__
-> CharBuffer -> [String]
-> IO String
hGetLineBufferedLoop :: Handle__ -> Buffer Char -> [String] -> IO String
hGetLineBufferedLoop handle_ :: Handle__
handle_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..}
buf :: Buffer Char
buf@Buffer{ bufL :: forall e. Buffer e -> Int
bufL=Int
r0, bufR :: forall e. Buffer e -> Int
bufR=Int
w, bufRaw :: forall e. Buffer e -> RawBuffer e
bufRaw=RawCharBuffer
raw0 } [String]
xss =
let
loop :: RawCharBuffer -> Int -> IO (Bool, Int)
loop RawCharBuffer
raw Int
r
| Int
r forall a. Eq a => a -> a -> Bool
== Int
w = forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
False, Int
w)
| Bool
otherwise = do
(Char
c,Int
r') <- RawCharBuffer -> Int -> IO (Char, Int)
readCharBuf RawCharBuffer
raw Int
r
if Char
c forall a. Eq a => a -> a -> Bool
== Char
'\n'
then forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
True, Int
r)
else RawCharBuffer -> Int -> IO (Bool, Int)
loop RawCharBuffer
raw Int
r'
in do
(Bool
eol, Int
off) <- RawCharBuffer -> Int -> IO (Bool, Int)
loop RawCharBuffer
raw0 Int
r0
String -> IO ()
debugIO (String
"hGetLineBufferedLoop: r=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
r0 forall a. [a] -> [a] -> [a]
++ String
", w=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
w forall a. [a] -> [a] -> [a]
++ String
", off=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
off)
(String
xs,Int
r') <- if Newline
haInputNL forall a. Eq a => a -> a -> Bool
== Newline
CRLF
then RawCharBuffer -> Int -> Int -> String -> IO (String, Int)
unpack_nl RawCharBuffer
raw0 Int
r0 Int
off String
""
else do String
xs <- RawCharBuffer -> Int -> Int -> String -> IO String
unpack RawCharBuffer
raw0 Int
r0 Int
off String
""
forall (m :: * -> *) a. Monad m => a -> m a
return (String
xs,Int
off)
if Bool
eol
then do forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Char)
haCharBuffer (forall e. Int -> Buffer e -> Buffer e
bufferAdjustL (Int
offforall a. Num a => a -> a -> a
+Int
1) Buffer Char
buf)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. [[a]] -> [a]
concat (forall a. [a] -> [a]
reverse (String
xsforall a. a -> [a] -> [a]
:[String]
xss)))
else do
let buf1 :: Buffer Char
buf1 = forall e. Int -> Buffer e -> Buffer e
bufferAdjustL Int
r' Buffer Char
buf
Maybe (Buffer Char)
maybe_buf <- Handle__ -> Buffer Char -> IO (Maybe (Buffer Char))
maybeFillReadBuffer Handle__
handle_ Buffer Char
buf1
case Maybe (Buffer Char)
maybe_buf of
Maybe (Buffer Char)
Nothing -> do
let pre :: String
pre = if Bool -> Bool
not (forall e. Buffer e -> Bool
isEmptyBuffer Buffer Char
buf1) then String
"\r" else String
""
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Char)
haCharBuffer Buffer Char
buf1{ bufL :: Int
bufL=Int
0, bufR :: Int
bufR=Int
0 }
let str :: String
str = forall a. [[a]] -> [a]
concat (forall a. [a] -> [a]
reverse (String
preforall a. a -> [a] -> [a]
:String
xsforall a. a -> [a] -> [a]
:[String]
xss))
if Bool -> Bool
not (forall a. [a] -> Bool
null String
str)
then forall (m :: * -> *) a. Monad m => a -> m a
return String
str
else forall a. IO a
ioe_EOF
Just Buffer Char
new_buf ->
Handle__ -> Buffer Char -> [String] -> IO String
hGetLineBufferedLoop Handle__
handle_ Buffer Char
new_buf (String
xsforall a. a -> [a] -> [a]
:[String]
xss)
maybeFillReadBuffer :: Handle__ -> CharBuffer -> IO (Maybe CharBuffer)
maybeFillReadBuffer :: Handle__ -> Buffer Char -> IO (Maybe (Buffer Char))
maybeFillReadBuffer Handle__
handle_ Buffer Char
buf
= forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catchException
(do Buffer Char
buf' <- Handle__ -> Buffer Char -> IO (Buffer Char)
getSomeCharacters Handle__
handle_ Buffer Char
buf
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just Buffer Char
buf')
)
(\IOError
e -> do if IOError -> Bool
isEOFError IOError
e
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
else forall a. IOError -> IO a
ioError IOError
e)
#define CHARBUF_UTF32
unpack :: RawCharBuffer -> Int -> Int -> [Char] -> IO [Char]
unpack :: RawCharBuffer -> Int -> Int -> String -> IO String
unpack !RawCharBuffer
buf !Int
r !Int
w String
acc0
| Int
r forall a. Eq a => a -> a -> Bool
== Int
w = forall (m :: * -> *) a. Monad m => a -> m a
return String
acc0
| Bool
otherwise =
forall e a. RawBuffer e -> (Ptr e -> IO a) -> IO a
withRawBuffer RawCharBuffer
buf forall a b. (a -> b) -> a -> b
$ \Ptr Char
pbuf ->
let
unpackRB :: String -> Int -> IO String
unpackRB String
acc !Int
i
| Int
i forall a. Ord a => a -> a -> Bool
< Int
r = forall (m :: * -> *) a. Monad m => a -> m a
return String
acc
| Bool
otherwise = do
#if defined(CHARBUF_UTF16)
c2 <- peekElemOff pbuf i
if (c2 < 0xdc00 || c2 > 0xdffff)
then unpackRB (unsafeChr (fromIntegral c2) : acc) (i-1)
else do c1 <- peekElemOff pbuf (i-1)
let c = (fromIntegral c1 - 0xd800) * 0x400 +
(fromIntegral c2 - 0xdc00) + 0x10000
case desurrogatifyRoundtripCharacter (unsafeChr c) of
{ C# c# -> unpackRB (C# c# : acc) (i-2) }
#else
Char
c <- forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr Char
pbuf Int
i
String -> Int -> IO String
unpackRB (Char
c forall a. a -> [a] -> [a]
: String
acc) (Int
iforall a. Num a => a -> a -> a
-Int
1)
#endif
in
String -> Int -> IO String
unpackRB String
acc0 (Int
wforall a. Num a => a -> a -> a
-Int
1)
unpack_nl :: RawCharBuffer -> Int -> Int -> [Char] -> IO ([Char],Int)
unpack_nl :: RawCharBuffer -> Int -> Int -> String -> IO (String, Int)
unpack_nl !RawCharBuffer
buf !Int
r !Int
w String
acc0
| Int
r forall a. Eq a => a -> a -> Bool
== Int
w = forall (m :: * -> *) a. Monad m => a -> m a
return (String
acc0, Int
0)
| Bool
otherwise =
forall e a. RawBuffer e -> (Ptr e -> IO a) -> IO a
withRawBuffer RawCharBuffer
buf forall a b. (a -> b) -> a -> b
$ \Ptr Char
pbuf ->
let
unpackRB :: String -> Int -> IO String
unpackRB String
acc !Int
i
| Int
i forall a. Ord a => a -> a -> Bool
< Int
r = forall (m :: * -> *) a. Monad m => a -> m a
return String
acc
| Bool
otherwise = do
Char
c <- forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr Char
pbuf Int
i
if (Char
c forall a. Eq a => a -> a -> Bool
== Char
'\n' Bool -> Bool -> Bool
&& Int
i forall a. Ord a => a -> a -> Bool
> Int
r)
then do
Char
c1 <- forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr Char
pbuf (Int
iforall a. Num a => a -> a -> a
-Int
1)
if (Char
c1 forall a. Eq a => a -> a -> Bool
== Char
'\r')
then String -> Int -> IO String
unpackRB (Char
'\n'forall a. a -> [a] -> [a]
:String
acc) (Int
iforall a. Num a => a -> a -> a
-Int
2)
else String -> Int -> IO String
unpackRB (Char
'\n'forall a. a -> [a] -> [a]
:String
acc) (Int
iforall a. Num a => a -> a -> a
-Int
1)
else
String -> Int -> IO String
unpackRB (Char
c forall a. a -> [a] -> [a]
: String
acc) (Int
iforall a. Num a => a -> a -> a
-Int
1)
in do
Char
c <- forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr Char
pbuf (Int
wforall a. Num a => a -> a -> a
-Int
1)
if (Char
c forall a. Eq a => a -> a -> Bool
== Char
'\r')
then do
String
str <- String -> Int -> IO String
unpackRB String
acc0 (Int
wforall a. Num a => a -> a -> a
-Int
2)
forall (m :: * -> *) a. Monad m => a -> m a
return (String
str, Int
wforall a. Num a => a -> a -> a
-Int
1)
else do
String
str <- String -> Int -> IO String
unpackRB String
acc0 (Int
wforall a. Num a => a -> a -> a
-Int
1)
forall (m :: * -> *) a. Monad m => a -> m a
return (String
str, Int
w)
hGetContents :: Handle -> IO String
hGetContents :: Handle -> IO String
hGetContents Handle
handle =
forall a.
String -> Handle -> (Handle__ -> IO (Handle__, a)) -> IO a
wantReadableHandle String
"hGetContents" Handle
handle forall a b. (a -> b) -> a -> b
$ \Handle__
handle_ -> do
String
xs <- Handle -> IO String
lazyRead Handle
handle
forall (m :: * -> *) a. Monad m => a -> m a
return (Handle__
handle_{ haType :: HandleType
haType=HandleType
SemiClosedHandle}, String
xs )
lazyRead :: Handle -> IO String
lazyRead :: Handle -> IO String
lazyRead Handle
handle =
forall a. IO a -> IO a
unsafeInterleaveIO forall a b. (a -> b) -> a -> b
$
forall a.
String -> Handle -> (Handle__ -> IO (Handle__, a)) -> IO a
withHandle String
"hGetContents" Handle
handle forall a b. (a -> b) -> a -> b
$ \ Handle__
handle_ -> do
case Handle__ -> HandleType
haType Handle__
handle_ of
HandleType
SemiClosedHandle -> Handle -> Handle__ -> IO (Handle__, String)
lazyReadBuffered Handle
handle Handle__
handle_
HandleType
ClosedHandle
-> forall a. IOError -> IO a
ioException
(Maybe Handle
-> IOErrorType
-> String
-> String
-> Maybe CInt
-> Maybe String
-> IOError
IOError (forall a. a -> Maybe a
Just Handle
handle) IOErrorType
IllegalOperation String
"hGetContents"
String
"delayed read on closed handle" forall a. Maybe a
Nothing forall a. Maybe a
Nothing)
HandleType
_ -> forall a. IOError -> IO a
ioException
(Maybe Handle
-> IOErrorType
-> String
-> String
-> Maybe CInt
-> Maybe String
-> IOError
IOError (forall a. a -> Maybe a
Just Handle
handle) IOErrorType
IllegalOperation String
"hGetContents"
String
"illegal handle type" forall a. Maybe a
Nothing forall a. Maybe a
Nothing)
lazyReadBuffered :: Handle -> Handle__ -> IO (Handle__, [Char])
lazyReadBuffered :: Handle -> Handle__ -> IO (Handle__, String)
lazyReadBuffered Handle
h handle_ :: Handle__
handle_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..} = do
Buffer Char
buf <- forall a. IORef a -> IO a
readIORef IORef (Buffer Char)
haCharBuffer
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
Exception.catch
(do
buf' :: Buffer Char
buf'@Buffer{Int
Word64
RawCharBuffer
BufferState
bufState :: forall e. Buffer e -> BufferState
bufSize :: forall e. Buffer e -> Int
bufOffset :: forall e. Buffer e -> Word64
bufR :: Int
bufL :: Int
bufOffset :: Word64
bufSize :: Int
bufState :: BufferState
bufRaw :: RawCharBuffer
bufR :: forall e. Buffer e -> Int
bufL :: forall e. Buffer e -> Int
bufRaw :: forall e. Buffer e -> RawBuffer e
..} <- Handle__ -> Buffer Char -> IO (Buffer Char)
getSomeCharacters Handle__
handle_ Buffer Char
buf
String
lazy_rest <- Handle -> IO String
lazyRead Handle
h
(String
s,Int
r) <- if Newline
haInputNL forall a. Eq a => a -> a -> Bool
== Newline
CRLF
then RawCharBuffer -> Int -> Int -> String -> IO (String, Int)
unpack_nl RawCharBuffer
bufRaw Int
bufL Int
bufR String
lazy_rest
else do String
s <- RawCharBuffer -> Int -> Int -> String -> IO String
unpack RawCharBuffer
bufRaw Int
bufL Int
bufR String
lazy_rest
forall (m :: * -> *) a. Monad m => a -> m a
return (String
s,Int
bufR)
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Char)
haCharBuffer (forall e. Int -> Buffer e -> Buffer e
bufferAdjustL Int
r Buffer Char
buf')
forall (m :: * -> *) a. Monad m => a -> m a
return (Handle__
handle_, String
s)
)
(\IOError
e -> do (Handle__
handle_', Maybe SomeException
_) <- Handle__ -> IO (Handle__, Maybe SomeException)
hClose_help Handle__
handle_
String -> IO ()
debugIO (String
"hGetContents caught: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show IOError
e)
let r :: String
r = if IOError -> Bool
isEOFError IOError
e
then if Bool -> Bool
not (forall e. Buffer e -> Bool
isEmptyBuffer Buffer Char
buf)
then String
"\r"
else String
""
else
forall a e. Exception e => e -> a
throw (IOError -> String -> Handle -> IOError
augmentIOError IOError
e String
"hGetContents" Handle
h)
forall (m :: * -> *) a. Monad m => a -> m a
return (Handle__
handle_', String
r)
)
getSomeCharacters :: Handle__ -> CharBuffer -> IO CharBuffer
getSomeCharacters :: Handle__ -> Buffer Char -> IO (Buffer Char)
getSomeCharacters handle_ :: Handle__
handle_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..} buf :: Buffer Char
buf@Buffer{Int
Word64
RawCharBuffer
BufferState
bufR :: Int
bufL :: Int
bufOffset :: Word64
bufSize :: Int
bufState :: BufferState
bufRaw :: RawCharBuffer
bufState :: forall e. Buffer e -> BufferState
bufSize :: forall e. Buffer e -> Int
bufOffset :: forall e. Buffer e -> Word64
bufR :: forall e. Buffer e -> Int
bufL :: forall e. Buffer e -> Int
bufRaw :: forall e. Buffer e -> RawBuffer e
..} =
case forall e. Buffer e -> Int
bufferElems Buffer Char
buf of
Int
0 -> Handle__ -> Buffer Char -> IO (Buffer Char)
readTextDevice Handle__
handle_ Buffer Char
buf
Int
1 | Newline
haInputNL forall a. Eq a => a -> a -> Bool
== Newline
CRLF -> do
(Char
c,Int
_) <- RawCharBuffer -> Int -> IO (Char, Int)
readCharBuf RawCharBuffer
bufRaw Int
bufL
if Char
c forall a. Eq a => a -> a -> Bool
== Char
'\r'
then do
Int
_ <- RawCharBuffer -> Int -> Char -> IO Int
writeCharBuf RawCharBuffer
bufRaw Int
0 Char
'\r'
let buf' :: Buffer Char
buf' = Buffer Char
buf{ bufL :: Int
bufL=Int
0, bufR :: Int
bufR=Int
1 }
Handle__ -> Buffer Char -> IO (Buffer Char)
readTextDevice Handle__
handle_ Buffer Char
buf'
else
forall (m :: * -> *) a. Monad m => a -> m a
return Buffer Char
buf
Int
_otherwise ->
forall (m :: * -> *) a. Monad m => a -> m a
return Buffer Char
buf
hGetContents' :: Handle -> IO String
hGetContents' :: Handle -> IO String
hGetContents' Handle
handle = do
Either SomeException String
es <- forall a.
String -> Handle -> (Handle__ -> IO (Handle__, a)) -> IO a
wantReadableHandle String
"hGetContents'" Handle
handle (Handle -> Handle__ -> IO (Handle__, Either SomeException String)
strictRead Handle
handle)
case Either SomeException String
es of
Right String
s -> forall (m :: * -> *) a. Monad m => a -> m a
return String
s
Left SomeException
e ->
case forall e. Exception e => SomeException -> Maybe e
fromException SomeException
e of
Just IOError
ioe -> forall e a. Exception e => e -> IO a
throwIO (IOError -> String -> Handle -> IOError
augmentIOError IOError
ioe String
"hGetContents'" Handle
handle)
Maybe IOError
Nothing -> forall e a. Exception e => e -> IO a
throwIO SomeException
e
strictRead :: Handle -> Handle__ -> IO (Handle__, Either SomeException String)
strictRead :: Handle -> Handle__ -> IO (Handle__, Either SomeException String)
strictRead Handle
h handle_ :: Handle__
handle_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..} = do
Buffer Char
cbuf <- forall a. IORef a -> IO a
readIORef IORef (Buffer Char)
haCharBuffer
[Buffer Char]
cbufs <- Handle__ -> [Buffer Char] -> Buffer Char -> IO [Buffer Char]
strictReadLoop' Handle__
handle_ [] Buffer Char
cbuf
(Handle__
handle_', Maybe SomeException
me) <- Handle__ -> IO (Handle__, Maybe SomeException)
hClose_help Handle__
handle_
case Maybe SomeException
me of
Just SomeException
e -> forall (m :: * -> *) a. Monad m => a -> m a
return (Handle__
handle_', forall a b. a -> Either a b
Left SomeException
e)
Maybe SomeException
Nothing -> do
String
s <- Newline -> [Buffer Char] -> String -> IO String
lazyBuffersToString Newline
haInputNL [Buffer Char]
cbufs String
""
forall (m :: * -> *) a. Monad m => a -> m a
return (Handle__
handle_', forall a b. b -> Either a b
Right String
s)
strictReadLoop :: Handle__ -> [CharBuffer] -> CharBuffer -> IO [CharBuffer]
strictReadLoop :: Handle__ -> [Buffer Char] -> Buffer Char -> IO [Buffer Char]
strictReadLoop Handle__
handle_ [Buffer Char]
cbufs Buffer Char
cbuf0 = do
Maybe (Buffer Char)
mcbuf <- forall e a. Exception e => IO a -> (e -> IO a) -> IO a
Exception.catch
(do Buffer Char
r <- Handle__ -> Buffer Char -> IO (Buffer Char)
readTextDevice Handle__
handle_ Buffer Char
cbuf0
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just Buffer Char
r))
(\IOError
e -> if IOError -> Bool
isEOFError IOError
e
then forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
else forall a e. Exception e => e -> a
throw IOError
e)
case Maybe (Buffer Char)
mcbuf of
Maybe (Buffer Char)
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return (Buffer Char
cbuf0 forall a. a -> [a] -> [a]
: [Buffer Char]
cbufs)
Just Buffer Char
cbuf1 -> Handle__ -> [Buffer Char] -> Buffer Char -> IO [Buffer Char]
strictReadLoop' Handle__
handle_ [Buffer Char]
cbufs Buffer Char
cbuf1
strictReadLoop' :: Handle__ -> [CharBuffer] -> CharBuffer -> IO [CharBuffer]
strictReadLoop' :: Handle__ -> [Buffer Char] -> Buffer Char -> IO [Buffer Char]
strictReadLoop' Handle__
handle_ [Buffer Char]
cbufs Buffer Char
cbuf
| forall e. Buffer e -> Bool
isFullCharBuffer Buffer Char
cbuf = do
Buffer Char
cbuf' <- Int -> BufferState -> IO (Buffer Char)
newCharBuffer Int
dEFAULT_CHAR_BUFFER_SIZE BufferState
ReadBuffer
Handle__ -> [Buffer Char] -> Buffer Char -> IO [Buffer Char]
strictReadLoop Handle__
handle_ (Buffer Char
cbuf forall a. a -> [a] -> [a]
: [Buffer Char]
cbufs) Buffer Char
cbuf'
| Bool
otherwise = Handle__ -> [Buffer Char] -> Buffer Char -> IO [Buffer Char]
strictReadLoop Handle__
handle_ [Buffer Char]
cbufs Buffer Char
cbuf
lazyBuffersToString :: Newline -> [CharBuffer] -> String -> IO String
lazyBuffersToString :: Newline -> [Buffer Char] -> String -> IO String
lazyBuffersToString Newline
LF = [Buffer Char] -> String -> IO String
loop where
loop :: [Buffer Char] -> String -> IO String
loop [] String
s = forall (m :: * -> *) a. Monad m => a -> m a
return String
s
loop (Buffer{Int
Word64
RawCharBuffer
BufferState
bufR :: Int
bufL :: Int
bufOffset :: Word64
bufSize :: Int
bufState :: BufferState
bufRaw :: RawCharBuffer
bufState :: forall e. Buffer e -> BufferState
bufSize :: forall e. Buffer e -> Int
bufOffset :: forall e. Buffer e -> Word64
bufR :: forall e. Buffer e -> Int
bufL :: forall e. Buffer e -> Int
bufRaw :: forall e. Buffer e -> RawBuffer e
..} : [Buffer Char]
cbufs) String
s = do
String
s' <- forall a. IO a -> IO a
unsafeInterleaveIO (RawCharBuffer -> Int -> Int -> String -> IO String
unpack RawCharBuffer
bufRaw Int
bufL Int
bufR String
s)
[Buffer Char] -> String -> IO String
loop [Buffer Char]
cbufs String
s'
lazyBuffersToString Newline
CRLF = Char -> [Buffer Char] -> String -> IO String
loop Char
'\0' where
loop :: Char -> [Buffer Char] -> String -> IO String
loop Char
before [] String
s = forall (m :: * -> *) a. Monad m => a -> m a
return String
s
loop Char
before (Buffer{Int
Word64
RawCharBuffer
BufferState
bufR :: Int
bufL :: Int
bufOffset :: Word64
bufSize :: Int
bufState :: BufferState
bufRaw :: RawCharBuffer
bufState :: forall e. Buffer e -> BufferState
bufSize :: forall e. Buffer e -> Int
bufOffset :: forall e. Buffer e -> Word64
bufR :: forall e. Buffer e -> Int
bufL :: forall e. Buffer e -> Int
bufRaw :: forall e. Buffer e -> RawBuffer e
..} : [Buffer Char]
cbufs) String
s
| Int
bufL forall a. Eq a => a -> a -> Bool
== Int
bufR = Char -> [Buffer Char] -> String -> IO String
loop Char
before [Buffer Char]
cbufs String
s
| Bool
otherwise = do
String
s1 <- if Char
before forall a. Eq a => a -> a -> Bool
== Char
'\n'
then forall (m :: * -> *) a. Monad m => a -> m a
return String
s
else do
Char
c <- RawCharBuffer -> Int -> IO Char
peekCharBuf RawCharBuffer
bufRaw (Int
bufR forall a. Num a => a -> a -> a
- Int
1)
if Char
c forall a. Eq a => a -> a -> Bool
== Char
'\r'
then forall (m :: * -> *) a. Monad m => a -> m a
return (Char
'\r' forall a. a -> [a] -> [a]
: String
s)
else forall (m :: * -> *) a. Monad m => a -> m a
return String
s
String
s2 <- forall a. IO a -> IO a
unsafeInterleaveIO (do
(String
s2, Int
_) <- RawCharBuffer -> Int -> Int -> String -> IO (String, Int)
unpack_nl RawCharBuffer
bufRaw Int
bufL Int
bufR String
s1
forall (m :: * -> *) a. Monad m => a -> m a
return String
s2)
Char
c0 <- RawCharBuffer -> Int -> IO Char
peekCharBuf RawCharBuffer
bufRaw Int
bufL
Char -> [Buffer Char] -> String -> IO String
loop Char
c0 [Buffer Char]
cbufs String
s2
hPutChar :: Handle -> Char -> IO ()
hPutChar :: Handle -> Char -> IO ()
hPutChar Handle
handle Char
c = do
Char
c seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return ()
forall a. String -> Handle -> (Handle__ -> IO a) -> IO a
wantWritableHandle String
"hPutChar" Handle
handle forall a b. (a -> b) -> a -> b
$ \ Handle__
handle_ ->
Handle__ -> Char -> IO ()
hPutcBuffered Handle__
handle_ Char
c
hPutcBuffered :: Handle__ -> Char -> IO ()
hPutcBuffered :: Handle__ -> Char -> IO ()
hPutcBuffered handle_ :: Handle__
handle_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..} Char
c = do
Buffer Char
buf <- forall a. IORef a -> IO a
readIORef IORef (Buffer Char)
haCharBuffer
if Char
c forall a. Eq a => a -> a -> Bool
== Char
'\n'
then do Buffer Char
buf1 <- if Newline
haOutputNL forall a. Eq a => a -> a -> Bool
== Newline
CRLF
then do
Buffer Char
buf1 <- Buffer Char -> Char -> IO (Buffer Char)
putc Buffer Char
buf Char
'\r'
Buffer Char -> Char -> IO (Buffer Char)
putc Buffer Char
buf1 Char
'\n'
else
Buffer Char -> Char -> IO (Buffer Char)
putc Buffer Char
buf Char
'\n'
Handle__ -> Buffer Char -> IO ()
writeCharBuffer Handle__
handle_ Buffer Char
buf1
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
isLine forall a b. (a -> b) -> a -> b
$ Handle__ -> IO ()
flushByteWriteBuffer Handle__
handle_
else do
Buffer Char
buf1 <- Buffer Char -> Char -> IO (Buffer Char)
putc Buffer Char
buf Char
c
Handle__ -> Buffer Char -> IO ()
writeCharBuffer Handle__
handle_ Buffer Char
buf1
forall (m :: * -> *) a. Monad m => a -> m a
return ()
where
isLine :: Bool
isLine = case BufferMode
haBufferMode of
BufferMode
LineBuffering -> Bool
True
BufferMode
_ -> Bool
False
putc :: Buffer Char -> Char -> IO (Buffer Char)
putc buf :: Buffer Char
buf@Buffer{ bufRaw :: forall e. Buffer e -> RawBuffer e
bufRaw=RawCharBuffer
raw, bufR :: forall e. Buffer e -> Int
bufR=Int
w } Char
c' = do
String -> IO ()
debugIO (String
"putc: " forall a. [a] -> [a] -> [a]
++ forall a. Buffer a -> String
summaryBuffer Buffer Char
buf)
Int
w' <- RawCharBuffer -> Int -> Char -> IO Int
writeCharBuf RawCharBuffer
raw Int
w Char
c'
forall (m :: * -> *) a. Monad m => a -> m a
return Buffer Char
buf{ bufR :: Int
bufR = Int
w' }
hPutStr :: Handle -> String -> IO ()
hPutStr :: Handle -> String -> IO ()
hPutStr Handle
handle String
str = Handle -> String -> Bool -> IO ()
hPutStr' Handle
handle String
str Bool
False
hPutStrLn :: Handle -> String -> IO ()
hPutStrLn :: Handle -> String -> IO ()
hPutStrLn Handle
handle String
str = Handle -> String -> Bool -> IO ()
hPutStr' Handle
handle String
str Bool
True
{-# NOINLINE hPutStr' #-}
hPutStr' :: Handle -> String -> Bool -> IO ()
hPutStr' :: Handle -> String -> Bool -> IO ()
hPutStr' Handle
handle String
str Bool
add_nl =
do
((BufferMode, Buffer Char)
buffer_mode, Newline
nl) <-
forall a. String -> Handle -> (Handle__ -> IO a) -> IO a
wantWritableHandle String
"hPutStr" Handle
handle forall a b. (a -> b) -> a -> b
$ \Handle__
h_ -> do
(BufferMode, Buffer Char)
bmode <- Handle__ -> IO (BufferMode, Buffer Char)
getSpareBuffer Handle__
h_
forall (m :: * -> *) a. Monad m => a -> m a
return ((BufferMode, Buffer Char)
bmode, Handle__ -> Newline
haOutputNL Handle__
h_)
case (BufferMode, Buffer Char)
buffer_mode of
(BufferMode
NoBuffering, Buffer Char
_) -> do
Handle -> String -> IO ()
hPutChars Handle
handle String
str
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
add_nl forall a b. (a -> b) -> a -> b
$ Handle -> Char -> IO ()
hPutChar Handle
handle Char
'\n'
(BufferMode
LineBuffering, Buffer Char
buf) ->
Handle -> Bool -> Bool -> Newline -> Buffer Char -> String -> IO ()
writeBlocks Handle
handle Bool
True Bool
add_nl Newline
nl Buffer Char
buf String
str
(BlockBuffering Maybe Int
_, Buffer Char
buf) ->
Handle -> Bool -> Bool -> Newline -> Buffer Char -> String -> IO ()
writeBlocks Handle
handle Bool
False Bool
add_nl Newline
nl Buffer Char
buf String
str
hPutChars :: Handle -> [Char] -> IO ()
hPutChars :: Handle -> String -> IO ()
hPutChars Handle
_ [] = forall (m :: * -> *) a. Monad m => a -> m a
return ()
hPutChars Handle
handle (Char
c:String
cs) = Handle -> Char -> IO ()
hPutChar Handle
handle Char
c forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Handle -> String -> IO ()
hPutChars Handle
handle String
cs
getSpareBuffer :: Handle__ -> IO (BufferMode, CharBuffer)
getSpareBuffer :: Handle__ -> IO (BufferMode, Buffer Char)
getSpareBuffer Handle__{haCharBuffer :: Handle__ -> IORef (Buffer Char)
haCharBuffer=IORef (Buffer Char)
ref, haBuffers :: Handle__ -> IORef (BufferList Char)
haBuffers=IORef (BufferList Char)
spare_ref, haBufferMode :: Handle__ -> BufferMode
haBufferMode=BufferMode
mode} =
case BufferMode
mode of
BufferMode
NoBuffering -> forall (m :: * -> *) a. Monad m => a -> m a
return (BufferMode
mode, forall a. String -> a
errorWithoutStackTrace String
"no buffer!")
BufferMode
_ -> do
BufferList Char
bufs <- forall a. IORef a -> IO a
readIORef IORef (BufferList Char)
spare_ref
Buffer Char
buf <- forall a. IORef a -> IO a
readIORef IORef (Buffer Char)
ref
case BufferList Char
bufs of
BufferListCons RawCharBuffer
b BufferList Char
rest -> do
forall a. IORef a -> a -> IO ()
writeIORef IORef (BufferList Char)
spare_ref BufferList Char
rest
forall (m :: * -> *) a. Monad m => a -> m a
return ( BufferMode
mode, forall e. RawBuffer e -> Int -> BufferState -> Buffer e
emptyBuffer RawCharBuffer
b (forall e. Buffer e -> Int
bufSize Buffer Char
buf) BufferState
WriteBuffer)
BufferList Char
BufferListNil -> do
Buffer Char
new_buf <- Int -> BufferState -> IO (Buffer Char)
newCharBuffer (forall e. Buffer e -> Int
bufSize Buffer Char
buf) BufferState
WriteBuffer
forall (m :: * -> *) a. Monad m => a -> m a
return (BufferMode
mode, Buffer Char
new_buf)
writeBlocks :: Handle -> Bool -> Bool -> Newline -> Buffer CharBufElem -> String -> IO ()
writeBlocks :: Handle -> Bool -> Bool -> Newline -> Buffer Char -> String -> IO ()
writeBlocks Handle
hdl Bool
line_buffered Bool
add_nl Newline
nl
buf :: Buffer Char
buf@Buffer{ bufRaw :: forall e. Buffer e -> RawBuffer e
bufRaw=RawCharBuffer
raw, bufSize :: forall e. Buffer e -> Int
bufSize=Int
len } String
s =
let
shoveString :: Int -> [Char] -> [Char] -> IO ()
shoveString :: Int -> String -> String -> IO ()
shoveString !Int
n [] [] =
Handle -> RawCharBuffer -> Int -> Int -> Bool -> Bool -> IO ()
commitBuffer Handle
hdl RawCharBuffer
raw Int
len Int
n Bool
False Bool
True
shoveString !Int
n [] String
rest =
Int -> String -> String -> IO ()
shoveString Int
n String
rest []
shoveString !Int
n (Char
c:String
cs) String
rest
| Int
n forall a. Num a => a -> a -> a
+ Int
1 forall a. Ord a => a -> a -> Bool
>= Int
len = do
Handle -> RawCharBuffer -> Int -> Int -> Bool -> Bool -> IO ()
commitBuffer Handle
hdl RawCharBuffer
raw Int
len Int
n Bool
False Bool
False
Int -> String -> String -> IO ()
shoveString Int
0 (Char
cforall a. a -> [a] -> [a]
:String
cs) String
rest
| Char
c forall a. Eq a => a -> a -> Bool
== Char
'\n' = do
Int
n' <- if Newline
nl forall a. Eq a => a -> a -> Bool
== Newline
CRLF
then do
Int
n1 <- RawCharBuffer -> Int -> Char -> IO Int
writeCharBuf RawCharBuffer
raw Int
n Char
'\r'
RawCharBuffer -> Int -> Char -> IO Int
writeCharBuf RawCharBuffer
raw Int
n1 Char
'\n'
else
RawCharBuffer -> Int -> Char -> IO Int
writeCharBuf RawCharBuffer
raw Int
n Char
c
if Bool
line_buffered
then do
Handle -> RawCharBuffer -> Int -> Int -> Bool -> Bool -> IO ()
commitBuffer Handle
hdl RawCharBuffer
raw Int
len Int
n' Bool
True Bool
False
Int -> String -> String -> IO ()
shoveString Int
0 String
cs String
rest
else
Int -> String -> String -> IO ()
shoveString Int
n' String
cs String
rest
| Bool
otherwise = do
Int
n' <- RawCharBuffer -> Int -> Char -> IO Int
writeCharBuf RawCharBuffer
raw Int
n Char
c
Int -> String -> String -> IO ()
shoveString Int
n' String
cs String
rest
in
Int -> String -> String -> IO ()
shoveString Int
0 String
s (if Bool
add_nl then String
"\n" else String
"")
commitBuffer :: Handle
-> RawCharBuffer -> Int
-> Int
-> Bool
-> Bool
-> IO ()
commitBuffer :: Handle -> RawCharBuffer -> Int -> Int -> Bool -> Bool -> IO ()
commitBuffer Handle
hdl !RawCharBuffer
raw !Int
sz !Int
count Bool
flush Bool
release =
forall a. String -> Handle -> (Handle__ -> IO a) -> IO a
wantWritableHandle String
"commitBuffer" Handle
hdl forall a b. (a -> b) -> a -> b
$ \h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..} -> do
let debugMsg :: String
debugMsg = (String
"commitBuffer: sz=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
sz forall a. [a] -> [a] -> [a]
++ String
", count=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
count
forall a. [a] -> [a] -> [a]
++ String
", flush=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Bool
flush forall a. [a] -> [a] -> [a]
++ String
", release=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Bool
release
forall a. [a] -> [a] -> [a]
++ String
", handle=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Handle
hdl)
String -> IO ()
debugIO String
debugMsg
Handle__ -> Buffer Char -> IO ()
writeCharBuffer Handle__
h_ Buffer{ bufRaw :: RawCharBuffer
bufRaw=RawCharBuffer
raw, bufState :: BufferState
bufState=BufferState
WriteBuffer, bufOffset :: Word64
bufOffset=Word64
0,
bufL :: Int
bufL=Int
0, bufR :: Int
bufR=Int
count, bufSize :: Int
bufSize=Int
sz }
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
flush forall a b. (a -> b) -> a -> b
$ Handle__ -> IO ()
flushByteWriteBuffer Handle__
h_
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
release forall a b. (a -> b) -> a -> b
$ do
old_buf :: Buffer Char
old_buf@Buffer{ bufSize :: forall e. Buffer e -> Int
bufSize=Int
size } <- forall a. IORef a -> IO a
readIORef IORef (Buffer Char)
haCharBuffer
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
sz forall a. Eq a => a -> a -> Bool
== Int
size) forall a b. (a -> b) -> a -> b
$ do
BufferList Char
spare_bufs <- forall a. IORef a -> IO a
readIORef IORef (BufferList Char)
haBuffers
forall a. IORef a -> a -> IO ()
writeIORef IORef (BufferList Char)
haBuffers (forall e. RawBuffer e -> BufferList e -> BufferList e
BufferListCons RawCharBuffer
raw BufferList Char
spare_bufs)
commitBuffer' :: RawCharBuffer -> Int -> Int -> Bool -> Bool -> Handle__
-> IO CharBuffer
commitBuffer' :: RawCharBuffer
-> Int -> Int -> Bool -> Bool -> Handle__ -> IO (Buffer Char)
commitBuffer' RawCharBuffer
raw sz :: Int
sz@(I# Int#
_) count :: Int
count@(I# Int#
_) Bool
flush Bool
release h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..}
= do
String -> IO ()
debugIO (String
"commitBuffer: sz=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
sz forall a. [a] -> [a] -> [a]
++ String
", count=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
count
forall a. [a] -> [a] -> [a]
++ String
", flush=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Bool
flush forall a. [a] -> [a] -> [a]
++ String
", release=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Bool
release)
let this_buf :: Buffer Char
this_buf = Buffer{ bufRaw :: RawCharBuffer
bufRaw=RawCharBuffer
raw, bufState :: BufferState
bufState=BufferState
WriteBuffer,
bufL :: Int
bufL=Int
0, bufR :: Int
bufR=Int
count, bufSize :: Int
bufSize=Int
sz, bufOffset :: Word64
bufOffset=Word64
0 }
Handle__ -> Buffer Char -> IO ()
writeCharBuffer Handle__
h_ Buffer Char
this_buf
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
flush forall a b. (a -> b) -> a -> b
$ Handle__ -> IO ()
flushByteWriteBuffer Handle__
h_
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
release forall a b. (a -> b) -> a -> b
$ do
old_buf :: Buffer Char
old_buf@Buffer{ bufSize :: forall e. Buffer e -> Int
bufSize=Int
size } <- forall a. IORef a -> IO a
readIORef IORef (Buffer Char)
haCharBuffer
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
sz forall a. Eq a => a -> a -> Bool
== Int
size) forall a b. (a -> b) -> a -> b
$ do
BufferList Char
spare_bufs <- forall a. IORef a -> IO a
readIORef IORef (BufferList Char)
haBuffers
forall a. IORef a -> a -> IO ()
writeIORef IORef (BufferList Char)
haBuffers (forall e. RawBuffer e -> BufferList e -> BufferList e
BufferListCons RawCharBuffer
raw BufferList Char
spare_bufs)
forall (m :: * -> *) a. Monad m => a -> m a
return Buffer Char
this_buf
hPutBuf :: Handle
-> Ptr a
-> Int
-> IO ()
hPutBuf :: forall a. Handle -> Ptr a -> Int -> IO ()
hPutBuf Handle
h Ptr a
ptr Int
count = do Int
_ <- forall a. Handle -> Ptr a -> Int -> Bool -> IO Int
hPutBuf' Handle
h Ptr a
ptr Int
count Bool
True
forall (m :: * -> *) a. Monad m => a -> m a
return ()
hPutBufNonBlocking
:: Handle
-> Ptr a
-> Int
-> IO Int
hPutBufNonBlocking :: forall a. Handle -> Ptr a -> Int -> IO Int
hPutBufNonBlocking Handle
h Ptr a
ptr Int
count = forall a. Handle -> Ptr a -> Int -> Bool -> IO Int
hPutBuf' Handle
h Ptr a
ptr Int
count Bool
False
hPutBuf':: Handle
-> Ptr a
-> Int
-> Bool
-> IO Int
hPutBuf' :: forall a. Handle -> Ptr a -> Int -> Bool -> IO Int
hPutBuf' Handle
handle Ptr a
ptr Int
count Bool
can_block
| Int
count forall a. Eq a => a -> a -> Bool
== Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return Int
0
| Int
count forall a. Ord a => a -> a -> Bool
< Int
0 = forall a. Handle -> String -> Int -> IO a
illegalBufferSize Handle
handle String
"hPutBuf" Int
count
| Bool
otherwise =
forall a. String -> Handle -> (Handle__ -> IO a) -> IO a
wantWritableHandle String
"hPutBuf" Handle
handle forall a b. (a -> b) -> a -> b
$
\ h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..} -> do
String -> IO ()
debugIO (String
"hPutBuf count=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
count)
Int
r <- Handle__ -> Ptr Word8 -> Int -> Bool -> IO Int
bufWrite Handle__
h_ (forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Int
count Bool
can_block
case BufferMode
haBufferMode of
BlockBuffering Maybe Int
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
BufferMode
_line_or_no_buffering -> Handle__ -> IO ()
flushWriteBuffer Handle__
h_
forall (m :: * -> *) a. Monad m => a -> m a
return Int
r
bufWrite :: Handle__-> Ptr Word8 -> Int -> Bool -> IO Int
bufWrite :: Handle__ -> Ptr Word8 -> Int -> Bool -> IO Int
bufWrite h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..} Ptr Word8
ptr !Int
count Bool
can_block = do
old_buf :: Buffer Word8
old_buf@Buffer{ bufR :: forall e. Buffer e -> Int
bufR=Int
w, bufSize :: forall e. Buffer e -> Int
bufSize=Int
size }
<- forall a. IORef a -> IO a
readIORef IORef (Buffer Word8)
haByteBuffer
Int
b <- if (Int
count forall a. Ord a => a -> a -> Bool
< Int
size Bool -> Bool -> Bool
&& Int
count forall a. Ord a => a -> a -> Bool
<= Int
size forall a. Num a => a -> a -> a
- Int
w)
then Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> IO Int
bufferChunk Handle__
h_ Buffer Word8
old_buf Ptr Word8
ptr Int
count
else do
Buffer Word8
flushed_buf <- Handle__ -> Buffer Word8 -> IO (Buffer Word8)
flushByteWriteBufferGiven Handle__
h_ Buffer Word8
old_buf
if Int
count forall a. Ord a => a -> a -> Bool
< Int
size
then Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> IO Int
bufferChunk Handle__
h_ Buffer Word8
flushed_buf Ptr Word8
ptr Int
count
else do
let offset :: Word64
offset = forall e. Buffer e -> Word64
bufOffset Buffer Word8
flushed_buf
!Int
bytes <- if Bool
can_block
then Handle__ -> Ptr Word8 -> Word64 -> Int -> IO Int
writeChunk Handle__
h_ (forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
ptr) Word64
offset Int
count
else Handle__ -> Ptr Word8 -> Word64 -> Int -> IO Int
writeChunkNonBlocking Handle__
h_ (forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
ptr) Word64
offset Int
count
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
haByteBuffer forall a b. (a -> b) -> a -> b
$! forall e. Int -> Buffer e -> Buffer e
bufferAddOffset Int
bytes Buffer Word8
flushed_buf
forall (m :: * -> *) a. Monad m => a -> m a
return Int
bytes
String -> IO ()
debugIO String
"hPutBuf: done"
forall (m :: * -> *) a. Monad m => a -> m a
return Int
b
flushByteWriteBufferGiven :: Handle__ -> Buffer Word8 -> IO (Buffer Word8)
flushByteWriteBufferGiven :: Handle__ -> Buffer Word8 -> IO (Buffer Word8)
flushByteWriteBufferGiven h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..} Buffer Word8
bbuf =
if (Bool -> Bool
not (forall e. Buffer e -> Bool
isEmptyBuffer Buffer Word8
bbuf))
then do
Buffer Word8
bbuf' <- forall dev.
BufferedIO dev =>
dev -> Buffer Word8 -> IO (Buffer Word8)
Buffered.flushWriteBuffer dev
haDevice Buffer Word8
bbuf
String -> IO ()
debugIO (String
"flushByteWriteBufferGiven: bbuf=" forall a. [a] -> [a] -> [a]
++ forall a. Buffer a -> String
summaryBuffer Buffer Word8
bbuf')
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
haByteBuffer Buffer Word8
bbuf'
forall (m :: * -> *) a. Monad m => a -> m a
return Buffer Word8
bbuf'
else
forall (m :: * -> *) a. Monad m => a -> m a
return Buffer Word8
bbuf
bufferChunk :: Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> IO Int
bufferChunk :: Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> IO Int
bufferChunk h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..} old_buf :: Buffer Word8
old_buf@Buffer{ bufRaw :: forall e. Buffer e -> RawBuffer e
bufRaw=RawBuffer Word8
raw, bufR :: forall e. Buffer e -> Int
bufR=Int
w, bufSize :: forall e. Buffer e -> Int
bufSize=Int
size } Ptr Word8
ptr !Int
count = do
String -> IO ()
debugIO (String
"hPutBuf: copying to buffer, w=" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
w)
forall e. RawBuffer e -> Int -> Ptr e -> Int -> IO ()
copyToRawBuffer RawBuffer Word8
raw Int
w Ptr Word8
ptr Int
count
let copied_buf :: Buffer Word8
copied_buf = Buffer Word8
old_buf{ bufR :: Int
bufR = Int
w forall a. Num a => a -> a -> a
+ Int
count }
if forall e. Buffer e -> Bool
isFullBuffer Buffer Word8
copied_buf
then do
String -> IO ()
debugIO String
"hPutBuf: flushing full buffer after writing"
Buffer Word8
_ <- Handle__ -> Buffer Word8 -> IO (Buffer Word8)
flushByteWriteBufferGiven Handle__
h_ Buffer Word8
copied_buf
forall (m :: * -> *) a. Monad m => a -> m a
return ()
else
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
haByteBuffer Buffer Word8
copied_buf
forall (m :: * -> *) a. Monad m => a -> m a
return Int
count
writeChunk :: Handle__ -> Ptr Word8 -> Word64 -> Int -> IO Int
writeChunk :: Handle__ -> Ptr Word8 -> Word64 -> Int -> IO Int
writeChunk h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..} Ptr Word8
ptr Word64
offset Int
bytes
= do forall a. RawIO a => a -> Ptr Word8 -> Word64 -> Int -> IO ()
RawIO.write dev
haDevice Ptr Word8
ptr Word64
offset Int
bytes
forall (m :: * -> *) a. Monad m => a -> m a
return Int
bytes
writeChunkNonBlocking :: Handle__ -> Ptr Word8 -> Word64 -> Int -> IO Int
writeChunkNonBlocking :: Handle__ -> Ptr Word8 -> Word64 -> Int -> IO Int
writeChunkNonBlocking h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..} Ptr Word8
ptr Word64
offset Int
bytes
= forall a. RawIO a => a -> Ptr Word8 -> Word64 -> Int -> IO Int
RawIO.writeNonBlocking dev
haDevice Ptr Word8
ptr Word64
offset Int
bytes
hGetBuf :: Handle -> Ptr a -> Int -> IO Int
hGetBuf :: forall a. Handle -> Ptr a -> Int -> IO Int
hGetBuf Handle
h !Ptr a
ptr Int
count
| Int
count forall a. Eq a => a -> a -> Bool
== Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return Int
0
| Int
count forall a. Ord a => a -> a -> Bool
< Int
0 = forall a. Handle -> String -> Int -> IO a
illegalBufferSize Handle
h String
"hGetBuf" Int
count
| Bool
otherwise =
forall a. String -> Handle -> (Handle__ -> IO a) -> IO a
wantReadableHandle_ String
"hGetBuf" Handle
h forall a b. (a -> b) -> a -> b
$ \ h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..} -> do
String -> IO ()
debugIO forall a b. (a -> b) -> a -> b
$ String
":: hGetBuf - " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Handle
h forall a. [a] -> [a] -> [a]
++ String
" - " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
count
Handle__ -> IO ()
flushCharReadBuffer Handle__
h_
buf :: Buffer Word8
buf@Buffer{ bufRaw :: forall e. Buffer e -> RawBuffer e
bufRaw=RawBuffer Word8
raw, bufR :: forall e. Buffer e -> Int
bufR=Int
w, bufL :: forall e. Buffer e -> Int
bufL=Int
r, bufSize :: forall e. Buffer e -> Int
bufSize=Int
sz }
<- forall a. IORef a -> IO a
readIORef IORef (Buffer Word8)
haByteBuffer
String -> IO ()
debugIO (String
"hGetBuf: " forall a. [a] -> [a] -> [a]
++ forall a. Buffer a -> String
summaryBuffer Buffer Word8
buf)
Int
res <- if forall e. Buffer e -> Bool
isEmptyBuffer Buffer Word8
buf
then Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadEmpty Handle__
h_ Buffer Word8
buf (forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Int
0 Int
count
else Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadNonEmpty Handle__
h_ Buffer Word8
buf (forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Int
0 Int
count
String -> IO ()
debugIO String
"** hGetBuf done."
forall (m :: * -> *) a. Monad m => a -> m a
return Int
res
bufReadNonEmpty :: Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadNonEmpty :: Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadNonEmpty h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..}
buf :: Buffer Word8
buf@Buffer{ bufRaw :: forall e. Buffer e -> RawBuffer e
bufRaw=RawBuffer Word8
raw, bufR :: forall e. Buffer e -> Int
bufR=Int
w, bufL :: forall e. Buffer e -> Int
bufL=Int
r, bufSize :: forall e. Buffer e -> Int
bufSize=Int
sz }
Ptr Word8
ptr !Int
so_far !Int
count
= do
String -> IO ()
debugIO String
":: bufReadNonEmpty"
let avail :: Int
avail = Int
w forall a. Num a => a -> a -> a
- Int
r
if (Int
count forall a. Ord a => a -> a -> Bool
< Int
avail)
then do
forall e. Ptr e -> RawBuffer e -> Int -> Int -> IO ()
copyFromRawBuffer Ptr Word8
ptr RawBuffer Word8
raw Int
r Int
count
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
haByteBuffer Buffer Word8
buf{ bufL :: Int
bufL = Int
r forall a. Num a => a -> a -> a
+ Int
count }
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
so_far forall a. Num a => a -> a -> a
+ Int
count)
else do
forall e. Ptr e -> RawBuffer e -> Int -> Int -> IO ()
copyFromRawBuffer Ptr Word8
ptr RawBuffer Word8
raw Int
r Int
avail
let buf' :: Buffer Word8
buf' = Buffer Word8
buf{ bufR :: Int
bufR=Int
0, bufL :: Int
bufL=Int
0 }
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
haByteBuffer Buffer Word8
buf'
let remaining :: Int
remaining = Int
count forall a. Num a => a -> a -> a
- Int
avail
so_far' :: Int
so_far' = Int
so_far forall a. Num a => a -> a -> a
+ Int
avail
ptr' :: Ptr b
ptr' = Ptr Word8
ptr forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
avail
String -> IO ()
debugIO (String
"bufReadNonEmpty: " forall a. [a] -> [a] -> [a]
++ forall a. Buffer a -> String
summaryBuffer Buffer Word8
buf' forall a. [a] -> [a] -> [a]
++ String
" s:" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
so_far' forall a. [a] -> [a] -> [a]
++ String
" r:" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
remaining)
Int
b <- if Int
remaining forall a. Eq a => a -> a -> Bool
== Int
0
then forall (m :: * -> *) a. Monad m => a -> m a
return Int
so_far'
else Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadEmpty Handle__
h_ Buffer Word8
buf' forall {b}. Ptr b
ptr' Int
so_far' Int
remaining
String -> IO ()
debugIO String
":: bufReadNonEmpty - done"
forall (m :: * -> *) a. Monad m => a -> m a
return Int
b
bufReadEmpty :: Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadEmpty :: Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadEmpty h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..}
buf :: Buffer Word8
buf@Buffer{ bufRaw :: forall e. Buffer e -> RawBuffer e
bufRaw=RawBuffer Word8
raw, bufR :: forall e. Buffer e -> Int
bufR=Int
w, bufL :: forall e. Buffer e -> Int
bufL=Int
_r, bufSize :: forall e. Buffer e -> Int
bufSize=Int
sz, bufOffset :: forall e. Buffer e -> Word64
bufOffset=Word64
bff }
Ptr Word8
ptr Int
so_far Int
count
| Int
count forall a. Ord a => a -> a -> Bool
> Int
sz
= do
Int
bytes_read <- forall dev. RawIO dev => dev -> Int -> Word64 -> Int -> IO Int
loop dev
haDevice Int
0 Word64
bff Int
count
let buf1 :: Buffer Word8
buf1 = forall e. Int -> Buffer e -> Buffer e
bufferAddOffset (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Int
bytes_read forall a. Num a => a -> a -> a
- Int
so_far) Buffer Word8
buf
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
haByteBuffer Buffer Word8
buf1
String -> IO ()
debugIO (String
"bufReadEmpty1.1: " forall a. [a] -> [a] -> [a]
++ forall a. Buffer a -> String
summaryBuffer Buffer Word8
buf1 forall a. [a] -> [a] -> [a]
++ String
" read:" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
bytes_read)
forall (m :: * -> *) a. Monad m => a -> m a
return Int
bytes_read
| Bool
otherwise = do
(Int
r,Buffer Word8
buf') <- forall dev.
BufferedIO dev =>
dev -> Buffer Word8 -> IO (Int, Buffer Word8)
Buffered.fillReadBuffer dev
haDevice Buffer Word8
buf
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
haByteBuffer Buffer Word8
buf'
if Int
r forall a. Eq a => a -> a -> Bool
== Int
0
then forall (m :: * -> *) a. Monad m => a -> m a
return Int
so_far
else Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadNonEmpty Handle__
h_ Buffer Word8
buf' Ptr Word8
ptr Int
so_far Int
count
where
loop :: RawIO.RawIO dev => dev -> Int -> Word64 -> Int -> IO Int
loop :: forall dev. RawIO dev => dev -> Int -> Word64 -> Int -> IO Int
loop dev
dev Int
delta Word64
off Int
bytes | Int
bytes forall a. Ord a => a -> a -> Bool
<= Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return (Int
so_far forall a. Num a => a -> a -> a
+ Int
delta)
loop dev
dev Int
delta Word64
off Int
bytes = do
Int
r <- forall a. RawIO a => a -> Ptr Word8 -> Word64 -> Int -> IO Int
RawIO.read dev
dev (Ptr Word8
ptr forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
delta) Word64
off Int
bytes
String -> IO ()
debugIO forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show Ptr Word8
ptr forall a. [a] -> [a] -> [a]
++ String
" - loop read@" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
delta forall a. [a] -> [a] -> [a]
++ String
": " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
r
String -> IO ()
debugIO forall a b. (a -> b) -> a -> b
$ String
"next:" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (Int
delta forall a. Num a => a -> a -> a
+ Int
r) forall a. [a] -> [a] -> [a]
++ String
" - left:" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (Int
bytes forall a. Num a => a -> a -> a
- Int
r)
if Int
r forall a. Eq a => a -> a -> Bool
== Int
0
then forall (m :: * -> *) a. Monad m => a -> m a
return (Int
so_far forall a. Num a => a -> a -> a
+ Int
delta)
else forall dev. RawIO dev => dev -> Int -> Word64 -> Int -> IO Int
loop dev
dev (Int
delta forall a. Num a => a -> a -> a
+ Int
r) (Word64
off forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
r) (Int
bytes forall a. Num a => a -> a -> a
- Int
r)
hGetBufSome :: Handle -> Ptr a -> Int -> IO Int
hGetBufSome :: forall a. Handle -> Ptr a -> Int -> IO Int
hGetBufSome Handle
h !Ptr a
ptr Int
count
| Int
count forall a. Eq a => a -> a -> Bool
== Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return Int
0
| Int
count forall a. Ord a => a -> a -> Bool
< Int
0 = forall a. Handle -> String -> Int -> IO a
illegalBufferSize Handle
h String
"hGetBufSome" Int
count
| Bool
otherwise =
forall a. String -> Handle -> (Handle__ -> IO a) -> IO a
wantReadableHandle_ String
"hGetBufSome" Handle
h forall a b. (a -> b) -> a -> b
$ \ h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..} -> do
Handle__ -> IO ()
flushCharReadBuffer Handle__
h_
buf :: Buffer Word8
buf@Buffer{ bufSize :: forall e. Buffer e -> Int
bufSize=Int
sz, bufOffset :: forall e. Buffer e -> Word64
bufOffset=Word64
offset } <- forall a. IORef a -> IO a
readIORef IORef (Buffer Word8)
haByteBuffer
if forall e. Buffer e -> Bool
isEmptyBuffer Buffer Word8
buf
then case Int
count forall a. Ord a => a -> a -> Bool
> Int
sz of
Bool
True -> do Int
bytes <- forall a. RawIO a => a -> Ptr Word8 -> Word64 -> Int -> IO Int
RawIO.read dev
haDevice (forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Word64
offset Int
count
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
haByteBuffer forall a b. (a -> b) -> a -> b
$! forall e. Int -> Buffer e -> Buffer e
bufferAddOffset Int
bytes Buffer Word8
buf
forall (m :: * -> *) a. Monad m => a -> m a
return Int
bytes
Bool
_ -> do (Int
r,Buffer Word8
buf') <- forall dev.
BufferedIO dev =>
dev -> Buffer Word8 -> IO (Int, Buffer Word8)
Buffered.fillReadBuffer dev
haDevice Buffer Word8
buf
if Int
r forall a. Eq a => a -> a -> Bool
== Int
0
then forall (m :: * -> *) a. Monad m => a -> m a
return Int
0
else do forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
haByteBuffer Buffer Word8
buf'
Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadNBNonEmpty Handle__
h_ Buffer Word8
buf' (forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Int
0 (forall a. Ord a => a -> a -> a
min Int
r Int
count)
else
let count' :: Int
count' = forall a. Ord a => a -> a -> a
min Int
count (forall e. Buffer e -> Int
bufferElems Buffer Word8
buf)
in Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadNBNonEmpty Handle__
h_ Buffer Word8
buf (forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Int
0 Int
count'
hGetBufNonBlocking :: Handle -> Ptr a -> Int -> IO Int
hGetBufNonBlocking :: forall a. Handle -> Ptr a -> Int -> IO Int
hGetBufNonBlocking Handle
h !Ptr a
ptr Int
count
| Int
count forall a. Eq a => a -> a -> Bool
== Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return Int
0
| Int
count forall a. Ord a => a -> a -> Bool
< Int
0 = forall a. Handle -> String -> Int -> IO a
illegalBufferSize Handle
h String
"hGetBufNonBlocking" Int
count
| Bool
otherwise =
forall a. String -> Handle -> (Handle__ -> IO a) -> IO a
wantReadableHandle_ String
"hGetBufNonBlocking" Handle
h forall a b. (a -> b) -> a -> b
$ \ h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..} -> do
Handle__ -> IO ()
flushCharReadBuffer Handle__
h_
buf :: Buffer Word8
buf@Buffer{ bufRaw :: forall e. Buffer e -> RawBuffer e
bufRaw=RawBuffer Word8
raw, bufR :: forall e. Buffer e -> Int
bufR=Int
w, bufL :: forall e. Buffer e -> Int
bufL=Int
r, bufSize :: forall e. Buffer e -> Int
bufSize=Int
sz }
<- forall a. IORef a -> IO a
readIORef IORef (Buffer Word8)
haByteBuffer
if forall e. Buffer e -> Bool
isEmptyBuffer Buffer Word8
buf
then Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadNBEmpty Handle__
h_ Buffer Word8
buf (forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Int
0 Int
count
else Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadNBNonEmpty Handle__
h_ Buffer Word8
buf (forall a b. Ptr a -> Ptr b
castPtr Ptr a
ptr) Int
0 Int
count
bufReadNBEmpty :: Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadNBEmpty :: Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadNBEmpty h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..}
buf :: Buffer Word8
buf@Buffer{ bufRaw :: forall e. Buffer e -> RawBuffer e
bufRaw=RawBuffer Word8
raw, bufR :: forall e. Buffer e -> Int
bufR=Int
w, bufL :: forall e. Buffer e -> Int
bufL=Int
_r, bufSize :: forall e. Buffer e -> Int
bufSize=Int
sz
, bufOffset :: forall e. Buffer e -> Word64
bufOffset=Word64
offset }
Ptr Word8
ptr Int
so_far Int
count
| Int
count forall a. Ord a => a -> a -> Bool
> Int
sz = do
Maybe Int
m <- forall a.
RawIO a =>
a -> Ptr Word8 -> Word64 -> Int -> IO (Maybe Int)
RawIO.readNonBlocking dev
haDevice Ptr Word8
ptr Word64
offset Int
count
case Maybe Int
m of
Maybe Int
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return Int
so_far
Just Int
n -> do
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
haByteBuffer forall a b. (a -> b) -> a -> b
$! forall e. Int -> Buffer e -> Buffer e
bufferAddOffset Int
n Buffer Word8
buf
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
so_far forall a. Num a => a -> a -> a
+ Int
n)
| Bool
otherwise = do
(Maybe Int
r,Buffer Word8
buf') <- forall dev.
BufferedIO dev =>
dev -> Buffer Word8 -> IO (Maybe Int, Buffer Word8)
Buffered.fillReadBuffer0 dev
haDevice Buffer Word8
buf
case Maybe Int
r of
Maybe Int
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return Int
so_far
Just Int
0 -> forall (m :: * -> *) a. Monad m => a -> m a
return Int
so_far
Just Int
r' -> do
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
haByteBuffer Buffer Word8
buf'
Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadNBNonEmpty Handle__
h_ Buffer Word8
buf' Ptr Word8
ptr Int
so_far (forall a. Ord a => a -> a -> a
min Int
count Int
r')
bufReadNBNonEmpty :: Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadNBNonEmpty :: Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadNBNonEmpty h_ :: Handle__
h_@Handle__{dev
Maybe (MVar Handle__)
Maybe (TextEncoder enc_state)
Maybe (TextDecoder dec_state)
Maybe TextEncoding
IORef (dec_state, Buffer Word8)
IORef (Buffer Char)
IORef (Buffer Word8)
IORef (BufferList Char)
BufferMode
HandleType
Newline
haOtherSide :: Maybe (MVar Handle__)
haOutputNL :: Newline
haInputNL :: Newline
haCodec :: Maybe TextEncoding
haDecoder :: Maybe (TextDecoder dec_state)
haEncoder :: Maybe (TextEncoder enc_state)
haBuffers :: IORef (BufferList Char)
haCharBuffer :: IORef (Buffer Char)
haLastDecode :: IORef (dec_state, Buffer Word8)
haBufferMode :: BufferMode
haByteBuffer :: IORef (Buffer Word8)
haType :: HandleType
haDevice :: dev
haType :: Handle__ -> HandleType
haOutputNL :: Handle__ -> Newline
haOtherSide :: Handle__ -> Maybe (MVar Handle__)
haLastDecode :: ()
haInputNL :: Handle__ -> Newline
haEncoder :: ()
haDevice :: ()
haDecoder :: ()
haCodec :: Handle__ -> Maybe TextEncoding
haCharBuffer :: Handle__ -> IORef (Buffer Char)
haByteBuffer :: Handle__ -> IORef (Buffer Word8)
haBuffers :: Handle__ -> IORef (BufferList Char)
haBufferMode :: Handle__ -> BufferMode
..}
buf :: Buffer Word8
buf@Buffer{ bufRaw :: forall e. Buffer e -> RawBuffer e
bufRaw=RawBuffer Word8
raw, bufR :: forall e. Buffer e -> Int
bufR=Int
w, bufL :: forall e. Buffer e -> Int
bufL=Int
r, bufSize :: forall e. Buffer e -> Int
bufSize=Int
sz }
Ptr Word8
ptr Int
so_far Int
count
= do
let avail :: Int
avail = Int
w forall a. Num a => a -> a -> a
- Int
r
if (Int
count forall a. Ord a => a -> a -> Bool
< Int
avail)
then do
forall e. Ptr e -> RawBuffer e -> Int -> Int -> IO ()
copyFromRawBuffer Ptr Word8
ptr RawBuffer Word8
raw Int
r Int
count
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
haByteBuffer Buffer Word8
buf{ bufL :: Int
bufL = Int
r forall a. Num a => a -> a -> a
+ Int
count }
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
so_far forall a. Num a => a -> a -> a
+ Int
count)
else do
forall e. Ptr e -> RawBuffer e -> Int -> Int -> IO ()
copyFromRawBuffer Ptr Word8
ptr RawBuffer Word8
raw Int
r Int
avail
let buf' :: Buffer Word8
buf' = Buffer Word8
buf{ bufR :: Int
bufR=Int
0, bufL :: Int
bufL=Int
0 }
forall a. IORef a -> a -> IO ()
writeIORef IORef (Buffer Word8)
haByteBuffer Buffer Word8
buf'
let remaining :: Int
remaining = Int
count forall a. Num a => a -> a -> a
- Int
avail
so_far' :: Int
so_far' = Int
so_far forall a. Num a => a -> a -> a
+ Int
avail
ptr' :: Ptr b
ptr' = Ptr Word8
ptr forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
avail
if Int
remaining forall a. Eq a => a -> a -> Bool
== Int
0
then forall (m :: * -> *) a. Monad m => a -> m a
return Int
so_far'
else Handle__ -> Buffer Word8 -> Ptr Word8 -> Int -> Int -> IO Int
bufReadNBEmpty Handle__
h_ Buffer Word8
buf' forall {b}. Ptr b
ptr' Int
so_far' Int
remaining
copyToRawBuffer :: RawBuffer e -> Int -> Ptr e -> Int -> IO ()
copyToRawBuffer :: forall e. RawBuffer e -> Int -> Ptr e -> Int -> IO ()
copyToRawBuffer RawBuffer e
raw Int
off Ptr e
ptr Int
bytes =
forall e a. RawBuffer e -> (Ptr e -> IO a) -> IO a
withRawBuffer RawBuffer e
raw forall a b. (a -> b) -> a -> b
$ \Ptr e
praw ->
do Ptr ()
_ <- forall a. Ptr a -> Ptr a -> CSize -> IO (Ptr ())
memcpy (Ptr e
praw forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
off) Ptr e
ptr (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
bytes)
forall (m :: * -> *) a. Monad m => a -> m a
return ()
copyFromRawBuffer :: Ptr e -> RawBuffer e -> Int -> Int -> IO ()
copyFromRawBuffer :: forall e. Ptr e -> RawBuffer e -> Int -> Int -> IO ()
copyFromRawBuffer Ptr e
ptr RawBuffer e
raw Int
off Int
bytes =
forall e a. RawBuffer e -> (Ptr e -> IO a) -> IO a
withRawBuffer RawBuffer e
raw forall a b. (a -> b) -> a -> b
$ \Ptr e
praw ->
do Ptr ()
_ <- forall a. Ptr a -> Ptr a -> CSize -> IO (Ptr ())
memcpy Ptr e
ptr (Ptr e
praw forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
off) (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
bytes)
forall (m :: * -> *) a. Monad m => a -> m a
return ()
foreign import ccall unsafe "memcpy"
memcpy :: Ptr a -> Ptr a -> CSize -> IO (Ptr ())
illegalBufferSize :: Handle -> String -> Int -> IO a
illegalBufferSize :: forall a. Handle -> String -> Int -> IO a
illegalBufferSize Handle
handle String
fn Int
sz =
forall a. IOError -> IO a
ioException (Maybe Handle
-> IOErrorType
-> String
-> String
-> Maybe CInt
-> Maybe String
-> IOError
IOError (forall a. a -> Maybe a
Just Handle
handle)
IOErrorType
InvalidArgument String
fn
(String
"illegal buffer size " forall a. [a] -> [a] -> [a]
++ forall a. Show a => Int -> a -> ShowS
showsPrec Int
9 Int
sz [])
forall a. Maybe a
Nothing forall a. Maybe a
Nothing)