BStringMap.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * @file BStringMap.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <misc/offset.h>
  32. #include <misc/compare.h>
  33. #include <stringmap/BStringMap.h>
  34. static int string_comparator (void *unused, char **str1, char **str2)
  35. {
  36. int c = strcmp(*str1, *str2);
  37. return B_COMPARE(c, 0);
  38. }
  39. static void free_entry (BStringMap *o, struct BStringMap_entry *e)
  40. {
  41. BAVL_Remove(&o->tree, &e->tree_node);
  42. free(e->value);
  43. free(e->key);
  44. free(e);
  45. }
  46. void BStringMap_Init (BStringMap *o)
  47. {
  48. // init tree
  49. BAVL_Init(&o->tree, OFFSET_DIFF(struct BStringMap_entry, key, tree_node), (BAVL_comparator)string_comparator, NULL);
  50. DebugObject_Init(&o->d_obj);
  51. }
  52. int BStringMap_InitCopy (BStringMap *o, const BStringMap *src)
  53. {
  54. BStringMap_Init(o);
  55. const char *key = BStringMap_First(src);
  56. while (key) {
  57. if (!BStringMap_Set(o, key, BStringMap_Get(src, key))) {
  58. goto fail1;
  59. }
  60. key = BStringMap_Next(src, key);
  61. }
  62. return 1;
  63. fail1:
  64. BStringMap_Free(o);
  65. return 0;
  66. }
  67. void BStringMap_Free (BStringMap *o)
  68. {
  69. DebugObject_Free(&o->d_obj);
  70. // free entries
  71. BAVLNode *tree_node;
  72. while (tree_node = BAVL_GetFirst(&o->tree)) {
  73. struct BStringMap_entry *e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
  74. free_entry(o, e);
  75. }
  76. }
  77. const char * BStringMap_Get (const BStringMap *o, const char *key)
  78. {
  79. DebugObject_Access(&o->d_obj);
  80. ASSERT(key)
  81. // lookup
  82. BAVLNode *tree_node = BAVL_LookupExact(&o->tree, &key);
  83. if (!tree_node) {
  84. return NULL;
  85. }
  86. struct BStringMap_entry *e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
  87. return e->value;
  88. }
  89. int BStringMap_Set (BStringMap *o, const char *key, const char *value)
  90. {
  91. DebugObject_Access(&o->d_obj);
  92. ASSERT(key)
  93. ASSERT(value)
  94. // alloc entry
  95. struct BStringMap_entry *e = malloc(sizeof(*e));
  96. if (!e) {
  97. goto fail0;
  98. }
  99. // alloc and set key
  100. if (!(e->key = malloc(strlen(key) + 1))) {
  101. goto fail1;
  102. }
  103. strcpy(e->key, key);
  104. // alloc and set value
  105. if (!(e->value = malloc(strlen(value) + 1))) {
  106. goto fail2;
  107. }
  108. strcpy(e->value, value);
  109. // try inserting to tree
  110. BAVLNode *ex_tree_node;
  111. if (!BAVL_Insert(&o->tree, &e->tree_node, &ex_tree_node)) {
  112. // remove existing entry
  113. struct BStringMap_entry *ex_e = UPPER_OBJECT(ex_tree_node, struct BStringMap_entry, tree_node);
  114. free_entry(o, ex_e);
  115. // insert new node
  116. ASSERT_EXECUTE(BAVL_Insert(&o->tree, &e->tree_node, NULL))
  117. }
  118. return 1;
  119. fail2:
  120. free(e->key);
  121. fail1:
  122. free(e);
  123. fail0:
  124. return 0;
  125. }
  126. void BStringMap_Unset (BStringMap *o, const char *key)
  127. {
  128. DebugObject_Access(&o->d_obj);
  129. ASSERT(key)
  130. // lookup
  131. BAVLNode *tree_node = BAVL_LookupExact(&o->tree, &key);
  132. if (!tree_node) {
  133. return;
  134. }
  135. struct BStringMap_entry *e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
  136. // remove
  137. free_entry(o, e);
  138. }
  139. const char * BStringMap_First (const BStringMap *o)
  140. {
  141. DebugObject_Access(&o->d_obj);
  142. // get first
  143. BAVLNode *tree_node = BAVL_GetFirst(&o->tree);
  144. if (!tree_node) {
  145. return NULL;
  146. }
  147. struct BStringMap_entry *e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
  148. return e->key;
  149. }
  150. const char * BStringMap_Next (const BStringMap *o, const char *key)
  151. {
  152. DebugObject_Access(&o->d_obj);
  153. ASSERT(key)
  154. ASSERT(BAVL_LookupExact(&o->tree, &key))
  155. // get entry
  156. struct BStringMap_entry *e = UPPER_OBJECT(BAVL_LookupExact(&o->tree, &key), struct BStringMap_entry, tree_node);
  157. // get next
  158. BAVLNode *tree_node = BAVL_GetNext(&o->tree, &e->tree_node);
  159. if (!tree_node) {
  160. return NULL;
  161. }
  162. struct BStringMap_entry *next_e = UPPER_OBJECT(tree_node, struct BStringMap_entry, tree_node);
  163. return next_e->key;
  164. }