BStringMap.c 5.5 KB

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