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.

53 lines
933 B

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <strings.h>
  4. #include "keyval.h"
  5. /*
  6. Create a new key/value pair.
  7. */
  8. keyval *keyval_new(char *key, void *value) {
  9. keyval *out = malloc(sizeof(keyval));
  10. if(out != NULL) {
  11. *out = (keyval){.key = key, .value = value};
  12. } else {
  13. fprintf(stderr, "Out of memory.\n");
  14. abort();
  15. }
  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. *out = *in;
  26. } else {
  27. fprintf(stderr, "Out of memory.\n");
  28. abort();
  29. }
  30. return out;
  31. }
  32. /*
  33. Free a existing key/value pair.
  34. */
  35. void keyval_free(keyval *in) {
  36. if(in != NULL) {
  37. free(in);
  38. }
  39. }
  40. /*
  41. Check if key of key/value pair matches given key.
  42. */
  43. int keyval_matches(const keyval *in, const char *key) {
  44. return !strcasecmp(in->key, key);
  45. }