#include <search.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "libdictionary.h"
Functions | |
void | dictionary_init (dictionary_t *d) |
Initialize the dictionary data structure. | |
int | dictionary_add (dictionary_t *d, const char *key, const char *value) |
Adds the key-value pair (key, value) to the dictionary, if and only if the dictionary does not already contain a key with the same name as key. | |
const char * | dictionary_get (dictionary_t *d, const char *key) |
Returns the value of the key-value element for a specific key. | |
int | dictionary_remove (dictionary_t *d, const char *key) |
Removes the key-value pair for a given key from the dictionary, if it exists. | |
int | dictionary_remove_free (dictionary_t *d, const char *key) |
Removes the key-value pair for a given key from the dictionary, if it exists, and free()s the key and value strings for the user. | |
void | dictionary_destroy (dictionary_t *d) |
Frees any memory associated with the dictionary. | |
void | dictionary_destroy_free (dictionary_t *d) |
Frees any memory associated with the dictionary. |
int dictionary_add | ( | dictionary_t * | d, | |
const char * | key, | |||
const char * | value | |||
) |
Adds the key-value pair (key, value) to the dictionary, if and only if the dictionary does not already contain a key with the same name as key.
This function does NOT make a copy of the key or value.
This function is thread-safe.
You may assume that:
d | Dictionary data structure. | |
key | The key to be added to the dictionary. | |
value | The value to be assoicated with the key in the dictionary. |
0 | Success | |
KEY_EXISTS | The dictionary already contains they specified key. |
void dictionary_destroy | ( | dictionary_t * | d | ) |
Frees any memory associated with the dictionary.
This function does not free() any keys or values of the elements contained in the dictionary.
You may assume that:
d | A pointer to an initalized dictionary data structure. |
void dictionary_destroy_free | ( | dictionary_t * | d | ) |
Frees any memory associated with the dictionary.
Additionally, this function will free() the key and value strings of all entries that still exist in the dictionary. To free only the internal memory to the dictionary,
You may assume that:
d | A pointer to an initalized dictionary data structure. |
const char* dictionary_get | ( | dictionary_t * | d, | |
const char * | key | |||
) |
Returns the value of the key-value element for a specific key.
If the key does not exist, this function returns NULL.
This function is thread-safe.
You may assume that:
d | A pointer to an initalized dictionary data structure. | |
key | The key to lookup in the dictionary. |
void dictionary_init | ( | dictionary_t * | d | ) |
Initialize the dictionary data structure.
This function must be called before any other libdictionary functions.
d | Dictionary data structure. |
int dictionary_remove | ( | dictionary_t * | d, | |
const char * | key | |||
) |
Removes the key-value pair for a given key from the dictionary, if it exists.
This function will not free() the key or value.
You may assume that:
d | A pointer to an initalized dictionary data structure. | |
key | The key to remove from the dictionary. |
0 | Success. | |
NO_KEY_EXISTS | The dictionary did not contain key. |
int dictionary_remove_free | ( | dictionary_t * | d, | |
const char * | key | |||
) |
Removes the key-value pair for a given key from the dictionary, if it exists, and free()s the key and value strings for the user.
You may assume that:
d | A pointer to an initalized dictionary data structure. | |
key | The key to remove from the dictionary. |
0 | Success. | |
NO_KEY_EXISTS | The dictionary did not contain key. |