SConfig (empty) → 0.1.0.0
raw patch · 4 files changed
+95/−0 lines, 4 filesdep +basedep +containerssetup-changed
Dependencies added: base, containers
Files
- LICENSE +20/−0
- SConfig.cabal +25/−0
- Setup.hs +2/−0
- src/Data/SConfig.hs +48/−0
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2014 Francesco Gazzetta++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ SConfig.cabal view
@@ -0,0 +1,25 @@+-- Initial SConfig.cabal generated by cabal init. For further +-- documentation, see http://haskell.org/cabal/users-guide/++name: SConfig+version: 0.1.0.0+synopsis: A simple config library+-- description: +homepage: https://github.com/fgaz/SConfig+license: MIT+license-file: LICENSE+author: Francesco Gazzetta+maintainer: fgaz@users.noreply.github.com+-- copyright: +category: Configuration+build-type: Simple+-- extra-source-files: +cabal-version: >=1.10++library+ exposed-modules: Data.SConfig+ -- other-modules: + -- other-extensions: + build-depends: base >=4.7 && <4.8, containers >=0.5 && <0.6+ hs-source-dirs: src+ default-language: Haskell2010
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Data/SConfig.hs view
@@ -0,0 +1,48 @@+module Data.SConfig+( parseConfig+, getValue+, Config+, Key+, Value+) where++import qualified Data.Map as M+import Data.List+import Data.Maybe (fromJust)++-- | Config key, alias for String.+type Key = String++-- | Config value, alias for String.+type Value = String++-- | Parsed configuration. Basically a Map String String+type Config = M.Map Key Value++-- | Parse configuration+parseConfig :: String -> Config+parseConfig str = foldl readConfigLine M.empty --apply readConfigLine to every line+ $ filter (elem '=') --make sure all lines are actual configuration lines+ $ concatEscaped+ $ filter (\x -> (not $ null x) && (take 1 x) /= "#") --remove comments & empty lines+ $ lines str++--inserts a parsed key-value pair into the Config accumulator+readConfigLine :: Config -> String -> Config+readConfigLine config str = M.insert+ (filter (/=' ') key) --spaces only cause errors.+ value+ config+ where (key,_:value) = splitAt+ ( fromJust $ --already checked that str contains '='+ elemIndex '=' str+ )+ str++--concatenate two lines if the first ends with a backslash+concatEscaped :: [String] -> [String]+concatEscaped lines = foldr (\x (a:acc) -> if last x == '\\' then ((init x) ++ a):acc else x:a:acc) [""] lines++-- | Lookup values in a parsed configuration (alias of Map.lookup)+getValue :: Key -> M.Map Key Value -> Maybe Value+getValue = M.lookup