acl  3.5.3.0
acl_token_tree.h
浏览该文件的文档.
1 #ifndef ACL_TOKEN_TREE_INCLUDE_H
2 #define ACL_TOKEN_TREE_INCLUDE_H
3 
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 #include "acl_define.h"
8 #include "acl_vstring.h"
9 #include "acl_iterator.h"
10 
11 #define ACL_PRINT_CHAR(x) \
12  ((((x) >= 'a' && (x) <='z') \
13  || ((x) >= 'A' && (x) <= 'Z') \
14  || ((x) >= '0' && (x) <= '9') \
15  || (x) == ';' || (x) == '!' \
16  || (x) == ':' || (x) == ',' \
17  || (x) == '.' || (x) == '@' \
18  || (x) == '#' || (x) == '$' \
19  || (x) == '%' || (x) == '^' \
20  || (x) == '&' || (x) == '*' \
21  || (x) == '(' || (x) == ')' \
22  || (x) == '-' || (x) == '=' \
23  || (x) == '|' || (x) == '\\' \
24  || (x) == '[' || (x) == ']' \
25  || (x) == '{' || (x) == '}' \
26  || (x) == '\'' || (x) == '"') \
27  ? (x) : '-')
28 
29 typedef struct ACL_TOKEN ACL_TOKEN;
30 
31 struct ACL_TOKEN {
32  unsigned char ch;
33  unsigned int flag;
34 #define ACL_TOKEN_F_NONE 0
35 #define ACL_TOKEN_F_STOP (1 << 0)
36 #define ACL_TOKEN_F_PASS (1 << 1)
37 #define ACL_TOKEN_F_DENY (1 << 2)
38 #define ACL_TOKEN_F_UTF8 (1 << 3)
39 
40 #define ACL_TOKEN_WIDTH 256
42  struct ACL_TOKEN *parent;
43  void *ctx;
44 
45  ACL_TOKEN *(*iter_head)(ACL_ITER*, ACL_TOKEN*);
46  ACL_TOKEN *(*iter_next)(ACL_ITER*, ACL_TOKEN*);
47 };
48 
49 #define ACL_TOKEN_TREE_WORD_MATCH(acl_token_tree_in, word_in, acl_token_out) \
50 { \
51  const unsigned char *_ptr = (const unsigned char*) word_in; \
52  ACL_TOKEN *_token_iter = acl_token_tree_in, *_token = NULL; \
53  while (*_ptr) { \
54  _token = _token_iter->tokens[*_ptr]; \
55  if (_token == NULL) \
56  break; \
57  _token_iter = _token; \
58  _ptr++; \
59  } \
60  if (_token && (_token->flag & ACL_TOKEN_F_STOP)) \
61  acl_token_out = _token; \
62  else \
63  acl_token_out = NULL; \
64 }
65 
66 #define ACL_TOKEN_TREE_MATCH(acl_token_tree_in, s_in, delim_in, delim_tab_in, acl_token_out) do \
67 { \
68  ACL_TOKEN *acl_token_iter = (acl_token_tree_in), *acl_token_tmp; \
69  (acl_token_out) = NULL; \
70  if (((const char*) delim_in)) { \
71  int _i; \
72  while (*(s_in)) { \
73  for (_i = 0; ((const char*) delim_in)[_i]; _i++) { \
74  if (*(s_in) == ((const char*) delim_in)[_i]) \
75  goto _END; \
76  } \
77  acl_token_tmp = acl_token_iter->tokens[*((const unsigned char*)(s_in))]; \
78  if (acl_token_tmp == NULL) { \
79  if ((acl_token_out)) \
80  break; \
81  acl_token_iter = (acl_token_tree_in); \
82  acl_token_tmp = acl_token_iter->tokens[*((const unsigned char*)(s_in))]; \
83  if (acl_token_tmp == NULL) { \
84  (s_in)++; \
85  continue; \
86  } \
87  } \
88  if ((acl_token_tmp->flag & ACL_TOKEN_F_STOP)) \
89  (acl_token_out) = acl_token_tmp; \
90  acl_token_iter = acl_token_tmp; \
91  (s_in)++; \
92  } \
93 _END: \
94  break; \
95  } else if (((char*) delim_tab_in)) { \
96  while (*(s_in)) { \
97  if (((char*) delim_tab_in)[*((const unsigned char*)(s_in))]) \
98  break; \
99  acl_token_tmp = acl_token_iter->tokens[*((const unsigned char*)(s_in))]; \
100  if (acl_token_tmp == NULL) { \
101  if ((acl_token_out)) \
102  break; \
103  acl_token_iter = (acl_token_tree_in); \
104  acl_token_tmp = acl_token_iter->tokens[*((const unsigned char*)(s_in))]; \
105  if (acl_token_tmp == NULL) { \
106  (s_in)++; \
107  continue; \
108  } \
109  } \
110  if ((acl_token_tmp->flag & ACL_TOKEN_F_STOP)) \
111  (acl_token_out) = acl_token_tmp; \
112  acl_token_iter = acl_token_tmp; \
113  (s_in)++; \
114  } \
115  } else { \
116  while (*(s_in)) { \
117  acl_token_tmp = acl_token_iter->tokens[*((const unsigned char*)(s_in))]; \
118  if (acl_token_tmp == NULL) { \
119  if ((acl_token_out)) \
120  break; \
121  acl_token_iter = (acl_token_tree_in); \
122  acl_token_tmp = acl_token_iter->tokens[*((const unsigned char*)(s_in))]; \
123  if (acl_token_tmp == NULL) { \
124  (s_in)++; \
125  continue; \
126  } \
127  } \
128  if ((acl_token_tmp->flag & ACL_TOKEN_F_STOP)) \
129  (acl_token_out) = acl_token_tmp; \
130  acl_token_iter = acl_token_tmp; \
131  (s_in)++; \
132  } \
133  } \
134 } while (0)
135 
136 ACL_API char *acl_token_delim_tab_new(const char *delim);
137 ACL_API void acl_token_delim_tab_free(char *delim_tab);
138 ACL_API ACL_TOKEN *acl_token_new(void);
139 ACL_API void acl_token_free(ACL_TOKEN *token);
140 ACL_API void acl_token_name(ACL_TOKEN *token, ACL_VSTRING *buf);
141 ACL_API const char *acl_token_name1(ACL_TOKEN *token);
142 ACL_API ACL_TOKEN *acl_token_tree_add(ACL_TOKEN *tree,
143  const char *word, unsigned int flag, const void *ctx);
145  const char *word, const char *word_map, unsigned int flag);
147  const char *word);
148 ACL_API void *acl_token_tree_word_remove(ACL_TOKEN *tree, const char *word);
150  const char **ptr, const char *delim, const char *delim_tab);
151 ACL_API void acl_token_tree_walk(ACL_TOKEN *tree,
152  void (*walk_fn)(ACL_TOKEN*, void*), void *arg);
153 ACL_API void acl_token_tree_print(ACL_TOKEN *tree);
154 ACL_API ACL_TOKEN *acl_token_tree_create(const char *s);
155 ACL_API ACL_TOKEN *acl_token_tree_create2(const char *s, const char *sep);
156 ACL_API void acl_token_tree_destroy(ACL_TOKEN *tree);
157 ACL_API void acl_token_tree_load_deny(const char *filepath, ACL_TOKEN *tree);
158 ACL_API void acl_token_tree_load_pass(const char *filepath, ACL_TOKEN *tree);
159 
160 #ifdef __cplusplus
161 }
162 #endif
163 
164 #endif
ACL_API const char * acl_token_name1(ACL_TOKEN *token)
ACL_API ACL_TOKEN * acl_token_tree_add(ACL_TOKEN *tree, const char *word, unsigned int flag, const void *ctx)
ACL_API ACL_TOKEN * acl_token_tree_add_word_map(ACL_TOKEN *tree, const char *word, const char *word_map, unsigned int flag)
ACL_API void * acl_token_tree_word_remove(ACL_TOKEN *tree, const char *word)
ACL_API void acl_token_tree_walk(ACL_TOKEN *tree, void(*walk_fn)(ACL_TOKEN *, void *), void *arg)
ACL_API void acl_token_name(ACL_TOKEN *token, ACL_VSTRING *buf)
ACL_API ACL_TOKEN * acl_token_tree_create(const char *s)
ACL_API ACL_TOKEN * acl_token_tree_match(ACL_TOKEN *tree, const char **ptr, const char *delim, const char *delim_tab)
struct ACL_TOKEN * parent
ACL_API void acl_token_tree_destroy(ACL_TOKEN *tree)
ACL_API void acl_token_delim_tab_free(char *delim_tab)
ACL_API ACL_TOKEN * acl_token_tree_create2(const char *s, const char *sep)
#define ACL_TOKEN_WIDTH
struct ACL_TOKEN * tokens[ACL_TOKEN_WIDTH]
ACL_API ACL_TOKEN * acl_token_new(void)
unsigned char ch
ACL_API void acl_token_tree_print(ACL_TOKEN *tree)
ACL_API ACL_TOKEN * acl_token_tree_word_match(ACL_TOKEN *tree, const char *word)
ACL_API char * acl_token_delim_tab_new(const char *delim)
unsigned int flag
ACL_API void acl_token_free(ACL_TOKEN *token)
void * ctx
ACL_API void acl_token_tree_load_deny(const char *filepath, ACL_TOKEN *tree)
ACL_API void acl_token_tree_load_pass(const char *filepath, ACL_TOKEN *tree)