module Language.Preprocessor.Cpphs.ReadFirst
( readFirst
) where
import IO (hPutStrLn, stderr)
import Directory (doesFileExist)
import List (intersperse)
import Language.Preprocessor.Cpphs.Position (Posn,directory)
import Language.Preprocessor.Cpphs.SymTab (SymTab,lookupST)
readFirst :: String
-> Posn
-> [String]
-> SymTab String
-> IO ( FilePath
, String
)
readFirst name demand path syms =
try (cons dd (".":path))
where
dd = directory demand
cons x xs = if null x then xs else x:xs
realname = real name syms
try [] = do
hPutStrLn stderr ("Warning: Can't find file \""++realname
++"\" in directories\n\t"
++concat (intersperse "\n\t" (cons dd (".":path)))
++"\n Asked for by: "++show demand)
return ("missing file: "++realname,"")
try (p:ps) = do
let file = p++'/':realname
ok <- doesFileExist file
if not ok then try ps
else do content <- readFile file
return (file,content)
real :: String -> SymTab String -> String
real name syms = case name of
('"':ns) -> init ns
('<':ns) -> init ns
_ -> case lookupST name syms of
Nothing -> name
Just f -> real f syms