[Aptitude-svn-commit] r3702 - in branches/aptitude-0.3/aptitude: . src/generic

Daniel Burrows dburrows at costa.debian.org
Thu Aug 4 22:41:45 UTC 2005


Author: dburrows
Date: Thu Aug  4 22:41:41 2005
New Revision: 3702

Modified:
   branches/aptitude-0.3/aptitude/ChangeLog
   branches/aptitude-0.3/aptitude/src/generic/util.cc
   branches/aptitude-0.3/aptitude/src/generic/util.h
Log:
Add printf-alike for std::string.

Modified: branches/aptitude-0.3/aptitude/ChangeLog
==============================================================================
--- branches/aptitude-0.3/aptitude/ChangeLog	(original)
+++ branches/aptitude-0.3/aptitude/ChangeLog	Thu Aug  4 22:41:41 2005
@@ -1,3 +1,10 @@
+2005-08-04  Daniel Burrows  <dburrows at debian.org>
+
+	* src/generic/util.cc, src/generic/util.h:
+
+	  Write generic (inefficient) code to produce string objects
+	  from formatting codes (like printf).
+
 2005-08-02  Daniel Burrows  <dburrows at debian.org>
 
 	* src/generic/matchers.cc, src/generic/matchers.h:

Modified: branches/aptitude-0.3/aptitude/src/generic/util.cc
==============================================================================
--- branches/aptitude-0.3/aptitude/src/generic/util.cc	(original)
+++ branches/aptitude-0.3/aptitude/src/generic/util.cc	Thu Aug  4 22:41:41 2005
@@ -20,6 +20,7 @@
 #include "util.h"
 
 #include <ctype.h>
+#include <stdarg.h>
 
 using namespace std;
 
@@ -39,3 +40,72 @@
     s.assign(s, start, end-start);
 }
 
+string ssprintf(const char *format, ...)
+{
+  va_list ap;
+
+  va_start(ap, format);
+  string rval = vssprintf(format, ap);
+  va_end(ap);
+
+  return rval;
+}
+
+const int initbufsize=512;
+
+string vssprintf(const char *format, va_list ap)
+{
+  char buf[initbufsize];
+  int amt = vsnprintf(buf, initbufsize, format, ap);
+
+  if(amt < initbufsize)
+    return buf;
+  else
+    {
+      char *buf2 = new char[amt+1];
+
+      int amt2 = vsnprintf(buf2, initbufsize, format, ap);
+
+      assert(amt2 < amt+1);
+
+      string rval(buf2, amt2);
+
+      delete[] buf2;
+
+      return rval;
+    }
+}
+
+wstring swsprintf(const wchar_t *format, ...)
+{
+  va_list ap;
+
+  va_start(ap, format);
+  wstring rval = vswsprintf(format, ap);
+  va_end(ap);
+
+  return rval;
+}
+
+wstring vswsprintf(const wchar_t *format, va_list ap)
+{
+  wchar_t buf[initbufsize];
+  int amt = vswprintf(buf, initbufsize, format, ap);
+
+  if(amt < initbufsize)
+    return buf;
+  else
+    {
+      wchar_t *buf2 = new wchar_t[amt+1];
+
+      int amt2 = vswprintf(buf2, initbufsize, format, ap);
+
+      assert(amt2 < amt+1);
+
+      wstring rval(buf2, amt2);
+
+      delete[] buf2;
+
+      return rval;
+    }
+}

Modified: branches/aptitude-0.3/aptitude/src/generic/util.h
==============================================================================
--- branches/aptitude-0.3/aptitude/src/generic/util.h	(original)
+++ branches/aptitude-0.3/aptitude/src/generic/util.h	Thu Aug  4 22:41:41 2005
@@ -25,4 +25,11 @@
 // Strip whitespace from the beginning and end of a string.
 void stripws(std::string &s);
 
+// Printf for std::string.
+std::string ssprintf(const char *format, ...);
+std::string vssprintf(const char *format, va_list ap);
+
+std::wstring swsprintf(const wchar_t *format, ...);
+std::wstring vswsprintf(const wchar_t *format, va_list ap);
+
 #endif



More information about the Aptitude-svn-commit mailing list