1
0

shell_udata.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * shell_udata.c
  3. */
  4. #include <assert.h>
  5. #include <string.h>
  6. #include "private.h"
  7. #include "clish/udata.h"
  8. /*-------------------------------------------------------- */
  9. static lub_list_node_t *find_udata_node(const clish_shell_t *this, const char *name)
  10. {
  11. lub_list_node_t *iter;
  12. clish_udata_t *udata;
  13. assert(this);
  14. if (!name)
  15. return NULL;
  16. for(iter = lub_list__get_head(this->udata);
  17. iter; iter = lub_list_node__get_next(iter)) {
  18. int res;
  19. udata = (clish_udata_t *)lub_list_node__get_data(iter);
  20. res = strcmp(clish_udata__get_name(udata), name);
  21. if (!res)
  22. return iter;
  23. if (res > 0) /* No chance to find name */
  24. break;
  25. }
  26. return NULL;
  27. }
  28. /*-------------------------------------------------------- */
  29. static clish_udata_t *find_udata(const clish_shell_t *this, const char *name)
  30. {
  31. lub_list_node_t *iter;
  32. if (!(iter = find_udata_node(this, name)))
  33. return NULL;
  34. return (clish_udata_t *)lub_list_node__get_data(iter);
  35. }
  36. /*-------------------------------------------------------- */
  37. void *clish_shell__get_udata(const clish_shell_t *this, const char *name)
  38. {
  39. clish_udata_t *udata;
  40. assert (this);
  41. udata = find_udata(this, name);
  42. return clish_udata__get_data(udata);
  43. }
  44. /*-------------------------------------------------------- */
  45. void *clish_shell__del_udata(clish_shell_t *this, const char *name)
  46. {
  47. lub_list_node_t *node = NULL;
  48. clish_udata_t *pdata = NULL;
  49. if (!this || !name)
  50. return NULL;
  51. if(!(node = find_udata_node(this, name)))
  52. return NULL;
  53. pdata = (clish_udata_t *)lub_list_node__get_data(node);
  54. lub_list_del(this->udata, node);
  55. lub_list_node_free(node);
  56. return clish_udata_free(pdata);
  57. }
  58. /*-------------------------------------------------------- */
  59. int clish_shell__set_udata(clish_shell_t *this,
  60. const char *name, void *data)
  61. {
  62. clish_udata_t *pdata = NULL;
  63. if (!this || !name)
  64. return -1;
  65. if ((pdata = find_udata(this, name))) {
  66. clish_udata__set_data(pdata, data);
  67. return 0;
  68. }
  69. if (!(pdata = clish_udata_new(name, data)))
  70. return -1;
  71. if (lub_list_add(this->udata, pdata))
  72. return 0;
  73. clish_udata_free(pdata);
  74. return -1;
  75. }
  76. /*-------------------------------------------------------- */