forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatching.c
63 lines (54 loc) · 1.7 KB
/
matching.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
#include "kdb.h"
#include "kdbglobbing.h"
#include <kdbhelper.h>
#include <string.h>
#ifdef __MINGW32__
bool specMatches (Key * specKey, Key * otherKey)
{
/**
* Known limitation: For MINGW builds fnmatch.h does not exist. Therefore, globbing can't be used.
* This means that there is no support for # and _ in key names.
* This function was copied from 68e9dff, doesn't use globbing and therefore doesn't require the globbing library which is not
* compatible with Windows:
*/
if (specKey == NULL || otherKey == NULL)
{
return false;
}
const char * spec = keyUnescapedName (specKey);
size_t specNsLen = strlen (spec) + 1;
spec += specNsLen; // skip namespace
const char * other = keyUnescapedName (otherKey);
size_t otherNsLen = strlen (other) + 1;
other += otherNsLen; // skip namespace
size_t const specSize = keyGetUnescapedNameSize (specKey) - specNsLen;
size_t const otherSize = keyGetUnescapedNameSize (otherKey) - otherNsLen;
return specSize == otherSize && memcmp (spec, other, specSize) == 0;
}
#else
/**
* Check whether the {@link otherKey} matches the {@link specKey}.
*
* @param specKey specification key
* @param otherKey the other key to match the specification key with
* @retval 1 - if the keys match
* @retval 0 - if the keys do not match
*/
bool specMatches (Key * specKey, Key * otherKey)
{
if (specKey == NULL || otherKey == NULL)
{
return false;
}
// ignore namespaces for globbing
char * untilFirstSlash = strchr (keyName (otherKey), '/');
if (untilFirstSlash == NULL)
{
return NULL;
}
Key * globKey = keyNew (untilFirstSlash, KEY_END);
bool matches = elektraKeyGlob (globKey, strchr (keyName (specKey), '/')) == 0;
keyDel (globKey);
return matches;
}
#endif