Tizen 2.1 base
[platform/upstream/hplip.git] / scan / sane / xml.c
1 /************************************************************************************\
2
3   xml.c - HP SANE backend support for xml parsing
4
5   (c) 2008 Copyright Hewlett-Packard Development Company, LP
6
7   Permission is hereby granted, free of charge, to any person obtaining a copy 
8   of this software and associated documentation files (the "Software"), to deal 
9   in the Software without restriction, including without limitation the rights 
10   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
11   of the Software, and to permit persons to whom the Software is furnished to do 
12   so, subject to the following conditions:
13
14   The above copyright notice and this permission notice shall be included in all
15   copies or substantial portions of the Software.
16
17   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
18   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
19   FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
20   COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
21   IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
22   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24   Author: David Suffield
25
26 \************************************************************************************/
27
28 #ifndef _GNU_SOURCE
29 #define _GNU_SOURCE
30 #endif
31
32 #include <string.h>
33 #include <stdlib.h>
34
35 int __attribute__ ((visibility ("hidden"))) get_array_size(const char *tag)
36 {
37    char *p, *tail;
38
39    if ((p = strstr(tag, "arraySize=\"")))
40       return strtol(p+11, &tail, 10);
41    else
42       return 0;
43 }
44
45 /* Get xml element from the buffer. The returned element is zero terminated. */
46 int __attribute__ ((visibility ("hidden"))) get_element(const char *buf, int buf_size, char *element, int element_size, char **tail)
47 {
48    int i, j;
49
50    element[0]=0;
51
52    for (i=0, j=0; buf[i] != '<' && j < (element_size-1) && i < buf_size; i++)
53       element[j++] = buf[i];
54
55    element[j]=0;   /* zero terminate */
56
57    if (tail != NULL)
58       *tail = (char *)buf + i;   /* tail points to next tag */
59
60    return j;  /* length does not include zero termination */
61 }
62
63 /* Get next xml tag from the buffer. The returned xml tag is zero terminated. */
64 int __attribute__ ((visibility ("hidden"))) get_tag(const char *buf, int buf_size, char *tag, int tag_size, char **tail)
65 {
66    int i=0, j=0, dd=0, lf=0;
67
68    tag[0]=0;
69
70    while (1)
71    {
72       for (; buf[i] != '<' && i < buf_size; i++);  /* eat up space before '<' */
73
74       if (buf[i] != '<')
75          break;
76
77       if (i < (buf_size-4) && (strncmp(&buf[i], "<!--", 4) == 0))
78       {
79          for (; buf[i] != '>' && i < buf_size; i++);  /* eat comment line */
80          i++;
81          continue;
82       }
83    
84       i++; /* eat '<' */
85
86       for (j=0; buf[i] != '>' && j < (tag_size-1) && i < buf_size; i++)
87       {
88          if (buf[i] == '\r')
89          {
90             tag[j++] = '\n';   /* convert CR to LF */
91             lf=1;            /* set remove back-to-back LF flag */
92          }
93          else if (buf[i] == '\n')
94          {
95             if (!lf)
96                tag[j++] = buf[i]; 
97          }
98          else if (buf[i] == ' ')
99          {
100             if (!dd)
101             {
102                tag[j++] = buf[i];
103                dd=1;             /* set remove back-to-back space flag */
104             }
105          }
106          else
107          {
108             tag[j++] = buf[i];
109             dd=0;
110             lf=0;
111          }
112       }
113       break;
114    }
115  
116    if (i < buf_size)
117       i++;     /* eat '>' */
118
119    tag[j]=0;  /* zero terminate */
120
121    if (tail != NULL)
122       *tail = (char *)buf + i;   /* tail points to next tag */
123
124    return j;   /* length does no include zero termination */
125 }
126