module Browse where import Directory import Maybe (catMaybes) -- shorter names for some imported utilities getDir = getDirectoryContents isFile = doesFileExist isDir = doesDirectoryExist -------------------- -- a Tree type for picture files and directories -- (Show instance below gives outlines, or derive it if you prefer) data DTree = Pic FilePath | Dir FilePath [DTree] -- deriving Show -------------------- -- the main computation returns Just a DTree (or Nothing) -- (we will store full paths at each node for ease of use, -- although it is redundant and thus wasteful of space) dirtree :: FilePath -> IO (Maybe DTree) dirtree fp = when (not (dotted fp)) (do f <- isFile fp if f then when (isGIF fp) (return (Just (Pic fp))) else do d <- isDir fp when d (subtrees fp)) subtrees fp = do dlist <- getDir fp trees <- mapM dirtree (map (path fp) dlist) return (Just (Dir fp (catMaybes trees))) -------------------- -- put a nested directory of pictures on your -- H drive and adjust this path to point to it test = dirtree path >>= print where path = "/Users/fruehr/hugs/pics" -- where path = "H:\\public_html\\pictures" -------------------- -- some utility functions for readability -- for unix directories (and Windows?) with "dot" links dotted fp = take 1 (reverse fp) == "." -- expand this definition if you use JPEGs, PNGs, etc. isGIF fp = ".gif" == snd (break (=='.') fp) path p f = p ++ '/' : f when b p = if b then p else return Nothing -- outline-style show function for DTrees instance Show (DTree) where show = sh "" where sh i (Pic f) = i ++ f sh i (Dir d []) = i ++ d ++ " [D]" sh i (Dir d ds) = i ++ d ++ " [D] \n" ++ unline (map (sh ('\t':i)) ds) unline = foldr1 (\x y->x++'\n':y)