1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- |
-- Module: Filesystem.Path
-- Copyright: 2010 John Millikin
-- License: MIT
--
-- Maintainer: jmillikin@gmail.com
-- Portability: portable
--
-- High‐level, byte‐based file and directory path
-- manipulations. You probably want to import "Filesystem.Path.CurrentOS"
-- instead, since it handles detecting which rules to use in the current
-- compilation.
--
module Filesystem.Path
( FilePath
, empty
-- * Basic properties
, null
, root
, directory
, parent
, filename
, dirname
, basename
, absolute
, relative
-- * Basic operations
, append
, (</>)
, concat
, commonPrefix
, stripPrefix
, collapse
-- * Extensions
, extension
, extensions
, hasExtension
, addExtension
, (<.>)
, dropExtension
, replaceExtension
, addExtensions
, dropExtensions
, replaceExtensions
, splitExtension
, splitExtensions
) where
import Prelude hiding (FilePath, concat, null)
import Data.List (foldl')
import Data.Maybe (isNothing)
import qualified Data.Monoid as M
import qualified Data.Text as T
import Filesystem.Path.Internal
instance M.Monoid FilePath where
mempty = empty
mappend = append
mconcat = concat
-------------------------------------------------------------------------------
-- Basic properties
-------------------------------------------------------------------------------
-- | @null p = (p == 'empty')@
null :: FilePath -> Bool
null = (== empty)
-- | Retrieves the 'FilePath'’s root.
root :: FilePath -> FilePath
root p = empty { pathRoot = pathRoot p }
-- | Retrieves the 'FilePath'’s directory. If the path is already a
-- directory, it is returned unchanged.
directory :: FilePath -> FilePath
directory p = empty
{ pathRoot = pathRoot p
, pathDirectories = let
starts = map Just [dot, dots]
dot' | safeHead (pathDirectories p) `elem` starts = []
| isNothing (pathRoot p) = [dot]
| otherwise = []
in dot' ++ pathDirectories p
}
-- | Retrieves the 'FilePath'’s parent directory.
parent :: FilePath -> FilePath
parent p = empty
{ pathRoot = pathRoot p
, pathDirectories = let
starts = map Just [dot, dots]
directories = if null (filename p)
then safeInit (pathDirectories p)
else pathDirectories p
dot' | safeHead directories `elem` starts = []
| isNothing (pathRoot p) = [dot]
| otherwise = []
in dot' ++ directories
}
-- | Retrieve a 'FilePath'’s filename component.
--
-- @
-- filename \"foo\/bar.txt\" == \"bar.txt\"
-- @
filename :: FilePath -> FilePath
filename p = empty
{ pathBasename = pathBasename p
, pathExtensions = pathExtensions p
}
-- | Retrieve a 'FilePath'’s directory name. This is only the
-- /file name/ of the directory, not its full path.
--
-- @
-- dirname \"foo\/bar\/baz.txt\" == \"bar\"
-- dirname \"/\" == \"\"
-- @
--
-- Since: 0.4.1
dirname :: FilePath -> FilePath
dirname p = case reverse (pathDirectories p) of
[] -> FilePath Nothing [] Nothing []
(d:_) -> case parseFilename d of
(base, exts) -> FilePath Nothing [] base exts
-- | Retrieve a 'FilePath'’s basename component.
--
-- @
-- basename \"foo/bar.txt\" == \"bar\"
-- @
basename :: FilePath -> FilePath
basename p = empty
{ pathBasename = pathBasename p
}
-- | Test whether a path is absolute.
absolute :: FilePath -> Bool
absolute p = case pathRoot p of
Just RootPosix -> True
Just (RootWindowsVolume _) -> True
_ -> False
-- | Test whether a path is relative.
relative :: FilePath -> Bool
relative p = case pathRoot p of
Just _ -> False
_ -> True
-------------------------------------------------------------------------------
-- Basic operations
-------------------------------------------------------------------------------
-- | Appends two 'FilePath's. If the second path is absolute, it is returned
-- unchanged.
append :: FilePath -> FilePath -> FilePath
append x y = cased where
cased = case pathRoot y of
Just RootPosix -> y
Just (RootWindowsVolume _) -> y
Just RootWindowsCurrentVolume -> case pathRoot x of
Just (RootWindowsVolume _) -> y { pathRoot = pathRoot x }
_ -> y
Nothing -> xy
xy = y
{ pathRoot = pathRoot x
, pathDirectories = directories
}
directories = xDirectories ++ pathDirectories y
xDirectories = (pathDirectories x ++) $ if null (filename x)
then []
else [filenameChunk x]
-- | An alias for 'append'.
(</>) :: FilePath -> FilePath -> FilePath
(</>) = append
-- | A fold over 'append'.
concat :: [FilePath] -> FilePath
concat [] = empty
concat ps = foldr1 append ps
-- | Find the greatest common prefix between a list of 'FilePath's.
commonPrefix :: [FilePath] -> FilePath
commonPrefix [] = empty
commonPrefix ps = foldr1 step ps where
step x y = if pathRoot x /= pathRoot y
then empty
else let cs = commonDirectories x y in
if cs /= pathDirectories x || pathBasename x /= pathBasename y
then empty { pathRoot = pathRoot x, pathDirectories = cs }
else let exts = commonExtensions x y in
x { pathExtensions = exts }
commonDirectories x y = common (pathDirectories x) (pathDirectories y)
commonExtensions x y = common (pathExtensions x) (pathExtensions y)
common [] _ = []
common _ [] = []
common (x:xs) (y:ys) = if x == y
then x : common xs ys
else []
-- Remove a prefix from a path.
--
-- @
-- stripPrefix "/foo/" "/foo/bar/baz.txt" == Just "bar/baz.txt"
-- stripPrefix "/foo/" "/bar/baz.txt" == Nothing
-- @
--
-- Since: 0.4.1
stripPrefix :: FilePath -> FilePath -> Maybe FilePath
stripPrefix x y = if pathRoot x /= pathRoot y
then case pathRoot x of
Nothing -> Just y
Just _ -> Nothing
else do
dirs <- strip (pathDirectories x) (pathDirectories y)
case dirs of
[] -> case (pathBasename x, pathBasename y) of
(Nothing, Nothing) -> do
exts <- strip (pathExtensions x) (pathExtensions y)
return (y { pathRoot = Nothing, pathDirectories = dirs, pathExtensions = exts })
(Nothing, Just _) -> case pathExtensions x of
[] -> Just (y { pathRoot = Nothing, pathDirectories = dirs })
_ -> Nothing
(Just x_b, Just y_b) | x_b == y_b -> do
exts <- strip (pathExtensions x) (pathExtensions y)
return (empty { pathExtensions = exts })
_ -> Nothing
_ -> case (pathBasename x, pathExtensions x) of
(Nothing, []) -> Just (y { pathRoot = Nothing, pathDirectories = dirs })
_ -> Nothing
strip :: Eq a => [a] -> [a] -> Maybe [a]
strip [] ys = Just ys
strip _ [] = Nothing
strip (x:xs) (y:ys) = if x == y
then strip xs ys
else Nothing
-- | Remove @\".\"@ and @\"..\"@ directories from a path.
--
-- Note that if any of the elements are symbolic links, 'collapse' may change
-- which file the path resolves to.
--
-- Since: 0.2
collapse :: FilePath -> FilePath
collapse p = p { pathDirectories = reverse newDirs } where
(_, newDirs) = foldl' step (True, []) (pathDirectories p)
step (True, acc) c = (False, c:acc)
step (_, acc) c | c == dot = (False, acc)
step (_, acc) c | c == dots = case acc of
[] -> (False, c:acc)
(h:ts) | h == dot -> (False, c:ts)
| h == dots -> (False, c:acc)
| otherwise -> (False, ts)
step (_, acc) c = (False, c:acc)
-------------------------------------------------------------------------------
-- Extensions
-------------------------------------------------------------------------------
-- | Get a 'FilePath'’s last extension, or 'Nothing' if it has no
-- extensions.
extension :: FilePath -> Maybe T.Text
extension p = case extensions p of
[] -> Nothing
es -> Just (last es)
-- | Get a 'FilePath'’s full extension list.
extensions :: FilePath -> [T.Text]
extensions = map unescape' . pathExtensions
-- | Get whether a 'FilePath'’s last extension is the predicate.
hasExtension :: FilePath -> T.Text -> Bool
hasExtension p e = extension p == Just e
-- | Append an extension to the end of a 'FilePath'.
addExtension :: FilePath -> T.Text -> FilePath
addExtension p ext = addExtensions p [ext]
-- | Append many extensions to the end of a 'FilePath'.
addExtensions :: FilePath -> [T.Text] -> FilePath
addExtensions p exts = p { pathExtensions = newExtensions } where
newExtensions = pathExtensions p ++ map escape exts
-- | An alias for 'addExtension'.
(<.>) :: FilePath -> T.Text -> FilePath
(<.>) = addExtension
-- | Remove a 'FilePath'’s last extension.
dropExtension :: FilePath -> FilePath
dropExtension p = p { pathExtensions = safeInit (pathExtensions p) }
-- | Remove all extensions from a 'FilePath'.
dropExtensions :: FilePath -> FilePath
dropExtensions p = p { pathExtensions = [] }
-- | Replace a 'FilePath'’s last extension.
replaceExtension :: FilePath -> T.Text -> FilePath
replaceExtension = addExtension . dropExtension
-- | Remove all extensions from a 'FilePath', and replace them with a new
-- list.
replaceExtensions :: FilePath -> [T.Text] -> FilePath
replaceExtensions = addExtensions . dropExtensions
-- | @splitExtension p = ('dropExtension' p, 'extension' p)@
splitExtension :: FilePath -> (FilePath, Maybe T.Text)
splitExtension p = (dropExtension p, extension p)
-- | @splitExtensions p = ('dropExtensions' p, 'extensions' p)@
splitExtensions :: FilePath -> (FilePath, [T.Text])
splitExtensions p = (dropExtensions p, extensions p)
-------------------------------------------------------------------------------
-- Utils
-------------------------------------------------------------------------------
safeInit :: [a] -> [a]
safeInit xs = case xs of
[] -> []
_ -> init xs
safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:_) = Just x
|