template.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2018 Markus Hennecke <markus-hennecke@markus-hennecke.de>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <err.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "template.h"
  20. void
  21. tmpl_name_free(struct tmpl_name *_entry)
  22. {
  23. free(_entry->name);
  24. }
  25. void
  26. tmpl_var_free(struct tmpl_var *_var)
  27. {
  28. tmpl_name_free(&_var->name);
  29. free(_var->value);
  30. free(_var);
  31. }
  32. struct tmpl_var *
  33. tmpl_var_new(const char *_name)
  34. {
  35. struct tmpl_var *var = malloc(sizeof(struct tmpl_var));
  36. if (NULL == var)
  37. err(1, NULL);
  38. var->name.name = strdup(_name);
  39. var->value = NULL;
  40. return var;
  41. }
  42. void
  43. tmpl_var_set(struct tmpl_var *_var, const char *_value)
  44. {
  45. free(_var->value);
  46. _var->value = strdup(_value);
  47. }
  48. struct tmpl_data *
  49. tmpl_data_new(void)
  50. {
  51. struct tmpl_data *data;
  52. data = malloc(sizeof(struct tmpl_data));
  53. if (NULL == data)
  54. err(1, NULL);
  55. TAILQ_INIT(&data->variables);
  56. TAILQ_INIT(&data->loops);
  57. return data;
  58. }
  59. void
  60. tmpl_data_free(struct tmpl_data *_data)
  61. {
  62. struct tmpl_var *var;
  63. struct tmpl_loop *loop;
  64. while ((var = TAILQ_FIRST(&_data->variables))) {
  65. TAILQ_REMOVE(&_data->variables, var, entry);
  66. tmpl_var_free(var);
  67. }
  68. while ((loop = TAILQ_FIRST(&_data->loops))) {
  69. TAILQ_REMOVE(&_data->loops, loop, entry);
  70. tmpl_loop_free(loop);
  71. }
  72. free(_data);
  73. }
  74. struct tmpl_var *
  75. tmpl_data_get_variable(struct tmpl_data *_data, const char *_name)
  76. {
  77. struct tmpl_var *var;
  78. TAILQ_FOREACH(var, &_data->variables, entry) {
  79. if (strcmp(_name, var->name.name) == 0)
  80. return var;
  81. }
  82. return NULL;
  83. }
  84. void
  85. tmpl_data_set_variable(struct tmpl_data *_data, const char *_name,
  86. const char *_value)
  87. {
  88. struct tmpl_var *var = tmpl_data_get_variable(_data, _name);
  89. if (var == NULL) {
  90. var = tmpl_var_new(_name);
  91. TAILQ_INSERT_TAIL(&_data->variables, var, entry);
  92. }
  93. tmpl_var_set(var, _value);
  94. }
  95. struct tmpl_loop *
  96. tmpl_data_get_loop(struct tmpl_data *_data, const char *_name)
  97. {
  98. struct tmpl_loop *loop;
  99. TAILQ_FOREACH(loop, &_data->loops, entry) {
  100. if (strcmp(_name, loop->name.name) == 0)
  101. return loop;
  102. }
  103. return NULL;
  104. }
  105. struct tmpl_loop *
  106. tmpl_data_add_loop(struct tmpl_data *_data, const char *_name)
  107. {
  108. struct tmpl_loop *loop = tmpl_data_get_loop(_data, _name);
  109. if (loop)
  110. return loop;
  111. loop = tmpl_loop_new(_name);
  112. TAILQ_INSERT_TAIL(&_data->loops, loop, entry);
  113. return loop;
  114. }
  115. void
  116. tmpl_loop_free(struct tmpl_loop *_loop)
  117. {
  118. struct tmpl_data *data;
  119. while ((data = TAILQ_FIRST(&_loop->data))) {
  120. TAILQ_REMOVE(&_loop->data, data, entry);
  121. tmpl_data_free(data);
  122. }
  123. tmpl_name_free(&_loop->name);
  124. free(_loop);
  125. }
  126. struct tmpl_loop *
  127. tmpl_loop_new(const char *_name)
  128. {
  129. struct tmpl_loop *loop = malloc(sizeof(struct tmpl_loop));
  130. if (NULL == loop)
  131. err(1, NULL);
  132. loop->name.name = strdup(_name);
  133. TAILQ_INIT(&loop->data);
  134. return loop;
  135. }
  136. bool
  137. tmpl_loop_isempty(struct tmpl_loop *_loop)
  138. {
  139. return !(_loop && TAILQ_FIRST(&_loop->data));
  140. }
  141. void
  142. tmpl_loop_add_data(struct tmpl_loop *_loop, struct tmpl_data *_data)
  143. {
  144. TAILQ_INSERT_TAIL(&_loop->data, _data, entry);
  145. }