OSDN Git Service

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