-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2e038af
Showing
6 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Vim swap files | ||
*.sw? | ||
|
||
# Haskell unversioned stuff | ||
dist | ||
dist-* | ||
cabal-dev | ||
*.o | ||
*.hi | ||
*.hie | ||
*.chi | ||
*.chs.h | ||
*.dyn_o | ||
*.dyn_hi | ||
.hpc | ||
.hsenv | ||
.cabal-sandbox/ | ||
cabal.sandbox.config | ||
*.prof | ||
*.aux | ||
*.hp | ||
*.eventlog | ||
.stack-work/ | ||
cabal.project.local | ||
cabal.project.local~ | ||
.HTF/ | ||
.ghc.environment.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Revision history for trivialini | ||
|
||
## 0.1.0.0 -- 2021-02-22 | ||
|
||
* First version. Released on an unsuspecting world. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2021 Mirko Westermeier | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import Distribution.Simple | ||
main = defaultMain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
-- | Ultra light weight ini file parser | ||
module Trivialini (config, configFile) where | ||
|
||
import Data.Map (Map, keys, empty, insert, (!)) | ||
import Text.Regex (mkRegex, matchRegex) | ||
|
||
-- | ||
-- Data structure and accessors | ||
-- | ||
|
||
-- data structure for "ini" file representation | ||
data Config = Empty | Config String (Map String String) Config | ||
|
||
instance Show Config where | ||
show Empty = "" | ||
show (Config sec defs rest) = secLn ++ defLns ++ "\n" ++ show rest | ||
where | ||
secLn = "[" ++ sec ++ "]\n" | ||
defLns = concatMap def $ keys defs | ||
def key = key ++ " = " ++ defs ! key ++ "\n" | ||
|
||
-- Extract the list of all section names | ||
sections :: Config -> [String] | ||
sections Empty = [] | ||
sections (Config name _ rest) = name : sections rest | ||
|
||
-- Extract the map of config values for a given section name | ||
section :: Config -> String -> Map String String | ||
section Empty _ = empty | ||
section (Config name map rest) query | ||
| name == query = map | ||
| otherwise = section rest query | ||
|
||
-- Extract a configuration data value for a given section and key | ||
value :: Config -> String -> String -> String | ||
value conf sec key = s ! key | ||
where s = section conf sec | ||
|
||
-- | ||
-- Parsing data structure from text file | ||
-- | ||
|
||
readConfig :: String -> Config | ||
-- Preparation of config parsing | ||
readConfig = rc "default" empty . lines | ||
|
||
rc :: String -> Map String String -> [String] -> Config | ||
-- Build config data line by line (first argument is the current section) | ||
rc sec values [] = Config sec values Empty | ||
rc sec values (line:lines) = case chkSec line of | ||
|
||
-- It's a section | ||
(Just [newS]) -> Config sec values $ rc newS empty lines | ||
|
||
-- It's not a section | ||
Nothing -> case chkKV line of | ||
|
||
-- It's a key value assignment | ||
(Just [key, value]) -> rc sec (insert key value values) lines | ||
|
||
-- Neither: ignore | ||
Nothing -> rc sec values lines | ||
|
||
where | ||
chkSec = matchRegex (mkRegex "^\\[(.+)\\]") | ||
chkKV = matchRegex (mkRegex "^(\\S+)\\s+=\\s+(.*)$") | ||
|
||
-- | ||
-- Public interface | ||
-- | ||
|
||
config :: String -> String -> String -> String | ||
-- ^Return the config value for a given section and key from the given config | ||
config = value . readConfig | ||
|
||
configFile :: String -> String -> String -> IO String | ||
-- ^Return the config file value for a given section and key | ||
configFile file sec key = do | ||
content <- readFile file | ||
return $ config content sec key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
-- Initial trivialini.cabal generated by cabal init. For further | ||
-- documentation, see http://haskell.org/cabal/users-guide/ | ||
|
||
name: trivialini | ||
version: 0.1.0.0 | ||
synopsis: Ultra light weight ini file parser | ||
-- description: | ||
homepage: https://github.com/memowe/trivialini | ||
license: MIT | ||
license-file: LICENSE | ||
author: Mirko Westermeier | ||
maintainer: [email protected] | ||
-- copyright: | ||
category: Text | ||
build-type: Simple | ||
extra-source-files: CHANGELOG.md | ||
cabal-version: >=1.10 | ||
|
||
library | ||
exposed-modules: | ||
Trivialini | ||
-- other-modules: | ||
-- other-extensions: | ||
build-depends: | ||
base >=4.12 && <4.13, | ||
containers, | ||
regex-compat | ||
hs-source-dirs: src | ||
default-language: Haskell2010 |