+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
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
#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);
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__ */