NCDModuleIndex.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @file NCDModuleIndex.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 <string.h>
  30. #include <stdlib.h>
  31. #include <misc/offset.h>
  32. #include <base/BLog.h>
  33. #include "NCDModuleIndex.h"
  34. #include <generated/blog_channel_NCDModuleIndex.h>
  35. static int string_comparator (void *user, const char *s1, const char *s2)
  36. {
  37. int cmp = strcmp(s1, s2);
  38. if (cmp < 0) {
  39. return -1;
  40. }
  41. if (cmp > 0) {
  42. return 1;
  43. }
  44. return 0;
  45. }
  46. static int string_pointer_comparator (void *user, const char **s1, const char **s2)
  47. {
  48. int cmp = strcmp(*s1, *s2);
  49. if (cmp < 0) {
  50. return -1;
  51. }
  52. if (cmp > 0) {
  53. return 1;
  54. }
  55. return 0;
  56. }
  57. static struct NCDModuleIndex_module * find_module (NCDModuleIndex *o, const char *type)
  58. {
  59. BAVLNode *node = BAVL_LookupExact(&o->modules_tree, (void *)type);
  60. if (!node) {
  61. return NULL;
  62. }
  63. struct NCDModuleIndex_module *m = UPPER_OBJECT(node, struct NCDModuleIndex_module, modules_tree_node);
  64. ASSERT(!strcmp(m->type, type))
  65. return m;
  66. }
  67. static struct NCDModuleIndex_base_type * find_base_type (NCDModuleIndex *o, const char *base_type)
  68. {
  69. BAVLNode *node = BAVL_LookupExact(&o->base_types_tree, &base_type);
  70. if (!node) {
  71. return NULL;
  72. }
  73. struct NCDModuleIndex_base_type *bt = UPPER_OBJECT(node, struct NCDModuleIndex_base_type, base_types_tree_node);
  74. ASSERT(!strcmp(bt->base_type, base_type))
  75. return bt;
  76. }
  77. void NCDModuleIndex_Init (NCDModuleIndex *o)
  78. {
  79. // init modules tree
  80. BAVL_Init(&o->modules_tree, OFFSET_DIFF(struct NCDModuleIndex_module, type, modules_tree_node), (BAVL_comparator)string_comparator, NULL);
  81. // init base types tree
  82. BAVL_Init(&o->base_types_tree, OFFSET_DIFF(struct NCDModuleIndex_base_type, base_type, base_types_tree_node), (BAVL_comparator)string_pointer_comparator, NULL);
  83. DebugObject_Init(&o->d_obj);
  84. }
  85. void NCDModuleIndex_Free (NCDModuleIndex *o)
  86. {
  87. DebugObject_Free(&o->d_obj);
  88. // free base types
  89. while (!BAVL_IsEmpty(&o->base_types_tree)) {
  90. struct NCDModuleIndex_base_type *bt = UPPER_OBJECT(BAVL_GetFirst(&o->base_types_tree), struct NCDModuleIndex_base_type, base_types_tree_node);
  91. BAVL_Remove(&o->base_types_tree, &bt->base_types_tree_node);
  92. free(bt);
  93. }
  94. // free modules
  95. while (!BAVL_IsEmpty(&o->modules_tree)) {
  96. struct NCDModuleIndex_module *m = UPPER_OBJECT(BAVL_GetFirst(&o->modules_tree), struct NCDModuleIndex_module, modules_tree_node);
  97. BAVL_Remove(&o->modules_tree, &m->modules_tree_node);
  98. free(m);
  99. }
  100. }
  101. int NCDModuleIndex_AddGroup (NCDModuleIndex *o, const struct NCDModuleGroup *group)
  102. {
  103. DebugObject_Access(&o->d_obj);
  104. for (const struct NCDModule *nm = group->modules; nm->type; nm++) {
  105. if (find_module(o, nm->type)) {
  106. BLog(BLOG_ERROR, "module type '%s' already exists", nm->type);
  107. goto loop_fail0;
  108. }
  109. if (strlen(nm->type) > NCDMODULEINDEX_MAX_TYPE_LEN) {
  110. BLog(BLOG_ERROR, "module type '%s' is too long", nm->type);
  111. goto loop_fail0;
  112. }
  113. struct NCDModuleIndex_module *m = malloc(sizeof(*m));
  114. if (!m) {
  115. BLog(BLOG_ERROR, "malloc failed");
  116. goto loop_fail0;
  117. }
  118. strcpy(m->type, nm->type);
  119. m->module = nm;
  120. ASSERT_EXECUTE(BAVL_Insert(&o->modules_tree, &m->modules_tree_node, NULL))
  121. const char *base_type = (nm->base_type ? nm->base_type : nm->type);
  122. struct NCDModuleIndex_base_type *bt = find_base_type(o, base_type);
  123. if (bt) {
  124. if (bt->group != group) {
  125. BLog(BLOG_ERROR, "module base type '%s' already exists in another module group", base_type);
  126. goto loop_fail1;
  127. }
  128. } else {
  129. if (!(bt = malloc(sizeof(*bt)))) {
  130. BLog(BLOG_ERROR, "malloc failed");
  131. goto loop_fail1;
  132. }
  133. bt->base_type = base_type;
  134. bt->group = group;
  135. ASSERT_EXECUTE(BAVL_Insert(&o->base_types_tree, &bt->base_types_tree_node, NULL))
  136. }
  137. continue;
  138. loop_fail1:
  139. BAVL_Remove(&o->modules_tree, &m->modules_tree_node);
  140. free(m);
  141. loop_fail0:
  142. return 0;
  143. }
  144. return 1;
  145. }
  146. const struct NCDModule * NCDModuleIndex_FindModule (NCDModuleIndex *o, const char *type)
  147. {
  148. DebugObject_Access(&o->d_obj);
  149. struct NCDModuleIndex_module *m = find_module(o, type);
  150. if (!m) {
  151. return NULL;
  152. }
  153. return m->module;
  154. }