forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookupre.c
94 lines (79 loc) · 2.58 KB
/
lookupre.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "validation.h"
/*
* Lookup for a key which any of its @p where components matches the
* @p regex regular expression.
*
* TODO: Does not work (no example, no test case)
*
* @deprecated Does not work
* @param ks the KeySet to lookup into
* @param where any of @p KEY_SWITCH_NAME, @p KEY_SWITCH_VALUE,
* @p KEY_SWITCH_OWNER, @p KEY_SWITCH_COMMENT ORed.
* @param regexp a regcomp(3) pre-compiled regular expression
*
* @return some of @p KEY_SWITCH_NAME, @p KEY_SWITCH_VALUE,
* @p KEY_SWITCH_OWNER, @p KEY_SWITCH_COMMENT switches ORed to
* indicate @p where the @p regex matched.
*
* @see ksLookupByName(), ksLookupByString(), keyCompare() for other types of
* lookups.
* @see kdbGetByName()
*
* @par Example:
* @code
KeySet *ks = ksNew (5,
keyNew ("user:/a", KEY_VALUE, "a", KEY_COMMENT, "does not match", KEY_END),
keyNew ("user:/b", KEY_VALUE, " a ", KEY_COMMENT, "does not match", KEY_END),
keyNew ("user:/c", KEY_VALUE, "\t\t", KEY_COMMENT, "match", KEY_END),
keyNew ("user:/d", KEY_VALUE, " \t \t ", KEY_COMMENT, "match", KEY_END),
KS_END);
Key *match = 0;
regex_t regex;
regcomp(®ex,"^[ \t]*$",REG_NOSUB);
// we start from the first key
ksRewind(ks);
// show the key that match this string
match=ksLookupRE(ks,®ex);
output_key (match);
regfree(®ex); // free regex resources
ksDel (ks);
* @endcode
*
* @par More examples of regular expressions (untesteds of regular expressions (untested)):
* @code
// The spaces between '*' and '/' and '*' chars are Doxygen mirages :)
regcomp(®ex,
"some value .* more text", // match this
REG_NEWLINE | REG_NOSUB); // all in a single line
regfree(®ex);
regcomp(®ex,
"Device/.* /Options/ *", // only interested in option keys
REG_ICASE | REG_NOSUB); // ignore case
regfree(®ex);
regcomp(®ex,
"^system:/folder/.* /basename$", // match real system:/ keys that end with 'basename'
REG_NOSUB); // always use REG_NOSUB to increase performance
regfree(®ex);
regcomp(®ex,
"^system:/sw/xorg/.* /Screen[0-9]* /Displays/[0-9]* /Depth$", // we want all X.org's depths of all displays of all screens
REG_ICASE | REG_NOSUB); // we don't care about the case
regfree(®ex); // don't forget to free resources
* @endcode
*/
Key * ksLookupRE (KeySet * ks, const regex_t * regexp)
{
regmatch_t offsets;
Key *walker = 0, *end = 0;
while ((walker = ksNext (ks)) != end)
{
if (!regexec (regexp, keyString (walker), 1, &offsets, 0)) return walker;
}
return 0;
}