summaryrefslogtreecommitdiff
path: root/SimpleConfig.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2014-07-30 20:34:13 -0400
committerjoe <joe@jerkface.net>2014-07-30 20:34:13 -0400
commit914c7404d0add86cd44a044c4aceafb3cdc8db4c (patch)
tree162c0f835550b1e56e5616f9b5d092e810b7823a /SimpleConfig.hs
parentb45a30e7a127363a3255d89909f54108f711a16d (diff)
SimpleConfig.hs for parsing key/path associations
Diffstat (limited to 'SimpleConfig.hs')
-rw-r--r--SimpleConfig.hs36
1 files changed, 36 insertions, 0 deletions
diff --git a/SimpleConfig.hs b/SimpleConfig.hs
new file mode 100644
index 0000000..9ffe7b1
--- /dev/null
+++ b/SimpleConfig.hs
@@ -0,0 +1,36 @@
1module SimpleConfig where
2
3import qualified Data.Map as Map
4import Data.Map ( Map )
5import Data.Char
6import Data.List
7import Data.Maybe
8import Control.Monad
9
10newtype SimpleConfig = SimpleConfig
11 { configMap :: Map String (Map String String)
12 }
13 deriving (Eq,Show)
14
15load :: FilePath -> IO SimpleConfig
16load path = parse `fmap` readFile path
17
18parse :: String -> SimpleConfig
19parse txt = SimpleConfig $ Map.fromList ss
20 where
21 ls = lines txt
22 cs = filter (not . null) $ map (takeWhile (/='#')) ls
23 css = groupBy (\_ s -> take 1 s /= "[") cs
24 ss = mapMaybe (\ds -> do
25 brkt <- listToMaybe $ take 1 ds
26 opn <- listToMaybe $ take 1 brkt
27 guard $ opn == '['
28 let sec = init (drop 1 brkt)
29 return (sec, Map.fromList $ map parseItem $ drop 1 ds)
30 )
31 css
32
33parseItem txt = (key,val)
34 where
35 (key,rst) = span (not . isSpace) $ dropWhile isSpace txt
36 val = dropWhile isSpace rst