~alcinnz/hurl

ref: 47f9ef33a45545cb312b5aa956da40ac2de2c99e hurl/src/Network/URI/XDG/Ini.hs -rw-r--r-- 915 bytes
47f9ef33 — Adrian Cochrane Improve error reporting & it's localization 4 years ago
                                                                                
6950ae14 Adrian Cochrane
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module Network.URI.XDG.Ini(parseIni) where

import Data.Char (isSpace)
import Data.List (dropWhile, dropWhileEnd)

type INI = [(String, [(String, String)])]

parseIni :: String -> INI
parseIni source = parseIni' $ filter (not . isComment) $ map strip $ lines source

strip cs = dropWhile isSpace $ dropWhileEnd isSpace cs
strip2 (a, b) = (strip a, strip b)

isComment ('#':_) = True
isComment "" = True
isComment _ = False

parseIni' (('[':cs):lines) | ']':header <- reverse cs =
    let (keys, rest) = parseKeys lines in (reverse header, keys) : parseIni' rest
parseIni' _ = []

parseKeys :: [String] -> ([(String, String)], [String])
parseKeys lines@(('[':_):_) = ([], lines)
parseKeys (line:lines) =
    let (keys, rest) = parseKeys lines in (strip2 (parseKey line) : keys, rest)
parseKeys [] = ([], [])

parseKey ('=':as) = ([], as)
parseKey (a:as) = let (x, y) = parseKey as in (a:x, y)
parseKey [] = ([], [])