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