dictionary.h File Reference
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 |
Functions | |
void | dictionary_del (dictionary *vd) |
void | dictionary_dump (dictionary *d, FILE *out) |
char * | dictionary_get (dictionary *d, char *key, char *def) |
char | dictionary_getchar (dictionary *d, char *key, char def) |
double | dictionary_getdouble (dictionary *d, char *key, double def) |
int | dictionary_getint (dictionary *d, char *key, int def) |
unsigned | dictionary_hash (char *key) |
dictionary * | dictionary_new (int size) |
void | dictionary_set (dictionary *vd, char *key, char *val) |
void | dictionary_setdouble (dictionary *d, char *key, double val) |
void | dictionary_setint (dictionary *d, char *key, int val) |
void | dictionary_unset (dictionary *d, char *key) |
Detailed Description
Implements a dictionary for string variables.
- Date:
- Aug 2000
- Version:
- Revision
- 1.11
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 Documentation
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.
Function Documentation
void dictionary_del | ( | dictionary * | vd | ) |
Delete a dictionary object.
- Parameters:
-
d dictionary object to deallocate.
- Returns:
- void
Deallocate a dictionary object and all memory associated to it.
void dictionary_dump | ( | dictionary * | d, | |
FILE * | out | |||
) |
Dump a dictionary to an opened file pointer.
- Parameters:
-
d Dictionary to dump f Opened file pointer.
- Returns:
- void
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.
char* dictionary_get | ( | dictionary * | d, | |
char * | key, | |||
char * | def | |||
) |
Get a value from a dictionary.
- Parameters:
-
d dictionary object to search. key Key to look for in the dictionary. def Default value to return if key not found.
- Returns:
- 1 pointer to internally allocated character string.
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.
char dictionary_getchar | ( | dictionary * | d, | |
char * | key, | |||
char | def | |||
) |
Get a value from a dictionary, as a char.
- Parameters:
-
d dictionary object to search. key Key to look for in the dictionary. def Default value for the key if not found.
- Returns:
- char
This function locates a key in a dictionary using dictionary_get, and returns the first char of the found string.
double dictionary_getdouble | ( | dictionary * | d, | |
char * | key, | |||
double | def | |||
) |
Get a value from a dictionary, as a double.
- Parameters:
-
d dictionary object to search. key Key to look for in the dictionary. def Default value for the key if not found.
- Returns:
- double
This function locates a key in a dictionary using dictionary_get, and applies atof on it to return a double. If the value cannot be found in the dictionary, the default is returned.
int dictionary_getint | ( | dictionary * | d, | |
char * | key, | |||
int | def | |||
) |
Get a value from a dictionary, as an int.
- Parameters:
-
d dictionary object to search. key Key to look for in the dictionary. def Default value for the key if not found.
- Returns:
- int
This function locates a key in a dictionary using dictionary_get, and applies atoi on it to return an int. If the value cannot be found in the dictionary, the default is returned.
unsigned dictionary_hash | ( | char * | key | ) |
Compute the hash key for a string.
- Parameters:
-
key Character string to use for key.
- Returns:
- 1 unsigned int on at least 32 bits.
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.
dictionary* dictionary_new | ( | int | size | ) |
Create a new dictionary object.
- Parameters:
-
size Optional initial size of the dictionary.
- Returns:
- 1 newly allocated dictionary objet.
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.
void dictionary_set | ( | dictionary * | vd, | |
char * | key, | |||
char * | val | |||
) |
Set a value in a dictionary.
- Parameters:
-
d dictionary object to modify. key Key to modify or add. val Value to add.
- Returns:
- void
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.
void dictionary_setdouble | ( | dictionary * | d, | |
char * | key, | |||
double | val | |||
) |
Set a key in a dictionary, providing a double.
- Parameters:
-
d Dictionary to update. key Key to modify or add val Double value to store (will be stored as a string).
- Returns:
- void
This helper function calls dictionary_set() with the provided double converted to a string using g.
void dictionary_setint | ( | dictionary * | d, | |
char * | key, | |||
int | val | |||
) |
Set a key in a dictionary, providing an int.
- Parameters:
-
d Dictionary to update. key Key to modify or add val Integer value to store (will be stored as a string).
- Returns:
- void
This helper function calls dictionary_set() with the provided integer converted to a string using d.
void dictionary_unset | ( | dictionary * | d, | |
char * | key | |||
) |
Delete a key in a dictionary.
- Parameters:
-
d dictionary object to modify. key Key to remove.
- Returns:
- void
This function deletes a key in a dictionary. Nothing is done if the key cannot be found.