1 2import Control.Exception 3import Data.List (isPrefixOf, isSuffixOf) 4import System.FilePath 5import System.Directory 6import System.IO 7 8-- Checks that openTempFile returns filenames with the right structure 9main :: IO () 10main = do 11 fp0 <- otf ".no_prefix.hs" 12 print (".hs" `isSuffixOf` fp0) 13 print (".no_prefix" `isPrefixOf` takeFileName fp0) 14 15 fp1 <- otf "no_suffix" 16 print (not ('.' `elem` fp1)) 17 print ("no_suffix" `isPrefixOf` takeFileName fp1) 18 19 fp2 <- otf "one_suffix.hs" 20 print (".hs" `isSuffixOf` fp2) 21 print ("one_suffix" `isPrefixOf` takeFileName fp2) 22 23 fp3 <- otf "two_suffixes.hs.blah" 24 print (".blah" `isSuffixOf` fp3) 25 print ("two_suffixes.hs" `isPrefixOf` takeFileName fp3) 26 27otf :: FilePath -> IO FilePath 28otf fp = do putStrLn fp 29 bracket (openTempFile "." fp) 30 (\(fp', h) -> do hClose h 31 removeFile fp') 32 (\(fp', _) -> case fp' of 33 '.' : '/' : fp'' -> return fp'' 34 '.' : '\\' : fp'' -> return fp'' 35 _ -> return fp') 36 37