module UHC.Util.Binary
( module Data.Binary
, module Data.Binary.Get
, module Data.Binary.Put
, module UHC.Util.Control.Monad
, module Data.Typeable
, module Data.Generics
, hGetBinary
, getBinaryFile
, getBinaryFPath
, hPutBinary
, putBinaryFile
, putBinaryFPath
, putEnum, getEnum
, putEnum8, getEnum8
, putList, getList
)
where
import qualified Data.ByteString.Lazy as L
import Data.Typeable
import Data.Generics (Data)
import Data.Binary
import Data.Binary.Put(runPut,putWord16be)
import Data.Binary.Get(runGet,getWord16be)
import System.IO
import Control.Monad
import UHC.Util.Control.Monad
import UHC.Util.FPath
hGetBinary :: Binary a => Handle -> IO a
hGetBinary h
= liftM decode (L.hGetContents h)
getBinaryFile :: Binary a => FilePath -> IO a
getBinaryFile fn
= do { h <- openBinaryFile fn ReadMode
; b <- hGetBinary h
; return b ;
}
getBinaryFPath :: Binary a => FPath -> IO a
getBinaryFPath fp
= getBinaryFile (fpathToStr fp)
hPutBinary :: Binary a => Handle -> a -> IO ()
hPutBinary h pt
= L.hPut h (encode pt)
putBinaryFile :: Binary a => FilePath -> a -> IO ()
putBinaryFile fn pt
= do { h <- openBinaryFile fn WriteMode
; hPutBinary h pt
; hClose h
}
putBinaryFPath :: Binary a => FPath -> a -> IO ()
putBinaryFPath fp pt
= do { fpathEnsureExists fp
; putBinaryFile (fpathToStr fp) pt
}
putEnum :: Enum x => x -> Put
putEnum x = put (fromEnum x)
getEnum :: Enum x => Get x
getEnum = do n <- get
return (toEnum n)
putEnum8 :: Enum x => x -> Put
putEnum8 x = putWord8 (fromIntegral $ fromEnum x)
getEnum8 :: Enum x => Get x
getEnum8 = do n <- getWord8
return (toEnum $ fromIntegral n)
putList :: (Binary a, Binary b) => (x -> Bool) -> (x -> (a,b)) -> x -> Put
putList isNil getCons x | isNil x = putWord8 0
| otherwise = let (a,b) = getCons x in putWord8 1 >> put a >> put b
getList :: (Binary a, Binary b) => x -> (a -> b -> x) -> Get x
getList nil cons
= do tag <- getWord8
case tag of
0 -> return nil
1 -> liftM2 cons get get