gameServer/Data/TConfig.hs
author nemo
Sun, 14 Aug 2011 23:52:45 -0400
changeset 5569 8313952b2811
parent 4995 d3ca68e4860e
permissions -rw-r--r--
suggestion of mikade's - delete old mines if the collision array shows signs of filling up. This is kind of an edge case, esp now that array is up to 1024, but should prevent (easiest) way to crash by collision array overflow (endless mines/minestrikes).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
     1
-- Module      : Data.TConfig
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
     2
-- Copyright   : (c) Anthony Simpson 2009
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
     3
-- License     : BSD3
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
     4
--
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
     5
-- Maintainer  : DiscipleRayne@gmail.com
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
     6
-- Stability   : relatively stable
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
     7
-- Portability : portable
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
     8
---------------------------------------------------
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
     9
{-|
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    10
  A small and simple text file configuration
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    11
  library written in Haskell. It is similar
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    12
  to the INI file format, but lacks a few of
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    13
  it's features, such as sections. It is
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    14
  suitable for simple games that need to
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    15
  keep track of certain information between
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    16
  plays.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    17
-}
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    18
module Data.TConfig
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    19
    (
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    20
     getValue
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    21
   , repConfig
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    22
   , readConfig
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    23
   , writeConfig
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    24
   , remKey
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    25
   , addKey
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    26
   , Conf ()
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    27
   ) where
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    28
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    29
import Data.Char
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    30
import qualified Data.Map as M
4992
408301a9d2d6 - Simplify insane TConfig code
unc0rr
parents: 4989
diff changeset
    31
import Control.Monad
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    32
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    33
type Key   = String
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    34
type Value = String
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    35
type Conf  = M.Map Key Value
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    36
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    37
-- |Adds a key and value to the end of the configuration.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    38
addKey :: Key -> Value -> Conf -> Conf
4992
408301a9d2d6 - Simplify insane TConfig code
unc0rr
parents: 4989
diff changeset
    39
addKey = M.insert
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    40
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    41
-- |Utility function.
4995
d3ca68e4860e Fix comments spelling
unc0rr
parents: 4993
diff changeset
    42
-- Removes a key and its value from the configuration.
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    43
remKey :: Key -> Conf -> Conf
4992
408301a9d2d6 - Simplify insane TConfig code
unc0rr
parents: 4989
diff changeset
    44
remKey = M.delete
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    45
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    46
-- |Utility function. Searches a configuration for a
4995
d3ca68e4860e Fix comments spelling
unc0rr
parents: 4993
diff changeset
    47
-- key, and returns its value.
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    48
getValue :: Key -> Conf -> Maybe Value
4992
408301a9d2d6 - Simplify insane TConfig code
unc0rr
parents: 4989
diff changeset
    49
getValue = M.lookup
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    50
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    51
-- |Utility function. Replaces the value
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    52
-- associated with a key in a configuration.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    53
repConfig :: Key -> Value -> Conf -> Conf
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    54
repConfig k rv conf = let f _ = Just rv
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    55
                      in M.alter f k conf
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    56
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    57
-- |Reads a file and parses to a Map String String.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    58
readConfig :: FilePath -> IO Conf
4993
905b349af377 Fix split
unc0rr
parents: 4992
diff changeset
    59
readConfig path = liftM (M.fromList . map ((\(a, b) -> (filter (not . isSpace) a, dropWhile isSpace $ tail b)) . break (== '=')) . filter (not . null) . lines) $ readFile path
4989
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    60
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    61
-- |Parses a parsed configuration back to a file.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    62
writeConfig :: FilePath -> Conf -> IO ()
4992
408301a9d2d6 - Simplify insane TConfig code
unc0rr
parents: 4989
diff changeset
    63
writeConfig path = writeFile path . unlines . map (\(a, b) -> a ++ " = " ++ b) . M.toList