1 /* xbt_os_file.cpp -- portable interface to file-related functions */
3 /* Copyright (c) 2017-2023. The SimGrid Team. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include "src/internal_config.h"
9 #include "xbt/asserts.h"
10 #include "xbt/file.hpp" /* this module */
20 #include <libgen.h> /* POSIX dirname */
22 FILE* simgrid::xbt::fopen_path(const std::string& name, const char* mode, const std::vector<std::string>& path)
24 if (name.c_str()[0] == '/') // don't mess with absolute file names
25 return fopen(name.c_str(), mode);
27 /* search relative files in the path */
28 for (auto const& path_elm : path) {
29 std::string buff = path_elm + "/" + name;
30 FILE* file = fopen(buff.c_str(), mode);
38 std::ifstream* simgrid::xbt::ifsopen_path(const std::string& name, const std::vector<std::string>& path)
40 xbt_assert(not name.empty());
42 auto* fs = new std::ifstream();
43 if (name.c_str()[0] == '/') // don't mess with absolute file names
44 fs->open(name.c_str(), std::ifstream::in);
46 /* search relative files in the path */
47 for (auto const& path_elm : path) {
48 std::string buff = path_elm + "/" + name;
49 fs->open(buff.c_str(), std::ifstream::in);
58 simgrid::xbt::Path::Path()
61 std::array<char, 2048> buffer;
62 const char* cwd = getcwd(buffer.data(), 2048);
63 xbt_assert(cwd != nullptr, "Error during getcwd: %s", strerror(errno));
70 std::string simgrid::xbt::Path::get_dir_name() const
73 return dirname(&p[0]);
76 std::string simgrid::xbt::Path::get_base_name() const
79 return basename(&p[0]);