X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/5ec2b80b686983f84ebab7c7398a29e73286deee..5dd78326c18ad61561204c0d11470890cd12d091:/src/xbt/xbt_os_file.cpp diff --git a/src/xbt/xbt_os_file.cpp b/src/xbt/xbt_os_file.cpp index 2786b5dfbe..32c7004e05 100644 --- a/src/xbt/xbt_os_file.cpp +++ b/src/xbt/xbt_os_file.cpp @@ -15,9 +15,61 @@ #include #endif +#include #include +#include #include /* POSIX dirname */ +static std::vector file_path; + +void simgrid::xbt::path_push(std::string const& str) +{ + file_path.push_back(str); +} +void simgrid::xbt::path_pop() +{ + file_path.pop_back(); +} +std::string simgrid::xbt::path_to_string() +{ + return boost::join(file_path, ":"); +} +FILE* simgrid::xbt::path_fopen(const std::string& name, const char* mode) +{ + if (name[0] == '/') // don't mess with absolute file names + return fopen(name.c_str(), mode); + + /* search relative files in the path */ + for (auto const& path_elm : file_path) { + std::string buff = path_elm + "/" + name; + FILE* file = fopen(buff.c_str(), mode); + + if (file) + return file; + } + return nullptr; +} + +std::ifstream* simgrid::xbt::path_ifsopen(const std::string& name) +{ + xbt_assert(not name.empty()); + + auto* fs = new std::ifstream(); + if (name[0] == '/') // don't mess with absolute file names + fs->open(name.c_str(), std::ifstream::in); + + /* search relative files in the path */ + for (auto const& path_elm : file_path) { + std::string buff = path_elm + "/" + name; + fs->open(buff.c_str(), std::ifstream::in); + + if (not fs->fail()) + return fs; + } + + return fs; +} + simgrid::xbt::Path::Path() { #if HAVE_UNISTD_H