From 2dc8459cbf387885fd89faa0f79075033e5453a0 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Thu, 21 Apr 2022 20:51:47 +0800 Subject: [PATCH] use effective way to check if file exists --- nasal_err.h | 4 ++-- nasal_import.h | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/nasal_err.h b/nasal_err.h index ffe0ec1..cda2f53 100644 --- a/nasal_err.h +++ b/nasal_err.h @@ -46,10 +46,10 @@ private: uint32_t error; public: 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; - std::cerr<<"["<"); + return {0,ast_root}; + } // start importing... lex.scan(filename); @@ -86,17 +91,21 @@ nasal_ast nasal_import::lib_import() "stl/lib.nas" }; - std::string filename; + std::string filename=""; for(auto& i:libpath) - { - std::ifstream fin(i); - if(!fin.fail()) + if(access(i.c_str(),F_OK)!=-1) { filename=i; - fin.close(); 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