Elektra  0.8.21
Functions
internal.c File Reference

Internal methods for Elektra. More...

#include "kdbinternal.h"
#include <kdbassert.h>
Include dependency graph for internal.c:

Functions

ssize_t elektraMemcpy (Key **array1, Key **array2, size_t size)
 Internal Methods for Elektra. More...
 
ssize_t elektraMemmove (Key **array1, Key **array2, size_t size)
 Copies the key array2 into where array1 points. More...
 
int elektraStrCmp (const char *s1, const char *s2)
 Compare Strings. More...
 
int elektraStrCaseCmp (const char *s1, const char *s2)
 Compare Strings ignoring case. More...
 
int elektraMemCaseCmp (const char *s1, const char *s2, size_t size)
 Compare two memory regions but make cmp chars uppercase before comparison. More...
 
int elektraRealloc (void **buffer, size_t size)
 Reallocate Storage in a save way. More...
 
void * elektraMalloc (size_t size)
 Allocate memory for Elektra. More...
 
void * elektraCalloc (size_t size)
 Allocate memory for Elektra. More...
 
void elektraFree (void *ptr)
 Free memory of Elektra or its backends. More...
 
char * elektraStrDup (const char *s)
 Copy string into new allocated memory. More...
 
char * elektraStrNDup (const char *s, size_t l)
 Copy buffer into new allocated memory. More...
 
size_t elektraStrLen (const char *s)
 Calculates the length in bytes of a string. More...
 
char * elektraFormat (const char *format,...)
 Does string formatting in fresh allocated memory. More...
 
char * elektraVFormat (const char *format, va_list arg_list)
 Does string formatting in fresh allocated memory. More...
 
int elektraValidateKeyName (const char *name, size_t size)
 Validates whether the supplied keyname is valid. More...
 

Detailed Description

Internal methods for Elektra.

Function Documentation

◆ elektraCalloc()

void* elektraCalloc ( size_t  size)

Allocate memory for Elektra.

Memory will be set to 0.

Parameters
sizethe requested size
See also
elektraMalloc

◆ elektraFormat()

char* elektraFormat ( const char *  format,
  ... 
)

Does string formatting in fresh allocated memory.

Parameters
formatas in printf()
...as in printf()
Returns
new allocated memory (free with elektraFree)

◆ elektraFree()

void elektraFree ( void *  ptr)

Free memory of Elektra or its backends.

Parameters
ptrthe pointer to free

If ptr is NULL, no operation is performed.

See also
elektraMalloc

◆ elektraMalloc()

void* elektraMalloc ( size_t  size)

Allocate memory for Elektra.

if ((buffer = elektraMalloc (length)) == 0) {
// here comes the failure handler
// no allocation happened here, so don't use buffer
#if DEBUG
fprintf (stderr, "Allocation error");
#endif
// return with error
}
Parameters
sizethe requested size

This function is compatible to ANSI-C malloc

See also
elektraFree
elektraCalloc

◆ elektraMemCaseCmp()

int elektraMemCaseCmp ( const char *  s1,
const char *  s2,
size_t  size 
)

Compare two memory regions but make cmp chars uppercase before comparison.

Parameters
s1The first string to be compared
s2The second string to be compared
sizeto be compared
Returns
a negative number if s1 is less than s2
Return values
0if s1 matches s2
Returns
a positive number if s1 is greater than s2

◆ elektraMemcpy()

ssize_t elektraMemcpy ( Key **  array1,
Key **  array2,
size_t  size 
)

Internal Methods for Elektra.

To use them:

#include <kdbinternal.h>

There are some areas where libraries have to reimplement some basic functions to archive support for non-standard systems, for testing purposes or to provide a little more convenience. Copies the key array2 into where array1 points. It copies size elements.

Overlapping is prohibited, use elektraMemmove() instead.

Parameters
array1the destination
array2the source
sizehow many pointer to Keys to copy
Return values
-1on null pointers
0if nothing was done
Returns
size how many keys were copied

◆ elektraMemmove()

ssize_t elektraMemmove ( Key **  array1,
Key **  array2,
size_t  size 
)

Copies the key array2 into where array1 points.

It copies size elements.

Overlapping is ok. If they do not overlap consider elektraMemcpy() instead.

Parameters
array1the destination
array2the source
sizehow many pointer to Keys to copy
Return values
-1on null pointers
0if nothing was done
Returns
size how many keys were copied

◆ elektraRealloc()

int elektraRealloc ( void **  buffer,
size_t  size 
)

Reallocate Storage in a save way.

if (elektraRealloc ((void **) & buffer, new_length) < 0) {
// here comes the failure handler
// you can still use the old buffer
#if DEBUG
fprintf (stderr, "Reallocation error\n");
#endif
elektraFree (buffer);
buffer = 0;
// return with error
}
Parameters
bufferis a pointer to a elektraMalloc
sizeis the new size for the memory
Return values
-1on failure
0on success

◆ elektraStrCaseCmp()

int elektraStrCaseCmp ( const char *  s1,
const char *  s2 
)

Compare Strings ignoring case.

Parameters
s1The first string to be compared
s2The second string to be compared
Returns
a negative number if s1 is less than s2
Return values
0if s1 matches s2
Returns
a positive number if s1 is greater than s2

◆ elektraStrCmp()

int elektraStrCmp ( const char *  s1,
const char *  s2 
)

Compare Strings.

Parameters
s1The first string to be compared
s2The second string to be compared
Returns
a negative number if s1 is less than s2
Return values
0if s1 matches s2
Returns
a positive number if s1 is greater than s2

◆ elektraStrDup()

char* elektraStrDup ( const char *  s)

Copy string into new allocated memory.

You need to free the memory yourself.

Note
that size is determined at runtime. So if you have a size information, don't use that function.
Parameters
sthe null-terminated string to duplicate
Returns
0 if out of memory, a pointer otherwise
Precondition
s must be a c-string.
See also
elektraFree
elektraStrLen
elektraStrNDup

◆ elektraStrLen()

size_t elektraStrLen ( const char *  s)

Calculates the length in bytes of a string.

This function differs from strlen() because it is Unicode and multibyte chars safe. While strlen() counts characters and ignores the final NULL, elektraStrLen() count bytes including the ending NULL.

It must not be used to search for / in the name, because it does not consider escaping. Instead use the unescaped name.

See also
keyUnescapedName()
Parameters
sthe string to get the length from
Returns
number of bytes used by the string, including the final NULL.

◆ elektraStrNDup()

char* elektraStrNDup ( const char *  s,
size_t  l 
)

Copy buffer into new allocated memory.

You need to free the memory yourself.

This function also works with \0 characters in the buffer. The length is taken as given, it must be correct.

Returns
0 if out of memory, a pointer otherwise
Parameters
smust be a allocated buffer
lthe length of s

◆ elektraValidateKeyName()

int elektraValidateKeyName ( const char *  name,
size_t  size 
)

Validates whether the supplied keyname is valid.

The function looks for tangling escape characters in the end and for a minimum length.

Does not check for valid namespaces

Precondition
size must be at least 2
Parameters
namethe key name that is to be checked
sizea elektraStrLen of the key name
Return values
trueif the supplied keyname part is valid
falseif its invalid

◆ elektraVFormat()

char* elektraVFormat ( const char *  format,
va_list  arg_list 
)

Does string formatting in fresh allocated memory.

Parameters
formatas in vprintf()
arg_listas in vprintf()
Returns
new allocated memory (free with elektraFree)