use effective way to check if file exists

This commit is contained in:
ValKmjolnir 2022-04-21 20:51:47 +08:00
parent 6e035d1951
commit 2dc8459cbf
2 changed files with 17 additions and 8 deletions

View File

@ -46,10 +46,10 @@ private:
uint32_t error; uint32_t error;
public: public:
nasal_err():error(0){} nasal_err():error(0){}
void err(const char* stage,const std::string& info) void err(const char* stage,const std::string& info,const char end='\n')
{ {
++error; ++error;
std::cerr<<"["<<stage<<"] "<<info<<'\n'; std::cerr<<"["<<stage<<"] "<<info<<end;
} }
void err(const char* stage,uint32_t line,uint32_t column,const std::string& info) void err(const char* stage,uint32_t line,uint32_t column,const std::string& info)
{ {

View File

@ -66,6 +66,11 @@ nasal_ast nasal_import::file_import(nasal_ast& node)
// avoid infinite loading loop // avoid infinite loading loop
if(check_exist(filename)) if(check_exist(filename))
return {0,ast_root}; return {0,ast_root};
if(access(filename.c_str(),F_OK)==-1)
{
nerr.err("link","cannot open file <"+filename+">");
return {0,ast_root};
}
// start importing... // start importing...
lex.scan(filename); lex.scan(filename);
@ -86,17 +91,21 @@ nasal_ast nasal_import::lib_import()
"stl/lib.nas" "stl/lib.nas"
}; };
std::string filename; std::string filename="";
for(auto& i:libpath) for(auto& i:libpath)
{ if(access(i.c_str(),F_OK)!=-1)
std::ifstream fin(i);
if(!fin.fail())
{ {
filename=i; filename=i;
fin.close();
break; break;
} }
fin.close(); if(!filename.length())
{
std::string paths="";
for(auto& i:libpath)
paths+=" "+i+"\n";
nerr.err("link","cannot find lib file in these paths:\n"+paths,' ');
nerr.chkerr();
return {0,ast_root};
} }
// avoid infinite loading loop // avoid infinite loading loop