OSDN Git Service

Merge branch 'branch_0.12.0' into branch_0.13.0
[modchxj/mod_chxj.git] / include / qs_parse_string.h
1 /*
2  * Copyright (C) 2005-2009 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) && (unsigned char)(c&0xff) <= 0x9f)  \
38                           || (0xe0 <= (unsigned char)(c&0xff) && (unsigned char)(c&0xff) <= 0xfc))
39 /**
40  * It is judged whether it is a byte of Japanese Shift_JIS "HANKAKU KANA". 
41  */
42 #define is_sjis_kana(c)   ((0xa1 <= (unsigned char)(c&0xff) && (unsigned char)(c&0xff) <= 0xdf))
43
44 /**
45  * It is judged whether it is a Japanese code part of EUC. 
46  */
47 #define is_euc_kanji(c)   (0xa1 <= (unsigned char)(c&0xff) && (unsigned char)(c&0xff) <= 0xfe)
48
49 /**
50  * It is judged whether it is a Japanese code part of EUC "KANA". 
51  */
52 #define is_euc_kana(c)    (0x8e == (unsigned char)(c&0xff))
53
54 /**
55  * True is returned for whitespace. 
56  */
57 #define is_white_space(c)     (' ' == (unsigned char)(c&0xff)  \
58                            || '\t' == (unsigned char)(c&0xff) \
59                            || '\n' == (unsigned char)(c&0xff) \
60                            || '\r' == (unsigned char)(c&0xff))
61
62 /**
63  * True is returned for the quotation mark. 
64  */
65 #define is_quote(c)       ('\'' == (unsigned char)(c&0xff) \
66                          ||'"'  == (unsigned char)(c&0xff))
67
68 /**
69  * It is judged whether the tag of the object has the child element. 
70  * The "<option>" tag has the child. Please write </ option >. 
71  */
72 #define has_child(c)       ((strcasecmp(c, "base"     ) != 0) \
73                         &&  (strcasecmp(c, "meta"     ) != 0) \
74                         &&  (strcasecmp(c, "br"       ) != 0) \
75                         &&  (strcasecmp(c, "hr"       ) != 0) \
76                         &&  (strcasecmp(c, "img"      ) != 0) \
77                         &&  (strcasecmp(c, "input"    ) != 0) \
78                         &&  (strcasecmp(c, "?xml"     ) != 0) \
79                         &&  (strcasecmp(c, "!doctype" ) != 0) \
80                         &&  (strcasecmp(c, "link"     ) != 0) \
81                         &&  (strcasecmp(c, "!--"      ) != 0) \
82                         &&  (strncasecmp(c, "![CDATA[",8) != 0))
83
84 /**
85  * It is judged whether the tag of the object has the child element. 
86  * The "<option>" tag has the child. Please write </ option >. 
87  */
88 #define has_child_hdml(c)  ((strcasecmp(c, "center"     ) != 0) \
89                         &&  (strcasecmp(c, "br"         ) != 0) \
90                         &&  (strcasecmp(c, "action"     ) != 0) \
91                         &&  (strcasecmp(c, "!--"      ) != 0))
92
93 #define QS_PARSE_NL_MARK "CrLf"
94
95 /**
96  * The structure of the attribute is defined.
97  */
98 typedef struct Attr Attr;
99
100 struct Attr {
101   struct Attr* next;
102   struct Node* parent;
103   char *name;
104   char *value;
105 };
106
107 /**
108  * The structure of the element is defined. 
109  */
110 typedef struct Node Node;
111
112 struct Node {
113   struct Node   *next;
114   struct Node   *parent;
115   struct Node   *prev;
116   struct Node   *child;
117   struct Node   *child_tail;
118   struct Attr   *attr;
119   struct Attr   *attr_tail;
120   char          *name;
121   char          *value;
122   int           size;
123   char          *otext;
124   int           line;
125   int           closed_by_itself;
126   void          *userData;
127 };
128
129 typedef struct pointer_table_t {
130   unsigned int            address;
131   unsigned long           size;
132   struct pointer_table_t *next;
133   struct pointer_table_t *prev;
134 } Pointer_Table;
135
136
137 typedef enum chxj_parse_mode_t {
138   PARSE_MODE_CHTML=0,
139   PARSE_MODE_NO_PARSE,
140 } ParseMode_t;
141
142 typedef struct _doc {
143   Node *now_parent_node;
144   Node *root_node;
145
146   int           do_init_flag;
147   unsigned long alloc_size;
148
149   Pointer_Table *pointer_table;
150   Pointer_Table *free_list_head;
151   Pointer_Table *free_list_tail;
152   Pointer_Table *allocated_list_head;
153   Pointer_Table *allocated_list_tail;
154
155   ParseMode_t    parse_mode;
156
157   apr_allocator_t *allocator;
158   apr_pool_t      *pool;
159
160   buf_object buf;
161
162 #ifndef __NON_MOD_CHXJ__
163   request_rec *r;
164 #endif
165 } Doc;
166
167 /*
168  * Prototype Declare
169  */
170 extern Node *qs_init_root_node(
171   Doc *doc);
172
173 extern void qs_add_child_node(
174   Doc* doc, 
175   Node*);
176
177 extern void qs_free_node(
178   Doc* doc,
179   Node*);
180
181 extern Node* qs_get_root(
182   Doc* doc) ;
183
184 extern Node* qs_parse_string(
185   Doc*        doc, 
186   const char* ss, 
187   int         len);
188
189 extern char* qs_get_node_value(
190   Doc*  doc,
191   Node* node);
192
193 extern char* qs_get_node_name(
194   Doc*  doc, 
195   Node* node);
196
197 extern int qs_get_node_size(Doc* doc, Node* node) ;
198
199 extern Node *qs_get_child_node(Doc *doc, Node *node);
200 extern Node *qs_get_next_node(Doc *doc, Node *node);
201 extern Node *qs_get_prev_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
208 extern int chxj_cut_tag(const char *s, int len);
209 #endif