X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/fd769219531a772284d75320187a0ef9f8118afd..ecd5f7562caf1d443bf22788fa5f4fac408776ec:/src/xbt/string.cpp diff --git a/src/xbt/string.cpp b/src/xbt/string.cpp index f378cec98a..7872cdc828 100644 --- a/src/xbt/string.cpp +++ b/src/xbt/string.cpp @@ -1,20 +1,59 @@ -/* Copyright (c) 2015. The SimGrid Team. +/* Copyright (c) 2015-2017. The SimGrid Team. * All rights reserved. */ /* 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 +#include + #include + #include +#include namespace simgrid { namespace xbt { -#if HAVE_MC +#if SIMGRID_HAVE_MC -const char string::NUL = '\0'; +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; +} + } }