]> Git — Sourcephile - comptalang.git/blob - lib/Hcompta/Lib/Path.hs
Ajout : Lib.Foldable : tests.
[comptalang.git] / lib / Hcompta / Lib / Path.hs
1 module Hcompta.Lib.Path where
2
3 import Control.Applicative ((<$>))
4 import Control.Monad (liftM)
5 import Control.Monad.IO.Class (liftIO)
6 import System.Directory (getHomeDirectory)
7 import System.FilePath ((</>))
8 import qualified System.FilePath.Posix as Path
9
10 -- | Return an absolute 'FilePath', given the current working directory and a path.
11 --
12 -- * "~" as prefix is expanded to the process's user's home directory
13 -- * "-" as path is unchanged
14 -- * ~USER is not supported
15 abs :: FilePath -> FilePath -> IO FilePath
16 abs _ "-" = return "-"
17 abs cwd path =
18 liftM
19 (if Path.isRelative path
20 then (cwd </>)
21 else id)
22 (expand path)
23 where
24 expand :: FilePath -> IO FilePath
25 expand ('~':sep:p) =
26 if Path.isPathSeparator sep
27 then liftIO $ (</> p) <$> getHomeDirectory
28 else fail "~USERNAME in path is not supported"
29 expand p = return p