Debian Codegraph @

c2hs/base/general/FileOps.hs

raw symbols SCIP
1 -- Compiler Toolkit: operations on file
2 --
3 -- Author : Manuel M T Chakravarty
4 -- Created: 6 November 1999
5 --
6 -- Version $Revision: 1.1.1.1 $ from $Date: 2004/11/13 16:42:49 $
7 --
8 -- Copyright (c) [1999..2003] Manuel M T Chakravarty
9 --
10 -- This file is free software; you can redistribute it and/or modify
11 -- it under the terms of the GNU General Public License as published by
12 -- the Free Software Foundation; either version 2 of the License, or
13 -- (at your option) any later version.
14 --
15 -- This file is distributed in the hope that it will be useful,
16 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
17 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 -- GNU General Public License for more details.
19 --
20 --- DESCRIPTION ---------------------------------------------------------------
21 --
22 -- Typical operations needed when manipulating file names.
23 --
24 --- DOCU ----------------------------------------------------------------------
25 --
26 -- language: Haskell 98
27 --
28 --- TODO ----------------------------------------------------------------------
29 --
30
31 module FileOps (fileFindIn, mktemp)
32 where
33
34 import Prelude hiding (catch)
35 -- standard libs
36 import Data.Char (chr, ord)
37 import System.Directory (doesFileExist)
38 import System.IO (Handle, IOMode(..), openFile)
39 import Control.Monad (liftM)
40 import Control.Exception (catch, SomeException)
41 import System.Random (newStdGen, randomRs)
42
43 import FNameOps (dirname, stripDirname, addPath)
44
45
46 -- search for the given file in the given list of directories (EXPORTED)
47 --
48 -- * if the file does not exist, an exception is raised
49 --
50 -- * if the given file name is absolute, it is first tried whether this file
51 -- exists, afterwards the path component is stripped and the given
52 -- directories are searched; otherwise, if the file name is not absolute,
53 -- the path component is retained while searching the directories
54 --
55 fileFindIn :: FilePath -> [FilePath] -> IO FilePath
56 "" `fileFindIn` paths = fail "Empty file name"
57 file `fileFindIn` paths =
58 do
59 let (paths', file') = if head file == '/'
60 then (dirname file : paths, stripDirname file)
61 else (paths, file)
62 files = map (`addPath` file') paths'
63 existsFlags <- mapM doesFileExist files
64 let existingFiles = [file | (file, flag) <- zip files existsFlags, flag]
65 if null existingFiles
66 then fail (file ++ ": File does not exist")
67 else return $ head existingFiles
68
69 -- |Create a temporary file with a unique name.
70 --
71 -- * A unique sequence of at least six characters and digits is added
72 -- in between the two given components (the latter of which must include the
73 -- file suffix if any is needed)
74 --
75 -- * Default permissions are used, which might not be optimal, but
76 -- unfortunately the Haskell standard libs don't support proper permission
77 -- management.
78 --
79 -- * We make 100 attempts on getting a unique filename before giving up.
80 --
81 mktemp :: FilePath -> FilePath -> IO (Handle, FilePath)
82 mktemp pre post =
83 do
84 rs <- liftM (randomRs (0, 61)) newStdGen
85 -- range for lower and upper case letters plus digits
86 createLoop 100 rs
87 where
88 createLoop 0 _ = fail "mktemp: failed 100 times"
89 createLoop attempts rs = let
90 (rs', fname) = nextName rs
91 in do
92 h <- openFile fname ReadWriteMode
93 return (h, fname)
94 `catch` handler attempts rs'
95 --
96 handler :: Int -> [Int] -> SomeException -> IO (Handle,FilePath)
97 handler attempts rs' _ = createLoop (attempts - 1) rs'
98
99 sixChars :: [Int] -> ([Int], String)
100 sixChars is =
101 let
102 (sixInts, is') = splitAt 6 is
103 --
104 toChar i | i < 10 = chr . (ord '0' +) $ i
105 | i < 36 = chr . (ord 'A' +) . (subtract 10) $ i
106 | otherwise = chr . (ord 'a' +) . (subtract 36) $ i
107 in
108 (is', map toChar sixInts)
109 --
110 nextName :: [Int] -> ([Int], String)
111 nextName is = let
112 (is', rndChars) = sixChars is
113 in
114 (is', pre ++ rndChars ++ post)