commit 3f30d167248c8e9a3a494ac679f1973e4a0229f5
parent e647526f7b8a4f77664ad9b239b7ce2b20fd2a6b
Author: hhvn <dev@hhvn.uk>
Date: Thu, 1 Dec 2022 18:34:16 +0000
Convenient enum setting/getting with bdb interface
Diffstat:
3 files changed, 56 insertions(+), 9 deletions(-)
diff --git a/src/bdb.c b/src/bdb.c
@@ -8,9 +8,11 @@
/* Bulk db interface
*
* Set: 't', key, val
+ * Set: 'e', key, val, char *(*func)(int)
* Set: 'T', key, val[], n
*
* Get: 't', key, &val
+ * Get: 'e', key, &val, int (*func)(char *)
* Get: 'T', key, val[], n
*
* Where 't'/'T' is:
@@ -19,6 +21,7 @@
* 'f' - float
* 'v' - vector
* 'p' - polar coordinate
+ * 'e' - enumeration (uses func to convert between str/int)
* 'S' - array of strings (will allocate memory for elements on get)
* 'I' - array of ints
* 'F' - array of floats
@@ -41,10 +44,12 @@ _bdbset(char *dir, char *group, ...) {
float f;
Vector v;
Polar p;
+ int e;
char **S;
int *I;
float *F;
} v;
+ char *(*func)(int);
int n, i;
va_start(ap, group);
@@ -77,6 +82,11 @@ _bdbset(char *dir, char *group, ...) {
v.p = va_arg(ap, Polar);
dbsetf(dir, group, key, "%f\t%f", v.p.r, v.p.theta);
break;
+ case 'e':
+ v.e = va_arg(ap, int);
+ func = va_arg(ap, char *(*)(int));
+ dbset(dir, group, key, func(v.e));
+ break;
case 'S':
v.S = va_arg(ap, char **);
n = va_arg(ap, int);
@@ -128,10 +138,12 @@ _bdbget(char *dir, char *group, ...) {
float *f;
Vector *v;
Polar *p;
+ int *e;
char **S;
int *I;
float *F;
} v;
+ int (*func)(char *);
int i, n;
va_start(ap, group);
@@ -169,6 +181,12 @@ _bdbget(char *dir, char *group, ...) {
dbgetf(dir, group, key, "%f\t%f",
&(*v.p).r, &(*v.p).theta);
break;
+ case 'e':
+ v.e = va_arg(ap, int *);
+ func = va_arg(ap, int (*)(char *));
+ str = dbget(dir, group, key);
+ *v.e = func(str);
+ break;
case 'S':
v.S = va_arg(ap, char **);
n = va_arg(ap, int);
diff --git a/tests/bdb.test b/tests/bdb.test
@@ -10,15 +10,37 @@ static unsigned char output[] = {
0x6f, 0x72, 0x09, 0x34, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x09,
0x35, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x0a, 0x70, 0x6f, 0x6c,
0x61, 0x72, 0x09, 0x32, 0x38, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
- 0x09, 0x39, 0x30, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x0a, 0x41,
- 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x09, 0x73, 0x69, 0x78, 0x09, 0x73,
- 0x65, 0x76, 0x65, 0x6e, 0x0a, 0x41, 0x69, 0x6e, 0x74, 0x09, 0x38, 0x09,
- 0x39, 0x09, 0x31, 0x30, 0x0a, 0x41, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x09,
- 0x31, 0x31, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x09, 0x31, 0x32,
- 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x09, 0x30, 0x2e, 0x30, 0x30,
- 0x30, 0x30, 0x30, 0x30, 0x0a, 0x00
+ 0x09, 0x39, 0x30, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x0a, 0x65,
+ 0x6e, 0x75, 0x6d, 0x09, 0x6e, 0x75, 0x6d, 0x0a, 0x41, 0x73, 0x74, 0x72,
+ 0x69, 0x6e, 0x67, 0x09, 0x73, 0x69, 0x78, 0x09, 0x73, 0x65, 0x76, 0x65,
+ 0x6e, 0x0a, 0x41, 0x69, 0x6e, 0x74, 0x09, 0x38, 0x09, 0x39, 0x09, 0x31,
+ 0x30, 0x0a, 0x41, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x09, 0x31, 0x31, 0x2e,
+ 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x09, 0x31, 0x32, 0x2e, 0x30, 0x30,
+ 0x30, 0x30, 0x30, 0x30, 0x09, 0x30, 0x2e, 0x30, 0x30, 0x30, 0x30, 0x30,
+ 0x30, 0x0a, 0x00
};
+enum Enum {
+ ENUM_ERR = -1,
+ ENUM = 77,
+};
+
+static char *
+strify(int num) {
+ if (num == ENUM)
+ return "num";
+ else
+ return "error";
+}
+
+static int
+enumify(char *str) {
+ if (streq(str, "num"))
+ return ENUM;
+ else
+ return ENUM_ERR;
+}
+
%{
FILE *fp;
char *dir = ".";
@@ -31,6 +53,7 @@ static unsigned char output[] = {
float f = 3;
Vector v = {4, 5};
Polar p = {28, 90};
+ enum Enum e = ENUM;
char *S[3] = {"six", "seven", NULL};
int I[3] = {8, 9, 10};
float F[4] = {11, 12, 0, 0};
@@ -43,6 +66,7 @@ static unsigned char output[] = {
'f', "float", f,
'v', "vector", v,
'p', "polar", p,
+ 'e', "enum", e, strify,
'S', "Astring", S, 2,
'I', "Aint", I, 3,
'F', "Afloat", F, 3);
@@ -63,6 +87,7 @@ static unsigned char output[] = {
SET(f);
SET(v);
SET(p);
+ SET(e);
#undef SET
#define SET(var) memset(var, 57, sizeof(var));
SET(S);
@@ -75,6 +100,7 @@ static unsigned char output[] = {
'f', "float", &f,
'v', "vector", &v,
'p', "polar", &p,
+ 'e', "enum", &e, enumify,
'S', "Astring", S, 3,
'I', "Aint", I, 3,
'F', "Afloat", F, 4);
@@ -88,6 +114,7 @@ static unsigned char output[] = {
A(I[0] == 8);
A(I[1] == 9);
A(I[2] == 10);
+ A(e == ENUM);
#undef A
#define A(x, y) ck_assert_float_eq_tol(x, y, 0.1)
diff --git a/tests/mktest.awk b/tests/mktest.awk
@@ -3,20 +3,22 @@
BEGIN {
print "#include <check.h>"
done = 0
+ intest = 0
}
/^%{$/ {
+ intest = 1
print "START_TEST(ck_"test") {"
}
-!/^%{$/ && !/^}$/ {
+!/^%{$/ && (!intest || !/^}$/) {
if (done)
print "error: excess lines" > "/dev/stderr"
else
print
}
-/^}$/ {
+/^}$/ && intest {
print "}"
print "END_TEST"
print ""