Skip to content

Commit

Permalink
Merge pull request #85 from jeromekelleher/has_key
Browse files Browse the repository at this point in the history
Updates for C API 1.1.0
  • Loading branch information
jeromekelleher authored Mar 28, 2019
2 parents f9c8e0a + 3123c85 commit 67c5a6a
Show file tree
Hide file tree
Showing 7 changed files with 352 additions and 74 deletions.
8 changes: 8 additions & 0 deletions c/CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
--------------------
[1.1.0] - 2019-03-19
--------------------

- Add `contains` function
- Add `oput` variants that transfer ownership of buffer.
- Various documentation updates.

--------------------
[1.0.1] - 2019-01-24
--------------------
Expand Down
96 changes: 96 additions & 0 deletions c/example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* This is a simple program to illustrate the kastore C API. With
* no arguments it writes a file 'example.kas'. If called with
* a single command line argument, reads this file in and prints.
*/
#include <err.h>
#include <stdlib.h>
#include <stdio.h>

#include <kastore.h>

static void
handle_kas_error(int line, int retval)
{
errx(1, "Error at line %d: %s", line, kas_strerror(retval));
}

static void
write_example(const char *path)
{
int ret;
kastore_t store;
const uint32_t a[] = {1, 2, 3, 4};
size_t b_length = 10;
uint32_t *b = calloc(b_length, sizeof(*b));

if (b == NULL) {
err(1, "Out of memory");
}

ret = kastore_open(&store, path, "w", 0);
if (ret != 0) {
handle_kas_error(__LINE__, ret);
}
/* This is the standard 'put' where the library takes a copy of the array.
* This is the recommended approach unless working with very large arrays. */
ret = kastore_puts_uint32(&store, "a", a, 4, 0);
if (ret != 0) {
handle_kas_error(__LINE__, ret);
}
/* This is the own-put variant, where the array is inserted into the store
* and ownership of the buffer (which must be a pointer returned from
* malloc/calloc) is passed to the store. The buffer will be freed by the
* library when 'close' is called. */
ret = kastore_oputs_uint32(&store, "b", b, b_length, 0);
if (ret != 0) {
/* The store only takes ownership of the buffer if oputs succeeds, so
* we must free b here to avoid leaking memory in error conditions */
free(b);
handle_kas_error(__LINE__, ret);
}

ret = kastore_close(&store);
if (ret != 0) {
handle_kas_error(__LINE__, ret);
}
}

static void
read_example(const char *path)
{
int ret;
kastore_t store;
uint32_t *array;
size_t j, k, array_len;
const char *keys[] = {"a", "b"};

ret = kastore_open(&store, path, "r", 0);
if (ret != 0) {
handle_kas_error(__LINE__, ret);
}
for (j = 0; j < sizeof(keys) / sizeof(*keys); j++) {
ret = kastore_gets_uint32(&store, keys[j], &array, &array_len);
if (ret != 0) {
handle_kas_error(__LINE__, ret);
}
printf("key: %s = [", keys[j]);
for (k = 0; k < array_len; k++) {
printf("%d%s", array[k], k == array_len - 1 ? "]\n": ", ");
}
}
ret = kastore_close(&store);
if (ret != 0) {
handle_kas_error(__LINE__, ret);
}
}

int
main(int argc, char **argv)
{
if (argc == 1) {
write_example("example.kas");
} else {
read_example(argv[1]);
}
return 0;
}
70 changes: 46 additions & 24 deletions c/kastore.c
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,28 @@ kastore_close(kastore_t *self)
return ret;
}

int KAS_WARN_UNUSED
kastore_contains(kastore_t *self, const char *key, size_t key_len)
{
void *array;
size_t array_len;
int type;
int ret = kastore_get(self, key, key_len, &array, &array_len, &type);

if (ret == 0) {
ret = 1;
} else if (ret == KAS_ERR_KEY_NOT_FOUND) {
ret = 0;
}
return ret;
}

int KAS_WARN_UNUSED
kastore_containss(kastore_t *self, const char *key)
{
return kastore_contains(self, key, strlen(key));
}

int KAS_WARN_UNUSED
kastore_get(kastore_t *self, const char *key, size_t key_len,
void **array, size_t *array_len, int *type)
Expand Down Expand Up @@ -745,7 +767,7 @@ kastore_put(kastore_t *self, const char *key, size_t key_len,
}
memcpy(array_copy, array, array_size);

ret = kastore_own_put(self, key, key_len, array_copy, array_len, type, flags);
ret = kastore_oput(self, key, key_len, array_copy, array_len, type, flags);
if (ret == 0) {
/* Kastore has taken ownership of the array, so we don't need to free it */
array_copy = NULL;
Expand All @@ -756,7 +778,7 @@ kastore_put(kastore_t *self, const char *key, size_t key_len,
}

int KAS_WARN_UNUSED
kastore_own_put(kastore_t *self, const char *key, size_t key_len,
kastore_oput(kastore_t *self, const char *key, size_t key_len,
void *array, size_t array_len, int type, int KAS_UNUSED(flags))
{
int ret = 0;
Expand Down Expand Up @@ -898,80 +920,80 @@ kastore_puts_float64(kastore_t *self, const char *key, const double *array, size
}

int KAS_WARN_UNUSED
kastore_own_puts(kastore_t *self, const char *key,
kastore_oputs(kastore_t *self, const char *key,
void *array, size_t array_len, int type, int flags)
{
return kastore_own_put(self, key, strlen(key), array, array_len, type, flags);
return kastore_oput(self, key, strlen(key), array, array_len, type, flags);
}

int KAS_WARN_UNUSED
kastore_own_puts_int8(kastore_t *self, const char *key, int8_t *array, size_t array_len,
kastore_oputs_int8(kastore_t *self, const char *key, int8_t *array, size_t array_len,
int flags)
{
return kastore_own_puts(self, key, (void *) array, array_len, KAS_INT8, flags);
return kastore_oputs(self, key, (void *) array, array_len, KAS_INT8, flags);
}

int KAS_WARN_UNUSED
kastore_own_puts_uint8(kastore_t *self, const char *key, uint8_t *array, size_t array_len,
kastore_oputs_uint8(kastore_t *self, const char *key, uint8_t *array, size_t array_len,
int flags)
{
return kastore_own_puts(self, key, (void *) array, array_len, KAS_UINT8, flags);
return kastore_oputs(self, key, (void *) array, array_len, KAS_UINT8, flags);
}

int KAS_WARN_UNUSED
kastore_own_puts_int16(kastore_t *self, const char *key, int16_t *array, size_t array_len,
kastore_oputs_int16(kastore_t *self, const char *key, int16_t *array, size_t array_len,
int flags)
{
return kastore_own_puts(self, key, (void *) array, array_len, KAS_INT16, flags);
return kastore_oputs(self, key, (void *) array, array_len, KAS_INT16, flags);
}

int KAS_WARN_UNUSED
kastore_own_puts_uint16(kastore_t *self, const char *key, uint16_t *array, size_t array_len,
kastore_oputs_uint16(kastore_t *self, const char *key, uint16_t *array, size_t array_len,
int flags)
{
return kastore_own_puts(self, key, (void *) array, array_len, KAS_UINT16, flags);
return kastore_oputs(self, key, (void *) array, array_len, KAS_UINT16, flags);
}

int KAS_WARN_UNUSED
kastore_own_puts_int32(kastore_t *self, const char *key, int32_t *array, size_t array_len,
kastore_oputs_int32(kastore_t *self, const char *key, int32_t *array, size_t array_len,
int flags)
{
return kastore_own_puts(self, key, (void *) array, array_len, KAS_INT32, flags);
return kastore_oputs(self, key, (void *) array, array_len, KAS_INT32, flags);
}

int KAS_WARN_UNUSED
kastore_own_puts_uint32(kastore_t *self, const char *key, uint32_t *array, size_t array_len,
kastore_oputs_uint32(kastore_t *self, const char *key, uint32_t *array, size_t array_len,
int flags)
{
return kastore_own_puts(self, key, (void *) array, array_len, KAS_UINT32, flags);
return kastore_oputs(self, key, (void *) array, array_len, KAS_UINT32, flags);
}

int KAS_WARN_UNUSED
kastore_own_puts_int64(kastore_t *self, const char *key, int64_t *array, size_t array_len,
kastore_oputs_int64(kastore_t *self, const char *key, int64_t *array, size_t array_len,
int flags)
{
return kastore_own_puts(self, key, (void *) array, array_len, KAS_INT64, flags);
return kastore_oputs(self, key, (void *) array, array_len, KAS_INT64, flags);
}

int KAS_WARN_UNUSED
kastore_own_puts_uint64(kastore_t *self, const char *key, uint64_t *array, size_t array_len,
kastore_oputs_uint64(kastore_t *self, const char *key, uint64_t *array, size_t array_len,
int flags)
{
return kastore_own_puts(self, key, (void *) array, array_len, KAS_UINT64, flags);
return kastore_oputs(self, key, (void *) array, array_len, KAS_UINT64, flags);
}

int KAS_WARN_UNUSED
kastore_own_puts_float32(kastore_t *self, const char *key, float *array, size_t array_len,
kastore_oputs_float32(kastore_t *self, const char *key, float *array, size_t array_len,
int flags)
{
return kastore_own_puts(self, key, (void *) array, array_len, KAS_FLOAT32, flags);
return kastore_oputs(self, key, (void *) array, array_len, KAS_FLOAT32, flags);
}

int KAS_WARN_UNUSED
kastore_own_puts_float64(kastore_t *self, const char *key, double *array, size_t array_len,
kastore_oputs_float64(kastore_t *self, const char *key, double *array, size_t array_len,
int flags)
{
return kastore_own_puts(self, key, (void *) array, array_len, KAS_FLOAT64, flags);
return kastore_oputs(self, key, (void *) array, array_len, KAS_FLOAT64, flags);
}

void
Expand Down
Loading

0 comments on commit 67c5a6a

Please sign in to comment.