Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
surf_fopen to ifsopen + no need to reimplement streambuffers
authorFrederic Suter <frederic.suter@cc.in2p3.fr>
Thu, 23 Mar 2017 12:27:56 +0000 (13:27 +0100)
committerFrederic Suter <frederic.suter@cc.in2p3.fr>
Thu, 23 Mar 2017 12:27:56 +0000 (13:27 +0100)
include/xbt/str.h
src/surf/surf_interface.cpp
src/surf/trace_mgr.cpp
src/surf/trace_mgr.hpp
src/xbt/xbt_str.cpp

index 91669e6..78d7b3b 100644 (file)
@@ -43,8 +43,6 @@ XBT_PUBLIC(char *) xbt_str_join_array(const char *const *strs, const char *sep);
 XBT_PUBLIC(void) xbt_str_subst(char *str, char from, char to, int amount);
 XBT_PUBLIC(char *) xbt_str_varsubst(const char *str, xbt_dict_t patterns);
 
 XBT_PUBLIC(void) xbt_str_subst(char *str, char from, char to, int amount);
 XBT_PUBLIC(char *) xbt_str_varsubst(const char *str, xbt_dict_t patterns);
 
-XBT_PUBLIC(char *) xbt_str_from_file(FILE * file);
-
 XBT_PUBLIC(long int) xbt_str_parse_int(const char* str, const char* error_msg);
 XBT_PUBLIC(double) xbt_str_parse_double(const char* str, const char* error_msg);
 
 XBT_PUBLIC(long int) xbt_str_parse_int(const char* str, const char* error_msg);
 XBT_PUBLIC(double) xbt_str_parse_double(const char* str, const char* error_msg);
 
index be75f0a..9010a94 100644 (file)
@@ -135,6 +135,7 @@ std::ifstream* surf_ifsopen(const char* name)
   if (__surf_is_absolute_file_path(name)) { /* don't mess with absolute file names */
     fs->open(name, std::ifstream::in);
   }
   if (__surf_is_absolute_file_path(name)) { /* don't mess with absolute file names */
     fs->open(name, std::ifstream::in);
   }
+
   /* search relative files in the path */
   for (auto path_elm : surf_path) {
     std::string buff = path_elm + FILE_DELIM + name;
   /* search relative files in the path */
   for (auto path_elm : surf_path) {
     std::string buff = path_elm + FILE_DELIM + name;
index 5455bdb..4656258 100644 (file)
@@ -9,19 +9,21 @@
 #include "xbt/log.h"
 #include "xbt/str.h"
 
 #include "xbt/log.h"
 #include "xbt/str.h"
 
-#include "src/surf/trace_mgr.hpp"
 #include "src/surf/surf_interface.hpp"
 #include "src/surf/surf_interface.hpp"
+#include "src/surf/trace_mgr.hpp"
 #include "surf_private.h"
 #include "xbt/RngStream.h"
 #include <boost/algorithm/string.hpp>
 #include <boost/algorithm/string/join.hpp>
 #include <boost/algorithm/string/split.hpp>
 #include "surf_private.h"
 #include "xbt/RngStream.h"
 #include <boost/algorithm/string.hpp>
 #include <boost/algorithm/string/join.hpp>
 #include <boost/algorithm/string/split.hpp>
+#include <fstream>
 #include <math.h>
 #include <math.h>
+#include <sstream>
 #include <unordered_map>
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_trace, surf, "Surf trace management");
 
 #include <unordered_map>
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_trace, surf, "Surf trace management");
 
-static std::unordered_map<const char *, simgrid::trace_mgr::trace*> trace_list;
+static std::unordered_map<const char*, simgrid::trace_mgr::trace*> trace_list;
 
 simgrid::trace_mgr::trace::trace()=default;
 simgrid::trace_mgr::trace::~trace()=default;
 
 simgrid::trace_mgr::trace::trace()=default;
 simgrid::trace_mgr::trace::~trace()=default;
@@ -32,7 +34,7 @@ simgrid::trace_mgr::future_evt_set::~future_evt_set()
   xbt_heap_free(p_heap);
 }
 
   xbt_heap_free(p_heap);
 }
 
-tmgr_trace_t tmgr_trace_new_from_string(const char *name, const char *input, double periodicity)
+tmgr_trace_t tmgr_trace_new_from_string(const char* name, std::string input, double periodicity)
 {
   int linecount = 0;
   tmgr_event_t last_event = nullptr;
 {
   int linecount = 0;
   tmgr_event_t last_event = nullptr;
@@ -53,13 +55,17 @@ tmgr_trace_t tmgr_trace_new_from_string(const char *name, const char *input, dou
     if (sscanf(val.c_str(), "PERIODICITY " "%lg" "\n", &periodicity) == 1)
       continue;
 
     if (sscanf(val.c_str(), "PERIODICITY " "%lg" "\n", &periodicity) == 1)
       continue;
 
-    xbt_assert(sscanf(val.c_str(), "%lg" " " "%lg" "\n", &event.delta, &event.value) == 2,
-        "%s:%d: Syntax error in trace\n%s", name, linecount, input);
+    xbt_assert(sscanf(val.c_str(), "%lg"
+                                   " "
+                                   "%lg"
+                                   "\n",
+                      &event.delta, &event.value) == 2,
+               "%s:%d: Syntax error in trace\n%s", name, linecount, input.c_str());
 
     if (last_event) {
       xbt_assert(last_event->delta <= event.delta,
 
     if (last_event) {
       xbt_assert(last_event->delta <= event.delta,
-          "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s",
-          name, linecount, last_event->delta, event.delta, input);
+                 "%s:%d: Invalid trace: Events must be sorted, but time %g > time %g.\n%s", name, linecount,
+                 last_event->delta, event.delta, input.c_str());
 
       last_event->delta = event.delta - last_event->delta;
     } else {
 
       last_event->delta = event.delta - last_event->delta;
     } else {
@@ -86,13 +92,14 @@ tmgr_trace_t tmgr_trace_new_from_file(const char *filename)
   xbt_assert(filename && filename[0], "Cannot parse a trace from the null or empty filename");
   xbt_assert(trace_list.find(filename) == trace_list.end(), "Refusing to define trace %s twice", filename);
 
   xbt_assert(filename && filename[0], "Cannot parse a trace from the null or empty filename");
   xbt_assert(trace_list.find(filename) == trace_list.end(), "Refusing to define trace %s twice", filename);
 
-  FILE *f = surf_fopen(filename, "r");
-  xbt_assert(f != nullptr, "Cannot open file '%s' (path=%s)", filename, (boost::join(surf_path, ":")).c_str());
+  std::ifstream* f = surf_ifsopen(filename);
+  xbt_assert(!f->fail(), "Cannot open file '%s' (path=%s)", filename, (boost::join(surf_path, ":")).c_str());
+
+  std::stringstream buffer;
+  buffer << f->rdbuf();
+  tmgr_trace_t trace = tmgr_trace_new_from_string(filename, buffer.str(), 0.);
 
 
-  char *tstr = xbt_str_from_file(f);
-  fclose(f);
-  tmgr_trace_t trace = tmgr_trace_new_from_string(filename, tstr, 0.);
-  xbt_free(tstr);
+  delete f;
 
   return trace;
 }
 
   return trace;
 }
index 3260d66..ac19633 100644 (file)
@@ -40,8 +40,8 @@ XBT_PUBLIC(void) tmgr_trace_event_unref(tmgr_trace_iterator_t *trace_event);
 
 XBT_PUBLIC(void) tmgr_finalize(void);
 
 
 XBT_PUBLIC(void) tmgr_finalize(void);
 
-XBT_PUBLIC(tmgr_trace_t) tmgr_trace_new_from_file(const char *filename);
-XBT_PUBLIC(tmgr_trace_t) tmgr_trace_new_from_string(const char *id, const char *input, double periodicity);
+XBT_PUBLIC(tmgr_trace_t) tmgr_trace_new_from_file(const charfilename);
+XBT_PUBLIC(tmgr_trace_t) tmgr_trace_new_from_string(const char* id, std::string, double periodicity);
 
 SG_END_DECL()
 
 
 SG_END_DECL()
 
index 523ec91..8496a21 100644 (file)
@@ -461,25 +461,6 @@ char *xbt_str_join_array(const char *const *strs, const char *sep)
   return res;
 }
 
   return res;
 }
 
-/** @brief creates a new string containing what can be read on a fd */
-char *xbt_str_from_file(FILE * file)
-{
-  xbt_strbuff_t buff = xbt_strbuff_new();
-  char *res;
-  char bread[1024];
-  memset(bread, 0, 1024);
-
-  while (!feof(file)) {
-    int got = fread(bread, 1, 1023, file);
-    bread[got] = '\0';
-    xbt_strbuff_append(buff, bread);
-  }
-
-  res = buff->data;
-  xbt_strbuff_free_container(buff);
-  return res;
-}
-
 /** @brief Parse an integer out of a string, or raise an error
  *
  * The @a str is passed as argument to your @a error_msg, as follows:
 /** @brief Parse an integer out of a string, or raise an error
  *
  * The @a str is passed as argument to your @a error_msg, as follows: