ACL_TOKEN support acl_foreach

This commit is contained in:
zhengshuxin 2017-06-05 12:21:02 +08:00
parent 6bfdac5843
commit 3612489a81
4 changed files with 86 additions and 4 deletions

View File

@ -1,6 +1,9 @@
修改历史列表:
------------------------------------------------------------------------
590) 2017.6.4
590.1) feature: ACL_TOKEN 对象增加针对 acl_foreach 的支持,可以遍历所有的关键词
589) 2017.6.3
589.1) feature: acl_token_tree.c 增加 acl_token_tree_word_remove 方法

View File

@ -6,6 +6,7 @@ extern "C" {
#endif
#include "acl_define.h"
#include "acl_vstring.h"
#include "acl_iterator.h"
#define ACL_PRINT_CHAR(x) \
((((x) >= 'a' && (x) <='z') \
@ -25,7 +26,9 @@ extern "C" {
|| (x) == '\'' || (x) == '"') \
? (x) : '-')
typedef struct ACL_TOKEN {
typedef struct ACL_TOKEN ACL_TOKEN;
struct ACL_TOKEN {
unsigned char ch;
unsigned int flag;
#define ACL_TOKEN_F_NONE 0
@ -34,11 +37,14 @@ typedef struct ACL_TOKEN {
#define ACL_TOKEN_F_DENY (1 << 2)
#define ACL_TOKEN_F_UTF8 (1 << 3)
#define ACL_TOKEN_WIDTH 255
#define ACL_TOKEN_WIDTH 256
struct ACL_TOKEN *tokens[ACL_TOKEN_WIDTH];
struct ACL_TOKEN *parent;
void *ctx;
} ACL_TOKEN;
ACL_TOKEN *(*iter_head)(ACL_ITER*, ACL_TOKEN*);
ACL_TOKEN *(*iter_next)(ACL_ITER*, ACL_TOKEN*);
};
#define ACL_TOKEN_TREE_WORD_MATCH(acl_token_tree_in, word_in, acl_token_out) \
{ \

View File

@ -113,7 +113,7 @@ struct ACL_XML {
unsigned flag; /**< 标志位: ACL_XML_FLAG_xxx */
/**< 是否允许一个 xml 文档中有多个根节点,内部缺省为允许 */
#define ACL_XML_FLAG_MULTI_ROOT (1 << 0)
#define ACL_XML_FLAG_MULTI_ROOT (1 << 0)
/**< 是否兼容单节点中没有 '/' 情况 */
#define ACL_XML_FLAG_IGNORE_SLASH (1 << 1)

View File

@ -42,6 +42,76 @@ void acl_token_delim_tab_free(char *delim_tab)
acl_myfree(delim_tab);
}
static ACL_TOKEN *iter_next(ACL_ITER *it, ACL_TOKEN *token);
static ACL_TOKEN *iter_head(ACL_ITER *it, ACL_TOKEN *token)
{
it->dlen = -1;
it->key = NULL;
it->klen = -1;
it->i = 0;
it->size = 0;
it->ptr = token;
assert(token->parent == NULL);
return iter_next(it, token);
}
static ACL_TOKEN *next_token(ACL_ITER *it, ACL_TOKEN *token)
{
ACL_TOKEN *parent;
unsigned i;
assert(token);
/* lookup the first left no null child of the current token */
for (i = 0; i < ACL_TOKEN_WIDTH; i++) {
if (token->tokens[i] != NULL) {
it->i = i;
it->ptr = token->tokens[i];
it->data = token->tokens[i];
return token->tokens[i];
}
}
/* lookup the right no null brother of the current token */
i = token->ch + 1;
parent = token->parent;
while (parent != NULL) {
for (; i < ACL_TOKEN_WIDTH; i++) {
if (parent->tokens[i] != NULL) {
it->i = i;
it->ptr = parent->tokens[i];
it->data = parent->tokens[i];
return parent->tokens[i];
}
}
i = parent->ch + 1;
parent = parent->parent;
}
it->ptr = it->data = NULL;
it->i = 0;
return NULL;
}
static ACL_TOKEN *iter_next(ACL_ITER *it, ACL_TOKEN *token acl_unused)
{
ACL_TOKEN *curr = (ACL_TOKEN *) it->ptr;
while (1) {
curr = next_token(it, curr);
if (curr == NULL)
return NULL;
if (curr->flag & ACL_TOKEN_F_STOP)
return curr;
}
}
ACL_TOKEN *acl_token_new(void)
{
ACL_TOKEN *token = (ACL_TOKEN*) acl_mycalloc(1, sizeof(ACL_TOKEN));
@ -51,6 +121,8 @@ ACL_TOKEN *acl_token_new(void)
token->tokens[i] = NULL;
token->ch = '-';
token->iter_head = iter_head;
token->iter_next = iter_next;
return token;
}
@ -224,6 +296,7 @@ void acl_token_tree_word_remove(ACL_TOKEN *tree, const char *word)
if (token == NULL)
return;
token->ctx = NULL;
for (i = 0; i < ACL_TOKEN_WIDTH; i++) {
if (token->tokens[i] != NULL) {