BStringMap.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * @file BStringMap.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <misc/offset.h>
  25. #include <stringmap/BStringMap.h>
  26. static int string_comparator (void *unused, char **str1, char **str2)
  27. {
  28. int c = strcmp(*str1, *str2);
  29. if (c < 0) {
  30. return -1;
  31. }
  32. if (c > 0) {
  33. return 1;
  34. }
  35. return 0;
  36. }
  37. static void free_entry (BStringMap *o, struct BStringMap_entry *e)
  38. {
  39. BAVL_Remove(&o->tree, &e->tree_node);
  40. free(e->value);
  41. free(e->key);
  42. free(e);
  43. }
  44. void BStringMap_Init (BStringMap *o)
  45. {
  46. // init tree
  47. BAVL_Init(&o->tree, OFFSET_DIFF(struct BStringMap_entry, key, tree_node), (BAVL_comparator)string_comparator, NULL);
  48. DebugObject_Init(&o->d_obj);
  49. }
  50. void BStringMap_Free (BStringMap *o)
  51. {
  52. DebugObject_Free(&o->d_obj);
  53. // free entries
  54. BAVLNode *tree_node;
  55. while (tree_node = BAVL_GetFirst(&o->tree)) {
  56. struct BStringMap_entry *e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
  57. free_entry(o, e);
  58. }
  59. }
  60. const char * BStringMap_Get (BStringMap *o, const char *key)
  61. {
  62. DebugObject_Access(&o->d_obj);
  63. ASSERT(key)
  64. // lookup
  65. BAVLNode *tree_node = BAVL_LookupExact(&o->tree, &key);
  66. if (!tree_node) {
  67. return NULL;
  68. }
  69. struct BStringMap_entry *e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
  70. return e->value;
  71. }
  72. int BStringMap_Set (BStringMap *o, const char *key, const char *value)
  73. {
  74. DebugObject_Access(&o->d_obj);
  75. ASSERT(key)
  76. ASSERT(value)
  77. // alloc entry
  78. struct BStringMap_entry *e = malloc(sizeof(*e));
  79. if (!e) {
  80. goto fail0;
  81. }
  82. // alloc and set key
  83. if (!(e->key = malloc(strlen(key) + 1))) {
  84. goto fail1;
  85. }
  86. strcpy(e->key, key);
  87. // alloc and set value
  88. if (!(e->value = malloc(strlen(value) + 1))) {
  89. goto fail2;
  90. }
  91. strcpy(e->value, value);
  92. // try inserting to tree
  93. BAVLNode *ex_tree_node;
  94. if (!BAVL_Insert(&o->tree, &e->tree_node, &ex_tree_node)) {
  95. // remove existing entry
  96. struct BStringMap_entry *ex_e = UPPER_OBJECT(ex_tree_node, struct BStringMap_entry, tree_node);
  97. free_entry(o, ex_e);
  98. // insert new node
  99. ASSERT_EXECUTE(BAVL_Insert(&o->tree, &e->tree_node, NULL))
  100. }
  101. return 1;
  102. fail2:
  103. free(e->key);
  104. fail1:
  105. free(e);
  106. fail0:
  107. return 0;
  108. }
  109. void BStringMap_Unset (BStringMap *o, const char *key)
  110. {
  111. DebugObject_Access(&o->d_obj);
  112. ASSERT(key)
  113. // lookup
  114. BAVLNode *tree_node = BAVL_LookupExact(&o->tree, &key);
  115. if (!tree_node) {
  116. return;
  117. }
  118. struct BStringMap_entry *e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
  119. // remove
  120. free_entry(o, e);
  121. }
  122. const char * BStringMap_First (BStringMap *o)
  123. {
  124. DebugObject_Access(&o->d_obj);
  125. // get first
  126. BAVLNode *tree_node = BAVL_GetFirst(&o->tree);
  127. if (!tree_node) {
  128. return NULL;
  129. }
  130. struct BStringMap_entry *e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
  131. return e->key;
  132. }
  133. const char * BStringMap_Next (BStringMap *o, const char *key)
  134. {
  135. DebugObject_Access(&o->d_obj);
  136. ASSERT(key)
  137. ASSERT(BAVL_LookupExact(&o->tree, &key))
  138. // get entry
  139. struct BStringMap_entry *e = UPPER_OBJECT(BAVL_LookupExact(&o->tree, &key), struct BStringMap_entry, tree_node);
  140. // get next
  141. BAVLNode *tree_node = BAVL_GetNext(&o->tree, &e->tree_node);
  142. if (!tree_node) {
  143. return NULL;
  144. }
  145. struct BStringMap_entry *next_e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
  146. return next_e->key;
  147. }