NCDModuleIndex.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 struct NCDModuleIndex_module * find_module (NCDModuleIndex *o, const char *type)
  40. {
  41. BAVLNode *node = BAVL_LookupExact(&o->modules_tree, (void *)type);
  42. if (!node) {
  43. return NULL;
  44. }
  45. struct NCDModuleIndex_module *m = UPPER_OBJECT(node, struct NCDModuleIndex_module, modules_tree_node);
  46. ASSERT(!strcmp(m->type, type))
  47. return m;
  48. }
  49. void NCDModuleIndex_Init (NCDModuleIndex *o)
  50. {
  51. // init modules tree
  52. BAVL_Init(&o->modules_tree, OFFSET_DIFF(struct NCDModuleIndex_module, type, modules_tree_node), (BAVL_comparator)string_comparator, NULL);
  53. DebugObject_Init(&o->d_obj);
  54. }
  55. void NCDModuleIndex_Free (NCDModuleIndex *o)
  56. {
  57. DebugObject_Free(&o->d_obj);
  58. while (!BAVL_IsEmpty(&o->modules_tree)) {
  59. struct NCDModuleIndex_module *m = UPPER_OBJECT(BAVL_GetFirst(&o->modules_tree), struct NCDModuleIndex_module, modules_tree_node);
  60. BAVL_Remove(&o->modules_tree, &m->modules_tree_node);
  61. free(m);
  62. }
  63. }
  64. int NCDModuleIndex_AddGroup (NCDModuleIndex *o, const struct NCDModuleGroup *group)
  65. {
  66. DebugObject_Access(&o->d_obj);
  67. for (const struct NCDModule *nm = group->modules; nm->type; nm++) {
  68. if (find_module(o, nm->type)) {
  69. BLog(BLOG_ERROR, "module type '%s' already exists", nm->type);
  70. return 0;
  71. }
  72. if (strlen(nm->type) > NCDMODULEINDEX_MAX_TYPE_LEN) {
  73. BLog(BLOG_ERROR, "module type '%s' is too long", nm->type);
  74. return 0;
  75. }
  76. struct NCDModuleIndex_module *m = malloc(sizeof(*m));
  77. if (!m) {
  78. BLog(BLOG_ERROR, "malloc failed");
  79. return 0;
  80. }
  81. strcpy(m->type, nm->type);
  82. m->module = nm;
  83. ASSERT_EXECUTE(BAVL_Insert(&o->modules_tree, &m->modules_tree_node, NULL))
  84. }
  85. return 1;
  86. }
  87. const struct NCDModule * NCDModuleIndex_FindModule (NCDModuleIndex *o, const char *type)
  88. {
  89. DebugObject_Access(&o->d_obj);
  90. struct NCDModuleIndex_module *m = find_module(o, type);
  91. if (!m) {
  92. return NULL;
  93. }
  94. return m->module;
  95. }