-- -- | -- Module : Utils -- Author : Sean Seefried (http://www.cse.unsw.edu.au/~sseefried) -- Copyright : (c) 2005 -- License : BSD3 -- Created : 18 Jun 2005 -- module Utils where import System.IO import Data.List import Data.Char import System.Info import System.Directory import System.Time -- -- These functions taken gratefully from Don Stewart. Modified by -- shelarcy for windows. -- -- -- | dirname : return the directory portion of a file path -- if null, return "." -- dirname :: FilePath -> FilePath dirname p = let x = findIndices (== '\\') p y = findIndices (== '/') p in if not $ null x then if not $ null y then if (maximum x) > (maximum y) then dirname' '\\' p else dirname' '/' p else dirname' '\\' p else dirname' '/' p where dirname' separator p = case reverse $ dropWhile (/= separator) $ reverse p of [] -> "." p' -> p' -- -- | basename : return the filename portion of a path -- basename :: FilePath -> FilePath basename p = let x = findIndices (== '\\') p y = findIndices (== '/') p in if not $ null x then if not $ null y then if (maximum x) > (maximum y) then basename' '\\' p else basename' '/' p else basename' '\\' p else basename' '/' p where basename' chr p = reverse $ takeWhile (/= chr) $ reverse p -- -- drop suffix -- dropSuffix :: FilePath -> FilePath dropSuffix f = reverse . tail . dropWhile (/= '.') $ reverse f -- -- e.g. /home/dons/foo.rc -> /home/dons/foo.o -- -- we depend on the suffix we are given having a lead '.' -- replaceSuffix :: FilePath -> String -> FilePath replaceSuffix [] _ = [] -- ? replaceSuffix f suf = case reverse $ dropWhile (/= '.') $ reverse f of [] -> f ++ suf -- no '.' in file name f' -> f' ++ tail suf -- -- Checks for an absolute path -- absolutePath :: FilePath -> Bool absolutePath str = if os == "mingw32" then absolutePathForWindows str else absolutePath' str where absolutePath' ('/':_) = True absolutePath' ('~':_) = True absolutePath' _ = False absolutePathForWindows (x:xs@(y:_)) = isAlpha x && (y == ':' || y == '\\') -- -- Expects a path beginnning with a tilde. Replaces it with the -- home direcotory -- expandTilde :: FilePath -> IO FilePath expandTilde ('~':path) = do homeDir <- getHomeDirectory return (homeDir ++ path) expandTilde path = return path -- | Debug putStr dPutStr :: String -> IO () dPutStr str = do putStr str hFlush stdout timeFromStart (TOD s1 ps1) = do (TOD s2 ps2) <- getClockTime return $ (fromIntegral (s2-s1) + (fromIntegral (ps2-ps1) / 10^12))