Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move a tiny bit of surf to xbt
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Fri, 20 Jan 2023 14:26:29 +0000 (15:26 +0100)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Fri, 20 Jan 2023 14:26:38 +0000 (15:26 +0100)
include/xbt/file.hpp
src/kernel/resource/profile/ProfileBuilder.cpp
src/plugins/file_system/s4u_FileSystem.cpp
src/simgrid/sg_config.cpp
src/surf/surf_interface.hpp
src/surf/xml/surfxml_sax_cb.cpp
src/xbt/xbt_os_file.cpp

index 5dde47a..d0447d3 100644 (file)
 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);
+void path_push(std::string const& str);
+void path_pop();
+FILE* path_fopen(const std::string& name, const char* mode);
+std::ifstream* path_ifsopen(const std::string& name);
+std::string path_to_string();
 
 class Path {
 public:
index ee634f0..742b854 100644 (file)
@@ -226,8 +226,9 @@ Profile* ProfileBuilder::from_string(const std::string& name, const std::string&
 Profile* ProfileBuilder::from_file(const std::string& filename)
 {
   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());
+  auto f = std::unique_ptr<std::ifstream>(simgrid::xbt::path_ifsopen(filename));
+  xbt_assert(not f->fail(), "Cannot open file '%s' (path=%s)", filename.c_str(),
+             simgrid::xbt::path_to_string().c_str());
 
   std::stringstream buffer;
   buffer << f->rdbuf();
index 6a79b28..4d6db1a 100644 (file)
@@ -364,9 +364,9 @@ std::map<std::string, sg_size_t, std::less<>>* FileSystemDiskExt::parse_content(
 
   auto* parse_content = new std::map<std::string, sg_size_t, std::less<>>();
 
-  auto fs = std::unique_ptr<std::ifstream>(simgrid::xbt::ifsopen_path(filename, surf_path));
+  auto fs = std::unique_ptr<std::ifstream>(simgrid::xbt::path_ifsopen(filename));
   xbt_assert(not fs->fail(), "Cannot open file '%s' (path=%s)", filename.c_str(),
-             (boost::join(surf_path, ":")).c_str());
+             simgrid::xbt::path_to_string().c_str());
 
   std::string line;
   std::vector<std::string> tokens;
index beafeb5..1e25468 100644 (file)
@@ -8,6 +8,7 @@
 #include <simgrid/instr.h>
 #include <simgrid/version.h>
 #include <xbt/config.hpp>
+#include <xbt/file.hpp>
 
 #include "simgrid/sg_config.hpp"
 #include "src/include/xbt/mmalloc.h"
@@ -285,9 +286,9 @@ void sg_config_init(int *argc, char **argv)
 
   /* Inclusion path */
   static simgrid::config::Flag<std::string> cfg_path{
-      "path", "Lookup path for inclusions in platform and deployment XML files", "", [](std::string const& path) {
+      "path", "Lookup path for inclusions in platform and deployment XML files", "./", [](std::string const& path) {
         if (not path.empty())
-          surf_path.push_back(path);
+          simgrid::xbt::path_push(path);
       }};
 
   static simgrid::config::Flag<bool> cfg_cpu_maxmin_selective_update{
@@ -349,9 +350,6 @@ void sg_config_init(int *argc, char **argv)
   static simgrid::config::Flag<bool> cfg_execution_cutpath{
       "exception/cutpath", "Whether to cut all path information from call traces, used e.g. in exceptions.", false};
 
-  if (surf_path.empty())
-    simgrid::config::set_default<std::string>("path", "./");
-
   _sg_cfg_init_status = 1;
 
   sg_config_cmd_line(argc, argv);
index 700b45c..7907a68 100644 (file)
@@ -28,7 +28,6 @@ XBT_PUBLIC_DATA double sg_maxmin_precision;
 XBT_PUBLIC_DATA double sg_surf_precision;
 XBT_PUBLIC_DATA int sg_concurrency_limit;
 
-extern XBT_PRIVATE std::vector<std::string> surf_path;
 extern XBT_PRIVATE std::unordered_map<std::string, simgrid::kernel::profile::Profile*> traces_set_list;
 
 /** set of hosts for which one want to be notified if they ever restart */
index ec8a58a..567a2b2 100644 (file)
@@ -832,9 +832,9 @@ void surf_parse_open(const std::string& file)
 {
   surf_parsed_filename = file;
   std::string dir      = simgrid::xbt::Path(file).get_dir_name();
-  surf_path.push_back(dir);
+  simgrid::xbt::path_push(dir);
 
-  surf_file_to_parse = simgrid::xbt::fopen_path(file, "r", surf_path);
+  surf_file_to_parse = simgrid::xbt::path_fopen(file, "r");
   if (surf_file_to_parse == nullptr)
     throw std::invalid_argument("Unable to open '" + file + "' from '" + simgrid::xbt::Path().get_name() +
                                 "'. Does this file exist?");
@@ -845,7 +845,7 @@ void surf_parse_open(const std::string& file)
 
 void surf_parse_close()
 {
-  surf_path.pop_back(); // remove the dirname of the opened file, that was added in surf_parse_open()
+  simgrid::xbt::path_pop(); // remove the dirname of the opened file, that was added in surf_parse_open()
 
   if (surf_file_to_parse) {
     surf_parse__delete_buffer(surf_input_buffer);
index 6779f56..32c7004 100644 (file)
 #include <unistd.h>
 #endif
 
+#include <boost/algorithm/string.hpp>
 #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)
+static std::vector<std::string> 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.c_str()[0] == '/') // don't mess with absolute file names
+  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 : path) {
+  for (auto const& path_elm : file_path) {
     std::string buff = path_elm + "/" + name;
     FILE* file       = fopen(buff.c_str(), mode);
 
@@ -35,16 +50,16 @@ FILE* simgrid::xbt::fopen_path(const std::string& name, const char* mode, const
   return nullptr;
 }
 
-std::ifstream* simgrid::xbt::ifsopen_path(const std::string& name, const std::vector<std::string>& path)
+std::ifstream* simgrid::xbt::path_ifsopen(const std::string& name)
 {
   xbt_assert(not name.empty());
 
   auto* fs = new std::ifstream();
-  if (name.c_str()[0] == '/') // don't mess with absolute file names
+  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 : path) {
+  for (auto const& path_elm : file_path) {
     std::string buff = path_elm + "/" + name;
     fs->open(buff.c_str(), std::ifstream::in);