1
0

udata.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * udata.c
  3. */
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "lub/list.h"
  9. #include "lub/string.h"
  10. #include "private.h"
  11. /*--------------------------------------------------------- */
  12. int clish_udata_compare(const void *first, const void *second)
  13. {
  14. const clish_udata_t *f = (const clish_udata_t *)first;
  15. const clish_udata_t *s = (const clish_udata_t *)second;
  16. return strcmp(f->name, s->name);
  17. }
  18. /*--------------------------------------------------------- */
  19. clish_udata_t *clish_udata_new(const char *name, void *data)
  20. {
  21. clish_udata_t *pdata =
  22. (clish_udata_t *)malloc(sizeof(clish_udata_t));
  23. if (!name) {
  24. free(pdata);
  25. return NULL;
  26. }
  27. memset(pdata, 0, sizeof(*pdata));
  28. pdata->name = lub_string_dup(name);
  29. pdata->data = data;
  30. return pdata;
  31. }
  32. /*--------------------------------------------------------- */
  33. void *clish_udata_free(clish_udata_t *this)
  34. {
  35. void *data;
  36. if (!this)
  37. return NULL;
  38. if (this->name)
  39. lub_string_free(this->name);
  40. data = this->data;
  41. free(this);
  42. return data;
  43. }
  44. CLISH_SET(udata, void *, data);
  45. CLISH_GET(udata, void *, data);
  46. CLISH_GET_STR(udata, name);