OSDN Git Service

* update copyright date.
[modchxj/mod_chxj.git] / src / qs_parse_attr.c
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 #include <stdio.h>
18 #include "qs_parse_string.h"
19 #include "qs_parse_attr.h"
20 #include "qs_log.h"
21 #include "qs_ignore_sp.h"
22
23
24 Attr*
25 qs_parse_attr(Doc* doc, const char*s, int len, int *pos) 
26 {
27   int   ii;
28   int   start_pos;
29   int   size;
30   int   novalue;
31   char* name;
32   char* value;
33   Attr* attr;
34   int   use_quote;
35   int   backslash;
36
37   use_quote = 0;
38   backslash = 0;
39
40   QX_LOGGER_DEBUG("start qs_parse_attr()");
41
42   /* ignore space */
43   ii = start_pos = qs_ignore_sp_and_quote(doc, s, len);
44   QX_LOGGER_DEBUG_INT("len",len);
45
46   /* get attr name */
47   for (;ii<len; ii++) {
48     if (is_white_space(s[ii])) 
49       break;
50
51     if (s[ii] == '=')
52       break;
53   }
54
55   size = ii - start_pos;
56   QX_LOGGER_DEBUG_INT("size", size);
57
58   /* 
59    * not found 
60    */
61   if (size == 0) {
62     *pos = ii;
63     return NULL;
64   }
65
66   name = (char*)apr_palloc(doc->pool,size+1);
67   memset(name, 0, size+1);
68   memcpy(name, &s[start_pos], size);
69
70   QX_LOGGER_DEBUG((char*)name);
71
72   novalue = 0;
73   /* find '=' */
74   for (;ii<len; ii++) {
75     if (is_white_space(s[ii])) 
76       /* ignore */
77       continue;
78     if (s[ii] == '=') 
79       ii++;
80     else 
81       /* novalue */
82       novalue = 1;
83     break;
84   }
85
86   if (ii == len) 
87     novalue = 1;
88
89   size = 0;
90   if (!novalue) {
91
92     /* 
93      * ignore space
94      */
95     ii += qs_ignore_sp(doc, &s[ii], len-ii);
96
97     backslash = 0;
98     for (;ii<len; ii++) {
99       if (s[ii] == '\\') {
100         backslash = 1;
101         break;
102       }
103       if (s[ii] == '\'' || s[ii] == '"') {
104         use_quote = 1;
105         ii++;
106         break;
107       }
108       if (!is_white_space(s[ii]))
109         break;
110     }
111   
112     start_pos = ii;
113     if (backslash && ii + 2 < len)
114       ii+=2;
115     
116     backslash = 0;
117     /* 
118      * get attr value 
119      */
120     for (;ii<len; ii++) {
121       if (is_sjis_kanji(s[ii])) {
122         ii++;
123         continue;
124       }
125
126       if (is_sjis_kana(s[ii])) 
127         continue;
128
129       if (is_white_space(s[ii])) {
130         if (! use_quote) 
131           break;
132       }
133
134       if (s[ii] == '\\') {
135         ii++;
136         continue;
137       }
138
139       if (s[ii] == '"') 
140         break;
141
142       if (s[ii] == '\'') 
143         break;
144     }
145     size = ii - start_pos;
146
147     QX_LOGGER_DEBUG_INT("size",size);
148   }
149
150   value = (char*)apr_palloc(doc->pool, size+1);
151   memset(value, 0, size+1);
152   if (size != 0) 
153     memcpy(value, &s[start_pos], size);
154
155   attr = qs_new_attr(doc);
156
157   attr->name  = name;
158   attr->value = value;
159
160   QX_LOGGER_DEBUG("end qs_parse_attr()");
161   *pos = ii;
162
163   return attr;
164 }
165
166
167 Attr*
168 qs_new_attr(Doc* doc) 
169 {
170   Attr* attr;
171
172   attr = (Attr*)apr_palloc(doc->pool,sizeof(Attr));
173
174   if (attr == NULL) {
175     QX_LOGGER_FATAL("Out Of Memory");
176   }
177
178   attr->next   = NULL;
179   attr->parent = NULL;
180   attr->name   = NULL;
181   attr->value  = NULL;
182
183   return attr;
184 }
185 /*
186  * vim:ts=2 et
187  */