123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- #include "private.h"
- #include "lub/argv.h"
- #include "lub/string.h"
- #include "lub/ctype.h"
- #include <assert.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- int cliconf_bt_compare(const void *clientnode, const void *clientkey)
- {
- const cliconf_t *this = clientnode;
- unsigned short *pri = (unsigned short *)clientkey;
- char *line = ((char *)clientkey + sizeof(unsigned short));
- if (cliconf__get_priority(this) == *pri)
- return lub_string_nocasecmp(this->line, line);
- return (cliconf__get_priority(this) - *pri);
- }
- static void cliconf_key(lub_bintree_key_t * key,
- unsigned short priority, const char *text)
- {
- unsigned short *pri = (unsigned short *)key;
- char *line = ((char *)key + sizeof(unsigned short));
-
- *pri = priority;
- strcpy(line, text);
- }
- void cliconf_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
- {
- const cliconf_t *this = clientnode;
- cliconf_key(key, cliconf__get_priority(this), this->line);
- }
- static void
- cliconf_init(cliconf_t * this, const char *line, unsigned short priority)
- {
-
- this->line = lub_string_dup(line);
- this->priority = priority;
- this->splitter = BOOL_TRUE;
-
- lub_bintree_node_init(&this->bt_node);
-
- lub_bintree_init(&this->tree,
- cliconf_bt_offset(),
- cliconf_bt_compare, cliconf_bt_getkey);
- }
- static void cliconf_fini(cliconf_t * this)
- {
- cliconf_t *conf;
-
- while ((conf = lub_bintree_findfirst(&this->tree))) {
-
- lub_bintree_remove(&this->tree, conf);
-
- cliconf_delete(conf);
- }
-
- lub_string_free(this->line);
- this->line = NULL;
- }
- size_t cliconf_bt_offset(void)
- {
- return offsetof(cliconf_t, bt_node);
- }
- cliconf_t *cliconf_new(const char *line, unsigned short priority)
- {
- cliconf_t *this = malloc(sizeof(cliconf_t));
- if (this) {
- cliconf_init(this, line, priority);
- }
- return this;
- }
- void cliconf_delete(cliconf_t * this)
- {
- cliconf_fini(this);
- free(this);
- }
- void cliconf_fprintf(cliconf_t * this, FILE * stream,
- const char *pattern, int depth,
- unsigned char prev_pri_hi)
- {
- cliconf_t *conf;
- lub_bintree_iterator_t iter;
- unsigned char pri = 0;
- if (this->line && *(this->line) != '\0') {
- char *space = NULL;
- if (depth > 0) {
- space = malloc(depth + 1);
- memset(space, ' ', depth);
- space[depth] = '\0';
- }
- if ((0 == depth) &&
- (this->splitter ||
- (cliconf__get_priority_hi(this) != prev_pri_hi)))
- fprintf(stream, "!\n");
- fprintf(stream, "%s%s\n", space ? space : "", this->line);
- free(space);
- }
-
- if (!(conf = lub_bintree_findfirst(&this->tree)))
- return;
- for(lub_bintree_iterator_init(&iter, &this->tree, conf);
- conf; conf = lub_bintree_iterator_next(&iter)) {
- if (pattern &&
- (lub_string_nocasestr(conf->line, pattern) != conf->line))
- continue;
- cliconf_fprintf(conf, stream, NULL, depth + 1, pri);
- pri = cliconf__get_priority_hi(conf);
- }
- }
- cliconf_t *cliconf_new_conf(cliconf_t * this,
- const char *line, unsigned short priority)
- {
-
- cliconf_t *conf = cliconf_new(line, priority);
- assert(conf);
-
- if (-1 == lub_bintree_insert(&this->tree, conf)) {
-
- cliconf_delete(conf);
- conf = NULL;
- }
- return conf;
- }
- cliconf_t *cliconf_find_conf(cliconf_t * this,
- const char *line, unsigned short priority)
- {
- cliconf_t *conf;
- lub_bintree_key_t key;
- lub_bintree_iterator_t iter;
- if (0 != priority) {
- cliconf_key(&key, priority, line);
- return lub_bintree_find(&this->tree, &key);
- }
-
- if (!(conf = lub_bintree_findfirst(&this->tree)))
- return NULL;
-
- lub_bintree_iterator_init(&iter, &this->tree, conf);
- do {
- if (0 == lub_string_nocasecmp(conf->line, line))
- return conf;
- } while ((conf = lub_bintree_iterator_next(&iter)));
- return NULL;
- }
- void cliconf_del_pattern(cliconf_t *this,
- const char *pattern)
- {
- cliconf_t *conf;
- lub_bintree_iterator_t iter;
-
- if (!(conf = lub_bintree_findfirst(&this->tree)))
- return;
- lub_bintree_iterator_init(&iter, &this->tree, conf);
- do {
- if (lub_string_nocasestr(conf->line, pattern) == conf->line) {
- lub_bintree_remove(&this->tree, conf);
- cliconf_delete(conf);
- }
- } while ((conf = lub_bintree_iterator_next(&iter)));
- }
- unsigned short cliconf__get_priority(const cliconf_t * this)
- {
- return this->priority;
- }
- unsigned char cliconf__get_priority_hi(const cliconf_t * this)
- {
- return (unsigned char)(this->priority >> 8);
- }
- unsigned char cliconf__get_priority_lo(const cliconf_t * this)
- {
- return (unsigned char)(this->priority & 0xff);
- }
- bool_t cliconf__get_splitter(const cliconf_t * this)
- {
- return this->splitter;
- }
- void cliconf__set_splitter(cliconf_t *this, bool_t splitter)
- {
- this->splitter = splitter;
- }
|