I have had to deal with cross-platform filepaths in code before and come
up with the following abstraction (this is an exceprt from a larger
project, hopefully the idea is clear), which I find much nicer than dealing
with string based paths. It is only translated to/from a platform
specific form on input/output. all transformations happen on the
abstract type. Something similar might be useful in darcs.
module FilePath where
data Root = Root | Cwd !Bool | Drive String
data Component = Dot | DotDot | Name String
data FilePath = FilePath Root [Component]
FilePath {} </> fp@(FilePath Root _) = fp
FilePath rt cs </> fp@(FilePath Cwd cs') = FilePath rt (cs ++ cs')
-- ^ the argument to 'Cwd' is true if contains any explicit path
-- components. so './ls' and 'ls' can be differentiated.
canonicalize :: Bool -> FilePath -> FilePath
canonicalize rdots fp = f fp where
f (FilePath Root (DotDot:cs)) = f $ FilePath Root cs
f (FilePath Root (Dot:cs)) = f $ FilePath Root cs
f (FilePath Root cs) = FilePath Root (g cs)
f (FilePath (Cwd False) cs@(DotDot:_)) = f $ FilePath (Cwd True) cs
f (FilePath (Cwd False) cs@(Dot:_)) = f $ FilePath (Cwd True) cs
f (FilePath cwd cs) = FilePath cwd (g cs)
g (Dot:cs) = g cs
g (c:DotDot:cs) | rdots = g cs
g (c:cs) = c:g cs
g [] = []
parseUnixPath :: String -> FilePath
parseUnixPath ('/':s) = FilePath Root (g $ dropWhile ('/' ==) s)
parseUnixPath s = FilePath (Cwd False) (g s) where
g rs = case span (/= '/') rs of
(a,b) -> t a:g (dropWhile ('/' ==) b)
g [] = []
t "." = Dot
t ".." = DotDot
t s = Name s
showPath :: FilePath -> String
showPath fp = f fp where
f (FilePath Root cs) = '/':g cs
f (FilePath (Cwd _) []) = "."
f (FilePath (Cwd True) cs) = "./" ++ g cs
f (FilePath (Cwd False) cs) = g cs
g (a:b:cs) = t a ++ g (b:cs)
g (a:[]) = t a
g "" = ""
t Dot = "."
t DotDot = ".."
t (Name s) = s
--
John Meacham - ⑆repetae.net⑆john⑈
_______________________________________________
darcs-devel mailing list (AUTOMATIC POSTINGS ONLY PLEASE!)
[EMAIL PROTECTED]
http://lists.osuosl.org/mailman/listinfo/darcs-devel
_______________________________________________
darcs-users mailing list
[email protected]
http://lists.osuosl.org/mailman/listinfo/darcs-users