{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
module Distribution.Utils.IOData
(
IOData (..)
, IODataMode (..)
, KnownIODataMode (..)
, withIOData
, null
, hPutContents
) where
import qualified Data.ByteString.Lazy as LBS
import Distribution.Compat.Prelude hiding (null)
import qualified Prelude
import qualified System.IO
data IOData
= IODataText String
| IODataBinary LBS.ByteString
withIOData :: IOData -> (forall mode. IODataMode mode -> mode -> r) -> r
withIOData (IODataText str) k = k IODataModeText str
withIOData (IODataBinary lbs) k = k IODataModeBinary lbs
null :: IOData -> Bool
null (IODataText s) = Prelude.null s
null (IODataBinary b) = LBS.null b
instance NFData IOData where
rnf (IODataText s) = rnf s
rnf (IODataBinary lbs) = rnf lbs
class NFData mode => KnownIODataMode mode where
hGetIODataContents :: System.IO.Handle -> Prelude.IO mode
toIOData :: mode -> IOData
iodataMode :: IODataMode mode
data IODataMode mode where
IODataModeText :: IODataMode String
IODataModeBinary :: IODataMode LBS.ByteString
instance a ~ Char => KnownIODataMode [a] where
hGetIODataContents h = do
System.IO.hSetBinaryMode h False
System.IO.hGetContents h
toIOData = IODataText
iodataMode = IODataModeText
instance KnownIODataMode LBS.ByteString where
hGetIODataContents h = do
System.IO.hSetBinaryMode h True
LBS.hGetContents h
toIOData = IODataBinary
iodataMode = IODataModeBinary
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
LBS.hPutStr h c
System.IO.hClose h