OSDN Git Service

*** empty log message ***
[modchxj/mod_chxj.git] / src / qs_parse_tag.c
1 /*
2  * Copyright (C) 2005 QSDN,Inc. All rights reserved.
3  * Copyright (C) 2005 Atsushi Konno 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 #include <stdio.h>
18 #include "qs_parse_string.h"
19 #include "qs_log.h"
20 #include "qs_parse_attr.h"
21 #include "qs_parse_tag.h"
22
23 static char* s_get_tag_name(Doc* doc, const char* s, int len) ; 
24
25
26
27 Node*
28 qs_parse_tag(Doc* doc, const char* s, int len) 
29 {
30   Node*  node;
31   char*  tag_name;
32   char*  sp;
33   int    ll;
34   int    next_point;
35
36   sp         = (char*)s;
37   ll         = len;
38   next_point = 0;
39
40   QX_LOGGER_DEBUG("start parse_tag()");
41
42
43   /* s[0] == '<' && s[len-1] == '>' */
44   tag_name = (char *)s_get_tag_name(doc, ++s, --ll);
45   QX_LOGGER_DEBUG_INT("ll",ll);
46
47   node = (Node*)qs_new_tag(doc);
48   node->name = tag_name;
49   node->otext = apr_palloc(doc->pool,len+2);
50   memset(node->otext, 0, len+2);
51   memcpy(node->otext, sp, len+1);
52
53   QX_LOGGER_DEBUG(tag_name);
54
55   ll -= (strlen(tag_name));
56   QX_LOGGER_DEBUG_INT("ll",ll);
57   sp += (strlen(tag_name)+1);
58   for (;;) {
59     Attr* attr = qs_parse_attr(doc,sp, ll, &next_point);
60     if (attr == NULL) {
61       QX_LOGGER_DEBUG("End of QS_PARSE_ATTR()");
62       break;
63     }
64     QX_LOGGER_DEBUG(attr->name);
65     QX_LOGGER_DEBUG(attr->value);
66     sp += next_point;
67     ll -= next_point;
68     QX_LOGGER_DEBUG_INT(sp, ll);
69     node = (Node*)qs_add_attr(doc,node, attr);
70   }
71
72   QX_LOGGER_DEBUG("end parse_tag()");
73   return node;
74 }
75
76
77
78
79 static char* 
80 s_get_tag_name(Doc* doc, const char* s, int len)  
81 {
82   int ii;
83   int sp;
84   int size;
85   char* return_value = NULL;
86
87   /* ignore space. */
88   for (ii = 0; ii < len; ii++) {
89     if (is_white_space(s[ii])) 
90       continue;
91     break;
92   }
93
94   sp = ii;
95   for (;ii<len; ii++) {
96     if (is_white_space(s[ii])) 
97       break;
98   }
99
100   size = ii-sp;
101
102   return_value = (char*)apr_palloc(doc->pool, size+1);
103
104   memset(return_value, 0, size+1);
105   memcpy(return_value, &s[sp], size);
106
107   QX_LOGGER_DEBUG(return_value);
108   return return_value;
109 }
110
111
112
113 Node*
114 qs_new_tag(Doc* doc) 
115 {
116   Node* node      = (Node*)apr_palloc(doc->pool, sizeof(Node));
117   node->next      = NULL;
118   node->parent    = NULL;
119   node->child     = NULL;
120   node->child_tail= NULL;
121   node->attr      = NULL;
122   node->attr_tail = NULL;
123   node->name      = NULL;
124   node->value     = NULL;
125
126   return node;
127 }
128
129
130
131
132 Node*
133 qs_add_attr(Doc* doc, Node* node, Attr* attr) 
134 {
135   if (node == NULL) {
136     QX_LOGGER_FATAL("qs_add_attr() node is null");
137   }
138
139   attr->parent = node;
140   attr->next   = NULL;
141   if (node->attr == NULL) {
142     node->attr      = attr;
143     node->attr_tail = attr;
144     return node;
145   }
146
147   node->attr_tail->next = attr;
148   node->attr_tail       = attr;
149   return node;
150 }
151 /*
152  * vim:ts=2 et
153  */