Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move two generic functions from surf to xbt
authorMartin Quinson <martin.quinson@ens-rennes.fr>
Sun, 15 Jan 2023 23:18:07 +0000 (00:18 +0100)
committerMartin Quinson <martin.quinson@ens-rennes.fr>
Mon, 16 Jan 2023 07:46:02 +0000 (08:46 +0100)
include/xbt/file.hpp
src/kernel/resource/profile/ProfileBuilder.cpp
src/plugins/file_system/s4u_FileSystem.cpp
src/surf/surf_interface.cpp
src/surf/surf_interface.hpp
src/surf/xml/surfxml_sax_cb.cpp
src/xbt/xbt_os_file.cpp

index 9a0a155..5dde47a 100644 (file)
@@ -7,11 +7,15 @@
 #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) */
index bcb0709..ee634f0 100644 (file)
@@ -8,6 +8,7 @@
 #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>
@@ -222,17 +223,17 @@ Profile* ProfileBuilder::from_string(const std::string& name, const std::string&
   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());
 }
 
 
index 48f906c..6a79b28 100644 (file)
@@ -11,6 +11,7 @@
 #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>
 
@@ -363,7 +364,7 @@ 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>(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());
 
index 7704619..b0721c7 100644 (file)
@@ -88,53 +88,6 @@ const std::vector<surf_model_description_t> surf_optimization_mode_description =
     {"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)
 {
index 2237a67..700b45c 100644 (file)
@@ -22,8 +22,6 @@
 /*********
  * 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;
index a47b4f2..ec8a58a 100644 (file)
@@ -834,7 +834,7 @@ void surf_parse_open(const std::string& file)
   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?");
index 2786b5d..6779f56 100644 (file)
 #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