]> Git — Sourcephile - comptalang.git/blob - lcc/Hcompta/LCC/Lib/FilePath.hs
Improve section checking and warn when a term is redefined.
[comptalang.git] / lcc / Hcompta / LCC / Lib / FilePath.hs
1 module Hcompta.LCC.Lib.FilePath where
2
3 import Control.Applicative ((<$>))
4 import Control.Monad (Monad(..))
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 -- | Return an absolute 'FilePath', given the current working directory and a path.
12 --
13 -- * "~" as prefix is expanded to the process's user's home directory
14 -- * "-" as path is unchanged
15 -- * ~USER is not supported
16 path_absolute :: FilePath -> FilePath -> IO FilePath
17 path_absolute _ "-" = return "-"
18 path_absolute cwd path =
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