X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/cd4198031d4f1c3c98cc0d21bcac25d1eb363259..b58b0ba2f6b92efa234677e19dd998346113504d:/src/xbt/xbt_os_file.cpp diff --git a/src/xbt/xbt_os_file.cpp b/src/xbt/xbt_os_file.cpp index 1e1e9f82e8..32c7004e05 100644 --- a/src/xbt/xbt_os_file.cpp +++ b/src/xbt/xbt_os_file.cpp @@ -1,42 +1,95 @@ -/* xbt_os_file.cpp -- portable interface to file-related functions */ +/* xbt_os_file.cpp -- portable interface to file-related functions */ -/* Copyright (c) 2017. The SimGrid Team. - * All rights reserved. */ +/* Copyright (c) 2017-2023. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ +#include "src/internal_config.h" +#include "xbt/asserts.h" #include "xbt/file.hpp" /* this module */ -#include "xbt/file.h" -#include "xbt/sysdep.h" - -#ifdef _WIN32 -#include +#if HAVE_UNISTD_H +#include +#include +#include #endif +#include #include +#include #include /* POSIX dirname */ -/** @brief Returns the file component of a path (reimplementation of POSIX basename) - * - * The argument is never modified, and the returned value must be freed after use. - */ -char *xbt_basename(const char *path) +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 xbt_strdup(simgrid::xbt::Path(path).getBasename().c_str()); + 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 + std::array buffer; + const char* cwd = getcwd(buffer.data(), 2048); + xbt_assert(cwd != nullptr, "Error during getcwd: %s", strerror(errno)); + path_ = cwd; +#else + path_ = "."; +#endif } -std::string simgrid::xbt::Path::getDirname() +std::string simgrid::xbt::Path::get_dir_name() const { std::string p(path_); - char *res = dirname(&p[0]); - return std::string(res, strlen(res)); + return dirname(&p[0]); } -std::string simgrid::xbt::Path::getBasename() +std::string simgrid::xbt::Path::get_base_name() const { std::string p(path_); - char *res = basename(&p[0]); - return std::string(res, strlen(res)); + return basename(&p[0]); }