File: tempfiles.hs

package info (click to toggle)
ghc 9.0.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 177,780 kB
  • sloc: haskell: 494,441; ansic: 70,262; javascript: 9,423; sh: 8,537; python: 2,646; asm: 1,725; makefile: 1,333; xml: 196; cpp: 167; perl: 143; ruby: 84; lisp: 7
file content (36 lines) | stat: -rw-r--r-- 1,155 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

import Control.Exception
import Data.List (isPrefixOf, isSuffixOf)
import System.FilePath
import System.Directory
import System.IO

-- Checks that openTempFile returns filenames with the right structure
main :: IO ()
main = do
 fp0 <- otf ".no_prefix.hs"
 print (".hs"        `isSuffixOf` fp0)
 print (".no_prefix" `isPrefixOf` takeFileName fp0)

 fp1 <- otf "no_suffix"
 print (not ('.' `elem` fp1))
 print ("no_suffix" `isPrefixOf` takeFileName fp1)

 fp2 <- otf "one_suffix.hs"
 print (".hs"        `isSuffixOf` fp2)
 print ("one_suffix" `isPrefixOf` takeFileName fp2)

 fp3 <- otf "two_suffixes.hs.blah"
 print (".blah"           `isSuffixOf` fp3)
 print ("two_suffixes.hs" `isPrefixOf` takeFileName fp3)

otf :: FilePath -> IO FilePath
otf fp = do putStrLn fp
            bracket (openTempFile "." fp)
                    (\(fp', h) -> do hClose h
                                     removeFile fp')
                    (\(fp', _) -> case fp' of
                                  '.' : '/'  : fp'' -> return fp''
                                  '.' : '\\' : fp'' -> return fp''
                                  _                 -> return fp')