tree.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * tree.c
  3. *
  4. * This file provides the implementation of a konf_tree class
  5. */
  6. #include "private.h"
  7. #include "lub/argv.h"
  8. #include "lub/string.h"
  9. #include "lub/ctype.h"
  10. #include <assert.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <sys/types.h>
  15. #include <regex.h>
  16. /*---------------------------------------------------------
  17. * PRIVATE META FUNCTIONS
  18. *--------------------------------------------------------- */
  19. int konf_tree_bt_compare(const void *clientnode, const void *clientkey)
  20. {
  21. const konf_tree_t *this = clientnode;
  22. unsigned short *pri = (unsigned short *)clientkey;
  23. char *line = ((char *)clientkey + sizeof(unsigned short));
  24. if (konf_tree__get_priority(this) == *pri)
  25. return lub_string_nocasecmp(this->line, line);
  26. return (konf_tree__get_priority(this) - *pri);
  27. }
  28. /*-------------------------------------------------------- */
  29. static void konf_tree_key(lub_bintree_key_t * key,
  30. unsigned short priority, const char *text)
  31. {
  32. unsigned short *pri = (unsigned short *)key;
  33. char *line = ((char *)key + sizeof(unsigned short));
  34. /* fill out the opaque key */
  35. *pri = priority;
  36. strcpy(line, text);
  37. }
  38. /*-------------------------------------------------------- */
  39. void konf_tree_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  40. {
  41. const konf_tree_t *this = clientnode;
  42. konf_tree_key(key, konf_tree__get_priority(this), this->line);
  43. }
  44. /*---------------------------------------------------------
  45. * PRIVATE METHODS
  46. *--------------------------------------------------------- */
  47. static void
  48. konf_tree_init(konf_tree_t * this, const char *line, unsigned short priority)
  49. {
  50. /* set up defaults */
  51. this->line = lub_string_dup(line);
  52. this->priority = priority;
  53. this->splitter = BOOL_TRUE;
  54. /* Be a good binary tree citizen */
  55. lub_bintree_node_init(&this->bt_node);
  56. /* initialise the tree of commands for this conf */
  57. lub_bintree_init(&this->tree,
  58. konf_tree_bt_offset(),
  59. konf_tree_bt_compare, konf_tree_bt_getkey);
  60. }
  61. /*--------------------------------------------------------- */
  62. static void konf_tree_fini(konf_tree_t * this)
  63. {
  64. konf_tree_t *conf;
  65. /* delete each conf held by this conf */
  66. while ((conf = lub_bintree_findfirst(&this->tree))) {
  67. /* remove the conf from the tree */
  68. lub_bintree_remove(&this->tree, conf);
  69. /* release the instance */
  70. konf_tree_delete(conf);
  71. }
  72. /* free our memory */
  73. lub_string_free(this->line);
  74. this->line = NULL;
  75. }
  76. /*---------------------------------------------------------
  77. * PUBLIC META FUNCTIONS
  78. *--------------------------------------------------------- */
  79. size_t konf_tree_bt_offset(void)
  80. {
  81. return offsetof(konf_tree_t, bt_node);
  82. }
  83. /*--------------------------------------------------------- */
  84. konf_tree_t *konf_tree_new(const char *line, unsigned short priority)
  85. {
  86. konf_tree_t *this = malloc(sizeof(konf_tree_t));
  87. if (this) {
  88. konf_tree_init(this, line, priority);
  89. }
  90. return this;
  91. }
  92. /*---------------------------------------------------------
  93. * PUBLIC METHODS
  94. *--------------------------------------------------------- */
  95. void konf_tree_delete(konf_tree_t * this)
  96. {
  97. konf_tree_fini(this);
  98. free(this);
  99. }
  100. /*--------------------------------------------------------- */
  101. void konf_tree_fprintf(konf_tree_t * this, FILE * stream,
  102. const char *pattern, int depth,
  103. unsigned char prev_pri_hi)
  104. {
  105. konf_tree_t *conf;
  106. lub_bintree_iterator_t iter;
  107. unsigned char pri = 0;
  108. if (this->line && *(this->line) != '\0') {
  109. char *space = NULL;
  110. if (depth > 0) {
  111. space = malloc(depth + 1);
  112. memset(space, ' ', depth);
  113. space[depth] = '\0';
  114. }
  115. if ((0 == depth) &&
  116. (this->splitter ||
  117. (konf_tree__get_priority_hi(this) != prev_pri_hi)))
  118. fprintf(stream, "!\n");
  119. fprintf(stream, "%s%s\n", space ? space : "", this->line);
  120. free(space);
  121. }
  122. /* iterate child elements */
  123. if (!(conf = lub_bintree_findfirst(&this->tree)))
  124. return;
  125. for(lub_bintree_iterator_init(&iter, &this->tree, conf);
  126. conf; conf = lub_bintree_iterator_next(&iter)) {
  127. if (pattern &&
  128. (lub_string_nocasestr(conf->line, pattern) != conf->line))
  129. continue;
  130. konf_tree_fprintf(conf, stream, NULL, depth + 1, pri);
  131. pri = konf_tree__get_priority_hi(conf);
  132. }
  133. }
  134. /*--------------------------------------------------------- */
  135. konf_tree_t *konf_tree_new_conf(konf_tree_t * this,
  136. const char *line, unsigned short priority)
  137. {
  138. /* allocate the memory for a new child element */
  139. konf_tree_t *conf = konf_tree_new(line, priority);
  140. assert(conf);
  141. /* ...insert it into the binary tree for this conf */
  142. if (-1 == lub_bintree_insert(&this->tree, conf)) {
  143. /* inserting a duplicate command is bad */
  144. konf_tree_delete(conf);
  145. conf = NULL;
  146. }
  147. return conf;
  148. }
  149. /*--------------------------------------------------------- */
  150. konf_tree_t *konf_tree_find_conf(konf_tree_t * this,
  151. const char *line, unsigned short priority)
  152. {
  153. konf_tree_t *conf;
  154. lub_bintree_key_t key;
  155. lub_bintree_iterator_t iter;
  156. if (0 != priority) {
  157. konf_tree_key(&key, priority, line);
  158. return lub_bintree_find(&this->tree, &key);
  159. }
  160. /* If tree is empty */
  161. if (!(conf = lub_bintree_findfirst(&this->tree)))
  162. return NULL;
  163. /* Iterate non-empty tree */
  164. lub_bintree_iterator_init(&iter, &this->tree, conf);
  165. do {
  166. if (0 == lub_string_nocasecmp(conf->line, line))
  167. return conf;
  168. } while ((conf = lub_bintree_iterator_next(&iter)));
  169. return NULL;
  170. }
  171. /*--------------------------------------------------------- */
  172. void konf_tree_del_pattern(konf_tree_t *this,
  173. const char *pattern)
  174. {
  175. konf_tree_t *conf;
  176. lub_bintree_iterator_t iter;
  177. regex_t regexp;
  178. /* Is tree empty? */
  179. if (!(conf = lub_bintree_findfirst(&this->tree)))
  180. return;
  181. /* Compile regular expression */
  182. regcomp(&regexp, pattern, REG_EXTENDED | REG_ICASE);
  183. /* Iterate configuration tree */
  184. lub_bintree_iterator_init(&iter, &this->tree, conf);
  185. do {
  186. if (0 == regexec(&regexp, conf->line, 0, NULL, 0)) {
  187. lub_bintree_remove(&this->tree, conf);
  188. konf_tree_delete(conf);
  189. }
  190. } while ((conf = lub_bintree_iterator_next(&iter)));
  191. regfree(&regexp);
  192. }
  193. /*--------------------------------------------------------- */
  194. unsigned short konf_tree__get_priority(const konf_tree_t * this)
  195. {
  196. return this->priority;
  197. }
  198. /*--------------------------------------------------------- */
  199. unsigned char konf_tree__get_priority_hi(const konf_tree_t * this)
  200. {
  201. return (unsigned char)(this->priority >> 8);
  202. }
  203. /*--------------------------------------------------------- */
  204. unsigned char konf_tree__get_priority_lo(const konf_tree_t * this)
  205. {
  206. return (unsigned char)(this->priority & 0xff);
  207. }
  208. /*--------------------------------------------------------- */
  209. bool_t konf_tree__get_splitter(const konf_tree_t * this)
  210. {
  211. return this->splitter;
  212. }
  213. /*--------------------------------------------------------- */
  214. void konf_tree__set_splitter(konf_tree_t *this, bool_t splitter)
  215. {
  216. this->splitter = splitter;
  217. }