OSDN Git Service

* Added test code.
[modchxj/mod_chxj.git] / include / qs_parse_string.h
1 /*
2  * Copyright (C) 2005-2008 Atsushi Konno All rights reserved.
3  * Copyright (C) 2005 QSDN,Inc. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef __QS_PARSE_STRING_H__
18 #define __QS_PARSE_STRING_H__
19
20 #include "chxj_apache.h"
21 #include "chxj_buffered_write.h"
22
23 /*
24 #define DEBUG
25 #define USE_LOG
26 */
27
28 /**
29  * Max of memory allocation times.
30  */
31 #define QX_ALLOC_MAX   (100*1024)
32
33 /**
34  * It is judged whether it is the first byte of Japanese Shift_JIS 
35  * "ZENKAKU KANJI". 
36  */
37 #define is_sjis_kanji(c)  ((0x81 <= (unsigned char)(c&0xff) && \
38                            (unsigned char)(c&0xff) <= 0x9f)  \
39                           || (0xe0 <= (unsigned char)(c&0xff) && \
40                            (unsigned char)(c&0xff) <= 0xfc))
41 /**
42  * It is judged whether it is a byte of Japanese Shift_JIS "HANKAKU KANA". 
43  */
44 #define is_sjis_kana(c)   ((0xa1 <= (unsigned char)(c&0xff) && (unsigned char)(c&0xff) <= 0xdf))
45
46 /**
47  * It is judged whether it is a Japanese code part of EUC. 
48  */
49 #define is_euc_kanji(c)   (0xa1 <= (unsigned char)(c&0xff) && (unsigned char)(c&0xff) <= 0xfe)
50
51 /**
52  * It is judged whether it is a Japanese code part of EUC "KANA". 
53  */
54 #define is_euc_kana(c)    (0x8e == (unsigned char)(c&0xff))
55
56 /**
57  * True is returned for whitespace. 
58  */
59 #define is_white_space(c)     (' ' == (unsigned char)(c&0xff)  \
60                            || '\t' == (unsigned char)(c&0xff) \
61                            || '\n' == (unsigned char)(c&0xff) \
62                            || '\r' == (unsigned char)(c&0xff))
63
64 /**
65  * True is returned for the quotation mark. 
66  */
67 #define is_quote(c)       ('\'' == (unsigned char)(c&0xff) \
68                          ||'"'  == (unsigned char)(c&0xff))
69
70 /**
71  * It is judged whether the tag of the object has the child element. 
72  * The "<option>" tag has the child. Please write </ option >. 
73  */
74 #define has_child(c)       ((strcasecmp(c, "base"     ) != 0) \
75                         &&  (strcasecmp(c, "meta"     ) != 0) \
76                         &&  (strcasecmp(c, "br"       ) != 0) \
77                         &&  (strcasecmp(c, "dt"       ) != 0) \
78                         &&  (strcasecmp(c, "dd"       ) != 0) \
79                         &&  (strcasecmp(c, "hr"       ) != 0) \
80                         &&  (strcasecmp(c, "img"      ) != 0) \
81                         &&  (strcasecmp(c, "input"    ) != 0) \
82                         &&  (strcasecmp(c, "p"        ) != 0) \
83                         &&  (strcasecmp(c, "plaintext") != 0) \
84                         &&  (strcasecmp(c, "?xml"     ) != 0) \
85                         &&  (strcasecmp(c, "!doctype" ) != 0) \
86                         &&  (strcasecmp(c, "link"     ) != 0) \
87                         &&  (strcasecmp(c, "!--"      ) != 0) \
88                         &&  (strncasecmp(c, "![CDATA[",8) != 0))
89
90 /**
91  * It is judged whether the tag of the object has the child element. 
92  * The "<option>" tag has the child. Please write </ option >. 
93  */
94 #define has_child_hdml(c)  ((strcasecmp(c, "center"     ) != 0) \
95                         &&  (strcasecmp(c, "br"         ) != 0) \
96                         &&  (strcasecmp(c, "action"     ) != 0) \
97                         &&  (strcasecmp(c, "!--"      ) != 0))
98
99 /**
100  * The structure of the attribute is defined.
101  */
102 typedef struct Attr Attr;
103
104 struct Attr {
105   struct Attr* next;
106   struct Node* parent;
107   char *name;
108   char *value;
109 };
110
111 /**
112  * The structure of the element is defined. 
113  */
114 typedef struct Node Node;
115
116 struct Node {
117   struct Node   *next;
118   struct Node   *parent;
119   struct Node   *child;
120   struct Node   *child_tail;
121   struct Attr   *attr;
122   struct Attr   *attr_tail;
123   char          *name;
124   char          *value;
125   int            size;
126   char          *otext;
127   int            line;
128 };
129
130 typedef struct pointer_table_t {
131   unsigned int            address;
132   unsigned long           size;
133   struct pointer_table_t *next;
134   struct pointer_table_t *prev;
135 } Pointer_Table;
136
137
138 typedef enum chxj_parse_mode_t {
139   PARSE_MODE_CHTML=0,
140   PARSE_MODE_NO_PARSE,
141 } ParseMode_t;
142
143 typedef struct _doc {
144   Node *now_parent_node;
145   Node *root_node;
146
147   int           do_init_flag;
148   unsigned long alloc_size;
149
150   Pointer_Table *pointer_table;
151   Pointer_Table *free_list_head;
152   Pointer_Table *free_list_tail;
153   Pointer_Table *allocated_list_head;
154   Pointer_Table *allocated_list_tail;
155
156   ParseMode_t    parse_mode;
157
158   apr_allocator_t *allocator;
159   apr_pool_t      *pool;
160
161   buf_object buf;
162
163 #ifndef __NON_MOD_CHXJ__
164   request_rec *r;
165 #endif
166 } Doc;
167
168 /*
169  * Prototype Declare
170  */
171 extern Node *qs_init_root_node(
172   Doc *doc);
173
174 extern void qs_add_child_node(
175   Doc* doc, 
176   Node*);
177
178 extern void qs_free_node(
179   Doc* doc,
180   Node*);
181
182 extern Node* qs_get_root(
183   Doc* doc) ;
184
185 extern Node* qs_parse_string(
186   Doc*        doc, 
187   const char* ss, 
188   int         len);
189
190 extern char* qs_get_node_value(
191   Doc*  doc,
192   Node* node);
193
194 extern char* qs_get_node_name(
195   Doc*  doc, 
196   Node* node);
197
198 extern int qs_get_node_size(Doc* doc, Node* node) ;
199
200 extern Node *qs_get_child_node(Doc *doc, Node *node) ;
201 extern Node *qs_get_next_node(Doc *doc, Node *node) ;
202
203 Attr* qs_get_attr(Doc* doc, Node* node) ;
204 Attr* qs_get_next_attr(Doc* doc, Attr* attr) ;
205 char* qs_get_attr_name(Doc* doc, Attr* attr) ;
206 char* qs_get_attr_value(Doc* doc, Attr* attr) ;
207 #endif