KTH framework for Nek5000 toolboxes; testing version
0.0.1
|
Implements a dictionary for string variables. More...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
Go to the source code of this file.
Data Structures | |
struct | _dictionary_ |
Dictionary object. More... | |
Typedefs | |
typedef struct _dictionary_ | dictionary |
Dictionary object. More... | |
Functions | |
unsigned | dictionary_hash (const char *key) |
Compute the hash key for a string. More... | |
dictionary * | dictionary_new (size_t size) |
Create a new dictionary object. More... | |
void | dictionary_del (dictionary *vd) |
Delete a dictionary object. More... | |
const char * | dictionary_get (const dictionary *d, const char *key, const char *def) |
Get a value from a dictionary. More... | |
int | dictionary_set (dictionary *vd, const char *key, const char *val) |
Set a value in a dictionary. More... | |
void | dictionary_unset (dictionary *d, const char *key) |
Delete a key in a dictionary. More... | |
void | dictionary_dump (const dictionary *d, FILE *out) |
Dump a dictionary to an opened file pointer. More... | |
Implements a dictionary for string variables.
This module implements a simple dictionary object, i.e. a list of string/string associations. This object is useful to store e.g. informations retrieved from a configuration file (ini files).
Definition in file dictionary.h.
typedef struct _dictionary_ dictionary |
Dictionary object.
This object contains a list of string/string associations. Each association is identified by a unique string key. Looking up values in the dictionary is speeded up by the use of a (hopefully collision-free) hash function.
void dictionary_del | ( | dictionary * | d | ) |
Delete a dictionary object.
d | dictionary object to deallocate. |
Deallocate a dictionary object and all memory associated to it.
Definition at line 177 of file dictionary.c.
References _dictionary_::hash, _dictionary_::key, _dictionary_::size, and _dictionary_::val.
void dictionary_dump | ( | const dictionary * | d, |
FILE * | out | ||
) |
Dump a dictionary to an opened file pointer.
d | Dictionary to dump |
f | Opened file pointer. |
Dumps a dictionary onto an opened file pointer. Key pairs are printed out as [Key]=[Value], one per line. It is Ok to provide stdout or stderr as output file pointers.
Definition at line 363 of file dictionary.c.
References _dictionary_::key, _dictionary_::n, _dictionary_::size, and _dictionary_::val.
const char* dictionary_get | ( | const dictionary * | d, |
const char * | key, | ||
const char * | def | ||
) |
Get a value from a dictionary.
d | dictionary object to search. |
key | Key to look for in the dictionary. |
def | Default value to return if key not found. |
This function locates a key in a dictionary and returns a pointer to its value, or the passed 'def' pointer if no such key can be found in dictionary. The returned character pointer points to data internal to the dictionary object, you should not try to free it or modify it.
Definition at line 209 of file dictionary.c.
References dictionary_hash(), _dictionary_::hash, _dictionary_::key, _dictionary_::size, and _dictionary_::val.
unsigned dictionary_hash | ( | const char * | key | ) |
Compute the hash key for a string.
key | Character string to use for key. |
This hash function has been taken from an Article in Dr Dobbs Journal. This is normally a collision-free function, distributing keys evenly. The key is stored anyway in the struct so that collision can be avoided by comparing the key itself in last resort.
Definition at line 118 of file dictionary.c.
dictionary* dictionary_new | ( | size_t | size | ) |
Create a new dictionary object.
size | Optional initial size of the dictionary. |
This function allocates a new dictionary object of given size and returns it. If you do not know in advance (roughly) the number of entries in the dictionary, give size=0.
Definition at line 150 of file dictionary.c.
References DICTMINSZ, _dictionary_::hash, _dictionary_::key, _dictionary_::size, and _dictionary_::val.
int dictionary_set | ( | dictionary * | d, |
const char * | key, | ||
const char * | val | ||
) |
Set a value in a dictionary.
d | dictionary object to modify. |
key | Key to modify or add. |
val | Value to add. |
If the given key is found in the dictionary, the associated value is replaced by the provided one. If the key cannot be found in the dictionary, it is added to it.
It is Ok to provide a NULL value for val, but NULL values for the dictionary or the key are considered as errors: the function will return immediately in such a case.
Notice that if you dictionary_set a variable to NULL, a call to dictionary_get will return a NULL value: the variable will be found, and its value (NULL) is returned. In other words, setting the variable content to NULL is equivalent to deleting the variable from the dictionary. It is not possible (in this implementation) to have a key in the dictionary without value.
This function returns non-zero in case of failure.
Definition at line 255 of file dictionary.c.
References dictionary_hash(), _dictionary_::hash, _dictionary_::key, _dictionary_::n, _dictionary_::size, and _dictionary_::val.
void dictionary_unset | ( | dictionary * | d, |
const char * | key | ||
) |
Delete a key in a dictionary.
d | dictionary object to modify. |
key | Key to remove. |
This function deletes a key in a dictionary. Nothing is done if the key cannot be found.
Definition at line 314 of file dictionary.c.
References dictionary_hash(), _dictionary_::hash, _dictionary_::key, _dictionary_::n, _dictionary_::size, and _dictionary_::val.