var.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * var.c
  3. *
  4. * This file provides the implementation of the "var" class
  5. */
  6. #include <assert.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include "lub/string.h"
  11. #include "private.h"
  12. /*--------------------------------------------------------- */
  13. static void clish_var_init(clish_var_t *this, const char *name)
  14. {
  15. this->name = lub_string_dup(name);
  16. this->dynamic = BOOL_FALSE;
  17. this->value = NULL;
  18. this->action = clish_action_new();
  19. this->saved = NULL;
  20. /* Be a good binary tree citizen */
  21. lub_bintree_node_init(&this->bt_node);
  22. }
  23. /*--------------------------------------------------------- */
  24. static void clish_var_fini(clish_var_t *this)
  25. {
  26. lub_string_free(this->name);
  27. lub_string_free(this->value);
  28. lub_string_free(this->saved);
  29. clish_action_delete(this->action);
  30. }
  31. /*--------------------------------------------------------- */
  32. int clish_var_bt_compare(const void *clientnode, const void *clientkey)
  33. {
  34. const clish_var_t *this = clientnode;
  35. const char *key = clientkey;
  36. return strcmp(this->name, key);
  37. }
  38. /*-------------------------------------------------------- */
  39. void clish_var_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  40. {
  41. const clish_var_t *this = clientnode;
  42. /* fill out the opaque key */
  43. strcpy((char *)key, this->name);
  44. }
  45. /*--------------------------------------------------------- */
  46. size_t clish_var_bt_offset(void)
  47. {
  48. return offsetof(clish_var_t, bt_node);
  49. }
  50. /*--------------------------------------------------------- */
  51. clish_var_t *clish_var_new(const char *name)
  52. {
  53. clish_var_t *this = malloc(sizeof(clish_var_t));
  54. if (this)
  55. clish_var_init(this, name);
  56. return this;
  57. }
  58. /*--------------------------------------------------------- */
  59. void clish_var_delete(void *data)
  60. {
  61. clish_var_t *this = (clish_var_t *)data;
  62. clish_var_fini(this);
  63. free(this);
  64. }
  65. CLISH_GET_STR(var, name);
  66. CLISH_SET(var, bool_t, dynamic);
  67. CLISH_GET(var, bool_t, dynamic);
  68. CLISH_SET_STR(var, value);
  69. CLISH_GET_STR(var, value);
  70. CLISH_SET_STR(var, saved);
  71. CLISH_GET_STR(var, saved);
  72. CLISH_GET(var, clish_action_t *, action);
  73. /*--------------------------------------------------------- */
  74. int clish_var_compare(const void *first, const void *second)
  75. {
  76. const clish_var_t *f = (const clish_var_t *)first;
  77. const clish_var_t *s = (const clish_var_t *)second;
  78. return lub_string_nocasecmp(f->name, s->name);
  79. }
  80. /*--------------------------------------------------------- */
  81. int clish_var_fn_find_by_name(const void *key, const void *data) {
  82. const char *name = (const char *)key;
  83. const clish_var_t *d = (const clish_var_t *)data;
  84. return lub_string_nocasecmp(name, clish_var__get_name(d));
  85. }