var.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. }
  21. /*--------------------------------------------------------- */
  22. static void clish_var_fini(clish_var_t *this)
  23. {
  24. lub_string_free(this->name);
  25. lub_string_free(this->value);
  26. lub_string_free(this->saved);
  27. clish_action_delete(this->action);
  28. }
  29. /*--------------------------------------------------------- */
  30. clish_var_t *clish_var_new(const char *name)
  31. {
  32. clish_var_t *this = malloc(sizeof(clish_var_t));
  33. if (this)
  34. clish_var_init(this, name);
  35. return this;
  36. }
  37. /*--------------------------------------------------------- */
  38. void clish_var_delete(void *data)
  39. {
  40. clish_var_t *this = (clish_var_t *)data;
  41. clish_var_fini(this);
  42. free(this);
  43. }
  44. CLISH_GET_STR(var, name);
  45. CLISH_SET(var, bool_t, dynamic);
  46. CLISH_GET(var, bool_t, dynamic);
  47. CLISH_SET_STR(var, value);
  48. CLISH_GET_STR(var, value);
  49. CLISH_SET_STR(var, saved);
  50. CLISH_GET_STR(var, saved);
  51. CLISH_GET(var, clish_action_t *, action);
  52. /*--------------------------------------------------------- */
  53. int clish_var_compare(const void *first, const void *second)
  54. {
  55. const clish_var_t *f = (const clish_var_t *)first;
  56. const clish_var_t *s = (const clish_var_t *)second;
  57. return lub_string_nocasecmp(f->name, s->name);
  58. }
  59. /*--------------------------------------------------------- */
  60. int clish_var_fn_find_by_name(const void *key, const void *data) {
  61. const char *name = (const char *)key;
  62. const clish_var_t *d = (const clish_var_t *)data;
  63. return lub_string_nocasecmp(name, clish_var__get_name(d));
  64. }