BStringMap.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. int BStringMap_InitCopy (BStringMap *o, const BStringMap *src)
  51. {
  52. BStringMap_Init(o);
  53. const char *key = BStringMap_First(src);
  54. while (key) {
  55. if (!BStringMap_Set(o, key, BStringMap_Get(src, key))) {
  56. goto fail1;
  57. }
  58. key = BStringMap_Next(src, key);
  59. }
  60. return 1;
  61. fail1:
  62. BStringMap_Free(o);
  63. return 0;
  64. }
  65. void BStringMap_Free (BStringMap *o)
  66. {
  67. DebugObject_Free(&o->d_obj);
  68. // free entries
  69. BAVLNode *tree_node;
  70. while (tree_node = BAVL_GetFirst(&o->tree)) {
  71. struct BStringMap_entry *e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
  72. free_entry(o, e);
  73. }
  74. }
  75. const char * BStringMap_Get (const BStringMap *o, const char *key)
  76. {
  77. DebugObject_Access(&o->d_obj);
  78. ASSERT(key)
  79. // lookup
  80. BAVLNode *tree_node = BAVL_LookupExact(&o->tree, &key);
  81. if (!tree_node) {
  82. return NULL;
  83. }
  84. struct BStringMap_entry *e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
  85. return e->value;
  86. }
  87. int BStringMap_Set (BStringMap *o, const char *key, const char *value)
  88. {
  89. DebugObject_Access(&o->d_obj);
  90. ASSERT(key)
  91. ASSERT(value)
  92. // alloc entry
  93. struct BStringMap_entry *e = malloc(sizeof(*e));
  94. if (!e) {
  95. goto fail0;
  96. }
  97. // alloc and set key
  98. if (!(e->key = malloc(strlen(key) + 1))) {
  99. goto fail1;
  100. }
  101. strcpy(e->key, key);
  102. // alloc and set value
  103. if (!(e->value = malloc(strlen(value) + 1))) {
  104. goto fail2;
  105. }
  106. strcpy(e->value, value);
  107. // try inserting to tree
  108. BAVLNode *ex_tree_node;
  109. if (!BAVL_Insert(&o->tree, &e->tree_node, &ex_tree_node)) {
  110. // remove existing entry
  111. struct BStringMap_entry *ex_e = UPPER_OBJECT(ex_tree_node, struct BStringMap_entry, tree_node);
  112. free_entry(o, ex_e);
  113. // insert new node
  114. ASSERT_EXECUTE(BAVL_Insert(&o->tree, &e->tree_node, NULL))
  115. }
  116. return 1;
  117. fail2:
  118. free(e->key);
  119. fail1:
  120. free(e);
  121. fail0:
  122. return 0;
  123. }
  124. void BStringMap_Unset (BStringMap *o, const char *key)
  125. {
  126. DebugObject_Access(&o->d_obj);
  127. ASSERT(key)
  128. // lookup
  129. BAVLNode *tree_node = BAVL_LookupExact(&o->tree, &key);
  130. if (!tree_node) {
  131. return;
  132. }
  133. struct BStringMap_entry *e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
  134. // remove
  135. free_entry(o, e);
  136. }
  137. const char * BStringMap_First (const BStringMap *o)
  138. {
  139. DebugObject_Access(&o->d_obj);
  140. // get first
  141. BAVLNode *tree_node = BAVL_GetFirst(&o->tree);
  142. if (!tree_node) {
  143. return NULL;
  144. }
  145. struct BStringMap_entry *e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
  146. return e->key;
  147. }
  148. const char * BStringMap_Next (const BStringMap *o, const char *key)
  149. {
  150. DebugObject_Access(&o->d_obj);
  151. ASSERT(key)
  152. ASSERT(BAVL_LookupExact(&o->tree, &key))
  153. // get entry
  154. struct BStringMap_entry *e = UPPER_OBJECT(BAVL_LookupExact(&o->tree, &key), struct BStringMap_entry, tree_node);
  155. // get next
  156. BAVLNode *tree_node = BAVL_GetNext(&o->tree, &e->tree_node);
  157. if (!tree_node) {
  158. return NULL;
  159. }
  160. struct BStringMap_entry *next_e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
  161. return next_e->key;
  162. }