module Data.Grib.Raw.KeysIterator
(
GribKeysIterator(..)
, gribKeysIteratorNew
, gribKeysIteratorNext
, gribKeysIteratorGetName
, gribKeysIteratorRewind
, gribKeysIteratorDelete
, withGribKeysIterator
, GribKeysIteratorFlag(..)
, gribKeysIteratorSetFlags
) where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Marshal.Utils as C2HSImp
import qualified Foreign.Ptr as C2HSImp
import Control.Exception (bracket, throw)
import Foreign
import Foreign.C
import Data.Grib.Raw.Exception
import Data.Grib.Raw.Handle
import Data.Grib.Raw.Marshal
data GribKeysIteratorFlag = GribKeysIteratorAllKeys
| GribKeysIteratorSkipReadOnly
| GribKeysIteratorSkipOptional
| GribKeysIteratorSkipEditionSpecific
| GribKeysIteratorSkipCoded
| GribKeysIteratorSkipComputed
| GribKeysIteratorSkipDuplicates
| GribKeysIteratorSkipFunction
deriving (Eq,Show)
instance Enum GribKeysIteratorFlag where
succ GribKeysIteratorAllKeys = GribKeysIteratorSkipReadOnly
succ GribKeysIteratorSkipReadOnly = GribKeysIteratorSkipOptional
succ GribKeysIteratorSkipOptional = GribKeysIteratorSkipEditionSpecific
succ GribKeysIteratorSkipEditionSpecific = GribKeysIteratorSkipCoded
succ GribKeysIteratorSkipCoded = GribKeysIteratorSkipComputed
succ GribKeysIteratorSkipComputed = GribKeysIteratorSkipDuplicates
succ GribKeysIteratorSkipDuplicates = GribKeysIteratorSkipFunction
succ GribKeysIteratorSkipFunction = error "GribKeysIteratorFlag.succ: GribKeysIteratorSkipFunction has no successor"
pred GribKeysIteratorSkipReadOnly = GribKeysIteratorAllKeys
pred GribKeysIteratorSkipOptional = GribKeysIteratorSkipReadOnly
pred GribKeysIteratorSkipEditionSpecific = GribKeysIteratorSkipOptional
pred GribKeysIteratorSkipCoded = GribKeysIteratorSkipEditionSpecific
pred GribKeysIteratorSkipComputed = GribKeysIteratorSkipCoded
pred GribKeysIteratorSkipDuplicates = GribKeysIteratorSkipComputed
pred GribKeysIteratorSkipFunction = GribKeysIteratorSkipDuplicates
pred GribKeysIteratorAllKeys = error "GribKeysIteratorFlag.pred: GribKeysIteratorAllKeys 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 GribKeysIteratorSkipFunction
fromEnum GribKeysIteratorAllKeys = 0
fromEnum GribKeysIteratorSkipReadOnly = 1
fromEnum GribKeysIteratorSkipOptional = 2
fromEnum GribKeysIteratorSkipEditionSpecific = 4
fromEnum GribKeysIteratorSkipCoded = 8
fromEnum GribKeysIteratorSkipComputed = 16
fromEnum GribKeysIteratorSkipDuplicates = 32
fromEnum GribKeysIteratorSkipFunction = 64
toEnum 0 = GribKeysIteratorAllKeys
toEnum 1 = GribKeysIteratorSkipReadOnly
toEnum 2 = GribKeysIteratorSkipOptional
toEnum 4 = GribKeysIteratorSkipEditionSpecific
toEnum 8 = GribKeysIteratorSkipCoded
toEnum 16 = GribKeysIteratorSkipComputed
toEnum 32 = GribKeysIteratorSkipDuplicates
toEnum 64 = GribKeysIteratorSkipFunction
toEnum unmatched = error ("GribKeysIteratorFlag.toEnum: Cannot match " ++ show unmatched)
newtype GribKeysIterator = GribKeysIterator (C2HSImp.Ptr (GribKeysIterator)) deriving (Eq, Show)
gribKeysIteratorNew :: (GribHandle)
-> ([GribKeysIteratorFlag])
-> (Maybe String)
-> IO ((GribKeysIterator))
gribKeysIteratorNew a1 a2 a3 =
(withGribHandle) a1 $ \a1' ->
let {a2' = fromFlagList a2} in
maybeWithCString a3 $ \a3' ->
gribKeysIteratorNew'_ a1' a2' a3' >>= \res ->
let {res' = checkKeysIterator res} in
return (res')
where checkKeysIterator kiter@(GribKeysIterator ptr)
| ptr == nullPtr = throw NullPtrReturned
| otherwise = kiter
gribKeysIteratorNext :: (GribKeysIterator) -> IO ((Bool))
gribKeysIteratorNext a1 =
let {a1' = id a1} in
gribKeysIteratorNext'_ a1' >>= \res ->
let {res' = C2HSImp.toBool res} in
return (res')
gribKeysIteratorGetName :: (GribKeysIterator) -> IO ((Key))
gribKeysIteratorGetName a1 =
let {a1' = id a1} in
gribKeysIteratorGetName'_ a1' >>= \res ->
peekCString res >>= \res' ->
return (res')
gribKeysIteratorDelete :: (GribKeysIterator) -> IO ()
gribKeysIteratorDelete a1 =
let {a1' = id a1} in
gribKeysIteratorDelete'_ a1' >>= \res ->
checkStatus res >>
return ()
gribKeysIteratorRewind :: (GribKeysIterator) -> IO ()
gribKeysIteratorRewind a1 =
let {a1' = id a1} in
gribKeysIteratorRewind'_ a1' >>= \res ->
checkStatus res >>
return ()
gribKeysIteratorSetFlags :: (GribKeysIterator) -> ([GribKeysIteratorFlag]) -> IO ()
gribKeysIteratorSetFlags a1 a2 =
let {a1' = id a1} in
let {a2' = fromFlagList a2} in
gribKeysIteratorSetFlags'_ a1' a2' >>= \res ->
checkStatus res >>
return ()
withGribKeysIterator :: GribHandle
-> [GribKeysIteratorFlag]
-> Maybe String
-> (GribKeysIterator -> IO a)
-> IO a
withGribKeysIterator h flags ns = bracket before after
where before = gribKeysIteratorNew h flags ns
after kiter = withGribHandle h $ \_ -> gribKeysIteratorDelete kiter
foreign import ccall safe "Data/Grib/Raw/KeysIterator.chs.h grib_keys_iterator_new"
gribKeysIteratorNew'_ :: ((C2HSImp.Ptr (GribHandle)) -> (C2HSImp.CULong -> ((C2HSImp.Ptr C2HSImp.CChar) -> (IO (GribKeysIterator)))))
foreign import ccall safe "Data/Grib/Raw/KeysIterator.chs.h grib_keys_iterator_next"
gribKeysIteratorNext'_ :: ((GribKeysIterator) -> (IO C2HSImp.CInt))
foreign import ccall safe "Data/Grib/Raw/KeysIterator.chs.h grib_keys_iterator_get_name"
gribKeysIteratorGetName'_ :: ((GribKeysIterator) -> (IO (C2HSImp.Ptr C2HSImp.CChar)))
foreign import ccall safe "Data/Grib/Raw/KeysIterator.chs.h grib_keys_iterator_delete"
gribKeysIteratorDelete'_ :: ((GribKeysIterator) -> (IO C2HSImp.CInt))
foreign import ccall safe "Data/Grib/Raw/KeysIterator.chs.h grib_keys_iterator_rewind"
gribKeysIteratorRewind'_ :: ((GribKeysIterator) -> (IO C2HSImp.CInt))
foreign import ccall safe "Data/Grib/Raw/KeysIterator.chs.h grib_keys_iterator_set_flags"
gribKeysIteratorSetFlags'_ :: ((GribKeysIterator) -> (C2HSImp.CULong -> (IO C2HSImp.CInt)))