module Hcompta.Lib.Path where import Control.Applicative ((<$>)) import Control.Monad (Monad(..), liftM) import Control.Monad.IO.Class (liftIO) import Prelude (($), FilePath, IO, id) import System.Directory (getHomeDirectory) import System.FilePath (()) import qualified System.FilePath.Posix as Path -- | Return an absolute 'FilePath', given the current working directory and a path. -- -- * "~" as prefix is expanded to the process's user's home directory -- * "-" as path is unchanged -- * ~USER is not supported abs :: FilePath -> FilePath -> IO FilePath abs _ "-" = return "-" abs cwd path = liftM (if Path.isRelative path then (cwd ) else id) (expand path) where expand :: FilePath -> IO FilePath expand ('~':sep:p) = if Path.isPathSeparator sep then liftIO $ ( p) <$> getHomeDirectory else fail "~USERNAME in path is not supported" expand p = return p