mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-22 13:45:38 +00:00
Added helpers for dealing with enumerations
This commit is contained in:
parent
313f1a7c0d
commit
47605e1dfa
@ -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
|
||||
|
22
src/util.c
22
src/util.c
@ -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
|
||||
|
28
src/util.h
28
src/util.h
@ -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__ */
|
||||
|
Loading…
Reference in New Issue
Block a user