| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- /**
- * GDBM wrapper functions for Vala bindings.
- *
- * These wrapper functions handle the datum struct conversion between
- * C and Vala types.
- */
- #include "gdbm_wrapper.h"
- #include <string.h>
- int gdbm_exists_wrapper(GDBM_FILE dbf, const char *key) {
- datum key_datum;
- key_datum.dptr = (char *)key;
- key_datum.dsize = strlen(key);
- return gdbm_exists(dbf, key_datum);
- }
- GBytes* gdbm_fetch_wrapper(GDBM_FILE dbf, const char *key) {
- datum key_datum;
- key_datum.dptr = (char *)key;
- key_datum.dsize = strlen(key);
-
- datum result = gdbm_fetch(dbf, key_datum);
- if (result.dptr == NULL) {
- return NULL;
- }
-
- GBytes *bytes = g_bytes_new(result.dptr, result.dsize);
- free(result.dptr); /* GDBM allocates memory that we need to free */
-
- return bytes;
- }
- int gdbm_store_wrapper(GDBM_FILE dbf, const char *key, GBytes *content, int flag) {
- datum key_datum;
- key_datum.dptr = (char *)key;
- key_datum.dsize = strlen(key);
-
- gsize content_size;
- gconstpointer content_data = g_bytes_get_data(content, &content_size);
-
- datum content_datum;
- content_datum.dptr = (char *)content_data;
- content_datum.dsize = content_size;
-
- return gdbm_store(dbf, key_datum, content_datum, flag);
- }
- int gdbm_delete_wrapper(GDBM_FILE dbf, const char *key) {
- datum key_datum;
- key_datum.dptr = (char *)key;
- key_datum.dsize = strlen(key);
-
- return gdbm_delete(dbf, key_datum);
- }
- char* gdbm_firstkey_wrapper(GDBM_FILE dbf) {
- datum key = gdbm_firstkey(dbf);
- if (key.dptr == NULL) {
- return NULL;
- }
-
- char *result = g_strndup(key.dptr, key.dsize);
- free(key.dptr); /* GDBM allocates memory that we need to free */
-
- return result;
- }
- char* gdbm_nextkey_wrapper(GDBM_FILE dbf, const char *prev_key) {
- datum prev_datum;
- prev_datum.dptr = (char *)prev_key;
- prev_datum.dsize = strlen(prev_key);
-
- datum key = gdbm_nextkey(dbf, prev_datum);
- if (key.dptr == NULL) {
- return NULL;
- }
-
- char *result = g_strndup(key.dptr, key.dsize);
- free(key.dptr); /* GDBM allocates memory that we need to free */
-
- return result;
- }
|