1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-22 10:19:57 +01:00

common: Add string operator gt,ge,le,lt to recsel.

* common/recsel.c (recsel_parse_expr): Add them.
(recsel_dump): Print them.
(recsel_select): Evaluate them.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2016-09-05 09:49:06 +02:00
parent c8e0d37f41
commit 959cd8903f
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
3 changed files with 101 additions and 3 deletions

View File

@ -48,7 +48,11 @@ typedef enum
SELECT_LE,
SELECT_GE,
SELECT_LT,
SELECT_GT
SELECT_GT,
SELECT_STRLE, /* String is less or equal. */
SELECT_STRGE,
SELECT_STRLT,
SELECT_STRGT
} select_op_t;
@ -347,6 +351,26 @@ recsel_parse_expr (recsel_expr_t *selector, const char *expression)
se->op = SELECT_ISTRUE;
s += 2;
}
else if (!strncmp (s, "-le", 3))
{
se->op = SELECT_STRLE;
s += 3;
}
else if (!strncmp (s, "-ge", 3))
{
se->op = SELECT_STRGE;
s += 3;
}
else if (!strncmp (s, "-lt", 3))
{
se->op = SELECT_STRLT;
s += 3;
}
else if (!strncmp (s, "-gt", 3))
{
se->op = SELECT_STRGT;
s += 3;
}
else
{
log_error ("invalid operator in expression\n");
@ -467,7 +491,12 @@ recsel_dump (recsel_expr_t selector)
se->op == SELECT_LT? "< ":
se->op == SELECT_LE? "<=":
se->op == SELECT_GT? "> ":
se->op == SELECT_GE? ">=":"[oops]",
se->op == SELECT_GE? ">=":
se->op == SELECT_STRLT? "-lt":
se->op == SELECT_STRLE? "-le":
se->op == SELECT_STRGT? "-gt":
se->op == SELECT_STRGE? "-ge":
/**/ "[oops]",
se->value);
}
log_debug ("--- End selectors ---\n");
@ -541,6 +570,30 @@ recsel_select (recsel_expr_t selector,
case SELECT_LE:
result = (numvalue <= se->numvalue);
break;
case SELECT_STRGT:
if (se->xcase)
result = strcmp (value, se->value) > 0;
else
result = strcasecmp (value, se->value) > 0;
break;
case SELECT_STRGE:
if (se->xcase)
result = strcmp (value, se->value) >= 0;
else
result = strcasecmp (value, se->value) >= 0;
break;
case SELECT_STRLT:
if (se->xcase)
result = strcmp (value, se->value) < 0;
else
result = strcasecmp (value, se->value) < 0;
break;
case SELECT_STRLE:
if (se->xcase)
result = strcmp (value, se->value) <= 0;
else
result = strcasecmp (value, se->value) <= 0;
break;
}
}

View File

@ -171,6 +171,8 @@ test_2_getval (void *cookie, const char *name)
return " ";
else if (!strcmp (name, "letters"))
return "abcde";
else if (!strcmp (name, "str1"))
return "aaa";
else
return cookie;
}
@ -263,6 +265,37 @@ run_test_2 (void)
fail (0, 0);
FREEEXPR();
ADDEXPR ("str1 -gt aa");
if (!recsel_select (se, test_2_getval, NULL))
fail (0, 0);
FREEEXPR();
ADDEXPR ("str1 -gt aaa");
if (recsel_select (se, test_2_getval, NULL))
fail (0, 0);
FREEEXPR();
ADDEXPR ("str1 -ge aaa");
if (!recsel_select (se, test_2_getval, NULL))
fail (0, 0);
FREEEXPR();
ADDEXPR ("str1 -lt aab");
if (!recsel_select (se, test_2_getval, NULL))
fail (0, 0);
FREEEXPR();
ADDEXPR ("str1 -le aaa");
if (!recsel_select (se, test_2_getval, NULL))
fail (0, 0);
FREEEXPR();
ADDEXPR ("-c str1 -lt AAB");
if (recsel_select (se, test_2_getval, NULL))
fail (0, 0);
FREEEXPR();
ADDEXPR ("str1 -lt AAB");
if (!recsel_select (se, test_2_getval, NULL))
fail (0, 0);
FREEEXPR();
ADDEXPR ("uid -n");
if (!recsel_select (se, test_2_getval, NULL))

View File

@ -3500,12 +3500,24 @@ The supported operators (@var{op}) are:
@item <
The numerical value of the field must be LT than the value.
@item >=
@item >
The numerical value of the field must be GT than the value.
@item >=
The numerical value of the field must be GE than the value.
@item -le
The string value of the field must be less or equal than the value.
@item -lt
The string value of the field must be less than the value.
@item -gt
The string value of the field must be greater than the value.
@item -ge
The string value of the field must be greater or equal than the value.
@item -n
True if value is not empty (no value allowed).