ofpbuf: Fix setting of 'msg' in ofpbuf_clone_with_headroom()
[cascardo/ovs.git] / lib / dynamic-string.c
index 5137d9f..a5a3460 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2016 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
 
 #include <config.h>
 #include "dynamic-string.h"
+#include <inttypes.h>
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
@@ -360,6 +361,29 @@ ds_swap(struct ds *a, struct ds *b)
     *b = temp;
 }
 
+void
+ds_put_hex(struct ds *ds, const void *buf_, size_t size)
+{
+    const uint8_t *buf = buf_;
+    bool printed = false;
+    int i;
+
+    for (i = 0; i < size; i++) {
+        uint8_t val = buf[i];
+        if (val || printed) {
+            if (!printed) {
+                ds_put_format(ds, "0x%"PRIx8, val);
+            } else {
+                ds_put_format(ds, "%02"PRIx8, val);
+            }
+            printed = true;
+        }
+    }
+    if (!printed) {
+        ds_put_char(ds, '0');
+    }
+}
+
 /* Writes the 'size' bytes in 'buf' to 'string' as hex bytes arranged 16 per
  * line.  Numeric offsets are also included, starting at 'ofs' for the first
  * byte in 'buf'.  If 'ascii' is true then the corresponding ASCII characters
@@ -383,12 +407,13 @@ ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
         n = end - start;
 
         /* Print line. */
-        ds_put_format(ds, "%08jx  ", (uintmax_t) ROUND_DOWN(ofs, per_line));
+        ds_put_format(ds, "%08"PRIxMAX"  ",
+                      (uintmax_t) ROUND_DOWN(ofs, per_line));
         for (i = 0; i < start; i++) {
             ds_put_format(ds, "   ");
         }
         for (; i < end; i++) {
-            ds_put_format(ds, "%02hhx%c",
+            ds_put_format(ds, "%02x%c",
                           buf[i - start], i == per_line / 2 - 1? '-' : ' ');
         }
         if (ascii) {
@@ -421,10 +446,13 @@ ds_last(const struct ds *ds)
     return ds->length > 0 ? (unsigned char) ds->string[ds->length - 1] : EOF;
 }
 
-void
+bool
 ds_chomp(struct ds *ds, int c)
 {
     if (ds->length > 0 && ds->string[ds->length - 1] == (char) c) {
         ds->string[--ds->length] = '\0';
+        return true;
+    } else {
+        return false;
     }
 }