var.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /* 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. }
  29. /*---------------------------------------------------------
  30. * PUBLIC META FUNCTIONS
  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. clish_var_expand_fn_t clish_var_expand_default;
  40. inline char *clish_var_expand_default(const char *str, void *context)
  41. {
  42. return lub_string_dup(str);
  43. }
  44. /*-------------------------------------------------------- */
  45. void clish_var_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  46. {
  47. const clish_var_t *this = clientnode;
  48. /* fill out the opaque key */
  49. strcpy((char *)key, this->name);
  50. }
  51. /*--------------------------------------------------------- */
  52. size_t clish_var_bt_offset(void)
  53. {
  54. return offsetof(clish_var_t, bt_node);
  55. }
  56. /*--------------------------------------------------------- */
  57. clish_var_t *clish_var_new(const char *name)
  58. {
  59. clish_var_t *this = malloc(sizeof(clish_var_t));
  60. if (this)
  61. clish_var_init(this, name);
  62. return this;
  63. }
  64. /*---------------------------------------------------------
  65. * PUBLIC METHODS
  66. *--------------------------------------------------------- */
  67. void clish_var_delete(clish_var_t *this)
  68. {
  69. clish_var_fini(this);
  70. free(this);
  71. }
  72. /*---------------------------------------------------------
  73. * PUBLIC ATTRIBUTES
  74. *--------------------------------------------------------- */
  75. const char *clish_var__get_name(const clish_var_t *this)
  76. {
  77. if (!this)
  78. return NULL;
  79. return this->name;
  80. }
  81. /*--------------------------------------------------------- */
  82. void clish_var__set_dynamic(clish_var_t *this, bool_t dynamic)
  83. {
  84. this->dynamic = dynamic;
  85. }
  86. /*--------------------------------------------------------- */
  87. bool_t clish_var__get_dynamic(const clish_var_t *this)
  88. {
  89. return this->dynamic;
  90. }
  91. /*--------------------------------------------------------- */
  92. void clish_var__set_value(clish_var_t *this, const char *value)
  93. {
  94. if (this->value)
  95. lub_string_free(this->value);
  96. this->value = lub_string_dup(value);
  97. }
  98. /*--------------------------------------------------------- */
  99. char *clish_var__get_value(const clish_var_t *this)
  100. {
  101. return this->value;
  102. }