-- Module      : Data.TConfig
-- Copyright   : (c) Anthony Simpson 2009
-- License     : see LICENSE
--
-- Maintainer  : DiscipleRayne@gmail.com
-- Stability   : stable
-- Portability : portable
---------------------------------------------------
{-|
  A small and simple text file configuration
  library written in Haskell. It is similar
  to the INI file format, but lacks a few of
  it's features, such as sections. It is 
  suitable for simple games that need to 
  keep track of certain information between 
  plays.
-}
module Data.TConfig
    (
     Configuration
   , getValue
   , repConfig
   , readConfig
   , writeConfig
   , remKey
   , addKey
   ) where

import IO
import Data.List
import Data.Char

type Key   = String
type Value = String

-- |The 'Configuration' type takes a key and a value,
-- and returns a Configuration type.
data Configuration = Config {
      key   :: Key
    , value :: Value
    } deriving (Show,Eq)

-- |Adds a key and value to the end of the Configuration.
addKey :: Key -> Value -> [Configuration] -> [Configuration]
addKey k v conf = Config k (addQuotes v) : conf

-- |Utility Function.Checks for the existence 
-- of a key.
checkKey :: Key -> [Configuration] -> Bool
checkKey k conf = case getValue k conf of
                    Just _  -> True
                    Nothing -> False

-- |Utility function.
-- Removes a key and it's value from the configuration.r
remKey :: Key -> [Configuration] -> [Configuration]
remKey k xs = foldl' step [] xs
    where step ys y = 
              if key y == k then ys else y : ys

-- |Utility function. Searches a Configuration for a
-- key, and returns it's value.
getValue :: Key -> [Configuration] -> Maybe Value
getValue s (x:xs) 
    | key x == s = Just $ stripQuotes $ value x
    | otherwise  = getValue s xs
getValue _ []    = Nothing

stripQuotes x | any isSpace x = filter (not . (== '\"')) x
              | otherwise     = x

-- |Returns a String with quotes of it contains spaces,
-- otherwise returns the string untouched.
addQuotes :: String -> String
addQuotes x | any isSpace x = "\"" ++ x ++ "\""
            | otherwise     = x

-- |Utility function. Replaces the value for a key in
-- a Configuration.
repConfig :: Key -> Value -> [Configuration] -> [Configuration]
repConfig k rv xs = foldl' step [] $ reverse xs
    where step ds d 
              | key d == k = Config (key d) (addQuotes rv) : ds
              | otherwise  = d : ds

-- |Reads and parses a string from a file into a Configuration.
readConfig :: FilePath -> IO [Configuration]
readConfig path = readFile path >>= return . parseConfig

-- |Parses a parsed Configuration back to a file.
writeConfig :: FilePath -> [Configuration] -> IO ()
writeConfig path con = writeFile path $ putTogether con

-- |Turns a list of configuration types back into a String
-- to write to a file.
putTogether :: [Configuration] -> String
putTogether = concat . putTogether' . backToString
    where putTogether' (x:y:xs) = x : " = " : y : "\n" : putTogether' xs
          putTogether' _        = []

-- |Turns a list of configuration types into a list of Strings
backToString :: [Configuration] -> [String]
backToString ((Config x y):xs) = x : y : backToString xs
backToString _                 = []

-- |Parses a string into a list of Configuration types.
parseConfig :: String -> [Configuration]
parseConfig x = 
    listConfig . popString . words . filter (not . (== '=')) $ stripHash x

-- |Turns a list of key value key value etc... pairs into
-- A list of Configuration types.
listConfig :: [String] -> [Configuration]
listConfig (x:y:xs) = Config x y : listConfig xs
listConfig _        = []

-- |Parses strings from the parseConfig'd file.
popString :: [String] -> [String]
popString []     = []
popString (x:xs) 
    | head x == '\"' = findClose $ break (('\"' ==) . last) xs
    | otherwise      = x : popString xs
    where findClose (y,ys) = 
              [unwords $ x : y ++ [head ys]] ++ popString (tail ys)

-- |Strips comments from the string from the
-- configuration file
stripHash :: String -> String
stripHash = unlines . filter (not . ('#' ==) . head) . lines