Elektra
0.9.5
|
Methods to do various operations on Key names. More...
Functions | |
const char * | keyName (const Key *key) |
Returns a pointer to the abbreviated real internal key name. More... | |
ssize_t | keyGetNameSize (const Key *key) |
Bytes needed to store the key name without owner. More... | |
const void * | keyUnescapedName (const Key *key) |
Returns a keyname which is null separated and does not use backslash for escaping. More... | |
ssize_t | keyGetUnescapedNameSize (const Key *key) |
return size of unescaped name with embedded and terminating null characters More... | |
ssize_t | keyGetName (const Key *key, char *returnedName, size_t maxSize) |
Get abbreviated key name (without owner name). More... | |
ssize_t | keyGetUnescapedName (const Key *key, char *returnedName, size_t maxSize) |
Copies the unescaped name of a key into provided buffer. More... | |
ssize_t | keySetName (Key *key, const char *newName) |
Set a new name to a key. More... | |
ssize_t | keyAddName (Key *key, const char *newName) |
Add an already escaped name to the keyname. More... | |
int | keyReplacePrefix (Key *key, const Key *oldPrefix, const Key *newPrefix) |
Replaces a prefix of the key name of key . More... | |
bool | elektraKeyNameValidate (const char *name, bool isComplete) |
Takes an escaped key name and validates it. More... | |
void | elektraKeyNameCanonicalize (const char *name, char **canonicalName, size_t *canonicalSizePtr, size_t offset, size_t *usizePtr) |
Takes a valid (non-)canonical key name and produces its canonical form. More... | |
void | elektraKeyNameUnescape (const char *canonicalName, char *unescapedName) |
Takes a canonical key name and unescapes it. More... | |
const char * | keyBaseName (const Key *key) |
Returns a pointer to the internal unescaped key name where the basename starts. More... | |
ssize_t | keyGetBaseNameSize (const Key *key) |
Calculates number of bytes needed to store basename of key . More... | |
ssize_t | keyGetBaseName (const Key *key, char *returned, size_t maxSize) |
Calculate the basename of a key name and put it in returned finalizing the string with NULL. More... | |
size_t | elektraKeyNameEscapePart (const char *part, char **escapedPart) |
Takes a single key name part and produces its escaped form. More... | |
ssize_t | keyAddBaseName (Key *key, const char *baseName) |
Adds baseName (that will be escaped) to the current key name. More... | |
ssize_t | keySetBaseName (Key *key, const char *baseName) |
Sets baseName as the new basename for key . More... | |
elektraNamespace | keyGetNamespace (const Key *key) |
For currently valid namespaces see elektraNamespace. More... | |
ssize_t | keySetNamespace (Key *key, elektraNamespace ns) |
Changes the namespace of a key. More... | |
Methods to do various operations on Key names.
To use them:
These functions make it easier for C programmers to work with key names.
spec:/something
for specification of other keys.proc:/something
for in-memory keys, e.g. commandline.dir:/something
for dir keys in current working directorysystem:/something
for system keys in /etc or /user:/something
for user keys in home directoryuser:username/something
for other users (deprecated: kdbGet() + kdbSet() currently unsupported)/something
for cascading keys (actually refers to one of the above, see also ksLookup())
void elektraKeyNameCanonicalize | ( | const char * | name, |
char ** | canonicalName, | ||
size_t * | canonicalSizePtr, | ||
size_t | offset, | ||
size_t * | usizePtr | ||
) |
Takes a valid (non-)canonical key name and produces its canonical form.
As a side-effect it can also calculate the size of the corresponding unescaped key name.
name | The key name that is processed |
canonicalName | Output buffer for the canonical name |
canonicalSizePtr | Pointer to size of canonicalName |
offset | Offset into canonicalName |
usizePtr | Output variable for the size of the unescaped name |
name
MUST be a valid (non-)canonical key name. If it is not, the result is undefined canonicalName
MUST be a valid first argument for elektraRealloc() when cast to void** canonicalSizePtr
>= offset
offset
MUST be 0 or *canonicalName + offset
MUST point to the zero-termintor of a valid canonical key name that starts at *canonicalName
offset
is 0 then *usizePtr
MUST 0, otherwise *usizePtr
MUST be the correct unescaped size of the existing canonical name in *canonicalName
size_t elektraKeyNameEscapePart | ( | const char * | part, |
char ** | escapedPart | ||
) |
Takes a single key name part and produces its escaped form.
part | A single key name part, i.e. contained '/' will be escaped, '\0' terminates part |
escapedPart | Output buffer for the escaped form |
escapedPart
MUST be a valid first argument for elektraRealloc() when cast to void**
void elektraKeyNameUnescape | ( | const char * | canonicalName, |
char * | unescapedName | ||
) |
Takes a canonical key name and unescapes it.
canonicalName | The canonical name to unescape |
unescapedName | Output buffer for the unescaped name |
canonicalName
MUST be a canonical key name. If this is not the case, the result is undefined. unescapedName
MUST be allocated to the correct size.bool elektraKeyNameValidate | ( | const char * | name, |
bool | isComplete | ||
) |
Takes an escaped key name and validates it.
Complete key names must inlcude a namespace or a leading slash.
name | The escaped key name to check |
isComplete | Whether or not name is supposed to be a complete key name |
#true | If name is a valid key name. |
#false | Otherwise |
ssize_t keyAddBaseName | ( | Key * | key, |
const char * | baseName | ||
) |
Adds baseName
(that will be escaped) to the current key name.
A new baseName will be added, no other part of the key name will be affected.
Assumes that key
is a directory and will append baseName
to it. The function adds the path separator for concatenating.
So if key
has name "system:/dir1/dir2"
and this method is called with baseName
"mykey"
, the resulting key will have the name "system:/dir1/dir2/mykey"
.
When baseName
is 0 nothing will happen and the size of the name is returned.
The escaping rules apply as in above .
A simple example is:
E.g. if you add . it will be escaped:
key | the key object to work with |
baseName | the string to append to the name |
-1 | if the key had no name |
-1 | on NULL pointers |
-1 | if key was inserted to a keyset before |
-1 | on allocation errors |
ssize_t keyAddName | ( | Key * | key, |
const char * | newName | ||
) |
Add an already escaped name to the keyname.
The same way as in keySetName() this method finds the canonical pathname:
For example:
Unlike keySetName() it adds relative to the previous name and cannot change the namespace of a key. For example:
The passed name needs to be valid according the key name rules . It is not allowed to:
key | the key where a name should be added |
newName | the new name to append |
key
MUST be a valid #Key-1 | if key == NULL , key is read-only, newName == NULL or newName is not a valid escaped name |
key
const char* keyBaseName | ( | const Key * | key | ) |
Returns a pointer to the internal unescaped key name where the basename
starts.
This is a much more efficient version of keyGetBaseName() and you should use it if you are responsible enough to not mess up things. The name might change or even point to a wrong place after a keySetName(). So make sure to copy the memory before the name changes.
keyBaseName() returns "" when there is no keyBaseName. The reason is
And there is also support for really empty basenames:
key | the object to obtain the basename from |
"" | when the key has no (base)name |
0 | on NULL pointer |
ssize_t keyGetBaseName | ( | const Key * | key, |
char * | returned, | ||
size_t | maxSize | ||
) |
Calculate the basename of a key name and put it in returned
finalizing the string with NULL.
Some examples:
system:/some/keyname
is keyname
"user:/tmp/some key"
is "some key"
key | the key to extract basename from |
returned | a pre-allocated buffer to store the basename |
maxSize | size of the returned buffer |
returned
1 | on empty name |
-1 | on NULL pointers |
-1 | when maxSize is 0 or larger than SSIZE_MAX |
ssize_t keyGetBaseNameSize | ( | const Key * | key | ) |
Calculates number of bytes needed to store basename of key
.
Key names that have only root names (e.g. "system:/"
or "user:/"
or "user:domain"
) does not have basenames, thus the function will return 1 bytes to store "".
Basenames are denoted as:
system:/some/thing/basename
-> basename
user:domain/some/thing/base\/name
> base\/name
key | the key object to work with |
key's
basename including ending NULL ssize_t keyGetName | ( | const Key * | key, |
char * | returnedName, | ||
size_t | maxSize | ||
) |
Get abbreviated key name (without owner name).
When there is not enough space to write the name, nothing will be written and -1 will be returned.
maxSize is limited to SSIZE_MAX. When this value is exceeded -1 will be returned. The reason for that is that any value higher is just a negative return value passed by accident. Of course elektraMalloc is not as failure tolerant and will try to allocate.
returnedName
1 | when only a null was written |
-1 | when keyname is longer then maxSize or 0 or any NULL pointer |
key | the key object to work with |
returnedName | pre-allocated memory to write the key name |
maxSize | maximum number of bytes that will fit in returnedName, including the final NULL |
ssize_t keyGetNameSize | ( | const Key * | key | ) |
Bytes needed to store the key name without owner.
For an empty key name you need one byte to store the ending NULL. For that reason 1 is returned.
key | the key object to work with |
1 | if there is is no key Name |
-1 | on NULL pointer |
elektraNamespace keyGetNamespace | ( | const Key * | key | ) |
For currently valid namespaces see elektraNamespace.
To handle every possible cases (including namespaces) a key can have:
To loop over all valid namespaces use:
key | the key object to work with |
ssize_t keyGetUnescapedName | ( | const Key * | key, |
char * | returnedName, | ||
size_t | maxSize | ||
) |
Copies the unescaped name of a key into provided buffer.
We will only copy the full name, if the buffer is to small an error code is returned.
To ensure your buffer is big enough, you can use keyGetUnescapedNameSize() to find the correct size.
key | the Key to extract the unescaped name from |
returnedName | output buffer |
maxSize | maximum bytes that can be copied into returnedName |
key
MUST be a valid #Key and key != NULL
returnedName
MUST be allocated to be at least maxSize
bytes big and returnedName != NULL
-1 | precondition error |
-2 | the size of the unescaped name is bigger then maxSize |
returnedName
ssize_t keyGetUnescapedNameSize | ( | const Key * | key | ) |
return size of unescaped name with embedded and terminating null characters
key | the object to work with |
-1 | on null pointer |
0 | if no name |
const char* keyName | ( | const Key * | key | ) |
Returns a pointer to the abbreviated real internal key
name.
This is a much more efficient version of keyGetName() and can use it if you are responsible enough to not mess up things. You are not allowed to change anything in the returned array. The content of that string may change after keySetName() and similar functions. If you need a copy of the name, consider using keyGetName().
"" | when there is no keyName. The reason is |
Valid key names are:
spec:/something
for specification of other keys.proc:/something
for in-memory keys, e.g. commandline.dir:/something
for dir keys in current working directorysystem:/something
for system keys in /etc or /user:/something
for user keys in home directoryuser:username/something
for other users (deprecated: kdbGet() + kdbSet() currently unsupported)/something
for cascading keys (actually refers to one of the above, see also ksLookup())
key | the key object to work with |
"" | when there is no (an empty) keyname |
0 | on NULL pointer |
int keyReplacePrefix | ( | Key * | key, |
const Key * | oldPrefix, | ||
const Key * | newPrefix | ||
) |
Replaces a prefix of the key name of key
.
The function only modifies key
, if is is below (or same as) oldPrefix
(see keyIsBelowOrSame()) and they both have the same namespace (this is not always the case with keyIsBelowOrSame()).
In simple terms this function operates as follows:
key
and oldPrefix
had the same name, then afterwards key
will have the same name as newPrefix
.key
was in the same namespace as and below oldPrefix
, then after calling this function key
will be in the same namespace as and below newPrefix
.key
will not be modified.Note: We use const Key *
arguments for the prefixes instead of const char *
to ensure only valid key names can be passed as arguments.
key | The key that will be manipulated. |
oldPrefix | The name of this key will be removed from the front of the name of key . |
newPrefix | The name of this key will be added to the front of key , after the name of oldPrefix is removed. |
-1 | if key , oldPrefix or newPrefix are NULL or the name of key is marked as read-only |
0 | if key is not below (or same as) oldPrefix , i.e. there is no prefix to replace |
1 | if the prefix was sucessfully replaced |
ssize_t keySetBaseName | ( | Key * | key, |
const char * | baseName | ||
) |
Sets baseName
as the new basename for key
.
Only the baseName will be affected and no other part of the key.
A simple example is:
All text after the last '/'
in the key
keyname is erased and baseName
is appended. If baseName
is 0 (NULL), then the last part of the keyname is removed without replacement.
Let us suppose key
has name "system:/dir1/dir2/key1"
. If baseName
is "key2"
, the resulting key name will be "system:/dir1/dir2/key2"
. If baseName
is 0 (NULL), the resulting key name will be "system:/dir1/dir2"
. If baseName
is empty, the resulting key name will be "system:/dir1/dir2/%"
, where "%"
denotes an empty base name, as also shown in the following code:
keySetBaseName() does proper escaping on the supplied name argument.
You can use character sequences as baseName
(e.g. "."
(dot), ".."
(dot-dot), "%"
(empty basename)). They will be properly escaped and will not have their usual meaning.
If you want to add and not change the basename, use keyAddBaseName() instead. If you do not want escaping, use keyAddName() instead.
key | the key object to work with |
baseName | the string used to overwrite the basename of the key |
-1 | on NULL pointers in key |
-1 | if key was inserted to a keyset before |
-1 | on allocation errors |
ssize_t keySetName | ( | Key * | key, |
const char * | newName | ||
) |
Set a new name to a key.
A valid name is one of the forms:
spec:/something
for specification of other keys.proc:/something
for in-memory keys, e.g. commandline.dir:/something
for dir keys in current working directorysystem:/something
for system keys in /etc or /user:/something
for user keys in home directoryuser:username/something
for other users (deprecated: kdbGet() + kdbSet() currently unsupported)/something
for cascading keys (actually refers to one of the above, see also ksLookup())An invalid name either has an invalid namespace or a wrongly escaped \ at the end of the name.
See key names for the exact rules.
The last form has explicitly set the owner, to let the library know in which user folder to save the key. A owner is a user name. If it is not defined (the second form) current user is used.
You should always follow the guidelines for key tree structure creation.
A private copy of the key name will be stored, and the newName
parameter can be freed after this call.
.., . and / will be handled as in filesystem paths. A valid name will be build out of the (valid) name what you pass, e.g. user:///sw/../sw//././MyApp -> user:/sw/MyApp
On invalid names, NULL or "" the name will be "" afterwards.
size | in bytes of this new key name including ending NULL |
0 | if newName is an empty string or a NULL pointer (name will be empty afterwards) |
-1 | if newName is invalid (name will be empty afterwards) |
-1 | if key was inserted to a keyset before |
key | the key object to work with |
newName | the new key name |
ssize_t keySetNamespace | ( | Key * | key, |
elektraNamespace | ns | ||
) |
Changes the namespace of a key.
The rest of the name remains unchanged.
key | The #Key whose namespace will be changed |
ns | The new namespace of for key |
ns
MUST be a valid namespace and not KEY_NS_NONE key
MUST be a valid #Key, especially key != NULL
-1 | precondition error |
{key}'s escaped name const void* keyUnescapedName | ( | const Key * | key | ) |
Returns a keyname which is null separated and does not use backslash for escaping.
Slashes are replaced with null bytes. So cascading keys start with a null byte. Otherwise escaped characters, e.g. non-hierarchy slash, will be unescaped.
This name is essential if you want to iterate over parts of the key name, want to compare keynames and want to check relations of keys in the hierarchy.
key | the object to work with |
0 | on null pointers |
"" | if no name |