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