var.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. * PRIVATE METHODS
  14. *--------------------------------------------------------- */
  15. static void clish_var_init(clish_var_t *this, const char *name)
  16. {
  17. this->name = lub_string_dup(name);
  18. this->dynamic = BOOL_FALSE;
  19. this->value = NULL;
  20. this->action = clish_action_new();
  21. /* Be a good binary tree citizen */
  22. lub_bintree_node_init(&this->bt_node);
  23. }
  24. /*--------------------------------------------------------- */
  25. static void clish_var_fini(clish_var_t *this)
  26. {
  27. lub_string_free(this->name);
  28. lub_string_free(this->value);
  29. clish_action_delete(this->action);
  30. }
  31. /*---------------------------------------------------------
  32. * PUBLIC META FUNCTIONS
  33. *--------------------------------------------------------- */
  34. int clish_var_bt_compare(const void *clientnode, const void *clientkey)
  35. {
  36. const clish_var_t *this = clientnode;
  37. const char *key = clientkey;
  38. return strcmp(this->name, key);
  39. }
  40. /*-------------------------------------------------------- */
  41. clish_var_expand_fn_t clish_var_expand_default;
  42. inline char *clish_var_expand_default(const char *str, void *context)
  43. {
  44. return lub_string_dup(str);
  45. }
  46. /*-------------------------------------------------------- */
  47. void clish_var_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  48. {
  49. const clish_var_t *this = clientnode;
  50. /* fill out the opaque key */
  51. strcpy((char *)key, this->name);
  52. }
  53. /*--------------------------------------------------------- */
  54. size_t clish_var_bt_offset(void)
  55. {
  56. return offsetof(clish_var_t, bt_node);
  57. }
  58. /*--------------------------------------------------------- */
  59. clish_var_t *clish_var_new(const char *name)
  60. {
  61. clish_var_t *this = malloc(sizeof(clish_var_t));
  62. if (this)
  63. clish_var_init(this, name);
  64. return this;
  65. }
  66. /*---------------------------------------------------------
  67. * PUBLIC METHODS
  68. *--------------------------------------------------------- */
  69. void clish_var_delete(clish_var_t *this)
  70. {
  71. clish_var_fini(this);
  72. free(this);
  73. }
  74. /*---------------------------------------------------------
  75. * PUBLIC ATTRIBUTES
  76. *--------------------------------------------------------- */
  77. const char *clish_var__get_name(const clish_var_t *this)
  78. {
  79. if (!this)
  80. return NULL;
  81. return this->name;
  82. }
  83. /*--------------------------------------------------------- */
  84. void clish_var__set_dynamic(clish_var_t *this, bool_t dynamic)
  85. {
  86. this->dynamic = dynamic;
  87. }
  88. /*--------------------------------------------------------- */
  89. bool_t clish_var__get_dynamic(const clish_var_t *this)
  90. {
  91. return this->dynamic;
  92. }
  93. /*--------------------------------------------------------- */
  94. void clish_var__set_value(clish_var_t *this, const char *value)
  95. {
  96. if (this->value)
  97. lub_string_free(this->value);
  98. this->value = lub_string_dup(value);
  99. }
  100. /*--------------------------------------------------------- */
  101. char *clish_var__get_value(const clish_var_t *this)
  102. {
  103. return this->value;
  104. }
  105. /*--------------------------------------------------------- */
  106. clish_action_t *clish_var__get_action(const clish_var_t *this)
  107. {
  108. return this->action;
  109. }