16 | show (Home xs) = "(Home " ++ show xs ++ ")"
17 | show (Absolute xs) = "(Absolute " ++ show xs ++ ")"
18 | show (Relative xs) = "(Relative " ++ show xs ++ ")"
21 | record FilePath where
22 | constructor MkFilePath
24 | fileName : Maybe String
28 | show x = "(MkFilePath " ++ (show (path x)) ++ " " ++ (show (fileName x)) ++ ")"
33 | (==) (MkFilePath (Home xs) fileName) (MkFilePath (Home ys) x) = (xs == ys) && (fileName == x)
34 | (==) (MkFilePath (Absolute xs) fileName) (MkFilePath (Absolute ys) x) = (xs == ys) && (fileName == x)
35 | (==) (MkFilePath (Relative xs) fileName) (MkFilePath (Relative ys) x) = (xs == ys) && (fileName == x)
39 | normalisePath : Dir -> Dir
40 | normalisePath [] = []
41 | normalisePath ("." :: xs) = normalisePath xs
42 | normalisePath (".." :: xs) = normalisePath xs
43 | normalisePath (x :: ".." :: xs) = normalisePath xs
44 | normalisePath (x :: xs) = x :: normalisePath xs
47 | normaliseFilePath : FilePath -> FilePath
48 | normaliseFilePath (MkFilePath (Home xs) fileName) = MkFilePath (Home (normalisePath xs)) fileName
49 | normaliseFilePath (MkFilePath (Absolute xs) fileName) = MkFilePath (Absolute (normalisePath xs)) fileName
50 | normaliseFilePath (MkFilePath (Relative xs) fileName) = MkFilePath (Relative (normalisePath xs)) fileName
53 | pathFromDir : Dir -> Path
54 | pathFromDir [] = Absolute []
55 | pathFromDir d@("." :: xs) = Relative d
56 | pathFromDir d@(".." :: xs) = Relative d
57 | pathFromDir d@("~" :: xs) = Home d
58 | pathFromDir d = Absolute d
61 | addSlashes : Dir -> String
62 | addSlashes x = concat (intersperse "/" x)
65 | prettyPrintPath : Path -> String
66 | prettyPrintPath (Home xs) = "~/" ++ (addSlashes xs)
67 | prettyPrintPath (Absolute xs) = "/" ++ (addSlashes xs)
68 | prettyPrintPath (Relative xs) = (addSlashes xs)
71 | splitOnFile : Dir -> (Dir, Maybe String)
72 | splitOnFile [] = ([], Nothing)
73 | splitOnFile [x] = ([], Just x)
74 | splitOnFile (x :: xs) =
75 | let (dir, file) = splitOnFile xs in
79 | mkFilePath : (Dir, Maybe String) -> (Dir -> Path) -> FilePath
80 | mkFilePath (a, b) f = MkFilePath (f a) b
83 | filePathFromPath : Path -> FilePath
84 | filePathFromPath (Home xs) = mkFilePath (splitOnFile xs) (Home)
85 | filePathFromPath (Absolute xs) = mkFilePath (splitOnFile xs) (Absolute)
86 | filePathFromPath (Relative xs) = mkFilePath (splitOnFile xs) (Relative)
89 | filePathForIO : FilePath -> String
90 | filePathForIO (MkFilePath path Nothing) = prettyPrintPath path
91 | filePathForIO (MkFilePath path (Just x)) = (prettyPrintPath path) ++ "/" ++ x