forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
name.c
86 lines (73 loc) · 1.5 KB
/
name.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
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "name.h"
#include <string.h>
#include "iterator.h"
#include "yajl.h"
/**
* @brief Count number of levels in name of key
*
* @param cur the key to count levels
*
* @return number of levels in key name
*/
ssize_t elektraKeyCountLevel (const Key * cur)
{
if (!cur)
{
return -1;
}
ssize_t curLevels = 0;
keyNameReverseIterator curIt = elektraKeyNameGetReverseIterator (cur);
while (elektraKeyNameReverseNext (&curIt))
{
++curLevels;
}
return curLevels;
}
/**
* @brief Count how many levels are equal between cur and cmp
* (starting from begin)
*
* @param cmp1 one key to compare
* @param cmp2 the other key to compare
*
* @retval 0 on null pointers or nothing equal
* @retval -1 when too many equal levels
*/
ssize_t elektraKeyCountEqualLevel (const Key * cmp1, const Key * cmp2)
{
if (!cmp1)
{
return 0;
}
if (!cmp2)
{
return 0;
}
const char * pcmp1 = keyUnescapedName (cmp1);
const char * pcmp2 = keyUnescapedName (cmp2);
size_t size1 = keyGetUnescapedNameSize (cmp1);
size_t size2 = keyGetUnescapedNameSize (cmp2);
size_t counter = 0;
const char * cur1 = pcmp1;
const char * cur2 = pcmp2;
while (strcmp (cur1, cur2) == 0)
{
const char * next1 = strchr (cur1, '\0');
const char * next2 = strchr (cur2, '\0');
++counter;
cur1 = next1 + 1;
cur2 = next2 + 1;
if (pcmp1 + size1 <= cur1 || pcmp2 + size2 <= cur2)
{
break;
}
}
return counter;
}