OSDN Git Service

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