Design Pattern Examples
Overview of object-oriented design patterns
formatstring.cpp
Go to the documentation of this file.
1
6
7#include <cstdlib>
8#include <cstring>
9#include <cstdarg>
10
11#include <vector>
12
13#include "formatstring.h"
14
15namespace Helpers
16{
17 std::string formatstring(const char* fmt, ...)
18 {
19 va_list args;
20 va_start(args, fmt);
21 va_list args2;
22 va_copy(args2, args);
23 int numCharsNeeded = 0;
24 std::vector<char> buffer;
25 if (fmt != NULL)
26 {
27 numCharsNeeded = vsnprintf(NULL, 0, fmt, args) + 1;
28 buffer.resize(numCharsNeeded);
29 memset(&buffer[0], 0, numCharsNeeded);
30 vsnprintf(&buffer[0], numCharsNeeded, fmt, args2);
31 }
32 else
33 {
34 buffer.resize(1);
35 buffer[0] = '\0';
36 }
37 va_end(args2);
38 va_end(args);
39
40 return std::string(&buffer[0]);
41 }
42
43} // end namespace Helpers
Declaration of the formatstring() function that replaces sprintf() and snprintf() by allocating the r...
The namespace containing all the "helper" functions in the C++ code.
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....