1
0

udata.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. /*--------------------------------------------------------- */
  45. void clish_udata_delete(void *data)
  46. {
  47. clish_udata_t *this = (clish_udata_t *)data;
  48. clish_udata_free(this);
  49. }
  50. CLISH_SET(udata, void *, data);
  51. CLISH_GET(udata, void *, data);
  52. CLISH_GET_STR(udata, name);