{-# LINE 1 "src/Codec/Compression/BZip/Foreign/Common.chs" #-}
{-# LANGUAGE DeriveDataTypeable #-}
module Codec.Compression.BZip.Foreign.Common (
BZAction (..)
, BZError (..)
, BzStream
, bzWrap
, bZ2BzlibVersion
) where
import qualified Foreign.C.String as C2HSImp
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Ptr as C2HSImp
import qualified System.IO.Unsafe as C2HSImp
import Control.Applicative
import Control.Exception (Exception, throw)
import Data.Typeable (Typeable)
import Foreign.C.Types (CInt)
data BZAction = BzRun
| BzFlush
| BzFinish
instance Enum BZAction where
succ :: BZAction -> BZAction
succ BZAction
BzRun = BZAction
BzFlush
succ BZAction
BzFlush = BZAction
BzFinish
succ BZAction
BzFinish = error [Char]
"BZAction.succ: BzFinish has no successor"
pred :: BZAction -> BZAction
pred BZAction
BzFlush = BZAction
BzRun
pred BZAction
BzFinish = BZAction
BzFlush
pred BZAction
BzRun = [Char] -> BZAction
forall a. HasCallStack => [Char] -> a
error [Char]
"BZAction.pred: BzRun has no predecessor"
enumFromTo from to = go from
where
end = fromEnum to
go v = case compare (fromEnum v) end of
LT -> v : go (succ v)
EQ -> [v]
GT -> []
enumFrom from = enumFromTo from BzFinish
fromEnum BzRun = 0
fromEnum BzFlush = 1
fromEnum BzFinish = 2
toEnum 0 = BzRun
toEnum 1 = BzFlush
toEnum 2 = BzFinish
toEnum unmatched = error ("BZAction.toEnum: Cannot match " ++ show unmatched)
{-# LINE 24 "src/Codec/Compression/BZip/Foreign/Common.chs" #-}
data BZError = BzConfigError
| BzOutbuffFull
| BzUnexpectedEof
| BzIoError
| BzDataErrorMagic
| BzDataError
| BzMemError
| BzParamError
| BzSequenceError
| BzOk
| BzRunOk
| BzFlushOk
| BzFinishOk
| BzStreamEnd
deriving (Eq,Show,Typeable)
instance Enum BZError where
succ BzConfigError = BzOutbuffFull
succ BzOutbuffFull = BzUnexpectedEof
succ BzUnexpectedEof = BzIoError
succ BzIoError = BzDataErrorMagic
succ BzDataErrorMagic = BzDataError
succ BzDataError = BzMemError
succ BzMemError = BzParamError
succ BzParamError = BzSequenceError
succ BzSequenceError = BzOk
succ BzOk = BzRunOk
succ BzRunOk = BzFlushOk
succ BzFlushOk = BzFinishOk
succ BzFinishOk = BzStreamEnd
succ BzStreamEnd = error "BZError.succ: BzStreamEnd has no successor"
pred BzOutbuffFull = BzConfigError
pred BzUnexpectedEof = BzOutbuffFull
pred BzIoError = BzUnexpectedEof
pred BzDataErrorMagic = BzIoError
pred BzDataError = BzDataErrorMagic
pred BzMemError = BzDataError
pred BzParamError = BzMemError
pred BzSequenceError = BzParamError
pred BzOk = BzSequenceError
pred BzRunOk = BzOk
pred BzFlushOk = BzRunOk
pred BzFinishOk = BzFlushOk
pred BzStreamEnd = BzFinishOk
pred BzConfigError = error "BZError.pred: BzConfigError has no predecessor"
enumFromTo :: BZError -> BZError -> [BZError]
enumFromTo BZError
from BZError
to = BZError -> [BZError]
forall {t}. Enum t => t -> [t]
go BZError
from
where
end :: Int
end = BZError -> Int
forall a. Enum a => a -> Int
fromEnum BZError
to
go :: t -> [t]
go t
v = case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (t -> Int
forall a. Enum a => a -> Int
fromEnum t
v) Int
end of
Ordering
LT -> t
v t -> [t] -> [t]
forall a. a -> [a] -> [a]
: t -> [t]
go (t -> t
forall a. Enum a => a -> a
succ t
v)
Ordering
EQ -> [t
v]
Ordering
GT -> []
enumFrom :: BZError -> [BZError]
enumFrom BZError
from = BZError -> BZError -> [BZError]
forall a. Enum a => a -> a -> [a]
enumFromTo BZError
from BZError
BzStreamEnd
fromEnum :: BZError -> Int
fromEnum BZError
BzConfigError = (-Int
9)
fromEnum BZError
BzOutbuffFull = (-Int
8)
fromEnum BZError
BzUnexpectedEof = (-Int
7)
fromEnum BZError
BzIoError = (-Int
6)
fromEnum BZError
BzDataErrorMagic = (-Int
5)
fromEnum BZError
BzDataError = (-Int
4)
fromEnum BZError
BzMemError = (-Int
3)
fromEnum BZError
BzParamError = (-Int
2)
fromEnum BZError
BzSequenceError = (-Int
1)
fromEnum BZError
BzOk = Int
0
fromEnum BZError
BzRunOk = Int
1
fromEnum BZError
BzFlushOk = Int
2
fromEnum BZError
BzFinishOk = Int
3
fromEnum BZError
BzStreamEnd = Int
4
toEnum :: Int -> BZError
toEnum (-9) = BZError
BzConfigError
toEnum (-8) = BZError
BzOutbuffFull
toEnum (-7) = BZError
BzUnexpectedEof
toEnum (-6) = BZError
BzIoError
toEnum (-5) = BZError
BzDataErrorMagic
toEnum (-4) = BZError
BzDataError
toEnum (-3) = BZError
BzMemError
toEnum (-2) = BZError
BzParamError
toEnum (-1) = BZError
BzSequenceError
toEnum Int
0 = BZError
BzOk
toEnum Int
1 = BZError
BzRunOk
toEnum Int
2 = BZError
BzFlushOk
toEnum Int
3 = BZError
BzFinishOk
toEnum Int
4 = BZError
BzStreamEnd
toEnum Int
unmatched = [Char] -> BZError
forall a. HasCallStack => [Char] -> a
error ([Char]
"BZError.toEnum: Cannot match " [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
unmatched)
{-# LINE 41 "src/Codec/Compression/BZip/Foreign/Common.chs" #-}
instance Exception BZError where
data BzStream
bZ2BzlibVersion :: (String)
bZ2BzlibVersion =
C2HSImp.unsafePerformIO $
bZ2BzlibVersion'_ >>= \res ->
C2HSImp.peekCString res >>= \res' ->
return (res')
{-# LINE 49 "src/Codec/Compression/BZip/Foreign/Common.chs" #-}
bzWrap :: CInt -> IO BZError
bzWrap err =
let err' = toEnum (fromIntegral err) in
case err' of
BzOk -> pure err'
BzRunOk -> pure err'
BzFlushOk -> pure err'
BzFinishOk -> pure err'
BzStreamEnd -> pure err'
x -> throw x
foreign import ccall safe "Codec/Compression/BZip/Foreign/Common.chs.h BZ2_bzlibVersion"
bZ2BzlibVersion'_ :: (IO (C2HSImp.Ptr C2HSImp.CChar))