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

Daniel Burrows dburrows at costa.debian.org
Mon Sep 26 20:02:55 UTC 2005


Author: dburrows
Date: Mon Sep 26 20:02:52 2005
New Revision: 4290

Modified:
   branches/aptitude-0.3/aptitude/ChangeLog
   branches/aptitude-0.3/aptitude/src/generic/util/util.cc
   branches/aptitude-0.3/aptitude/src/generic/util/util.h
Log:
Write a safe wrapper for strftime.

Modified: branches/aptitude-0.3/aptitude/ChangeLog
==============================================================================
--- branches/aptitude-0.3/aptitude/ChangeLog	(original)
+++ branches/aptitude-0.3/aptitude/ChangeLog	Mon Sep 26 20:02:52 2005
@@ -1,5 +1,9 @@
 2005-09-26  Daniel Burrows  <dburrows at debian.org>
 
+	* src/generic/util/util.cc, src/generic/util/util.h:
+
+	  Write a safe strftime wrapper.
+
 	* src/edit_pkg_hier.cc, src/generic/apt/apt.cc, src/generic/apt/pkg_changelog.cc, src/ui.cc:
 
 	  Consistently use pkg_homedir() instead of getenv("HOME").

Modified: branches/aptitude-0.3/aptitude/src/generic/util/util.cc
==============================================================================
--- branches/aptitude-0.3/aptitude/src/generic/util/util.cc	(original)
+++ branches/aptitude-0.3/aptitude/src/generic/util/util.cc	Mon Sep 26 20:02:52 2005
@@ -23,6 +23,7 @@
 #include <pwd.h>
 #include <stdarg.h>
 #include <sys/types.h>
+#include <time.h>
 #include <unistd.h>
 
 using namespace std;
@@ -113,6 +114,34 @@
     }
 }
 
+string sstrftime(const char *format, const tm *tm)
+{
+  size_t bufsize = 512;
+
+  while(bufsize < 512 * 512)
+    {
+      char *buf = new char[bufsize];
+
+      buf[0] = '\1';
+      size_t result = strftime(buf, bufsize, format, tm);
+
+      if(result == 0 && buf[0] != '\0')
+	{
+	  delete[] buf;
+	  bufsize *= 2;
+	}
+      else
+	{
+	  // Could eliminate this with an "array smart pointer".
+	  string rval(buf);
+	  delete[] buf;
+	  return rval;
+	}
+    }
+
+  return "";
+}
+
 string get_homedir()
 {
   passwd pwbuf;

Modified: branches/aptitude-0.3/aptitude/src/generic/util/util.h
==============================================================================
--- branches/aptitude-0.3/aptitude/src/generic/util/util.h	(original)
+++ branches/aptitude-0.3/aptitude/src/generic/util/util.h	Mon Sep 26 20:02:52 2005
@@ -23,6 +23,8 @@
 #include <string>
 #include <utility>
 
+struct tm;
+
 // Strip whitespace from the beginning and end of a string.
 void stripws(std::string &s);
 
@@ -37,6 +39,11 @@
 
 std::wstring vswsprintf(const wchar_t *format, va_list ap);
 
+/** Like strftime, but handles all memory allocation and returns a C++
+ *  string.
+ */
+std::string sstrftime(const char *format, const tm *tm);
+
 /** \return the home directory of the current user as given in the
  *     password database.  If no home directory can be looked up,
  *     returns an empty string.



More information about the Aptitude-svn-commit mailing list