keyval.c 748 B

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