For converting bitmap data to human-readable strings.
---
src/libvirt_private.syms | 1 +
src/util/virbitmap.c | 25 +++++++++++++++++++++++++
src/util/virbitmap.h | 4 ++++
src/util/virstring.h | 1 -
tests/virbitmaptest.c | 14 +++++++++++++-
5 files changed, 43 insertions(+), 2 deletions(-)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index d73a9f5..f3dc39b 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -970,6 +970,7 @@ virBitmapClearAll;
virBitmapClearBit;
virBitmapCopy;
virBitmapCountBits;
+virBitmapDataToString;
virBitmapEqual;
virBitmapFormat;
virBitmapFree;
diff --git a/src/util/virbitmap.c b/src/util/virbitmap.c
index 81aaa80..fb0df81 100644
--- a/src/util/virbitmap.c
+++ b/src/util/virbitmap.c
@@ -705,3 +705,28 @@ virBitmapCountBits(virBitmapPtr bitmap)
return ret;
}
+
+/**
+ * virBitmapDataToString:
+ * @data: the data
+ * @len: length of @data in bytes
+ *
+ * Convert a chunk of data containing bits information to a human
+ * readable string, e.g.: 0-1,4
+ *
+ * Returns: a string representation of the data, or NULL on error
+ */
+char *
+virBitmapDataToString(void *data,
+ int len)
+{
+ virBitmapPtr map = NULL;
+ char *ret = NULL;
+
+ if (!(map = virBitmapNewData(data, len)))
+ return NULL;
+
+ ret = virBitmapFormat(map);
+ virBitmapFree(map);
+ return ret;
+}
diff --git a/src/util/virbitmap.h b/src/util/virbitmap.h
index 6573aa2..142a218 100644
--- a/src/util/virbitmap.h
+++ b/src/util/virbitmap.h
@@ -111,4 +111,8 @@ ssize_t virBitmapNextClearBit(virBitmapPtr bitmap, ssize_t pos)
size_t virBitmapCountBits(virBitmapPtr bitmap)
ATTRIBUTE_NONNULL(1);
+char *virBitmapDataToString(void *data,
+ int len)
+ ATTRIBUTE_NONNULL(1);
+
#endif
diff --git a/src/util/virstring.h b/src/util/virstring.h
index 0ab9d96..6ddcff5 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -255,5 +255,4 @@ char *virStringReplace(const char *haystack,
const char *newneedle)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
-
#endif /* __VIR_STRING_H__ */
diff --git a/tests/virbitmaptest.c b/tests/virbitmaptest.c
index cc34216..139b8e2 100644
--- a/tests/virbitmaptest.c
+++ b/tests/virbitmaptest.c
@@ -266,7 +266,7 @@ test4(const void *data ATTRIBUTE_UNUSED)
return -1;
}
-/* test for virBitmapNewData/ToData */
+/* test for virBitmapNewData/ToData/DataToString */
static int
test5(const void *v ATTRIBUTE_UNUSED)
{
@@ -278,6 +278,7 @@ test5(const void *v ATTRIBUTE_UNUSED)
size_t i;
ssize_t j;
int ret = -1;
+ char *str = NULL;
bitmap = virBitmapNewData(data, sizeof(data));
if (!bitmap)
@@ -307,8 +308,19 @@ test5(const void *v ATTRIBUTE_UNUSED)
data2[4] != 0x04)
goto error;
+ if (!(str = virBitmapDataToString(data, sizeof(data))))
+ goto error;
+ if (STRNEQ(str, "0,9,34"))
+ goto error;
+ VIR_FREE(str);
+ if (!(str = virBitmapDataToString(data2, sizeof(data2))))
+ goto error;
+ if (STRNEQ(str, "0,2,9,15,34"))
+ goto error;
+
ret = 0;
error:
+ VIR_FREE(str);
virBitmapFree(bitmap);
VIR_FREE(data2);
return ret;
--
1.8.3.2