tree.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * conf.c
  3. *
  4. * This file provides the implementation of a conf 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. /*---------------------------------------------------------
  15. * PRIVATE META FUNCTIONS
  16. *--------------------------------------------------------- */
  17. int konf_tree_bt_compare(const void *clientnode, const void *clientkey)
  18. {
  19. const konf_tree_t *this = clientnode;
  20. unsigned short *pri = (unsigned short *)clientkey;
  21. char *line = ((char *)clientkey + sizeof(unsigned short));
  22. /* printf("COMPARE: node_pri=%d node_line=[%s] key_pri=%d key_line=[%s]\n",
  23. konf_tree__get_priority(this), this->line, *pri, line);
  24. */
  25. if (konf_tree__get_priority(this) == *pri)
  26. return lub_string_nocasecmp(this->line, line);
  27. return (konf_tree__get_priority(this) - *pri);
  28. }
  29. /*-------------------------------------------------------- */
  30. static void konf_tree_key(lub_bintree_key_t * key,
  31. unsigned short priority, const char *text)
  32. {
  33. unsigned short *pri = (unsigned short *)key;
  34. char *line = ((char *)key + sizeof(unsigned short));
  35. /* fill out the opaque key */
  36. *pri = priority;
  37. strcpy(line, text);
  38. }
  39. /*-------------------------------------------------------- */
  40. void konf_tree_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  41. {
  42. const konf_tree_t *this = clientnode;
  43. konf_tree_key(key, konf_tree__get_priority(this), this->line);
  44. }
  45. /*---------------------------------------------------------
  46. * PRIVATE METHODS
  47. *--------------------------------------------------------- */
  48. static void
  49. konf_tree_init(konf_tree_t * this, const char *line, unsigned short priority)
  50. {
  51. /* set up defaults */
  52. this->line = lub_string_dup(line);
  53. this->priority = priority;
  54. this->splitter = BOOL_TRUE;
  55. /* Be a good binary tree citizen */
  56. lub_bintree_node_init(&this->bt_node);
  57. /* initialise the tree of commands for this conf */
  58. lub_bintree_init(&this->tree,
  59. konf_tree_bt_offset(),
  60. konf_tree_bt_compare, konf_tree_bt_getkey);
  61. }
  62. /*--------------------------------------------------------- */
  63. static void konf_tree_fini(konf_tree_t * this)
  64. {
  65. konf_tree_t *conf;
  66. /* delete each conf held by this conf */
  67. while ((conf = lub_bintree_findfirst(&this->tree))) {
  68. /* remove the conf from the tree */
  69. lub_bintree_remove(&this->tree, conf);
  70. /* release the instance */
  71. konf_tree_delete(conf);
  72. }
  73. /* free our memory */
  74. lub_string_free(this->line);
  75. this->line = NULL;
  76. }
  77. /*---------------------------------------------------------
  78. * PUBLIC META FUNCTIONS
  79. *--------------------------------------------------------- */
  80. size_t konf_tree_bt_offset(void)
  81. {
  82. return offsetof(konf_tree_t, bt_node);
  83. }
  84. /*--------------------------------------------------------- */
  85. konf_tree_t *konf_tree_new(const char *line, unsigned short priority)
  86. {
  87. konf_tree_t *this = malloc(sizeof(konf_tree_t));
  88. if (this) {
  89. konf_tree_init(this, line, priority);
  90. }
  91. return this;
  92. }
  93. /*---------------------------------------------------------
  94. * PUBLIC METHODS
  95. *--------------------------------------------------------- */
  96. void konf_tree_delete(konf_tree_t * this)
  97. {
  98. konf_tree_fini(this);
  99. free(this);
  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. /* Empty tree */
  178. if (!(conf = lub_bintree_findfirst(&this->tree)))
  179. return;
  180. lub_bintree_iterator_init(&iter, &this->tree, conf);
  181. do {
  182. if (lub_string_nocasestr(conf->line, pattern) == conf->line) {
  183. lub_bintree_remove(&this->tree, conf);
  184. konf_tree_delete(conf);
  185. }
  186. } while ((conf = lub_bintree_iterator_next(&iter)));
  187. }
  188. /*--------------------------------------------------------- */
  189. unsigned short konf_tree__get_priority(const konf_tree_t * this)
  190. {
  191. return this->priority;
  192. }
  193. /*--------------------------------------------------------- */
  194. unsigned char konf_tree__get_priority_hi(const konf_tree_t * this)
  195. {
  196. return (unsigned char)(this->priority >> 8);
  197. }
  198. /*--------------------------------------------------------- */
  199. unsigned char konf_tree__get_priority_lo(const konf_tree_t * this)
  200. {
  201. return (unsigned char)(this->priority & 0xff);
  202. }
  203. /*--------------------------------------------------------- */
  204. bool_t konf_tree__get_splitter(const konf_tree_t * this)
  205. {
  206. return this->splitter;
  207. }
  208. /*--------------------------------------------------------- */
  209. void konf_tree__set_splitter(konf_tree_t *this, bool_t splitter)
  210. {
  211. this->splitter = splitter;
  212. }