------------------------------------------------------------------------------- |-- Module : ReadFirst-- Copyright : 2004 Malcolm Wallace-- Licence : LGPL-- -- Maintainer : Malcolm Wallace <Malcolm.Wallace@cs.york.ac.uk>-- Stability : experimental-- Portability : All---- Read the first file that matches in a list of search paths.-----------------------------------------------------------------------------
module Language.Preprocessor.Cpphs.ReadFirst
( readFirst
) where
import System.IO (hPutStrLn, stderr)
import System.Directory (doesFileExist)
import Data.List (intersperse)
import Control.Monad (when)
import Language.Preprocessor.Cpphs.Position (Posn,directory,cleanPath)
-- | Attempt to read the given file from any location within the search path.-- The first location found is returned, together with the file content.-- (The directory of the calling file is always searched first, then-- the current directory, finally any specified search path.)readFirst :: String-- ^ filename
-> Posn-- ^ inclusion point
-> [String] -- ^ search path
-> Bool-- ^ report warnings?
-> IO ( FilePath
, String
) -- ^ discovered filepath, and file contentsreadFirstnamedemandpathwarn =
try (consdd (".":path))
where
dd = directorydemandconsxxs = if nullx then xs else x:xstry [] = do
whenwarn$hPutStrLnstderr ("Warning: Can't find file \""++name++"\" in directories\n\t"++concat (intersperse"\n\t" (consdd (".":path)))
++"\n Asked for by: "++showdemand)
return ("missing file: "++name,"")
try (p:ps) = do
let file = cleanPathp++'/':cleanPathnameok <- doesFileExistfile
if notok then tryps
else do content <- readFilefilereturn (file,content)