Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Methods for accessing key names.
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include <kdb.h>
10 :
11 : #include <string.h>
12 :
13 : /**
14 : * @brief get relative position of key based on parentKey
15 : *
16 : * @pre parentKey is either the same key as cur, or one of its parents
17 : * @post a pointer to the relevant part of the parent key's name, the full
18 : * name if there is no relation to the parentKey
19 : *
20 : * If the parentKey does not fulfill the precondition, the result won't be the
21 : * correct relative key of cur.
22 : *
23 : * @param cur the key below parentKey we want to get the relative basename of
24 : * @param parentKey the key that defines the root/base
25 : *
26 : * @return a pointer to the relative part name of the key cur
27 : */
28 1280 : const char * elektraKeyGetRelativeName (Key const * cur, Key const * parentKey)
29 : {
30 1280 : ssize_t offset = 0;
31 :
32 1280 : if (strcmp (keyName (parentKey), "/"))
33 : {
34 1274 : offset = keyGetNameSize (parentKey);
35 1274 : if (keyName (parentKey)[0] == '/' && keyName (cur)[0] != '/')
36 : {
37 43 : offset += strstr (keyName (cur), keyName (parentKey)) - keyName (cur);
38 : }
39 : }
40 1280 : if (offset == keyGetNameSize (cur))
41 : {
42 140 : offset = keyGetNameSize (cur) - 1; // equality of the keys
43 : }
44 1140 : else if (offset > keyGetNameSize (cur))
45 : {
46 2 : offset = 0; // no relation or invalid arguments, return full name
47 : }
48 1280 : return keyName (cur) + offset;
49 : }
|