pair.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /** @file pair.c
  2. * Ini file pairs key/value.
  3. */
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <assert.h>
  7. #include "private.h"
  8. #include "faux/str.h"
  9. #include "faux/ini.h"
  10. int faux_pair_compare(const void *first, const void *second) {
  11. const faux_pair_t *f = (const faux_pair_t *)first;
  12. const faux_pair_t *s = (const faux_pair_t *)second;
  13. return strcmp(f->name, s->name);
  14. }
  15. int faux_pair_kcompare(const void *key, const void *list_item) {
  16. const char *f = (const char *)key;
  17. const faux_pair_t *s = (const faux_pair_t *)list_item;
  18. return strcmp(f, s->name);
  19. }
  20. faux_pair_t *faux_pair_new(const char *name, const char *value) {
  21. faux_pair_t *pair = NULL;
  22. pair = faux_zmalloc(sizeof(*pair));
  23. assert(pair);
  24. if (!pair)
  25. return NULL;
  26. // Initialize
  27. pair->name = faux_str_dup(name);
  28. pair->value = faux_str_dup(value);
  29. return pair;
  30. }
  31. void faux_pair_free(void *ptr) {
  32. faux_pair_t *pair = (faux_pair_t *)ptr;
  33. assert(pair);
  34. if (!pair)
  35. return;
  36. faux_str_free(pair->name);
  37. faux_str_free(pair->value);
  38. faux_free(pair);
  39. }
  40. const char *faux_pair_name(const faux_pair_t *pair) {
  41. assert(pair);
  42. if (!pair)
  43. return NULL;
  44. return pair->name;
  45. }
  46. void faux_pair_set_name(faux_pair_t *pair, const char *name) {
  47. assert(pair);
  48. if (!pair)
  49. return;
  50. faux_str_free(pair->name);
  51. pair->name = faux_str_dup(name);
  52. }
  53. const char *faux_pair_value(const faux_pair_t *pair) {
  54. assert(pair);
  55. if (!pair)
  56. return NULL;
  57. return pair->value;
  58. }
  59. void faux_pair_set_value(faux_pair_t *pair, const char *value) {
  60. assert(pair);
  61. if (!pair)
  62. return;
  63. faux_str_free(pair->value);
  64. pair->value = faux_str_dup(value);
  65. }