gdbm_wrapper.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * GDBM wrapper functions for Vala bindings.
  3. *
  4. * These wrapper functions handle the datum struct conversion between
  5. * C and Vala types.
  6. */
  7. #include "gdbm_wrapper.h"
  8. #include <string.h>
  9. int gdbm_exists_wrapper(GDBM_FILE dbf, const char *key) {
  10. datum key_datum;
  11. key_datum.dptr = (char *)key;
  12. key_datum.dsize = strlen(key);
  13. return gdbm_exists(dbf, key_datum);
  14. }
  15. GBytes* gdbm_fetch_wrapper(GDBM_FILE dbf, const char *key) {
  16. datum key_datum;
  17. key_datum.dptr = (char *)key;
  18. key_datum.dsize = strlen(key);
  19. datum result = gdbm_fetch(dbf, key_datum);
  20. if (result.dptr == NULL) {
  21. return NULL;
  22. }
  23. GBytes *bytes = g_bytes_new(result.dptr, result.dsize);
  24. free(result.dptr); /* GDBM allocates memory that we need to free */
  25. return bytes;
  26. }
  27. int gdbm_store_wrapper(GDBM_FILE dbf, const char *key, GBytes *content, int flag) {
  28. datum key_datum;
  29. key_datum.dptr = (char *)key;
  30. key_datum.dsize = strlen(key);
  31. gsize content_size;
  32. gconstpointer content_data = g_bytes_get_data(content, &content_size);
  33. datum content_datum;
  34. content_datum.dptr = (char *)content_data;
  35. content_datum.dsize = content_size;
  36. return gdbm_store(dbf, key_datum, content_datum, flag);
  37. }
  38. int gdbm_delete_wrapper(GDBM_FILE dbf, const char *key) {
  39. datum key_datum;
  40. key_datum.dptr = (char *)key;
  41. key_datum.dsize = strlen(key);
  42. return gdbm_delete(dbf, key_datum);
  43. }
  44. char* gdbm_firstkey_wrapper(GDBM_FILE dbf) {
  45. datum key = gdbm_firstkey(dbf);
  46. if (key.dptr == NULL) {
  47. return NULL;
  48. }
  49. char *result = g_strndup(key.dptr, key.dsize);
  50. free(key.dptr); /* GDBM allocates memory that we need to free */
  51. return result;
  52. }
  53. char* gdbm_nextkey_wrapper(GDBM_FILE dbf, const char *prev_key) {
  54. datum prev_datum;
  55. prev_datum.dptr = (char *)prev_key;
  56. prev_datum.dsize = strlen(prev_key);
  57. datum key = gdbm_nextkey(dbf, prev_datum);
  58. if (key.dptr == NULL) {
  59. return NULL;
  60. }
  61. char *result = g_strndup(key.dptr, key.dsize);
  62. free(key.dptr); /* GDBM allocates memory that we need to free */
  63. return result;
  64. }