[Haskell-cafe] File path programme

robert dockins robdockins at fastmail.fm
Wed Jan 26 09:25:34 EST 2005


After the discussion about file paths over the last several days I went 
home and put together a quick trial implementation for unix file paths, 
with the idea of adding windows, SMB and maybe VMS (why not?) paths.  It 
is based on a Path class.  I'll post it later when I get home.  However, 
I will attempt to recreate the class definition from memory now to see 
if we can get some discussion going about the methods needed/desired, 
and whether or not this is a good approach.

data PathRoot
    = UnixFilesystemRoot
    | WindowsDrive Char
    | SMBShare String String
    | VMSDevice String
    | ... -- whatever else we need


class (Show p) => Path p where
    isAbsolute :: p -> Bool
    isRelative :: p -> Bool
    isRelative = not . isAbsolute
    basename :: p -> String
    parent :: p -> p
    pathAppend :: p -> p -> p  -- 2nd arg must be a relative path
    pathSeparator :: p -> Char
    pathParser :: Parser p     -- parsec parser
    parsePath :: String -> p
    parsePath str = case parse pathParser "" str of
                        Left e = error (Show e)
                        Right p = p


Other operations I think might be worthwhile:

pathRoot :: p -> Maybe PathRoot -- relative paths return Nothing
pathCleanup :: p -> p           -- remove .. and suchlike
commonAncestors :: p -> p -> p
pathFrom :: p -> p -> p       -- returns a relative path from the 1st to 
the 2nd
pathCompare :: p -> p -> ???  -- not sure what this should mean or return
hasExtension :: p -> String -> Bool
pathToForeign :: p -> IO (Ptr CChar)
pathFromForeign :: Ptr CChar -> IO p



More information about the Haskell-Cafe mailing list