<commit_before>{-# LANGUAGE CPP               #-}
{-# LANGUAGE FlexibleInstances #-}

#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Trustworthy       #-}
#endif

-- | Input and output streams for file 'Handle's.
module System.IO.Streams.Handle
 ( -- * Handle conversions
   handleToInputStream
 , handleToOutputStream
 , inputStreamToHandle
 , outputStreamToHandle
 , streamPairToHandle

   -- * Standard system handles
 , stdin
 , stdout
 , stderr
 ) where

------------------------------------------------------------------------------
import           Data.ByteString            (ByteString)
import qualified Data.ByteString            as S
import qualified GHC.IO.Handle              as H
import           System.IO                  (Handle, hFlush)
import qualified System.IO                  as IO
import           System.IO.Unsafe           (unsafePerformIO)
------------------------------------------------------------------------------
import           System.IO.Streams.Internal (InputStream, OutputStream, SP (..), lockingInputStream, lockingOutputStream, makeInputStream, makeOutputStream)


------------------------------------------------------------------------------
bUFSIZ :: Int
bUFSIZ = 32752


------------------------------------------------------------------------------
-- | Converts a read-only handle into an 'InputStream' of strict 'ByteString's.
--
-- Note that the wrapped handle is /not/ closed when it yields end-of-stream;
-- you can use 'System.IO.Streams.Combinators.atEndOfInput' to close the handle
-- if you would like this behaviour.
handleToInputStream :: Handle -> IO (InputStream ByteString)
handleToInputStream h = makeInputStream f
  where
    f = do
        x <- S.hGetSome h bUFSIZ
        return $! if S.null x then Nothing else Just x


------------------------------------------------------------------------------
-- | Converts a writable handle into an 'OutputStream' of strict 'ByteString's.
--
-- Note that the wrapped handle is /not/ closed when it receives end-of-stream;
-- you can use 'System.IO.Streams.Combinators.atEndOfOutput' to close the
-- handle if you would like this behaviour.
--
-- /Note/: to force the 'Handle' to be flushed, you can write a null string to
-- the returned 'OutputStream':
--
-- > Streams.write (Just "") os
handleToOutputStream :: Handle -> IO (OutputStream ByteString)
handleToOutputStream h = makeOutputStream f
  where
    f Nothing  = hFlush h
    f (Just x) = if S.null x
                   then hFlush h
                   else S.hPut h x


------------------------------------------------------------------------------<commit_msg>Added handleToStreams function to match Network module.<commit_after>{-# LANGUAGE CPP               #-}
{-# LANGUAGE FlexibleInstances #-}

#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Trustworthy       #-}
#endif

-- | Input and output streams for file 'Handle's.
module System.IO.Streams.Handle
 ( -- * Handle conversions
   handleToInputStream
 , handleToOutputStream
 , handleToStreams
 , inputStreamToHandle
 , outputStreamToHandle
 , streamPairToHandle

   -- * Standard system handles
 , stdin
 , stdout
 , stderr
 ) where

------------------------------------------------------------------------------
import           Data.ByteString            (ByteString)
import qualified Data.ByteString            as S
import qualified GHC.IO.Handle              as H
import           System.IO                  (Handle, hFlush)
import qualified System.IO                  as IO
import           System.IO.Unsafe           (unsafePerformIO)
------------------------------------------------------------------------------
import           System.IO.Streams.Internal (InputStream, OutputStream, SP (..), lockingInputStream, lockingOutputStream, makeInputStream, makeOutputStream)


------------------------------------------------------------------------------
bUFSIZ :: Int
bUFSIZ = 32752


------------------------------------------------------------------------------
-- | Converts a read-only handle into an 'InputStream' of strict 'ByteString's.
--
-- Note that the wrapped handle is /not/ closed when it yields end-of-stream;
-- you can use 'System.IO.Streams.Combinators.atEndOfInput' to close the handle
-- if you would like this behaviour.
handleToInputStream :: Handle -> IO (InputStream ByteString)
handleToInputStream h = makeInputStream f
  where
    f = do
        x <- S.hGetSome h bUFSIZ
        return $! if S.null x then Nothing else Just x


------------------------------------------------------------------------------
-- | Converts a writable handle into an 'OutputStream' of strict 'ByteString's.
--
-- Note that the wrapped handle is /not/ closed when it receives end-of-stream;
-- you can use 'System.IO.Streams.Combinators.atEndOfOutput' to close the
-- handle if you would like this behaviour.
--
-- /Note/: to force the 'Handle' to be flushed, you can write a null string to
-- the returned 'OutputStream':
--
-- > Streams.write (Just "") os
handleToOutputStream :: Handle -> IO (OutputStream ByteString)
handleToOutputStream h = makeOutputStream f
  where
    f Nothing  = hFlush h
    f (Just x) = if S.null x
                   then hFlush h
                   else S.hPut h x


------------------------------------------------------------------------------
-- | Converts a readable and writable handle into an 'InputStream'/'OutputStream'
-- of strict 'ByteString's.
--
-- Note that the wrapped handle is /not/ closed when it receives
-- end-of-stream; you can use
-- 'System.IO.Streams.Combinators.atEndOfOutput' to close the handle
-- if you would like this behaviour.
--
-- /Note/: to force the 'Handle' to be flushed, you can write a null string to
-- the returned 'OutputStream':
--
-- > Streams.write (Just "") os
handleToStreams :: Handle
                -> IO (InputStream ByteString, OutputStream ByteString)
handleToStreams h = do
    is <- handleToInputStream h
    os <- handleToOutputStream h
    return $! (is, os)


------------------------------------------------------------------------------
{'fraction_non_alphanumeric': 0.2436405682193591, 'fraction_numerical': 0.002642880740006607, 'mean_word_length': 1.5208159866777686, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7565966', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1434, 'n_words': 519}