var.c 3.6 KB

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