OSDN Git Service

* Writing is changed.
[modchxj/mod_chxj.git] / src / chxj_load_device_data.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 "mod_chxj.h"
18 #include <qs_ignore_sp.h>
19 #include <qs_log.h>
20 #include <qs_malloc.h>
21 #include <qs_parse_attr.h>
22 #include <qs_parse_file.h>
23 #include <qs_parse_string.h>
24 #include <qs_parse_tag.h>
25 #include "chxj_load_device_data.h"
26 #include "chxj_str_util.h"
27
28
29 static void s_set_devices_data(Doc* doc, apr_pool_t* p, mod_chxj_config* conf, Node* node) ;
30 static void s_set_user_agent_data(Doc* doc, apr_pool_t* p, mod_chxj_config* conf, Node* node) ;
31 static void s_set_device_data(Doc* doc, apr_pool_t* p, device_table_list_t* dtl, Node* node) ;
32 /**
33  * load device_data.xml
34  */
35 void
36 chxj_load_device_data(Doc* doc, apr_pool_t *p, mod_chxj_config* conf) 
37 {
38   conf->devices = NULL;
39   s_set_devices_data(doc, p, conf,qs_get_root(doc));
40
41 }
42
43 /**
44  * <devices>
45  */
46 static void
47 s_set_devices_data(Doc* doc, apr_pool_t* p, mod_chxj_config* conf, Node* node) 
48 {
49   Node* child;
50
51   for (child = qs_get_child_node(doc,node); 
52        child ; 
53        child = qs_get_next_node(doc,child)) {
54     char* name = qs_get_node_name(doc,child);
55     if (strcasecmp(name, "devices") == 0)
56       s_set_user_agent_data(doc, p, conf, child);
57   }
58 }
59
60 /**
61  * <user_agent>
62  */
63 static void
64 s_set_user_agent_data(Doc* doc, apr_pool_t* p, mod_chxj_config* conf, Node* node) 
65 {
66   Node* child;
67   device_table_list_t* t;
68
69   for (child = qs_get_child_node(doc,node);
70        child ;
71        child = qs_get_next_node(doc,child)) {
72     char* name = qs_get_node_name(doc,child);
73
74     if (strcasecmp(name, "user_agent") == 0 ) {
75       Attr* attr;
76       device_table_list_t* dtl;
77
78       if (! conf->devices) {
79         conf->devices = apr_pcalloc(p, sizeof(device_table_list_t));
80         conf->devices->next    = NULL;
81         conf->devices->pattern = NULL;
82         conf->devices->table   = NULL;
83         conf->devices->tail    = NULL;
84         dtl = conf->devices;
85       }
86       else {
87         for (t = conf->devices; t ; t = t->next) {
88           if (! t->next)
89             break;
90         }
91         t->next = apr_pcalloc(p, sizeof(device_table_list_t));
92         t->next->next    = NULL;
93         t->next->pattern = NULL;
94         t->next->table   = NULL;
95         t->next->tail    = NULL;
96         dtl = t->next;
97       }
98
99       for (attr = qs_get_attr(doc,child); 
100            attr ; 
101            attr = qs_get_next_attr(doc,attr)) {
102         if (strcasecmp(qs_get_attr_name(doc,attr), "pattern") == 0) {
103             dtl->pattern = apr_pstrdup(p, qs_get_attr_value(doc,attr));
104             dtl->regexp = ap_pregcomp(p, dtl->pattern, AP_REG_EXTENDED);
105         }
106       }
107       s_set_device_data(doc, p, dtl, child);
108     }
109   }
110 }
111
112 static void
113 s_set_device_data(Doc* doc, apr_pool_t* p, device_table_list_t* dtl, Node* node) 
114 {
115   Node* child;
116   device_table_t* dt;
117
118   dt = apr_pcalloc(p, sizeof(device_table_t));
119   dt->next           = NULL;
120   dt->device_id      = NULL;
121   dt->device_name    = NULL;
122   dt->html_spec_type = CHXJ_SPEC_Chtml_3_0;
123   dt->width          = 0;
124   dt->heigh          = 0;
125   dt->wp_width       = 0;
126   dt->wp_heigh       = 0;
127   dt->cache          = 5;
128   dt->emoji_type     = NULL;
129   dt->color          = 256;
130   dt->dpi_width      = 96;
131   dt->dpi_heigh      = 96;
132
133   for (child = qs_get_child_node(doc,node); 
134        child ;
135        child = qs_get_next_node(doc,child)) {
136     char* name = qs_get_node_name(doc,child);
137     if (strcasecmp(name, "device") == 0) {
138       s_set_device_data(doc,p, dtl, child);
139     }
140     else
141     if (strcasecmp(name, "device_id") == 0) {
142       Node* ch = qs_get_child_node(doc, child);
143       if (ch &&  strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
144         dt->device_id = apr_pstrdup(p, qs_get_node_value(doc, ch));
145       }
146     }
147     else
148     if (strcasecmp(name, "device_name") == 0) {
149       Node* ch = qs_get_child_node(doc, child);
150       if (ch &&  strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
151         dt->device_name = apr_pstrdup(p, qs_get_node_value(doc, ch));
152       }
153     }
154     else
155     if (strcasecmp(name, "html_spec_type") == 0) {
156       Node* ch = qs_get_child_node(doc, child);
157       if (ch &&  strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
158         char* vv = qs_get_node_value(doc, ch);
159         if (strcasecmp(vv, "xhtml_mobile_1_0") == 0) {
160           dt->html_spec_type = CHXJ_SPEC_XHtml_Mobile_1_0;
161         }
162         else
163         if (strcasecmp(vv, "chtml_1_0") == 0) {
164           dt->html_spec_type = CHXJ_SPEC_Chtml_1_0;
165         }
166         else
167         if (strcasecmp(vv, "chtml_2_0") == 0) {
168           dt->html_spec_type = CHXJ_SPEC_Chtml_2_0;
169         }
170         else
171         if (strcasecmp(vv, "chtml_3_0") == 0) {
172           dt->html_spec_type = CHXJ_SPEC_Chtml_3_0;
173         }
174         else
175         if (strcasecmp(vv, "chtml_4_0") == 0) {
176           dt->html_spec_type = CHXJ_SPEC_Chtml_4_0;
177         }
178         else
179         if (strcasecmp(vv, "chtml_5_0") == 0) {
180           dt->html_spec_type = CHXJ_SPEC_Chtml_5_0;
181         }
182         else
183         if (strcasecmp(vv, "hdml") == 0) {
184           dt->html_spec_type = CHXJ_SPEC_Hdml;
185         }
186         else
187         if (strcasecmp(vv, "jhtml") == 0) {
188           dt->html_spec_type = CHXJ_SPEC_Jhtml;
189         }
190       }
191     }
192     else 
193     if (strcasecmp(name, "width") == 0) {
194       Node* ch = qs_get_child_node(doc, child);
195       if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
196         char *vv = qs_get_node_value(doc,ch);
197         int ii;
198         for (ii=0; ii<strlen(vv); ii++) {
199           if ((vv[ii] >= '1' && vv[ii] <= '9') || vv[ii] == '0')
200             continue;
201           break;
202         }
203         if (ii == strlen(vv))
204           dt->width = atoi(qs_get_node_value(doc,ch));
205         else 
206           dt->width = 0;
207       }
208     }
209     else
210     if (strcasecmp(name, "heigh") == 0) {
211       Node* ch = qs_get_child_node(doc, child);
212       if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
213         char *vv = qs_get_node_value(doc,ch);
214         int ii;
215         for (ii=0; ii<strlen(vv); ii++) {
216           if ((vv[ii] >= '1' && vv[ii] <= '9') || vv[ii] == '0') 
217             continue;
218           break;
219         }
220
221         if (ii == strlen(vv)) 
222           dt->heigh = atoi(qs_get_node_value(doc,ch));
223         else 
224           dt->heigh = 0;
225       }
226     }
227     else
228     if (strcasecmp(name, "gif") == 0) {
229       Node* ch = qs_get_child_node(doc, child);
230       if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
231         char *vv = qs_get_node_value(doc,ch);
232
233         if (strcasecmp(vv, "true") == 0)
234           dt->available_gif = 1;
235         else
236           dt->available_gif = 0;
237       }
238     }
239     else
240     if (strcasecmp(name, "jpeg") == 0 || strcasecmp(name, "jpg") == 0) {
241       Node* ch = qs_get_child_node(doc, child);
242       if (ch != NULL && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
243         char *vv = qs_get_node_value(doc,ch);
244
245         if (strcasecmp(vv, "true") == 0) 
246           dt->available_jpeg = 1;
247         else 
248           dt->available_jpeg = 0;
249       }
250     }
251     else
252     if (strcasecmp(name, "png") == 0) {
253       Node* ch = qs_get_child_node(doc, child);
254       if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
255         char *vv = qs_get_node_value(doc,ch);
256
257         if (strcasecmp(vv, "true") == 0) 
258           dt->available_png = 1;
259         else
260           dt->available_png = 0;
261       }
262     }
263     else
264     if (strcasecmp(name, "bmp2") == 0) {
265       Node* ch = qs_get_child_node(doc, child);
266       if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
267         char *vv = qs_get_node_value(doc,ch);
268         if (strcasecmp(vv, "true") == 0) 
269         {
270           dt->available_bmp2 = 1;
271         }
272         else
273         {
274           dt->available_bmp2 = 0;
275         }
276       }
277     }
278     else
279     if (strcasecmp(name, "bmp4") == 0) 
280     {
281       Node* ch = qs_get_child_node(doc, child);
282       if (ch != NULL && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) 
283       {
284         char *vv = qs_get_node_value(doc,ch);
285         if (strcasecmp(vv, "true") == 0) 
286         {
287           dt->available_bmp4 = 1;
288         }
289         else
290         {
291           dt->available_bmp4 = 0;
292         }
293       }
294     }
295     else
296     if (strcasecmp(name, "color") == 0) 
297     {
298       Node* ch = qs_get_child_node(doc, child);
299       if (ch != NULL && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) 
300       {
301         char *vv = qs_get_node_value(doc,ch);
302         if (chxj_chk_numeric(vv) != 0)
303         {
304           dt->color = 0;
305         }
306         else 
307         {
308           dt->color = chxj_atoi(vv);
309         }
310       }
311     }
312     else
313     if (strcasecmp(name, "emoji_type") == 0) 
314     {
315       Node* ch = qs_get_child_node(doc, child);
316       if (ch != NULL && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) 
317       {
318         dt->emoji_type = apr_pstrdup(p, qs_get_node_value(doc, ch));
319       }
320     }
321     else 
322     if (strcasecmp(name, "wp_width") == 0) 
323     {
324       Node* ch = qs_get_child_node(doc, child);
325       if (ch != NULL && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) 
326       {
327         char *vv = qs_get_node_value(doc,ch);
328         int ii;
329         for (ii=0; ii<strlen(vv); ii++) 
330         {
331           if ((vv[ii] >= '1' && vv[ii] <= '9') || vv[ii] == '0') 
332           {
333             continue;
334           }
335           break;
336         }
337         if (ii == strlen(vv)) 
338         {
339           dt->wp_width = atoi(qs_get_node_value(doc,ch));
340         }
341         else 
342         {
343           dt->wp_width = 0;
344         }
345       }
346     }
347     else
348     if (strcasecmp(name, "wp_heigh") == 0) 
349     {
350       Node* ch = qs_get_child_node(doc, child);
351       if (ch != NULL && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) 
352       {
353         char *vv = qs_get_node_value(doc,ch);
354         int ii;
355         for (ii=0; ii<strlen(vv); ii++) 
356         {
357           if ((vv[ii] >= '1' && vv[ii] <= '9') || vv[ii] == '0') 
358           {
359             continue;
360           }
361           break;
362         }
363         if (ii == strlen(vv)) 
364         {
365           dt->wp_heigh = atoi(qs_get_node_value(doc,ch));
366         }
367         else 
368         {
369           dt->wp_heigh = 0;
370         }
371       }
372     }
373     else
374     if (strcasecmp(name, "cache") == 0) 
375     {
376       Node* ch = qs_get_child_node(doc, child);
377       if (ch != NULL && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) 
378       {
379         char *vv = qs_get_node_value(doc,ch);
380         int ii;
381         for (ii=0; ii<strlen(vv); ii++) 
382         {
383           if ((vv[ii] >= '1' && vv[ii] <= '9') || vv[ii] == '0') 
384           {
385             continue;
386           }
387           break;
388         }
389         if (ii == strlen(vv)) 
390         {
391           dt->cache = atoi(qs_get_node_value(doc,ch));
392         }
393         else 
394         {
395           dt->cache = 0;
396         }
397       }
398     }
399     else
400     if (strcasecmp(name, "dpi_width") == 0) 
401     {
402       Node* ch = qs_get_child_node(doc, child);
403       if (ch != NULL && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) 
404       {
405         char *vv = qs_get_node_value(doc,ch);
406         int ii;
407         for (ii=0; ii<strlen(vv); ii++) 
408         {
409           if ((vv[ii] >= '1' && vv[ii] <= '9') || vv[ii] == '0') 
410           {
411             continue;
412           }
413           break;
414         }
415         if (ii == strlen(vv)) 
416         {
417           dt->dpi_width = atoi(qs_get_node_value(doc,ch));
418         }
419         else 
420         {
421           dt->dpi_width = 0;
422         }
423       }
424     }
425     else
426     if (strcasecmp(name, "dpi_heigh") == 0) 
427     {
428       Node* ch = qs_get_child_node(doc, child);
429       if (ch != NULL && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) 
430       {
431         char *vv = qs_get_node_value(doc,ch);
432         int ii;
433         for (ii=0; ii<strlen(vv); ii++) 
434         {
435           if ((vv[ii] >= '1' && vv[ii] <= '9') || vv[ii] == '0') 
436           {
437             continue;
438           }
439           break;
440         }
441         if (ii == strlen(vv)) 
442         {
443           dt->dpi_heigh = atoi(qs_get_node_value(doc,ch));
444         }
445         else 
446         {
447           dt->dpi_heigh = 0;
448         }
449       }
450     }
451   }
452
453   if (dt->device_id != NULL) 
454   {
455     if (dtl->table == NULL) 
456     {
457       dtl->table = dt;
458       dtl->tail = dt;
459     }
460     else 
461     {
462       dtl->tail->next = dt;
463       dtl->tail = dt;
464     }
465   }
466 }
467 /*
468  * vim:ts=2 et
469  */