Python-like Dictionary Library
Dictionary library similar to Python dict functionality
Loading...
Searching...
No Matches
Functions
cdict.c File Reference
#include <cdict.h>
Include dependency graph for cdict.c:

Go to the source code of this file.

Functions

cdict_ctxcdict_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_keylistcdict_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.
 

Function Documentation

◆ cdict_free()

void cdict_free ( cdict_ctx ctx)

Frees the memory allocated for the dictionary.

Call this method after using the dictionary.

Parameters
dictA pointer to #DICT structure.

Definition at line 403 of file cdict.c.

◆ cdict_free_keylist()

void cdict_free_keylist ( cdict_keylist klst,
int  clone_keys 
)

This function free the keylist structure returned by cdict_keys()

Parameters
klstA pointer to cdict_keylist structure
Returns
Nothing

Definition at line 509 of file cdict.c.

◆ cdict_get()

void * cdict_get ( cdict_ctx ctx,
char *  key 
)
Parameters
dict
key
Returns
A pointer to the stored value as void*. You don't need to free the return value after use. Calling cdict_free() will free all the values.

Definition at line 170 of file cdict.c.

◆ cdict_get_nocase()

void * cdict_get_nocase ( cdict_ctx ctx,
char *  key 
)

get a value from the dictionary by providing the key (case-insensitive key)

Parameters
dict
keythe key to get the value for. The comparisons of the keys are case-insensitive
Returns
A pointer to the stored value as void*. You don't need to free the return value after use. Calling cdict_free() will free all the values.

Definition at line 209 of file cdict.c.

◆ cdict_has_key()

int cdict_has_key ( cdict_ctx ctx,
char *  key 
)

Checks if the key exists in the dictionary.

Parameters
dictPointer to #DICT structure
keyThe key we want to check the existence
Returns
Returns 1 if key exists otherwise 0

Definition at line 439 of file cdict.c.

◆ cdict_has_key_nocase()

int cdict_has_key_nocase ( cdict_ctx ctx,
char *  key 
)

Checks if the key exists in the dictionary (case-insensitive comparison)

Parameters
dictPointer to #DICT structure
keyThe key we want to check the existence
Returns
Returns 1 if key exists otherwise 0

Definition at line 473 of file cdict.c.

◆ cdict_init()

cdict_ctx * cdict_init ( void(*)(void *)  free_func,
void *(*)(void *)  copy_func 
)

Initializes the Dictionary in the memory.

Parameters
free_funcpointer to the function that free the values in the dictionary.
copy_funcpointer 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.

Returns
A pointer to #DICT structure on success or NULL on failure

Definition at line 109 of file cdict.c.

◆ cdict_keys()

cdict_keylist * cdict_keys ( cdict_ctx ctx,
int  clone_keys 
)

Get the list of keys from the dictionary.

Parameters
dictThe dictionary to set the key-value in
clone_keyspass 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.

Returns
returns a char** array (User is responsible to free the key list by calling free() function)

Definition at line 533 of file cdict.c.

◆ cdict_remove()

void cdict_remove ( cdict_ctx ctx,
char *  key 
)

Removes a key-value pair from the dictionary.

Parameters
ctxcontext returned by cdict_init()
keykey to remove from dictionary
Returns
nothing!

Definition at line 588 of file cdict.c.

◆ cdict_set()

int cdict_set ( cdict_ctx ctx,
char *  key,
void *  value 
)

Sets the new value for a given key.

Parameters
ctxThe context returned by cdict_init()
keyThe key for the value
valueThe value to set for the given key

We internally copy both key and value. So you can free the params after calling cdict_set().

Returns
Returns 0 on success and non-zero if fails

Definition at line 339 of file cdict.c.

◆ cdict_set_nocase()

int cdict_set_nocase ( cdict_ctx ctx,
char *  key,
void *  value 
)

Sets the new value for a given key (make key lower case first).

Parameters
ctxThe context returned by cdict_init()
keyThe key for the value which we convert it to lowercase first
valueThe value to set for the given key

We internally copy both key and value. So you can free the params after calling cdict_set().

Returns
Returns 0 on success and non-zero if fails

Definition at line 263 of file cdict.c.