gameServer/Data/TConfig.hs
author unc0rr
Sun, 06 Mar 2011 21:54:37 +0300
changeset 4989 4771fed9272e
child 4992 408301a9d2d6
permissions -rw-r--r--
- Write server config into .ini file on change - Import Data.TConfig into project, make it export Conf constructor, remove all workarounds for missing constructor in server.
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
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    31
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    32
type Key   = String
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    33
type Value = String
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    34
type Conf  = M.Map Key Value
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    35
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    36
-- |Adds a key and value to the end of the configuration.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    37
addKey :: Key -> Value -> Conf -> Conf
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    38
addKey k v conf = M.insert k (addQuotes v) conf
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    39
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    40
-- |Utility Function. Checks for the existence
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    41
-- of a key.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    42
checkKey :: Key -> Conf -> Bool
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    43
checkKey k conf = M.member k conf
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    44
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    45
-- |Utility function.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    46
-- Removes a key and it's value from the configuration.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    47
remKey :: Key -> Conf -> Conf
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    48
remKey k conf = M.delete k conf
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    49
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    50
-- |Utility function. Searches a configuration for a
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    51
-- key, and returns it's value.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    52
getValue :: Key -> Conf -> Maybe Value
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    53
getValue k conf = case M.lookup k conf of
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    54
                    Just val -> Just $ stripQuotes val
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    55
                    Nothing  -> Nothing
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
stripQuotes :: String -> String
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    58
stripQuotes x | any isSpace x = filter (/= '\"') x
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    59
              | otherwise     = x
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
-- |Returns a String wrapped in quotes if it
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    62
-- contains spaces, otherwise returns the string
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    63
-- untouched.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    64
addQuotes :: String -> String
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    65
addQuotes x | any isSpace x = "\"" ++ x ++ "\""
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    66
            | otherwise     = x
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    67
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    68
-- |Utility function. Replaces the value
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    69
-- associated with a key in a configuration.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    70
repConfig :: Key -> Value -> Conf -> Conf
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    71
repConfig k rv conf = let f _ = Just rv
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    72
                      in M.alter f k conf
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    73
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    74
-- |Reads a file and parses to a Map String String.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    75
readConfig :: FilePath -> IO Conf
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    76
readConfig path = readFile path >>= return . parseConfig
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    77
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    78
-- |Parses a parsed configuration back to a file.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    79
writeConfig :: FilePath -> Conf -> IO ()
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    80
writeConfig path con = writeFile path $ putTogether con
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    81
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    82
-- |Turns a list of configuration types back into a String
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    83
-- to write to a file.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    84
putTogether :: Conf -> String
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    85
putTogether = concat . putTogether' . backToString
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    86
    where putTogether' (x:y:xs) = x : " = " : y : "\n" : putTogether' xs
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    87
          putTogether' _        = []
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    88
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    89
-- |Turns a list of configuration types into a list of Strings
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    90
backToString :: Conf -> [String]
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    91
backToString conf = backToString' $ M.toList conf
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    92
    where backToString' ((x,y):xs) = x : y : backToString' xs
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    93
          backToString' _          = []
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    94
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    95
-- |Parses a string into a list of Configuration types.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    96
parseConfig :: String -> Conf
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    97
parseConfig = listConfig . popString . parse
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    98
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
    99
parse :: String -> [String]
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
   100
parse = words . filter (/= '=')
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
   101
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
   102
-- |Turns a list of key value key value etc... pairs into
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
   103
-- A list of Configuration types.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
   104
listConfig :: [String] -> Conf
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
   105
listConfig = M.fromList . helper
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
   106
    where helper (x:y:xs) = (x,y) : helper xs
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
   107
          helper _        = []
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
   108
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
   109
-- |Parses strings from the parseConfig'd file.
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
   110
popString :: [String] -> [String]
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
   111
popString []     = []
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
   112
popString (x:xs)
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
   113
    | head x == '\"' = findClose $ break (('\"' ==) . last) xs
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
   114
    | otherwise      = x : popString xs
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
   115
    where findClose (y,ys) =
4771fed9272e - Write server config into .ini file on change
unc0rr
parents:
diff changeset
   116
              [unwords $ x : y ++ [head ys]] ++ popString (tail ys)