var.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. 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. * PUBLIC METHODS
  60. *--------------------------------------------------------- */
  61. void clish_var_delete(clish_var_t *this)
  62. {
  63. clish_var_fini(this);
  64. free(this);
  65. }
  66. /*---------------------------------------------------------
  67. * PUBLIC ATTRIBUTES
  68. *--------------------------------------------------------- */
  69. const char *clish_var__get_name(const clish_var_t *this)
  70. {
  71. if (!this)
  72. return NULL;
  73. return this->name;
  74. }
  75. /*--------------------------------------------------------- */
  76. void clish_var__set_dynamic(clish_var_t *this, bool_t dynamic)
  77. {
  78. this->dynamic = dynamic;
  79. }
  80. /*--------------------------------------------------------- */
  81. bool_t clish_var__get_dynamic(const clish_var_t *this)
  82. {
  83. return this->dynamic;
  84. }
  85. /*--------------------------------------------------------- */
  86. void clish_var__set_value(clish_var_t *this, const char *value)
  87. {
  88. if (this->value)
  89. lub_string_free(this->value);
  90. this->value = lub_string_dup(value);
  91. }
  92. /*--------------------------------------------------------- */
  93. char *clish_var__get_value(const clish_var_t *this)
  94. {
  95. return this->value;
  96. }