template.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. if (_value)
  47. _var->value = strdup(_value);
  48. else
  49. _var->value = NULL;
  50. }
  51. void
  52. tmpl_var_setn(struct tmpl_var *_var, const char *_value, size_t _len)
  53. {
  54. free(_var->value);
  55. _var->value = strndup(_value, _len);
  56. }
  57. struct tmpl_data *
  58. tmpl_data_new(void)
  59. {
  60. struct tmpl_data *data;
  61. data = malloc(sizeof(struct tmpl_data));
  62. if (NULL == data)
  63. err(1, NULL);
  64. TAILQ_INIT(&data->variables);
  65. TAILQ_INIT(&data->loops);
  66. return data;
  67. }
  68. void
  69. tmpl_data_free(struct tmpl_data *_data)
  70. {
  71. struct tmpl_var *var;
  72. struct tmpl_loop *loop;
  73. if (_data == NULL)
  74. return;
  75. while ((var = TAILQ_FIRST(&_data->variables))) {
  76. TAILQ_REMOVE(&_data->variables, var, entry);
  77. tmpl_var_free(var);
  78. }
  79. while ((loop = TAILQ_FIRST(&_data->loops))) {
  80. TAILQ_REMOVE(&_data->loops, loop, entry);
  81. tmpl_loop_free(loop);
  82. }
  83. free(_data);
  84. }
  85. struct tmpl_var *
  86. tmpl_data_get_variable(struct tmpl_data *_data, const char *_name)
  87. {
  88. struct tmpl_var *var;
  89. TAILQ_FOREACH(var, &_data->variables, entry) {
  90. if (strcmp(_name, var->name.name) == 0)
  91. return var;
  92. }
  93. return NULL;
  94. }
  95. void
  96. tmpl_data_set_variable(struct tmpl_data *_data, const char *_name,
  97. const char *_value)
  98. {
  99. struct tmpl_var *var = tmpl_data_get_variable(_data, _name);
  100. if (var == NULL) {
  101. var = tmpl_var_new(_name);
  102. TAILQ_INSERT_TAIL(&_data->variables, var, entry);
  103. }
  104. tmpl_var_set(var, _value);
  105. }
  106. void
  107. tmpl_data_move_variable(struct tmpl_data *_data, const char *_name,
  108. char *_value)
  109. {
  110. struct tmpl_var *var = tmpl_data_get_variable(_data, _name);
  111. if (var == NULL) {
  112. var = tmpl_var_new(_name);
  113. TAILQ_INSERT_TAIL(&_data->variables, var, entry);
  114. } else {
  115. free(var->value);
  116. }
  117. var->value = _value;
  118. }
  119. void
  120. tmpl_data_set_variablen(struct tmpl_data *_data, const char *_name,
  121. const char *_value, size_t _len)
  122. {
  123. struct tmpl_var *var = tmpl_data_get_variable(_data, _name);
  124. if (var == NULL) {
  125. var = tmpl_var_new(_name);
  126. TAILQ_INSERT_TAIL(&_data->variables, var, entry);
  127. }
  128. tmpl_var_setn(var, _value, _len);
  129. }
  130. struct tmpl_loop *
  131. tmpl_data_get_loop(struct tmpl_data *_data, const char *_name)
  132. {
  133. struct tmpl_loop *loop;
  134. TAILQ_FOREACH(loop, &_data->loops, entry) {
  135. if (strcmp(_name, loop->name.name) == 0)
  136. return loop;
  137. }
  138. return NULL;
  139. }
  140. struct tmpl_loop *
  141. tmpl_data_add_loop(struct tmpl_data *_data, const char *_name)
  142. {
  143. struct tmpl_loop *loop = tmpl_data_get_loop(_data, _name);
  144. if (loop)
  145. return loop;
  146. loop = tmpl_loop_new(_name);
  147. TAILQ_INSERT_TAIL(&_data->loops, loop, entry);
  148. return loop;
  149. }
  150. void
  151. tmpl_data_set_loop(struct tmpl_data *_data, const char *_name,
  152. struct tmpl_loop *_loop)
  153. {
  154. struct tmpl_loop *loop = tmpl_data_get_loop(_data, _name);
  155. if (loop) {
  156. TAILQ_REMOVE(&_data->loops, loop, entry);
  157. tmpl_loop_free(loop);
  158. }
  159. TAILQ_INSERT_TAIL(&_data->loops, _loop, entry);
  160. }
  161. void
  162. tmpl_loop_free(struct tmpl_loop *_loop)
  163. {
  164. struct tmpl_data *data;
  165. while ((data = TAILQ_FIRST(&_loop->data))) {
  166. TAILQ_REMOVE(&_loop->data, data, entry);
  167. tmpl_data_free(data);
  168. }
  169. tmpl_name_free(&_loop->name);
  170. free(_loop);
  171. }
  172. struct tmpl_loop *
  173. tmpl_loop_new(const char *_name)
  174. {
  175. struct tmpl_loop *loop = malloc(sizeof(struct tmpl_loop));
  176. if (NULL == loop)
  177. err(1, NULL);
  178. loop->name.name = strdup(_name);
  179. TAILQ_INIT(&loop->data);
  180. return loop;
  181. }
  182. bool
  183. tmpl_loop_isempty(struct tmpl_loop *_loop)
  184. {
  185. return !(_loop && TAILQ_FIRST(&_loop->data));
  186. }
  187. void
  188. tmpl_loop_add_data(struct tmpl_loop *_loop, struct tmpl_data *_data)
  189. {
  190. TAILQ_INSERT_TAIL(&_loop->data, _data, entry);
  191. }