Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
I think I just killed a simcall
[simgrid.git] / src / xbt / string.cpp
index 4489c33..f23e429 100644 (file)
@@ -4,17 +4,56 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
+#include <cstdarg>
+#include <cstdio>
+
 #include <simgrid_config.h>
+
 #include <xbt/string.hpp>
+#include <xbt/sysdep.h>
 
 namespace simgrid {
 namespace xbt {
 
-#ifdef HAVE_MC
+#if HAVE_MC
 
 const char string::NUL = '\0';
 
 #endif
 
+std::string string_vprintf(const char *fmt, va_list ap)
+{
+  // Get the size:
+  va_list ap2;
+  va_copy(ap2, ap);
+  int size = std::vsnprintf(nullptr, 0, fmt, ap2);
+  va_end(ap2);
+  if (size < 0)
+    xbt_die("string_vprintf error");
+
+  // Allocate the string and format:
+  std::string res;
+  res.resize(size);
+  if (size != 0 && std::vsnprintf(&res[0], size + 1, fmt, ap) != size)
+    xbt_die("string_vprintf error");
+  return res;
+}
+
+std::string string_printf(const char *fmt, ...)
+{
+  va_list ap;
+  va_start(ap, fmt);
+  std::string res;
+  try {
+    res = string_vprintf(fmt, ap);
+  }
+  catch(...) {
+    va_end(ap);
+    throw;
+  }
+  va_end(ap);
+  return res;
+}
+
 }
 }