keyval.c 731 B

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