/[zanavi_public1]/navit/navit/maptool/osm_xml.c
ZANavi

Contents of /navit/navit/maptool/osm_xml.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8 - (hide annotations) (download)
Fri Oct 28 21:39:42 2011 UTC (9 years, 2 months ago) by zoff99
File MIME type: text/plain
File size: 5630 byte(s)
import
1 zoff99 8 /**
2     * Navit, a modular navigation system.
3     * Copyright (C) 2005-2011 Navit Team
4     *
5     * This program is free software; you can redistribute it and/or
6     * modify it under the terms of the GNU General Public License
7     * version 2 as published by the Free Software Foundation.
8     *
9     * This program is distributed in the hope that it will be useful,
10     * but WITHOUT ANY WARRANTY; without even the implied warranty of
11     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     * GNU General Public License for more details.
13     *
14     * You should have received a copy of the GNU General Public License
15     * along with this program; if not, write to the
16     * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17     * Boston, MA 02110-1301, USA.
18     */
19     #include <string.h>
20     #include <stdlib.h>
21     #include <math.h>
22     #include <unistd.h>
23     #include "maptool.h"
24    
25     int
26     osm_xml_get_attribute(char *xml, char *attribute, char *buffer, int buffer_size)
27     {
28     int len=strlen(attribute);
29     char *pos,*i,s,attr[len+2];
30     strcpy(attr, attribute);
31     strcpy(attr+len, "=");
32     pos=strstr(xml, attr);
33     if (! pos)
34     return 0;
35     pos+=len+1;
36     s=*pos++;
37     if (! s)
38     return 0;
39     i=strchr(pos, s);
40     if (! i)
41     return 0;
42     if (i - pos > buffer_size) {
43     fprintf(stderr,"Buffer overflow %ld vs %d\n", (long)(i-pos), buffer_size);
44     return 0;
45     }
46     strncpy(buffer, pos, i-pos);
47     buffer[i-pos]='\0';
48     return 1;
49     }
50    
51     static struct entity {
52     char *entity;
53     char c;
54     } entities[]= {
55     {"&quot;",'"'},
56     {"&apos;",'\''},
57     {"&amp;",'&'},
58     {"&lt;",'<'},
59     {"&gt;",'>'},
60     {"&#34;",'"'},
61     {"&#39;",'\''},
62     {"&#38;",'&'},
63     {"&#60;",'<'},
64     {"&#62;",'>'},
65     };
66    
67     void
68     osm_xml_decode_entities(char *buffer)
69     {
70     char *pos=buffer;
71     int i,len,found;
72    
73     while ((pos=strchr(pos, '&'))) {
74     found=0;
75     for (i = 0 ; i < sizeof(entities)/sizeof(struct entity); i++) {
76     len=strlen(entities[i].entity);
77     if (!strncmp(pos, entities[i].entity, len)) {
78     *pos=entities[i].c;
79     memmove(pos+1, pos+len, strlen(pos+len)+1);
80     found=1;
81     break;
82     }
83     }
84     pos++;
85     }
86     }
87    
88     static int
89     parse_tag(char *p)
90     {
91     char k_buffer[BUFFER_SIZE];
92     char v_buffer[BUFFER_SIZE];
93     if (!osm_xml_get_attribute(p, "k", k_buffer, BUFFER_SIZE))
94     return 0;
95     if (!osm_xml_get_attribute(p, "v", v_buffer, BUFFER_SIZE))
96     return 0;
97     osm_xml_decode_entities(v_buffer);
98     osm_add_tag(k_buffer, v_buffer);
99     return 1;
100     }
101    
102    
103     static int
104     parse_node(char *p)
105     {
106     char id_buffer[BUFFER_SIZE];
107     char lat_buffer[BUFFER_SIZE];
108     char lon_buffer[BUFFER_SIZE];
109     if (!osm_xml_get_attribute(p, "id", id_buffer, BUFFER_SIZE))
110     return 0;
111     if (!osm_xml_get_attribute(p, "lat", lat_buffer, BUFFER_SIZE))
112     return 0;
113     if (!osm_xml_get_attribute(p, "lon", lon_buffer, BUFFER_SIZE))
114     return 0;
115     osm_add_node(atoll(id_buffer), atof(lat_buffer), atof(lon_buffer));
116     return 1;
117     }
118    
119    
120     static int
121     parse_way(char *p)
122     {
123     char id_buffer[BUFFER_SIZE];
124     if (!osm_xml_get_attribute(p, "id", id_buffer, BUFFER_SIZE))
125     return 0;
126     osm_add_way(atoll(id_buffer));
127     return 1;
128     }
129    
130     static int
131     parse_relation(char *p)
132     {
133     char id_buffer[BUFFER_SIZE];
134     if (!osm_xml_get_attribute(p, "id", id_buffer, BUFFER_SIZE))
135     return 0;
136     osm_add_relation(atoll(id_buffer));
137     return 1;
138     }
139    
140     static int
141     parse_member(char *p)
142     {
143     char type_buffer[BUFFER_SIZE];
144     char ref_buffer[BUFFER_SIZE];
145     char role_buffer[BUFFER_SIZE];
146     int type;
147     if (!osm_xml_get_attribute(p, "type", type_buffer, BUFFER_SIZE))
148     return 0;
149     if (!osm_xml_get_attribute(p, "ref", ref_buffer, BUFFER_SIZE))
150     return 0;
151     if (!osm_xml_get_attribute(p, "role", role_buffer, BUFFER_SIZE))
152     return 0;
153     if (!strcmp(type_buffer,"node"))
154     type=1;
155     else if (!strcmp(type_buffer,"way"))
156     type=2;
157     else if (!strcmp(type_buffer,"relation"))
158     type=3;
159     else {
160     fprintf(stderr,"Unknown type %s\n",type_buffer);
161     type=0;
162     }
163     osm_add_member(type, atoll(ref_buffer), role_buffer);
164    
165     return 1;
166     }
167    
168     static int
169     parse_nd(char *p)
170     {
171     char ref_buffer[BUFFER_SIZE];
172     if (!osm_xml_get_attribute(p, "ref", ref_buffer, BUFFER_SIZE))
173     return 0;
174     osm_add_nd(atoll(ref_buffer));
175     return 1;
176     }
177    
178     int
179     map_collect_data_osm(FILE *in, FILE *out_ways, FILE *out_nodes, FILE *out_turn_restrictions, FILE *out_boundaries)
180     {
181     int size=BUFFER_SIZE;
182     char buffer[size];
183     char *p;
184     sig_alrm(0);
185     while (fgets(buffer, size, in)) {
186     p=strchr(buffer,'<');
187     if (! p) {
188     fprintf(stderr,"WARNING: wrong line %s\n", buffer);
189     continue;
190     }
191     if (!strncmp(p, "<?xml ",6)) {
192     } else if (!strncmp(p, "<osm ",5)) {
193     } else if (!strncmp(p, "<bound ",7)) {
194     } else if (!strncmp(p, "<node ",6)) {
195     if (!parse_node(p))
196     fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
197     processed_nodes++;
198     } else if (!strncmp(p, "<tag ",5)) {
199     if (!parse_tag(p))
200     fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
201     } else if (!strncmp(p, "<way ",5)) {
202     if (!parse_way(p))
203     fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
204     processed_ways++;
205     } else if (!strncmp(p, "<nd ",4)) {
206     if (!parse_nd(p))
207     fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
208     } else if (!strncmp(p, "<relation ",10)) {
209     if (!parse_relation(p))
210     fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
211     processed_relations++;
212     } else if (!strncmp(p, "<member ",8)) {
213     if (!parse_member(p))
214     fprintf(stderr,"WARNING: failed to parse %s\n", buffer);
215     } else if (!strncmp(p, "</node>",7)) {
216     osm_end_node(out_nodes);
217     } else if (!strncmp(p, "</way>",6)) {
218     osm_end_way(out_ways);
219     } else if (!strncmp(p, "</relation>",11)) {
220     osm_end_relation(out_turn_restrictions, out_boundaries);
221     } else if (!strncmp(p, "</osm>",6)) {
222     } else {
223     fprintf(stderr,"WARNING: unknown tag in %s\n", buffer);
224     }
225     }
226     sig_alrm(0);
227     sig_alrm_end();
228     return 1;
229     }

   
Visit the ZANavi Wiki