keyval.c 715 B

12345678910111213141516171819202122232425262728
  1. // Borrowed from https://github.com/b-k/21st-Century-Examples
  2. #include <stdlib.h> //malloc
  3. #include <strings.h> //strcasecmp (from POSIX)
  4. #include "keyval.h"
  5. keyval *keyval_new(char *key, void *value){
  6. keyval *out = malloc(sizeof(keyval));
  7. *out = (keyval){.key = key, .value=value};
  8. return out;
  9. }
  10. /** Copy a key/value pair. The new pair has pointers to
  11. the values in the old pair, not copies of their data. */
  12. keyval *keyval_copy(keyval const *in){
  13. keyval *out = malloc(sizeof(keyval));
  14. *out = *in;
  15. return out;
  16. }
  17. void keyval_free(keyval *in){
  18. free(in->key);
  19. free(in->value);
  20. free(in);
  21. }
  22. int keyval_matches(keyval const *in, char const *key){
  23. return !strcasecmp(in->key, key);
  24. }