~alcinnz/hurl

ref: a841435e2a825ecd8d7f27b0140a2a46e6408fb6 hurl/src/Network/URI/XDG/Ini.hs -rw-r--r-- 1.5 KiB
a841435e — Adrian Cochrane Support clientside certificates for HTTPS & Gemini. 2 years ago
                                                                                
1fec182a Adrian Cochrane
6950ae14 Adrian Cochrane
1fec182a Adrian Cochrane
6950ae14 Adrian Cochrane
1fec182a Adrian Cochrane
6950ae14 Adrian Cochrane
8d239eb6 Adrian Cochrane
6950ae14 Adrian Cochrane
1fec182a Adrian Cochrane
0c50ae13 Adrian Cochrane
1fec182a Adrian Cochrane
0c50ae13 Adrian Cochrane
1fec182a Adrian Cochrane
0c50ae13 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Network.URI.XDG.Ini(INI, parseIni, iniLookup, iniLookupLocalized) where

import Data.Char (isSpace, toLower)
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 $ map toLower 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 (strip $ 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 [] = ([], [])

---

iniLookup :: String -> String -> INI -> Maybe String
iniLookup group key ini = lookup group ini >>= lookup key

iniLookupLocalized :: [String] -> String -> String -> INI -> Maybe String
iniLookupLocalized (locale:locales) group key ini
    | Just ret <- iniLookup group (key ++ "[" ++ locale' ++ "]") ini = Just ret
    | otherwise = iniLookupLocalized locales group key ini
    where locale' = map dash2under locale
iniLookupLocalized [] group key ini = iniLookup group key ini

dash2under '-' = '_'
dash2under c = c