udata.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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__get_data(const clish_udata_t *this)
  46. {
  47. if (!this)
  48. return NULL;
  49. return this->data;
  50. }
  51. /*--------------------------------------------------------- */
  52. int clish_udata__set_data(clish_udata_t *this, void *data)
  53. {
  54. if (!this)
  55. return -1;
  56. this->data = data;
  57. return 0;
  58. }
  59. /*--------------------------------------------------------- */
  60. char *clish_udata__get_name(const clish_udata_t *this)
  61. {
  62. if (!this)
  63. return NULL;
  64. return this->name;
  65. }
  66. /*--------------------------------------------------------- */