]> xenbits.xensource.com Git - libvirt.git/commitdiff
Added helpers for dealing with enumerations
authorDaniel P. Berrange <berrange@redhat.com>
Tue, 24 Jun 2008 15:00:15 +0000 (15:00 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Tue, 24 Jun 2008 15:00:15 +0000 (15:00 +0000)
ChangeLog
src/util.c
src/util.h

index a3fe8dccdcb7413e9ca0c90f69648f029092d39b..0c6ef6de3f3249e2c5ea996b72af9720909e7bf0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Tue Jun 24 15:59:33 EST 2008 Daniel P. Berrange <berrange@redhat.com>
+
+       * src/util.h, src/util.c: Added helpers for managing enumerations
+       and conversion to/from string vs integer format
+
 Tue Jun 24 15:29:33 EST 2008 Daniel P. Berrange <berrange@redhat.com>
 
        * src/storage_backend.h, src/storage_backend.c: Fix const-ness
index 5e50ef2f3b6e7a3169144d651d2c949fa894ed69..3980d1ba5b12ffe1464968ed7bd00468446e423e 100644 (file)
@@ -764,6 +764,28 @@ virParseMacAddr(const char* str, unsigned char *addr)
     return -1;
 }
 
+int virEnumFromString(const char *const*types,
+                      unsigned int ntypes,
+                      const char *type)
+{
+    unsigned int i;
+    for (i = 0 ; i < ntypes ; i++)
+        if (STREQ(types[i], type))
+            return i;
+
+    return -1;
+}
+
+const char *virEnumToString(const char *const*types,
+                            unsigned int ntypes,
+                            int type)
+{
+    if (type < 0 || type >= ntypes)
+        return NULL;
+
+    return types[type];
+}
+
 /* Translates a device name of the form (regex) "[fhv]d[a-z]+" into
  * the corresponding index (e.g. sda => 1, hdz => 26, vdaa => 27)
  * @param name The name of the device
index 9a634672ab4f371e8f0c39b6527dc1a822e87286..a0cd109d573d08e618456fac0e992364a8e67de1 100644 (file)
@@ -25,6 +25,7 @@
 #define __VIR_UTIL_H__
 
 #include "util-lib.h"
+#include "verify.h"
 
 int virExec(virConnectPtr conn, char **argv, int *retpid,
             int infd, int *outfd, int *errfd);
@@ -88,4 +89,31 @@ int virParseMacAddr(const char* str, unsigned char *addr);
 
 int virDiskNameToIndex(const char* str);
 
+
+int virEnumFromString(const char *const*types,
+                      unsigned int ntypes,
+                      const char *type);
+
+const char *virEnumToString(const char *const*types,
+                            unsigned int ntypes,
+                            int type);
+
+#define VIR_ENUM_IMPL(name, lastVal, ...)                               \
+    static const char const *name ## TypeList[] = { __VA_ARGS__ };      \
+    verify(ARRAY_CARDINALITY(name ## TypeList) == lastVal);             \
+    const char *name ## TypeToString(int type) {                        \
+        return virEnumToString(name ## TypeList,                        \
+                               ARRAY_CARDINALITY(name ## TypeList),     \
+                               type);                                   \
+    }                                                                   \
+    int name ## TypeFromString(const char *type) {                      \
+        return virEnumFromString(name ## TypeList,                      \
+                                 ARRAY_CARDINALITY(name ## TypeList),   \
+                                 type);                                 \
+    }
+
+#define VIR_ENUM_DECL(name)                             \
+    const char *name ## TypeToString(int type);         \
+    int name ## TypeFromString(const char*type);
+
 #endif /* __VIR_UTIL_H__ */