#define XBT_FILE_HPP
#include <string>
+#include <vector>
#include <xbt/base.h>
namespace simgrid {
namespace xbt {
+FILE* fopen_path(const std::string& name, const char* mode, const std::vector<std::string>& path);
+std::ifstream* ifsopen_path(const std::string& name, const std::vector<std::string>& path);
+
class Path {
public:
/** Build a path from the current working directory (CWD) */
#include "src/kernel/resource/profile/Profile.hpp"
#include "src/kernel/resource/profile/StochasticDatedValue.hpp"
#include "src/surf/surf_interface.hpp"
+#include "xbt/file.hpp"
#include <boost/algorithm/string.hpp>
#include <boost/intrusive/options.hpp>
return new Profile(name,cb,cb.get_repeat_delay());
}
-Profile* ProfileBuilder::from_file(const std::string& path)
+Profile* ProfileBuilder::from_file(const std::string& filename)
{
- xbt_assert(not path.empty(), "Cannot parse a trace from an empty filename");
- auto f = std::unique_ptr<std::ifstream>(surf_ifsopen(path));
- xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", path.c_str(), (boost::join(surf_path, ":")).c_str());
+ xbt_assert(not filename.empty(), "Cannot parse a trace from an empty filename");
+ auto f = std::unique_ptr<std::ifstream>(simgrid::xbt::ifsopen_path(filename, surf_path));
+ xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", filename.c_str(), (boost::join(surf_path, ":")).c_str());
std::stringstream buffer;
buffer << f->rdbuf();
LegacyUpdateCb cb(buffer.str(), -1);
- return new Profile(path,cb,cb.get_repeat_delay());
+ return new Profile(filename, cb, cb.get_repeat_delay());
}
#include <simgrid/simix.hpp>
#include <xbt/asserts.h>
#include <xbt/config.hpp>
+#include <xbt/file.hpp>
#include <xbt/log.h>
#include <xbt/parse_units.hpp>
auto* parse_content = new std::map<std::string, sg_size_t, std::less<>>();
- auto fs = std::unique_ptr<std::ifstream>(surf_ifsopen(filename));
+ auto fs = std::unique_ptr<std::ifstream>(simgrid::xbt::ifsopen_path(filename, surf_path));
xbt_assert(not fs->fail(), "Cannot open file '%s' (path=%s)", filename.c_str(),
(boost::join(surf_path, ":")).c_str());
{"Full", "Full update of remaining and variables. Slow but may be useful when debugging.", nullptr},
};
-/* returns whether #file_path is an absolute file path. Surprising, isn't it ? */
-static bool is_absolute_file_path(const std::string& file_path)
-{
- return (file_path.c_str()[0] == '/');
-}
-
-std::ifstream* surf_ifsopen(const std::string& name)
-{
- xbt_assert(not name.empty());
-
- auto* fs = new std::ifstream();
- if (is_absolute_file_path(name)) { /* 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 : surf_path) {
- std::string buff = path_elm + "/" + name;
- fs->open(buff.c_str(), std::ifstream::in);
-
- if (not fs->fail()) {
- XBT_DEBUG("Found file at %s", buff.c_str());
- return fs;
- }
- }
-
- return fs;
-}
-
-FILE* surf_fopen(const std::string& name, const char* mode)
-{
- FILE* file = nullptr;
-
- if (is_absolute_file_path(name)) /* don't mess with absolute file names */
- return fopen(name.c_str(), mode);
-
- /* search relative files in the path */
- for (auto const& path_elm : surf_path) {
- std::string buff = path_elm + "/" + name;
- file = fopen(buff.c_str(), mode);
-
- if (file)
- return file;
- }
- return nullptr;
-}
-
/** Displays the long description of all registered models, and quit */
void model_help(const char* category, const std::vector<surf_model_description_t>& table)
{
/*********
* Utils *
*********/
-XBT_PRIVATE FILE* surf_fopen(const std::string& name, const char* mode);
-XBT_PRIVATE std::ifstream* surf_ifsopen(const std::string& name);
/* user-visible parameters */
XBT_PUBLIC_DATA double sg_maxmin_precision;
std::string dir = simgrid::xbt::Path(file).get_dir_name();
surf_path.push_back(dir);
- surf_file_to_parse = surf_fopen(file, "r");
+ surf_file_to_parse = simgrid::xbt::fopen_path(file, "r", surf_path);
if (surf_file_to_parse == nullptr)
throw std::invalid_argument("Unable to open '" + file + "' from '" + simgrid::xbt::Path().get_name() +
"'. Does this file exist?");
#endif
#include <cstring>
+#include <fstream>
#include <libgen.h> /* POSIX dirname */
+FILE* simgrid::xbt::fopen_path(const std::string& name, const char* mode, const std::vector<std::string>& path)
+{
+ if (name.c_str()[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 : path) {
+ std::string buff = path_elm + "/" + name;
+ FILE* file = fopen(buff.c_str(), mode);
+
+ if (file)
+ return file;
+ }
+ return nullptr;
+}
+
+std::ifstream* simgrid::xbt::ifsopen_path(const std::string& name, const std::vector<std::string>& path)
+{
+ xbt_assert(not name.empty());
+
+ auto* fs = new std::ifstream();
+ if (name.c_str()[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 : 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