{-# LANGUAGE RecordWildCards #-}
module Exomizer.Data.Args
(
CrunchOptions(..)
, defaultCrunchOptions
, fixCrunchOptions
, validCrunchOptions
, CrunchInfo(..)
) where
data CrunchOptions =
CrunchOptions
{ coEncoding :: Maybe String
, coMaxPasses :: Int
, coMaxLen :: Int
, coMaxOffset :: Int
, coUseLiteralSequences :: Bool
, coFavourSpeed :: Bool
}
deriving (Eq, Show)
defaultCrunchOptions :: CrunchOptions
defaultCrunchOptions =
CrunchOptions
{ coEncoding = Nothing
, coMaxPasses = 65535
, coMaxLen = 65535
, coMaxOffset = 65535
, coUseLiteralSequences = True
, coFavourSpeed = False
}
fixCrunchOptions :: CrunchOptions -> CrunchOptions
fixCrunchOptions CrunchOptions{..} =
CrunchOptions
{ coEncoding = checkEncoding coEncoding
, coMaxPasses = 1 `max` coMaxPasses `min` 65535
, coMaxLen = 0 `max` coMaxLen `min` 65535
, coMaxOffset = 0 `max` coMaxOffset `min` 65535
, coUseLiteralSequences = coUseLiteralSequences
, coFavourSpeed = coFavourSpeed
}
where
checkEncoding enc =
case (check16Hex . checkComma . check16Hex . checkComma . check4Hex . checkComma . check16Hex) enc of
Just "" -> enc
_ -> Nothing
check16Hex = check4Hex . check4Hex . check4Hex . check4Hex
check4Hex = checkHex . checkHex . checkHex . checkHex
checkHex (Just (x:xs))
| x `elem` ("0123456789abcdefABCDEF" :: String) = Just xs
checkHex _ = Nothing
checkComma (Just (',':xs)) = Just xs
checkComma _ = Nothing
validCrunchOptions :: CrunchOptions -> Bool
{-# INLINE validCrunchOptions #-}
validCrunchOptions co = co == fixCrunchOptions co
data CrunchInfo =
CrunchInfo
{ ciLiteralSequencesUsed :: Bool
, ciNeededSafetyOffset :: Int
, ciUsedEncoding :: String
}
deriving (Eq, Show)