acl/lib_acl/samples/token_tree/main.c

178 lines
3.9 KiB
C
Raw Normal View History

2014-11-19 00:25:21 +08:00
#include "lib_acl.h"
#include <stdio.h>
#include <stdlib.h>
#define STR acl_vstring_str
static void token_tree_test(const char *tokens, const char *test_tab[])
{
ACL_TOKEN *token_tree;
ACL_TOKEN *token;
2014-11-19 00:25:21 +08:00
const char *ptr, *psaved;
ACL_VSTRING *buf = acl_vstring_alloc(256);
int i;
token_tree = acl_token_tree_create(tokens);
acl_token_tree_print(token_tree);
for (i = 0; test_tab[i] != NULL; i++) {
ptr = psaved = test_tab[i];
token = acl_token_tree_match(token_tree, &ptr, ";", NULL);
if (token) {
ACL_VSTRING_RESET(buf);
acl_token_name(token, buf);
printf("match %s %s, token's name: %s\n", psaved,
(token->flag & ACL_TOKEN_F_DENY) ? "DENY"
: (token->flag & ACL_TOKEN_F_PASS ? "PASS" : "NONE"),
STR(buf));
} else
printf("match %s none\n", psaved);
}
acl_token_tree_destroy(token_tree);
acl_vstring_free(buf);
}
static const char *__test_tab[] = {
2014-11-19 00:25:21 +08:00
"<EFBFBD>й<EFBFBD>"
"<EFBFBD>й<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",
"<EFBFBD>й<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",
"<EFBFBD>й<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ž<EFBFBD>",
NULL
};
static void test(void)
{
const char *tokens = "<EFBFBD>й<EFBFBD>|p <20>й<EFBFBD><D0B9><EFBFBD>|p <20>й<EFBFBD><D0B9><EFBFBD><EFBFBD><EFBFBD>|p <20>й<EFBFBD><D0B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>|p";
token_tree_test(tokens, __test_tab);
2014-11-19 00:25:21 +08:00
}
static void test2(void)
{
ACL_TOKEN *tree;
ACL_TOKEN *token;
2014-11-19 00:25:21 +08:00
const char *n1 = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1", *n2 = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>2", *n3 = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>3";
const char *v1 = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1", *v2 = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>2", *v3 = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>3";
const char *s = "<EFBFBD>й<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2? "
"<EFBFBD>Ҳ<EFBFBD>֪<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><EFBFBD>...";
const char *p = s;
if (1)
{
tree = acl_token_tree_create(NULL);
acl_token_tree_add(tree, n1, ACL_TOKEN_F_STOP, v1);
acl_token_tree_add(tree, n2, ACL_TOKEN_F_STOP, v2);
acl_token_tree_add(tree, n3, ACL_TOKEN_F_STOP, v3);
}
else
tree = acl_token_tree_create("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1|p <20><><EFBFBD><EFBFBD>2|p <20><><EFBFBD><EFBFBD>3|p");
printf("-----------------------------------\n");
acl_token_tree_print(tree);
token = acl_token_tree_word_match(tree, "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>1");
if (token)
printf("find, %s: %s\n", acl_token_name1(token),
(const char*) token->ctx);
else
printf("no find\n");
printf("-----------------------------------\n");
while (*p) {
token = acl_token_tree_match(tree, &p, NULL, NULL);
if (token == NULL)
break;
printf("%s: %s\n", acl_token_name1(token),
(const char*) token->ctx);
}
printf("-----------------------------------\n");
acl_token_tree_destroy(tree);
}
static void token_word_test(const char *tokens, const char *test_tab[])
{
ACL_TOKEN *token_tree;
const char *ptr;
int i;
token_tree = acl_token_tree_create(tokens);
acl_token_tree_print(token_tree);
for (i = 0; test_tab[i] != NULL; i++) {
ptr = test_tab[i];
printf("match %s %s\n", ptr,
acl_token_tree_word_match(token_tree, ptr) ? "yes" : "no");
}
acl_token_tree_destroy(token_tree);
}
static void test3(void)
{
const char *tokens1 = "hello world he is a man he"
" <20><> <20>л<EFBFBD> <20>л<EFBFBD><D0BB><EFBFBD> <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD> <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD>񹲺<EFBFBD> <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD>񹲺͹<F1B9B2BA>"
" <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD>񹲺͹<F1B9B2BA><CDB9><EFBFBD><EFBFBD><EFBFBD> <20>л<EFBFBD><D0BB><EFBFBD><EFBFBD>񹲺͹<F1B9B2BA><CDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"
" <20><><EFBFBD>ֹ<EFBFBD>|d <20>о<EFBFBD><D0BE><EFBFBD><EFBFBD>ֹ<EFBFBD>|d <20><><EFBFBD>Է<EFBFBD><D4B7>ֹ<EFBFBD>|p <20><><EFBFBD>ֹ<EFBFBD>Э<EFBFBD><D0AD>|d";
const char *tokens2 = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ|d <20>й<EFBFBD>|p ˵<><CBB5>|d";
static const char *test1_tab[] = {
"<EFBFBD>л<EFBFBD>",
"<EFBFBD>л<EFBFBD><EFBFBD><EFBFBD>",
"<EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",
"<EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",
"<EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD>񹲺<EFBFBD>",
"<EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD>񹲺͹<EFBFBD>",
"<EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD>񹲺͹<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",
"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD>񹲺͹<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",
"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD>񹲺͹<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",
"<EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD>",
"<EFBFBD><EFBFBD><EFBFBD>Է<EFBFBD><EFBFBD>ֹ<EFBFBD>",
"<EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD>Э<EFBFBD><EFBFBD>",
"<EFBFBD><EFBFBD><EFBFBD>Է<EFBFBD><EFBFBD>ֹ<EFBFBD>Э<EFBFBD><EFBFBD>",
"<EFBFBD>о<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD>",
NULL
};
static const char *test2_tab[] = {
"hello",
"shello",
"<EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD>񹲺͹<EFBFBD>",
"<EFBFBD>л<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",
NULL
};
static const char *test3_tab[] = {
"<EFBFBD>Ұ<EFBFBD><EFBFBD>з<EFBFBD><EFBFBD>ֹ<EFBFBD>",
"<EFBFBD>й<EFBFBD>",
"<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ",
"<EFBFBD>й<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ",
"<EFBFBD><EFBFBD>˵<EFBFBD>Ĺ<EFBFBD><EFBFBD><EFBFBD>",
"<EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD>й",
NULL
};
token_tree_test(tokens1, test1_tab);
token_word_test(tokens1, test2_tab);
token_tree_test(tokens2, test3_tab);
}
2014-11-19 00:25:21 +08:00
int main(void)
{
test();
#if 0
acl_token_tree_test();
#endif
test2();
test3();
2014-11-19 00:25:21 +08:00
#ifdef ACL_MS_WINDOWS
printf("enter any key to exit ...\n");
getchar();
#endif
return (0);
}