diff options
author | Joey Hess <joey@kitenet.net> | 2011-04-25 13:36:39 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2011-04-25 13:36:39 -0400 |
commit | e433c6f0bb2ee5f03217b85e3b677b961f5d391a (patch) | |
tree | 162d625dd8a0e8bfc4d09899766e33c12aa2e5f0 /Utility.hs | |
parent | b0b413c69f76bcfa46d01ff1623027707483c63c (diff) |
generalized relPathDirTo functions
Diffstat (limited to 'Utility.hs')
-rw-r--r-- | Utility.hs | 37 |
1 files changed, 16 insertions, 21 deletions
diff --git a/Utility.hs b/Utility.hs index 5639a8799..13ebbfccb 100644 --- a/Utility.hs +++ b/Utility.hs @@ -13,8 +13,8 @@ module Utility ( parentDir, absPath, absPathFrom, - relPathCwdToDir, - relPathDirToDir, + relPathCwdToFile, + relPathDirToFile, boolSystem, shellEscape, shellUnEscape, @@ -29,7 +29,7 @@ module Utility ( prop_idempotent_shellEscape, prop_idempotent_shellEscape_multiword, prop_parentDir_basics, - prop_relPathDirToDir_basics + prop_relPathDirToFile_basics ) where import System.IO @@ -180,26 +180,21 @@ absPathFrom cwd file = Just f -> f Nothing -> error $ "unable to normalize " ++ file -{- Constructs a relative path from the CWD to a directory. +{- Constructs a relative path from the CWD to a file. - - For example, assuming CWD is /tmp/foo/bar: - - relPathCwdToDir "/tmp/foo" == "../" - - relPathCwdToDir "/tmp/foo/bar" == "" + - relPathCwdToFile "/tmp/foo" == ".." + - relPathCwdToFile "/tmp/foo/bar" == "" -} -relPathCwdToDir :: FilePath -> IO FilePath -relPathCwdToDir dir = liftM2 relPathDirToDir getCurrentDirectory (absPath dir) +relPathCwdToFile :: FilePath -> IO FilePath +relPathCwdToFile f = liftM2 relPathDirToFile getCurrentDirectory (absPath f) -{- Constructs a relative path from one directory to another. +{- Constructs a relative path from a directory to a file. - - - Both directories must be absolute, and normalized (eg with absNormpath). - - - - The path will end with "/", unless it is empty. + - Both must be absolute, and normalized (eg with absNormpath). -} -relPathDirToDir :: FilePath -> FilePath -> FilePath -relPathDirToDir from to = - if not $ null path - then addTrailingPathSeparator path - else "" +relPathDirToFile :: FilePath -> FilePath -> FilePath +relPathDirToFile from to = path where s = [pathSeparator] pfrom = split s from @@ -211,12 +206,12 @@ relPathDirToDir from to = numcommon = length common path = join s $ dotdots ++ uncommon -prop_relPathDirToDir_basics :: FilePath -> FilePath -> Bool -prop_relPathDirToDir_basics from to +prop_relPathDirToFile_basics :: FilePath -> FilePath -> Bool +prop_relPathDirToFile_basics from to | from == to = null r - | otherwise = not (null r) && (last r == '/') + | otherwise = not (null r) where - r = relPathDirToDir from to + r = relPathDirToFile from to {- Removes a FileMode from a file. - For example, call with otherWriteMode to chmod o-w -} |