conf.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 cliconf_bt_compare(const void *clientnode, const void *clientkey)
  18. {
  19. const cliconf_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. cliconf__get_priority(this), this->line, *pri, line);
  24. */
  25. if (cliconf__get_priority(this) == *pri)
  26. return lub_string_nocasecmp(this->line, line);
  27. return (cliconf__get_priority(this) - *pri);
  28. }
  29. /*-------------------------------------------------------- */
  30. static void cliconf_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 cliconf_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  41. {
  42. const cliconf_t *this = clientnode;
  43. cliconf_key(key, cliconf__get_priority(this), this->line);
  44. }
  45. /*---------------------------------------------------------
  46. * PRIVATE METHODS
  47. *--------------------------------------------------------- */
  48. static void
  49. cliconf_init(cliconf_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. cliconf_bt_offset(),
  60. cliconf_bt_compare, cliconf_bt_getkey);
  61. }
  62. /*--------------------------------------------------------- */
  63. static void cliconf_fini(cliconf_t * this)
  64. {
  65. cliconf_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. cliconf_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 cliconf_bt_offset(void)
  81. {
  82. return offsetof(cliconf_t, bt_node);
  83. }
  84. /*--------------------------------------------------------- */
  85. cliconf_t *cliconf_new(const char *line, unsigned short priority)
  86. {
  87. cliconf_t *this = malloc(sizeof(cliconf_t));
  88. if (this) {
  89. cliconf_init(this, line, priority);
  90. }
  91. return this;
  92. }
  93. /*---------------------------------------------------------
  94. * PUBLIC METHODS
  95. *--------------------------------------------------------- */
  96. void cliconf_delete(cliconf_t * this)
  97. {
  98. cliconf_fini(this);
  99. free(this);
  100. }
  101. void cliconf_fprintf(cliconf_t * this, FILE * stream,
  102. const char *pattern, int depth,
  103. unsigned char prev_pri_hi)
  104. {
  105. cliconf_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. (cliconf__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. cliconf_fprintf(conf, stream, NULL, depth + 1, pri);
  131. pri = cliconf__get_priority_hi(conf);
  132. }
  133. }
  134. /*--------------------------------------------------------- */
  135. cliconf_t *cliconf_new_conf(cliconf_t * this,
  136. const char *line, unsigned short priority)
  137. {
  138. /* allocate the memory for a new child element */
  139. cliconf_t *conf = cliconf_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. cliconf_delete(conf);
  145. conf = NULL;
  146. }
  147. return conf;
  148. }
  149. /*--------------------------------------------------------- */
  150. cliconf_t *cliconf_find_conf(cliconf_t * this,
  151. const char *line, unsigned short priority)
  152. {
  153. cliconf_t *conf;
  154. lub_bintree_key_t key;
  155. lub_bintree_iterator_t iter;
  156. if (0 != priority) {
  157. cliconf_key(&key, priority, line);
  158. return lub_bintree_find(&this->tree, &key);
  159. }
  160. /* Empty tree */
  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. while ((conf = lub_bintree_iterator_next(&iter))) {
  166. if (0 == lub_string_nocasecmp(conf->line, line))
  167. return conf;
  168. }
  169. return NULL;
  170. }
  171. /*--------------------------------------------------------- */
  172. void cliconf_del_pattern(cliconf_t *this,
  173. const char *pattern)
  174. {
  175. cliconf_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. while ((conf = lub_bintree_iterator_next(&iter))) {
  182. if (lub_string_nocasestr(conf->line, pattern) == conf->line) {
  183. lub_bintree_remove(&this->tree, conf);
  184. cliconf_delete(conf);
  185. }
  186. }
  187. }
  188. /*--------------------------------------------------------- */
  189. unsigned short cliconf__get_priority(const cliconf_t * this)
  190. {
  191. return this->priority;
  192. }
  193. /*--------------------------------------------------------- */
  194. unsigned char cliconf__get_priority_hi(const cliconf_t * this)
  195. {
  196. return (unsigned char)(this->priority >> 8);
  197. }
  198. /*--------------------------------------------------------- */
  199. unsigned char cliconf__get_priority_lo(const cliconf_t * this)
  200. {
  201. return (unsigned char)(this->priority & 0xff);
  202. }
  203. /*--------------------------------------------------------- */
  204. bool_t cliconf__get_splitter(const cliconf_t * this)
  205. {
  206. return this->splitter;
  207. }
  208. /*--------------------------------------------------------- */
  209. void cliconf__set_splitter(cliconf_t *this, bool_t splitter)
  210. {
  211. this->splitter = splitter;
  212. }