Make sure to set 'port_no' in ofp_port_mod messages.
[sliver-openvswitch.git] / lib / dynamic-string.c
index 68dd341..3193d43 100644 (file)
  * derivatives without specific, written prior permission.
  */
 
+#include <config.h>
 #include "dynamic-string.h"
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 #include "util.h"
 
 void
@@ -45,6 +47,21 @@ ds_init(struct ds *ds)
     ds->allocated = 0;
 }
 
+void
+ds_clear(struct ds *ds) 
+{
+    ds->length = 0;
+}
+
+void
+ds_truncate(struct ds *ds, size_t new_length)
+{
+    if (ds->length > new_length) {
+        ds->length = new_length;
+        ds->string[new_length] = '\0';
+    }
+}
+
 void
 ds_reserve(struct ds *ds, size_t min_length)
 {
@@ -111,6 +128,34 @@ ds_put_format_valist(struct ds *ds, const char *format, va_list args_)
     }
 }
 
+void
+ds_put_printable(struct ds *ds, const char *s, size_t n) 
+{
+    ds_reserve(ds, ds->length + n);
+    while (n-- > 0) {
+        unsigned char c = *s++;
+        if (c < 0x20 || c > 0x7e || c == '\\' || c == '"') {
+            ds_put_format(ds, "\\%03o", (int) c);
+        } else {
+            ds_put_char(ds, c);
+        }
+    }
+}
+
+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)
 {
@@ -179,3 +224,17 @@ ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
       size -= n;
     }
 }
+
+int
+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';
+    }
+}