| 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 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 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) |