MRPT  1.9.9
format.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "core-precomp.h" // Precompiled headers
11 
12 #include <mrpt/core/format.h>
13 #include <cstdarg>
14 
15 // A sprintf-like function for std::string
16 std::string mrpt::format_impl(const char* fmt, ...)
17 {
18  if (!fmt) return std::string();
19 
20  int result = -1, length = 2048;
21  std::string buffer;
22  while (result == -1)
23  {
24  buffer.resize(length);
25 
26  va_list args; // This must be done WITHIN the loop
27  va_start(args, fmt);
28 #if defined(_MSC_VER)
29  result = ::vsnprintf_s(&buffer[0], length, _TRUNCATE, fmt, args);
30 #else
31  result = ::vsnprintf(&buffer[0], length, fmt, args);
32 #endif
33  va_end(args);
34 
35  // Truncated?
36  if (result >= length) result = -1;
37  length *= 2;
38 
39  // Ok?
40  if (result >= 0)
41  {
42  buffer.resize(result);
43  }
44  }
45  return buffer;
46 }
std::string format_impl(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
int vsnprintf(char *buf, size_t bufSize, const char *format, va_list args) noexcept
An OS-independent version of vsnprintf (Notice the bufSize param, which may be ignored in some compil...
Definition: os.cpp:230



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020