forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
name.c
81 lines (70 loc) · 1.48 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
/**
* @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"
// TODO defined privately in keyhelpers.c, API break possible..
char * keyNameGetOneLevel (const char * name, size_t * size);
/**
* @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 = keyName (cmp1);
const char * pcmp2 = keyName (cmp2);
size_t size1 = 0;
size_t size2 = 0;
ssize_t counter = 0;
while (*(pcmp1 = keyNameGetOneLevel (pcmp1 + size1, &size1)) && *(pcmp2 = keyNameGetOneLevel (pcmp2 + size2, &size2)) &&
size1 == size2 && !strncmp (pcmp1, pcmp2, size1))
{
++counter;
}
if (counter < 0)
{
counter = -1;
}
return counter;
}