#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
Classes | |
struct | _dictionary_ |
Dictionary object. More... | |
Typedefs | |
typedef _dictionary_ | dictionary |
Dictionary object. | |
Functions | |
unsigned | dictionary_hash (char *key) |
Compute the hash key for a string. | |
dictionary * | dictionary_new (int size) |
Create a new dictionary object. | |
void | dictionary_del (dictionary *vd) |
Delete a dictionary object. | |
char * | dictionary_get (dictionary *d, char *key, char *def) |
Get a value from a dictionary. | |
char | dictionary_getchar (dictionary *d, char *key, char def) |
Get a value from a dictionary, as a char. | |
int | dictionary_getint (dictionary *d, char *key, int def) |
Get a value from a dictionary, as an int. | |
double | dictionary_getdouble (dictionary *d, char *key, double def) |
Get a value from a dictionary, as a double. | |
void | dictionary_set (dictionary *vd, char *key, char *val) |
Set a value in a dictionary. | |
void | dictionary_unset (dictionary *d, char *key) |
Delete a key in a dictionary. | |
void | dictionary_setint (dictionary *d, char *key, int val) |
Set a key in a dictionary, providing an int. | |
void | dictionary_setdouble (dictionary *d, char *key, double val) |
Set a key in a dictionary, providing a double. | |
void | dictionary_dump (dictionary *d, FILE *out) |
Dump a dictionary to an opened file pointer. |
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. |
void dictionary_dump | ( | dictionary * | d, | |
FILE * | out | |||
) |
Dump a dictionary to an opened file pointer.
d | Dictionary to dump | |
f | Opened file pointer. |
[Key]=[Value], one per line. It is Ok to provide stdout or stderr as output file pointers.
char* dictionary_get | ( | dictionary * | d, | |
char * | key, | |||
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. |
char dictionary_getchar | ( | dictionary * | d, | |
char * | key, | |||
char | def | |||
) |
Get a value from a dictionary, as a char.
d | dictionary object to search. | |
key | Key to look for in the dictionary. | |
def | Default value for the key if not found. |
double dictionary_getdouble | ( | dictionary * | d, | |
char * | key, | |||
double | def | |||
) |
Get a value from a dictionary, as a double.
d | dictionary object to search. | |
key | Key to look for in the dictionary. | |
def | Default value for the key if not found. |
int dictionary_getint | ( | dictionary * | d, | |
char * | key, | |||
int | def | |||
) |
Get a value from a dictionary, as an int.
d | dictionary object to search. | |
key | Key to look for in the dictionary. | |
def | Default value for the key if not found. |
unsigned dictionary_hash | ( | char * | key | ) |
Compute the hash key for a string.
key | Character string to use for key. |
dictionary* dictionary_new | ( | int | size | ) |
Create a new dictionary object.
size | Optional initial size of the dictionary. |
void dictionary_set | ( | dictionary * | d, | |
char * | key, | |||
char * | val | |||
) |
Set a value in a dictionary.
d | dictionary object to modify. | |
key | Key to modify or add. | |
val | Value to add. |
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.
void dictionary_setdouble | ( | dictionary * | d, | |
char * | key, | |||
double | val | |||
) |
Set a key in a dictionary, providing a double.
d | Dictionary to update. | |
key | Key to modify or add | |
val | Double value to store (will be stored as a string). |
void dictionary_setint | ( | dictionary * | d, | |
char * | key, | |||
int | val | |||
) |
Set a key in a dictionary, providing an int.
d | Dictionary to update. | |
key | Key to modify or add | |
val | Integer value to store (will be stored as a string). |
void dictionary_unset | ( | dictionary * | d, | |
char * | key | |||
) |
Delete a key in a dictionary.
d | dictionary object to modify. | |
key | Key to remove. |