|
Python-like Dictionary Library
Dictionary library similar to Python dict functionality
|
#include <cdict.h>
Go to the source code of this file.
Functions | |
| cdict_ctx * | cdict_init (void(*free_func)(void *), void *(*copy_func)(void *)) |
| Initializes the Dictionary in the memory. | |
| void * | cdict_get (cdict_ctx *ctx, char *key) |
| void * | cdict_get_nocase (cdict_ctx *ctx, char *key) |
| get a value from the dictionary by providing the key (case-insensitive key) | |
| int | cdict_set_nocase (cdict_ctx *ctx, char *key, void *value) |
| Sets the new value for a given key (make key lower case first). | |
| int | cdict_set (cdict_ctx *ctx, char *key, void *value) |
| Sets the new value for a given key. | |
| void | cdict_free (cdict_ctx *ctx) |
| Frees the memory allocated for the dictionary. | |
| int | cdict_has_key (cdict_ctx *ctx, char *key) |
| Checks if the key exists in the dictionary. | |
| int | cdict_has_key_nocase (cdict_ctx *ctx, char *key) |
| Checks if the key exists in the dictionary (case-insensitive comparison) | |
| void | cdict_free_keylist (cdict_keylist *klst, int clone_keys) |
| This function free the keylist structure returned by cdict_keys() | |
| cdict_keylist * | cdict_keys (cdict_ctx *ctx, int clone_keys) |
| Get the list of keys from the dictionary. | |
| void | cdict_remove (cdict_ctx *ctx, char *key) |
| Removes a key-value pair from the dictionary. | |
| void cdict_free | ( | cdict_ctx * | ctx | ) |
| void cdict_free_keylist | ( | cdict_keylist * | klst, |
| int | clone_keys | ||
| ) |
This function free the keylist structure returned by cdict_keys()
| klst | A pointer to cdict_keylist structure |
| void * cdict_get | ( | cdict_ctx * | ctx, |
| char * | key | ||
| ) |
| dict | |
| key |
| void * cdict_get_nocase | ( | cdict_ctx * | ctx, |
| char * | key | ||
| ) |
get a value from the dictionary by providing the key (case-insensitive key)
| dict | |
| key | the key to get the value for. The comparisons of the keys are case-insensitive |
| int cdict_has_key | ( | cdict_ctx * | ctx, |
| char * | key | ||
| ) |
| int cdict_has_key_nocase | ( | cdict_ctx * | ctx, |
| char * | key | ||
| ) |
| cdict_ctx * cdict_init | ( | void(*)(void *) | free_func, |
| void *(*)(void *) | copy_func | ||
| ) |
Initializes the Dictionary in the memory.
| free_func | pointer to the function that free the values in the dictionary. |
| copy_func | pointer to the function that copy the value provided by the user. |
This function initialize a dictionary context on success.
The input parameters are pointers to the functions written by the user to free and copy the value member of the NODE structure.
since users may store any type of data structure (string, integer or custum struct) in the value part of the key-value record of the dictionary, the code must know how to free() that value to prevent the memory leak. That's why you need to pass a pointer to the free function and we will call this function inside cdict_free()
When the user stores a key-value pair to the dictionary, the dictionary will keep a copy of the value. Therefore, it needs to be passed a function pointer that can correctly copy the value data.
If you store basic types like int, float, char or double, then you can pass NULL as the parameters.
| cdict_keylist * cdict_keys | ( | cdict_ctx * | ctx, |
| int | clone_keys | ||
| ) |
Get the list of keys from the dictionary.
| dict | The dictionary to set the key-value in |
| clone_keys | pass 1 if you want to get a copy of the keys or 0 to return a pointer to keys |
if clone_keys is 1, then the function will copy all the internal keys using strdup(). This means that users must free the return cdict_keylist result by calling cdict_free_keylist() and setting the clone_keys to 1.
This method is good for removing key-value from a dictionary in a loop.
| void cdict_remove | ( | cdict_ctx * | ctx, |
| char * | key | ||
| ) |
Removes a key-value pair from the dictionary.
| ctx | context returned by cdict_init() |
| key | key to remove from dictionary |
| int cdict_set | ( | cdict_ctx * | ctx, |
| char * | key, | ||
| void * | value | ||
| ) |
Sets the new value for a given key.
| ctx | The context returned by cdict_init() |
| key | The key for the value |
| value | The value to set for the given key |
We internally copy both key and value. So you can free the params after calling cdict_set().
| int cdict_set_nocase | ( | cdict_ctx * | ctx, |
| char * | key, | ||
| void * | value | ||
| ) |
Sets the new value for a given key (make key lower case first).
| ctx | The context returned by cdict_init() |
| key | The key for the value which we convert it to lowercase first |
| value | The value to set for the given key |
We internally copy both key and value. So you can free the params after calling cdict_set().