{- Config file handling by jgoerzen Copyright (c) 2005 Jesper Louis Andersen Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -} ----------------------------------------------------------------------------- -- | -- Module : Config -- Copyright : (c) Jesper Louis Andersen, 2005 -- License : BSD-style (see the file libraries/base/LICENSE) -- -- Maintainer : jgoerzen\@complete.org -- Stability : Highly Experimental code -- Portability : portable -- -- Configuration file support for Conjure. -- The configuration file supported here is versatile, supports -- string interpolation, comments, multi-line fields, sections, -- and defaults. See -- -- for more details. ----------------------------------------------------------------------------- module Config ( readCfg, getRarestFirstCutoffPoint ) where import MissingH.ConfigParser import System.Directory(getHomeDirectory, doesFileExist) import MissingH.Either(forceEither) defaultCP :: ConfigParser defaultCP = forceEither $ set (emptyCP {accessfunc = interpolatingAccess 10}) "DEFAULT" "rarest_first_cutoff_point" "10" readCfg :: IO ConfigParser readCfg = do homedir <- getHomeDirectory let cfgfile = homedir ++ "/.conjure" exists <- doesFileExist cfgfile if exists then do val <- readfile defaultCP cfgfile return $ forceEither val else return defaultCP getRarestFirstCutoffPoint :: ConfigParser -> Int getRarestFirstCutoffPoint cp = case get cp "main" "rarest_first_cutoff_point" of Left x -> error $ "Couldn't find rarest_first_cutoff_point: " ++ (show x) Right y -> y