var.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. static void clish_var_init(clish_var_t *this, const char *name)
  14. {
  15. this->name = lub_string_dup(name);
  16. this->dynamic = BOOL_FALSE;
  17. this->value = NULL;
  18. this->action = clish_action_new();
  19. this->saved = 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. clish_action_delete(this->action);
  29. lub_string_free(this->saved);
  30. }
  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. void clish_var_delete(clish_var_t *this)
  60. {
  61. clish_var_fini(this);
  62. free(this);
  63. }
  64. CLISH_GET_STR(var, name);
  65. CLISH_SET(var, bool_t, dynamic);
  66. CLISH_GET(var, bool_t, dynamic);
  67. CLISH_SET_STR(var, value);
  68. CLISH_GET_STR(var, value);
  69. CLISH_SET_STR(var, saved);
  70. CLISH_GET_STR(var, saved);
  71. CLISH_GET(var, clish_action_t *, action);