{-# LANGUAGE CPP, TypeApplications #-}
module Data.Tini
(
Ini
, parseIni, showIni
, Key, SectionName
, module Data.Tini.Ops
, readIniFile, writeIniFile
, IniValue (..)
) where
import Control.Exception (SomeException, try)
import System.IO
import Data.Tini.IniValue
import Data.Tini.Ops
import Data.Tini.Parser (parseIni)
import Data.Tini.Rename (rename)
import Data.Tini.Types (Ini, Key, SectionName, showIni)
readIniFile :: FilePath -> IO (Maybe Ini)
readIniFile file = do
ini <- try $ withFile file ReadMode $ \h -> do
(pure $!) . parseIni =<< hGetContents h
case ini :: Either SomeException (Maybe Ini) of
Right s -> return $! s
_ -> return Nothing
writeIniFile :: FilePath -> Ini -> IO ()
writeIniFile path ini = do
(tmpfile, h) <- openTempFile dir file
hPutStrLn h (show ini) >> hFlush h >> hClose h
rename tmpfile path
where
(dir, file) = splitPath path
#ifdef mingw32_HOST_OS
dirSeparator = '\\'
#else
dirSeparator = '/'
#endif
splitPath p =
case break (== dirSeparator) (reverse p) of
(f, "") -> (".", reverse f)
(f, _:d) -> (reverse d, reverse f)