Main MRPT website > C++ reference for MRPT 1.5.7
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 
13 #include <mrpt/utils/utils_defs.h>
14 #include <mrpt/system/os.h>
15 #include <mrpt/system/memory.h>
16 
17 #include <cstdarg>
18 
19 
20 // A sprintf-like function for std::string
21 std::string mrpt::format(const char *fmt, ...)
22 {
23  if (!fmt) return std::string();
24 
25  int result = -1, length = 2048;
27  while (result == -1)
28  {
29  buffer.resize(length);
30 
31  va_list args; // This must be done WITHIN the loop
32  va_start(args,fmt);
33  result = mrpt::system::os::vsnprintf(&buffer[0], length, fmt, args);
34  va_end(args);
35 
36  // Truncated?
37  if (result>=length) result=-1;
38  length*=2;
39 
40  // Ok?
41  if (result >= 0) {
42  buffer.resize(result);
43  }
44  }
45 
46  return buffer;
47 }
48 
int BASE_IMPEXP vsnprintf(char *buf, size_t bufSize, const char *format, va_list args) MRPT_NO_THROWS
An OS-independent version of vsnprintf (Notice the bufSize param, which may be ignored in some compil...
Definition: os.cpp:229
GLuint buffer
Definition: glext.h:3775
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:21
GLsizei const GLchar ** string
Definition: glext.h:3919
GLuint GLsizei GLsizei * length
Definition: glext.h:3900



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019