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)
readFirst :: String
-> Posn
-> [String]
-> Bool
-> IO ( FilePath
, String
)
readFirst name demand path warn =
try (cons dd (".":path))
where
dd = directory demand
cons x xs = if null x then xs else x:xs
try [] = do
when warn $
hPutStrLn stderr ("Warning: Can't find file \""++name
++"\" in directories\n\t"
++concat (intersperse "\n\t" (cons dd (".":path)))
++"\n Asked for by: "++show demand)
return ("missing file: "++name,"")
try (p:ps) = do
let file = p++'/':name
ok <- doesFileExist file
if not ok then try ps
else do content <- readFile file
return (file,content)