Simple C library for a key value data structure
Clone project, copy keval.c and keval.c wherever you want.
Any C compiler, run preferrably on Linux.
Calling kv_spawn
will create a new instance of keval.
You will also have to set it as your active instance in order to use the functions to manipulate the structure. It is currently not thread safe!
struct kv_data *data = kv_spawn();
kv_set_instance(data);
Adding elements to the data structure is as simple as using the build in functions. It will not allow duplicate keys, but doesn't use hashing as it is designed for a small amount of keys only. As such, adding a key will be O(n).
char key_test[] = "I_am_a_key";
char value_test[] = "123";
kv_add_key(key_test);
// You can set a value using its string O(n)
kv_set_val_s(key_test, value_test));
// or you can set a value using its index
kv_set_val_i(0, value_test);
It's as simple as adding them! Just call the retrieval function
//with the key string
char *result_s = kv_get_val_s(key_test);
//with an index
char *result_i = kv_get_val_i(0);
You can get a key string by its index like this:
char *key = kv_get_key(0);
You can also search for a key's index. Just pass the key.
int index = kv_key_search(key_test);
As easy as it gets!
//with an index
kv_remove_key_i(0);
//with the key string
kv_remove_key_s(key_test);
You can also call a function that prints a key value pair to console in case you need to find out what's being used without writing your own function for it.
kv_print_pair(0)
Output if key = "I_am_a_key"
and value = "123"
I_am_a_key: 123
Destroying a keval instance will free all memory allocated, so you don't have to worry about anything.
Simply call the destructor like this
data = kv_destroy(data);
Main.c provides a quick example and some tests on how to use the data structure.
This project is licensed under the MIT License - see the LICENSE.md file for details
- Seems to be memory leak free according to valgrind and our limited testing.
- keval.h and keval.c are 100% klausified.