Learning by doing: Reading books and trying to understand the (code) examples
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
905 B

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <strings.h>
  4. #include "memory.h"
  5. #include "keyval.h"
  6. /*
  7. Create a new key/value pair.
  8. */
  9. keyval *keyval_new(char *key, void *value) {
  10. keyval *out = malloc(sizeof(keyval));
  11. if (out == NULL) {
  12. fprintf(stderr, "Out of memory.\n");
  13. abort();
  14. }
  15. *out = (keyval){.key = key, .value = value};
  16. return out;
  17. }
  18. /*
  19. Copy a key/value pair. The new pair has pointers to
  20. the values in the old pair, not copies of their data.
  21. */
  22. keyval *keyval_copy(const keyval *in) {
  23. keyval *out = malloc(sizeof(keyval));
  24. if (out == NULL) {
  25. fprintf(stderr, "Out of memory.\n");
  26. abort();
  27. }
  28. *out = *in;
  29. return out;
  30. }
  31. /*
  32. Free a existing key/value pair.
  33. */
  34. void keyval_free(keyval *in) {
  35. safeFree(in);
  36. }
  37. /*
  38. Check if key of key/value pair matches given key.
  39. */
  40. int keyval_matches(const keyval *in, const char *key) {
  41. return !strcmp(in->key, key);
  42. }