|
From: <ufo...@li...> - 2010-05-22 21:38:09
|
Revision: 1135
http://ufo2000.svn.sourceforge.net/ufo2000/?rev=1135&view=rev
Author: ssvb
Date: 2010-05-22 21:38:03 +0000 (Sat, 22 May 2010)
Log Message:
-----------
Added C++ variant of lua 'string.format' (a safe sprintf replacement)
New function 'string_format' function works just like 'sprintf', but
uses std::string as a destination.
From: Siarhei Siamashka <sia...@gm...>
Modified Paths:
--------------
trunk/src/text.cpp
trunk/src/text.h
Modified: trunk/src/text.cpp
===================================================================
--- trunk/src/text.cpp 2010-05-22 19:17:55 UTC (rev 1134)
+++ trunk/src/text.cpp 2010-05-22 21:38:03 UTC (rev 1135)
@@ -1049,3 +1049,29 @@
popup_dialog(help_dialog, 2);
font = old_font;
}
+
+std::string string_vformat(const char *fmt, va_list arglist)
+{
+ int bufsize = 1024;
+ char *buf = new char[bufsize];
+ while (true) {
+ int result = vsnprintf(buf, bufsize, fmt, arglist);
+ if (result >= 0 && result < bufsize)
+ break;
+ delete [] buf;
+ bufsize *= 2;
+ buf = new char[bufsize];
+ }
+ std::string text = buf;
+ delete [] buf;
+ return text;
+}
+
+std::string string_format(const char *fmt, ...)
+{
+ va_list arglist;
+ va_start(arglist, fmt);
+ std::string text = string_vformat(fmt, arglist);
+ va_end(arglist);
+ return text;
+}
Modified: trunk/src/text.h
===================================================================
--- trunk/src/text.h 2010-05-22 19:17:55 UTC (rev 1134)
+++ trunk/src/text.h 2010-05-22 21:38:03 UTC (rev 1135)
@@ -77,5 +77,12 @@
//! Show a large dialog with formatted text.
void show_help(const char *text);
+//! A C++ variant of lua 'string.format' (a safe sprintf replacement)
+#if defined(__GNUC__)
+__attribute__((__format__(__printf__,1,2)))
#endif
+std::string string_format(const char *fmt, ...);
+std::string string_vformat(const char *fmt, va_list arglist);
+#endif
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|