var.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. void clish_var_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  44. {
  45. const clish_var_t *this = clientnode;
  46. /* fill out the opaque key */
  47. strcpy((char *)key, this->name);
  48. }
  49. /*--------------------------------------------------------- */
  50. size_t clish_var_bt_offset(void)
  51. {
  52. return offsetof(clish_var_t, bt_node);
  53. }
  54. /*--------------------------------------------------------- */
  55. clish_var_t *clish_var_new(const char *name)
  56. {
  57. clish_var_t *this = malloc(sizeof(clish_var_t));
  58. if (this)
  59. clish_var_init(this, name);
  60. return this;
  61. }
  62. /*---------------------------------------------------------
  63. * PUBLIC METHODS
  64. *--------------------------------------------------------- */
  65. void clish_var_delete(clish_var_t *this)
  66. {
  67. clish_var_fini(this);
  68. free(this);
  69. }
  70. /*---------------------------------------------------------
  71. * PUBLIC ATTRIBUTES
  72. *--------------------------------------------------------- */
  73. const char *clish_var__get_name(const clish_var_t *this)
  74. {
  75. if (!this)
  76. return NULL;
  77. return this->name;
  78. }
  79. /*--------------------------------------------------------- */
  80. void clish_var__set_dynamic(clish_var_t *this, bool_t dynamic)
  81. {
  82. this->dynamic = dynamic;
  83. }
  84. /*--------------------------------------------------------- */
  85. bool_t clish_var__get_dynamic(const clish_var_t *this)
  86. {
  87. return this->dynamic;
  88. }
  89. /*--------------------------------------------------------- */
  90. void clish_var__set_value(clish_var_t *this, const char *value)
  91. {
  92. if (this->value)
  93. lub_string_free(this->value);
  94. this->value = lub_string_dup(value);
  95. }
  96. /*--------------------------------------------------------- */
  97. char *clish_var__get_value(const clish_var_t *this)
  98. {
  99. return this->value;
  100. }
  101. /*--------------------------------------------------------- */
  102. clish_action_t *clish_var__get_action(const clish_var_t *this)
  103. {
  104. return this->action;
  105. }
  106. /*--------------------------------------------------------- */
  107. void clish_var__set_saved(clish_var_t *this, const char *value)
  108. {
  109. if (this->saved)
  110. lub_string_free(this->saved);
  111. this->saved = lub_string_dup(value);
  112. }
  113. /*--------------------------------------------------------- */
  114. char *clish_var__get_saved(const clish_var_t *this)
  115. {
  116. return this->saved;
  117. }