vul_sprintf.cxx
Go to the documentation of this file.
1 // This is core/vul/vul_sprintf.cxx
2 //
3 // Author: Andrew W. Fitzgibbon, Oxford RRG
4 // Created: 08 Aug 96
5 //
6 //-----------------------------------------------------------------------------
7 
8 #include <cstdarg>
9 #include <cstring>
10 #include <iostream>
11 #include <cstdio>
12 #include "vul_sprintf.h"
13 
14 #ifdef _MSC_VER
15 # include <vcl_msvc_warnings.h>
16 #endif
17 #undef vsprintf // this works around a bug in libintl.h
18 
19 vul_sprintf::vul_sprintf(char const *fmt, ...) : std::string("")
20 {
21  std::va_list ap;
22  va_start(ap, fmt);
23 
24  char s[65536];
25  std::vsprintf(s, fmt, ap);
26  if (std::strlen(s) >= sizeof s)
27  std::cerr << __FILE__ ": WARNING! Possible memory corruption after call to vsprintf()\n";
28  std::string::operator=(s);
29 
30  va_end(ap);
31 }
32 
33 std::ostream & operator<<(std::ostream &os,const vul_sprintf& s)
34 {
35  return os << (char const*)s;
36 }
37 
38 //--------------------------------------------------------------------------------
39 
40 #ifdef RUNTEST
41 main()
42 {
43  std::cout << vul_sprintf("fred%d\n", 3);
44  std::string fmt("foobar%d\n");
45  std::cout << vul_sprintf(fmt, 4);
46 }
47 #endif
std::ostream & operator<<(std::ostream &os, const vul_sprintf &s)
Definition: vul_sprintf.cxx:33
vul_sprintf(char const *fmt,...)
Definition: vul_sprintf.cxx:19
C++ conforming replacement to the ANSI C functions sprintf and printf.
Definition: vul_sprintf.h:31
creates a formatted ANSI C++ string