NCDModuleIndex.c 5.2 KB

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