core/vul/vul_sprintf.cxx
Go to the documentation of this file.
00001 // This is core/vul/vul_sprintf.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //
00006 // Author: Andrew W. Fitzgibbon, Oxford RRG
00007 // Created: 08 Aug 96
00008 //
00009 //-----------------------------------------------------------------------------
00010 
00011 #include "vul_sprintf.h"
00012 
00013 #include <vcl_cstdarg.h>
00014 #include <vcl_cstring.h>
00015 #include <vcl_iostream.h>
00016 #undef vsprintf // this works around a bug in libintl.h
00017 #include <vcl_cstdio.h> // for vsprintf()
00018 
00019 vul_sprintf::vul_sprintf(char const *fmt, ...) : vcl_string("")
00020 {
00021   vcl_va_list ap;
00022   va_start(ap, fmt);
00023 
00024   char s[65536];
00025   vcl_vsprintf(s, fmt, ap);
00026   if (vcl_strlen(s) >= sizeof s)
00027     vcl_cerr << __FILE__ ": WARNING! Possible memory corruption after call to vsprintf()\n";
00028   vcl_string::operator=(s);
00029 
00030   va_end(ap);
00031 }
00032 
00033 vcl_ostream & operator<<(vcl_ostream &os,const vul_sprintf& s)
00034 {
00035   return os << (char const*)s;
00036 }
00037 
00038 //--------------------------------------------------------------------------------
00039 
00040 #ifdef RUNTEST
00041 main()
00042 {
00043   vcl_cout << vul_sprintf("fred%d\n", 3);
00044   vcl_string fmt("foobar%d\n");
00045   vcl_cout << vul_sprintf(fmt, 4);
00046 }
00047 #endif