Python-like Dictionary Library
Dictionary library similar to Python dict functionality
Loading...
Searching...
No Matches
cdict.h
1
2
3#include <string.h>
4#include <stdlib.h>
5#include <stdio.h>
6#include <time.h>
7#include <stdint.h>
8
9#ifndef CDICT_H
10#define CDICT_H
11
12
13#define ERROR_OK 0
14#define CDICT_ERROR_CAN_NOT_ALLOCATE_MEMORY 1
15#define ERROR_ITEM_NOT_FOUND 2
16#define ERROR_NULL_VALUE_FOR_KEY 3
17#define ERROR_DICT_POINTER_IS_NULL 4
18
19#define TABLE_SIZE 0x100001
20
21typedef uint32_t WORD;
22
23typedef struct _NODE NODE, *PNODE;
24
28struct _NODE{
29 char * key;
30 void * value;
31 struct _NODE * next;
32};
33
34
35typedef struct _DICT cdict_ctx;
36
40struct _DICT{
42 WORD * hash_table;
43 char * errmsg;
44 int err;
45 void (*free_func)(void*);
46 void *(*copy_func)(void*);
47};
48
49typedef struct {
50 char ** lst;
51 unsigned int len;
53
54
55
56/*start of function definitions*/
57cdict_ctx * cdict_init(void(*free_func)(void*), void*(*copy_func)(void*));
59int cdict_has_key_nocase(cdict_ctx* ctx, char * key);
60int cdict_set_nocase(cdict_ctx *ctx, char * key, void * value);
61void * cdict_get(cdict_ctx*, char *);
62void * cdict_get_nocase(cdict_ctx* ctx, char * key);
63int cdict_set(cdict_ctx*, char *, void *);
65cdict_keylist * cdict_keys(cdict_ctx *ctx, int clone_keys);
66int cdict_has_key(cdict_ctx*, char * key);
67void cdict_remove(cdict_ctx *, char *);
68/*end of function definitions*/
69#endif
70
void * cdict_get_nocase(cdict_ctx *ctx, char *key)
get a value from the dictionary by providing the key (case-insensitive key)
Definition cdict.c:209
int cdict_has_key_nocase(cdict_ctx *ctx, char *key)
Checks if the key exists in the dictionary (case-insensitive comparison)
Definition cdict.c:473
void cdict_free_keylist(cdict_keylist *klst, int clone_keys)
This function free the keylist structure returned by cdict_keys()
Definition cdict.c:509
void cdict_free(cdict_ctx *ctx)
Frees the memory allocated for the dictionary.
Definition cdict.c:403
int cdict_has_key(cdict_ctx *ctx, char *key)
Checks if the key exists in the dictionary.
Definition cdict.c:439
int cdict_set_nocase(cdict_ctx *ctx, char *key, void *value)
Sets the new value for a given key (make key lower case first).
Definition cdict.c:263
cdict_ctx * cdict_init(void(*free_func)(void *), void *(*copy_func)(void *))
Initializes the Dictionary in the memory.
Definition cdict.c:109
void cdict_remove(cdict_ctx *ctx, char *key)
Removes a key-value pair from the dictionary.
Definition cdict.c:588
int cdict_set(cdict_ctx *ctx, char *key, void *value)
Sets the new value for a given key.
Definition cdict.c:339
void * cdict_get(cdict_ctx *ctx, char *key)
Definition cdict.c:170
cdict_keylist * cdict_keys(cdict_ctx *ctx, int clone_keys)
Get the list of keys from the dictionary.
Definition cdict.c:533
Definition cdict.h:40
PNODE table
Pointer to an array of NODE structure.
Definition cdict.h:41
int err
Possible Error code.
Definition cdict.h:44
WORD * hash_table
Pointer to the hash table for hashing.
Definition cdict.h:42
char * errmsg
Description of the possible error.
Definition cdict.h:43
void(* free_func)(void *)
a pointer to the free function provided by user
Definition cdict.h:45
Definition cdict.h:28
char * key
key for finding matches. Key is a null-terminated string
Definition cdict.h:29
void * value
Value to store. value is a pointer to void so it can be any type.
Definition cdict.h:30
struct _NODE * next
A pointer points to the next node in case of collision.
Definition cdict.h:31