module Music.Theory.Directory where
import Data.List
import Data.Maybe
import System.Directory
import System.FilePath
path_scan :: [FilePath] -> FilePath -> IO (Maybe FilePath)
path_scan p fn =
case p of
[] -> return Nothing
dir:p' -> let nm = dir </> fn
f x = if x then return (Just nm) else path_scan p' fn
in doesFileExist nm >>= f
path_scan_err :: [FilePath] -> FilePath -> IO FilePath
path_scan_err p x =
let err = (error ("path_scan: " ++ show p ++ ": " ++ x))
in fmap (fromMaybe err) (path_scan p x)
dir_subset :: [String] -> FilePath -> IO [FilePath]
dir_subset ext dir = do
let f nm = takeExtension nm `elem` ext
c <- getDirectoryContents dir
return (map (dir </>) (sort (filter f c)))
to_absolute_cwd :: FilePath -> IO FilePath
to_absolute_cwd x =
if isAbsolute x
then return x
else fmap (</> x) getCurrentDirectory