module Data.Ini
(
readIniFile
,parseIni
,lookupValue
,readValue
,writeIniFile
,printIni
,Ini(..)
,iniParser
,sectionParser
,keyValueParser
)
where
import Control.Monad
import Data.Attoparsec.Combinator
import Data.Attoparsec.Text
import Data.Char
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as M
import Data.Monoid
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Prelude hiding (takeWhile)
newtype Ini = Ini { unIni :: HashMap Text (HashMap Text Text) }
deriving (Show)
readIniFile :: FilePath -> IO (Either String Ini)
readIniFile = fmap parseIni . T.readFile
parseIni :: Text -> Either String Ini
parseIni = parseOnly iniParser
lookupValue :: Ini -> Text -> Text -> Either String Text
lookupValue (Ini ini) name key =
case M.lookup name ini of
Nothing -> Left ("Couldn't find section: " ++ T.unpack name)
Just section ->
case M.lookup key section of
Nothing -> Left ("Couldn't find key: " ++ T.unpack key)
Just value -> return value
readValue :: Ini -> Text -> Text -> (Text -> Either String (a, Text))
-> Either String a
readValue ini section key f =
lookupValue ini section key >>= f >>= return . fst
writeIniFile :: FilePath -> Ini -> IO ()
writeIniFile fp = T.writeFile fp . printIni
printIni :: Ini -> Text
printIni (Ini sections) =
T.concat (map buildSection (M.toList sections))
where buildSection (name,pairs) =
"[" <> name <> "]\n" <>
T.concat (map buildPair (M.toList pairs))
buildPair (name,value) =
name <> ": " <> value <> "\n"
iniParser :: Parser Ini
iniParser = fmap Ini (fmap M.fromList (many1 sectionParser))
sectionParser :: Parser (Text,HashMap Text Text)
sectionParser =
do _ <- char '['
name <- takeWhile (\c -> c /=']' && c /= '[')
_ <- char ']'
skipEndOfLine
values <- many1 keyValueParser
return (name,M.fromList values)
keyValueParser :: Parser (Text,Text)
keyValueParser =
do key <- takeWhile1 (\c -> not (isDelim c || c == '[' || c == ']'))
delim <- satisfy isDelim
value <- fmap (clean delim) (takeWhile (not . isEndOfLine))
skipEndOfLine
return (key,value)
where clean ':' = T.drop 1
clean _ = id
isDelim :: Char -> Bool
isDelim x = x == '=' || x == ':'
skipEndOfLine :: Parser ()
skipEndOfLine = skipWhile (\c -> isEndOfLine c || isSpace c)