[MINOR] pattern: make the converter more flexible by supporting void* and int args
The pattern type converters currently support a string arg and a length.
Sometimes we'll prefer to pass them a list or a structure. So let's convert
the string and length into a generic void* and int that each converter may
use as it likes.
diff --git a/include/types/pattern.h b/include/types/pattern.h
index c6f8ec8..811625e 100644
--- a/include/types/pattern.h
+++ b/include/types/pattern.h
@@ -54,8 +54,8 @@
/* pattern conversion */
struct pattern_conv {
const char *kw; /* configuration keyword */
- int (*process)(const char *arg,
- int arg_len,
+ int (*process)(const void *arg_p,
+ int arg_i,
union pattern_data *data); /* process function */
unsigned int in_type; /* input needed pattern type */
unsigned int out_type; /* output pattern type */
@@ -65,8 +65,8 @@
struct pattern_conv_expr {
struct list list; /* member of a pattern expression */
struct pattern_conv *conv; /* pattern conversion */
- char *arg; /* configured keyword argument */
- int arg_len; /* configured keyword argument length */
+ void *arg_p; /* pointer arg, most often a string argument */
+ int arg_i; /* int arg, most often the argument's length */
};
/* pattern fetch */
diff --git a/src/pattern.c b/src/pattern.c
index c593404..a5cb31f 100644
--- a/src/pattern.c
+++ b/src/pattern.c
@@ -432,10 +432,9 @@
conv_expr->conv = conv;
if (end != endw) {
- conv_expr->arg_len = end - endw - 2;
- conv_expr->arg = malloc(conv_expr->arg_len + 1);
- conv_expr->arg = memcpy(conv_expr->arg, endw + 1, conv_expr->arg_len);
- conv_expr->arg[expr->arg_len] = '\0';
+ conv_expr->arg_i = end - endw - 2;
+ conv_expr->arg_p = calloc(1, conv_expr->arg_i + 1);
+ memcpy(conv_expr->arg_p, endw + 1, conv_expr->arg_i);
}
}
return expr;
@@ -471,7 +470,7 @@
return NULL;
p->type = conv_expr->conv->in_type;
- if (!conv_expr->conv->process(conv_expr->arg, conv_expr->arg_len, &p->data))
+ if (!conv_expr->conv->process(conv_expr->arg_p, conv_expr->arg_i, &p->data))
return NULL;
p->type = conv_expr->conv->out_type;
@@ -532,7 +531,7 @@
/* Pattern format convert functions */
/*****************************************************************/
-static int pattern_conv_str2lower(const char *arg, int arg_len, union pattern_data *data)
+static int pattern_conv_str2lower(const void *arg_p, int arg_i, union pattern_data *data)
{
int i;
@@ -543,7 +542,7 @@
return 1;
}
-static int pattern_conv_str2upper(const char *arg, int arg_len, union pattern_data *data)
+static int pattern_conv_str2upper(const void *arg_p, int arg_i, union pattern_data *data)
{
int i;