提交 47605e1d 编写于 作者: D Daniel P. Berrange

Added helpers for dealing with enumerations

上级 313f1a7c
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
......
......@@ -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
......
......@@ -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__ */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册