Main MRPT website > C++ reference for MRPT 1.9.9
format.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
12 #include <mrpt/utils/utils_defs.h>
13 #include <mrpt/system/os.h>
14 #include <mrpt/system/memory.h>
15 
16 #include <cstdarg>
17 
18 // A sprintf-like function for std::string
19 std::string mrpt::format(const char* fmt, ...)
20 {
21  if (!fmt) return std::string();
22 
23  int result = -1, length = 2048;
25  while (result == -1)
26  {
27  buffer.resize(length);
28 
29  va_list args; // This must be done WITHIN the loop
30  va_start(args, fmt);
31  result = mrpt::system::os::vsnprintf(&buffer[0], length, fmt, args);
32  va_end(args);
33 
34  // Truncated?
35  if (result >= length) result = -1;
36  length *= 2;
37 
38  // Ok?
39  if (result >= 0)
40  {
41  buffer.resize(result);
42  }
43  }
44 
45  return buffer;
46 }
GLuint buffer
Definition: glext.h:3917
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
GLsizei const GLchar ** string
Definition: glext.h:4101
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:227
GLuint GLsizei GLsizei * length
Definition: glext.h:4064



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019