From 1fec182a0be7faaccf727add3ee0b97594064615 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Thu, 16 Jan 2020 22:22:00 +1300 Subject: [PATCH] Implement lookups of ini-file keys. --- src/Network/URI/XDG/Ini.hs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Network/URI/XDG/Ini.hs b/src/Network/URI/XDG/Ini.hs index aa96682..09a2d6e 100644 --- a/src/Network/URI/XDG/Ini.hs +++ b/src/Network/URI/XDG/Ini.hs @@ -1,6 +1,6 @@ -module Network.URI.XDG.Ini(parseIni) where +module Network.URI.XDG.Ini(INI, parseIni, iniLookup, iniLookupLocalized) where -import Data.Char (isSpace) +import Data.Char (isSpace, toLower) import Data.List (dropWhile, dropWhileEnd) type INI = [(String, [(String, String)])] @@ -8,7 +8,7 @@ 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 +strip cs = dropWhile isSpace $ dropWhileEnd isSpace $ map toLower cs strip2 (a, b) = (strip a, strip b) isComment ('#':_) = True @@ -28,3 +28,14 @@ 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 +iniLookupLocalized [] group key ini = iniLookup group key ini -- 2.30.2