module Files (createDirectory,doesFileExist,doesDirectoryExist, reportError,baseName,newSuffix,isNewer) where import Directory (createDirectory,getModificationTime,doesFileExist,doesDirectoryExist) import Control.Exception (tryJust,ioErrors) import System.IO (stderr,hPutStrLn) import Regex (mkRegex,mkSub,doSub,isMatch) reportError :: String -> String -> IO () reportError msg who = hPutStrLn stderr $ "annote: " ++ who ++ ": " ++ msg baseName :: FilePath -> FilePath baseName name = doSub name $ mkSub ("^.*/", "") newSuffix :: FilePath -> String -> FilePath newSuffix name ext = if isMatch name $ mkRegex "[.]" then doSub name $ mkSub ("[.][^.]*$", suffix) else name ++ suffix where suffix = '.' : ext isNewer :: FilePath -> FilePath -> IO Bool isNewer name1 name2 = do exists1 <- doesFileExist name1 if exists1 then do exists2 <- doesFileExist name2 if exists2 then do try <- tryJust ioErrors newer case try of Right t -> return t Left _ -> do reportError perr $ name1 ++ " or " ++ name2 return False else do return True else do reportError derr name1 return False where newer = do t1 <- getModificationTime name1 t2 <- getModificationTime name2 return $ t1 > t2 derr = "file does not exist" perr = "file permission error"