Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,2 @@
|
|||
import Data.Char (toUpper)
|
||||
import qualified System.IO.Strict as S
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
data INI = INI { entries :: [Entry] } deriving Show
|
||||
|
||||
data Entry = Comment String
|
||||
| Field String String
|
||||
| Flag String Bool
|
||||
| EmptyLine
|
||||
|
||||
instance Show Entry where
|
||||
show entry = case entry of
|
||||
Comment text -> "# " ++ text
|
||||
Field f v -> f ++ " " ++ v
|
||||
Flag f True -> f
|
||||
Flag f False -> "; " ++ f
|
||||
EmptyLine -> ""
|
||||
|
||||
instance Read Entry where
|
||||
readsPrec _ s = [(interprete (clean " " s), "")]
|
||||
where
|
||||
clean chs = dropWhile (`elem` chs)
|
||||
interprete ('#' : text) = Comment text
|
||||
interprete (';' : f)= flag (clean " ;" f) False
|
||||
interprete entry = case words entry of
|
||||
[] -> EmptyLine
|
||||
[f] -> flag f True
|
||||
f : v -> field f (unwords v)
|
||||
field f = Field (toUpper <$> f)
|
||||
flag f = Flag (toUpper <$> f)
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
setValue :: String -> String -> INI -> INI
|
||||
setValue f v = INI . replaceOn (eqv f) (Field f v) . entries
|
||||
|
||||
setFlag :: String -> Bool -> INI -> INI
|
||||
setFlag f v = INI . replaceOn (eqv f) (Flag f v) . entries
|
||||
|
||||
enable f = setFlag f True
|
||||
disable f = setFlag f False
|
||||
|
||||
eqv f entry = (toUpper <$> f) == (toUpper <$> field entry)
|
||||
where field (Field f _) = f
|
||||
field (Flag f _) = f
|
||||
field _ = ""
|
||||
|
||||
replaceOn p x lst = prev ++ x : post
|
||||
where
|
||||
(prev,post) = case break p lst of
|
||||
(lst, []) -> (lst, [])
|
||||
(lst, _:xs) -> (lst, xs)
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
readIni :: String -> IO INI
|
||||
readIni file = INI . map read . lines <$> S.readFile file
|
||||
|
||||
writeIni :: String -> INI -> IO ()
|
||||
writeIni file = writeFile file . unlines . map show . entries
|
||||
|
||||
updateIni :: String -> (INI -> INI) -> IO ()
|
||||
updateIni file f = readIni file >>= writeIni file . f
|
||||
|
||||
main = updateIni "test.ini" $
|
||||
disable "NeedsPeeling" .
|
||||
enable "SeedsRemoved" .
|
||||
setValue "NumberOfBananas" "1024" .
|
||||
setValue "NumberOfStrawberries" "62000"
|
||||
Loading…
Add table
Add a link
Reference in a new issue