module Distribution.Utils.IOData
(
IOData(..)
, IODataMode(..)
, null
, hGetContents
, hPutContents
) where
import qualified Data.ByteString.Lazy as BS
import Distribution.Compat.Prelude hiding (null)
import qualified Prelude
import qualified System.IO
data IOData = IODataText String
| IODataBinary BS.ByteString
null :: IOData -> Bool
null (IODataText s) = Prelude.null s
null (IODataBinary b) = BS.null b
instance NFData IOData where
rnf (IODataText s) = rnf s
#if MIN_VERSION_bytestring(0,10,0)
rnf (IODataBinary bs) = rnf bs
#else
rnf (IODataBinary bs) = rnf (BS.length bs)
#endif
data IODataMode = IODataModeText | IODataModeBinary
hGetContents :: System.IO.Handle -> IODataMode -> Prelude.IO IOData
hGetContents h IODataModeText = do
System.IO.hSetBinaryMode h False
IODataText <$> System.IO.hGetContents h
hGetContents h IODataModeBinary = do
System.IO.hSetBinaryMode h True
IODataBinary <$> BS.hGetContents h
hPutContents :: System.IO.Handle -> IOData -> Prelude.IO ()
hPutContents h (IODataText c) = do
System.IO.hSetBinaryMode h False
System.IO.hPutStr h c
System.IO.hClose h
hPutContents h (IODataBinary c) = do
System.IO.hSetBinaryMode h True
BS.hPutStr h c
System.IO.hClose h