New functions ds_put_strftime(), ds_chomp().
[openvswitch] / lib / dynamic-string.c
index 4b836819fa224c5dfe85dd5a55d453b77a6fdf8f..3193d438f03d6cc7add8a0490c63ffd5265e605d 100644 (file)
@@ -36,6 +36,7 @@
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 #include "util.h"
 
 void
@@ -141,6 +142,20 @@ ds_put_printable(struct ds *ds, const char *s, size_t n)
     }
 }
 
+void
+ds_put_strftime(struct ds *ds, const char *template, const struct tm *tm)
+{
+    for (;;) {
+        size_t avail = ds->string ? ds->allocated - ds->length + 1 : 0;
+        size_t used = strftime(&ds->string[ds->length], avail, template, tm);
+        if (used) {
+            ds->length += used;
+            return;
+        }
+        ds_reserve(ds, ds->length + (avail < 32 ? 64 : 2 * avail)); 
+    }
+}
+
 char *
 ds_cstr(struct ds *ds)
 {
@@ -215,3 +230,11 @@ ds_last(const struct ds *ds)
 {
     return ds->length > 0 ? (unsigned char) ds->string[ds->length - 1] : EOF;
 }
+
+void
+ds_chomp(struct ds *ds, int c)
+{
+    if (ds->length > 0 && ds->string[ds->length - 1] == (char) c) {
+        ds->string[--ds->length] = '\0';
+    }
+}