| 1 |
{-# LANGUAGE DeriveDataTypeable #-} |
| 2 |
|
| 3 |
module Codec.Compression.BZip.Foreign.Common ( -- * Types |
| 4 |
BZAction (..) |
| 5 |
, BZError (..) |
| 6 |
, BzStream |
| 7 |
-- * Helper |
| 8 |
, bzWrap |
| 9 |
-- * Contributed functions |
| 10 |
, bZ2BzlibVersion |
| 11 |
) where |
| 12 |
|
| 13 |
import Control.Applicative |
| 14 |
import Control.Exception (Exception, throw) |
| 15 |
import Data.Typeable (Typeable) |
| 16 |
import Foreign.C.Types (CInt) |
| 17 |
|
| 18 |
#include <bzlib.h> |
| 19 |
|
| 20 |
{# enum define BZAction { BZ_RUN as BzRun |
| 21 |
, BZ_FLUSH as BzFlush |
| 22 |
, BZ_FINISH as BzFinish |
| 23 |
} |
| 24 |
#} |
| 25 |
|
| 26 |
{# enum define BZError { BZ_OK as BzOk |
| 27 |
, BZ_RUN_OK as BzRunOk |
| 28 |
, BZ_FLUSH_OK as BzFlushOk |
| 29 |
, BZ_FINISH_OK as BzFinishOk |
| 30 |
, BZ_STREAM_END as BzStreamEnd |
| 31 |
, BZ_SEQUENCE_ERROR as BzSequenceError |
| 32 |
, BZ_PARAM_ERROR as BzParamError |
| 33 |
, BZ_MEM_ERROR as BzMemError |
| 34 |
, BZ_DATA_ERROR as BzDataError |
| 35 |
, BZ_DATA_ERROR_MAGIC as BzDataErrorMagic |
| 36 |
, BZ_IO_ERROR as BzIoError |
| 37 |
, BZ_UNEXPECTED_EOF as BzUnexpectedEof |
| 38 |
, BZ_OUTBUFF_FULL as BzOutbuffFull |
| 39 |
, BZ_CONFIG_ERROR as BzConfigError |
| 40 |
} deriving (Eq, Show, Typeable) |
| 41 |
#} |
| 42 |
|
| 43 |
instance Exception BZError where |
| 44 |
|
| 45 |
-- | Abstract type |
| 46 |
data BzStream |
| 47 |
|
| 48 |
-- Contributed functions |
| 49 |
{# fun pure BZ2_bzlibVersion as ^ { } -> `String' #} |
| 50 |
|
| 51 |
bzWrap :: CInt -> IO BZError |
| 52 |
bzWrap err = |
| 53 |
let err' = toEnum (fromIntegral err) in |
| 54 |
case err' of |
| 55 |
BzOk -> pure err' |
| 56 |
BzRunOk -> pure err' |
| 57 |
BzFlushOk -> pure err' |
| 58 |
BzFinishOk -> pure err' |
| 59 |
BzStreamEnd -> pure err' |
| 60 |
x -> throw x |