File: tempfiles.hs

package info (click to toggle)
ghc 9.6.6-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 158,216 kB
  • sloc: haskell: 648,228; ansic: 81,656; cpp: 11,808; javascript: 8,444; sh: 5,831; fortran: 3,527; python: 3,277; asm: 2,523; makefile: 2,298; yacc: 1,570; lisp: 532; xml: 196; perl: 145; csh: 2
file content (36 lines) | stat: -rw-r--r-- 1,155 bytes parent folder | download | duplicates (3)
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')