NCDConfig.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * @file NCDConfig.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 <ncdconfig/NCDConfig.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. void NCDConfig_free_interfaces (struct NCDConfig_interfaces *v)
  26. {
  27. if (!v) {
  28. return;
  29. }
  30. free(v->name);
  31. NCDConfig_free_statements(v->statements);
  32. NCDConfig_free_interfaces(v->next);
  33. free(v);
  34. }
  35. void NCDConfig_free_statements (struct NCDConfig_statements *v)
  36. {
  37. if (!v) {
  38. return;
  39. }
  40. NCDConfig_free_strings(v->names);
  41. NCDConfig_free_strings(v->args);
  42. NCDConfig_free_statements(v->next);
  43. free(v);
  44. }
  45. void NCDConfig_free_strings (struct NCDConfig_strings *v)
  46. {
  47. if (!v) {
  48. return;
  49. }
  50. free(v->value);
  51. NCDConfig_free_strings(v->next);
  52. free(v);
  53. }
  54. struct NCDConfig_interfaces * NCDConfig_make_interfaces (char *name, struct NCDConfig_statements *statements, int need_next, struct NCDConfig_interfaces *next)
  55. {
  56. if (!name || !statements || (need_next && !next)) {
  57. goto fail;
  58. }
  59. struct NCDConfig_interfaces *v = malloc(sizeof(*v));
  60. if (!v) {
  61. goto fail;
  62. }
  63. v->name = name;
  64. v->statements = statements;
  65. v->next = next;
  66. return v;
  67. fail:
  68. free(name);
  69. NCDConfig_free_statements(statements);
  70. NCDConfig_free_interfaces(next);
  71. return NULL;
  72. }
  73. struct NCDConfig_statements * NCDConfig_make_statements (struct NCDConfig_strings *names, int need_args, struct NCDConfig_strings *args, int need_next, struct NCDConfig_statements *next)
  74. {
  75. if (!names || (need_args && !args) || (need_next && !next)) {
  76. goto fail;
  77. }
  78. struct NCDConfig_statements *v = malloc(sizeof(*v));
  79. if (!v) {
  80. goto fail;
  81. }
  82. v->names = names;
  83. v->args = args;
  84. v->next = next;
  85. return v;
  86. fail:
  87. NCDConfig_free_strings(names);
  88. NCDConfig_free_strings(args);
  89. NCDConfig_free_statements(next);
  90. return NULL;
  91. }
  92. struct NCDConfig_strings * NCDConfig_make_strings (char *value, int need_next, struct NCDConfig_strings *next)
  93. {
  94. if (!value || (need_next && !next)) {
  95. goto fail;
  96. }
  97. struct NCDConfig_strings *v = malloc(sizeof(*v));
  98. if (!v) {
  99. goto fail;
  100. }
  101. v->value = value;
  102. v->next = next;
  103. return v;
  104. fail:
  105. free(value);
  106. NCDConfig_free_strings(next);
  107. return NULL;
  108. }