/[zanavi_public1]/navit/navit/map/textfile/textfile.c
ZANavi

Contents of /navit/navit/map/textfile/textfile.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 40 - (show annotations) (download)
Wed Mar 4 14:00:54 2015 UTC (9 years ago) by zoff99
File MIME type: text/plain
File size: 9727 byte(s)
new market version, lots of fixes
1 /**
2 * ZANavi, Zoff Android Navigation system.
3 * Copyright (C) 2011-2013 Zoff <zoff@zoff.cc>
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
20 /**
21 * Navit, a modular navigation system.
22 * Copyright (C) 2005-2008 Navit Team
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * version 2 as published by the Free Software Foundation.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the
35 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
36 * Boston, MA 02110-1301, USA.
37 */
38
39 #include <glib.h>
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <math.h>
44 #include "config.h"
45 #include "debug.h"
46 #include "plugin.h"
47 #include "projection.h"
48 #include "item.h"
49 #include "map.h"
50 #include "maptype.h"
51 #include "attr.h"
52 #include "transform.h"
53 #include "file.h"
54
55 #include "textfile.h"
56
57 static int map_id;
58
59 static void
60 get_line(struct map_rect_priv *mr)
61 {
62 if(mr->f)
63 {
64 if (!mr->m->is_pipe)
65 {
66 mr->pos=ftell(mr->f);
67 }
68 else
69 {
70 mr->pos+=mr->lastlen;
71 }
72
73 fgets(mr->line, SIZE, mr->f);
74 mr->lastlen=strlen(mr->line)+1;
75
76 if (strlen(mr->line) >= SIZE-1)
77 {
78 printf("line too long\n");
79 }
80 }
81 }
82
83 static void
84 map_destroy_textfile(struct map_priv *m)
85 {
86 // dbg(1,"map_destroy_textfile\n");
87 g_free(m->filename);
88 if(m->charset)
89 {
90 g_free(m->charset);
91 }
92 g_free(m);
93 }
94
95 static void
96 textfile_coord_rewind(void *priv_data)
97 {
98 }
99
100 static int
101 parse_line(struct map_rect_priv *mr, int attr)
102 {
103 int pos;
104
105 pos=coord_parse(mr->line, projection_mg, &mr->c);
106 if (pos < strlen(mr->line) && attr)
107 {
108 strcpy(mr->attrs, mr->line+pos);
109 }
110 return pos;
111 }
112
113 static int
114 textfile_coord_get(void *priv_data, struct coord *c, int count)
115 {
116 struct map_rect_priv *mr=priv_data;
117 int ret=0;
118 // dbg(1,"textfile_coord_get %d\n",count);
119 while (count--)
120 {
121 if (mr->f && !feof(mr->f) && (!mr->item.id_hi || !mr->eoc) && parse_line(mr, mr->item.id_hi))
122 {
123 *c=mr->c;
124 //dbg(1,"c=0x%x,0x%x\n", c->x, c->y);
125 c++;
126 ret++;
127 get_line(mr);
128 if (mr->item.id_hi)
129 {
130 mr->eoc=1;
131 }
132 }
133 else
134 {
135 mr->more=0;
136 break;
137 }
138 }
139 return ret;
140 }
141
142 static void
143 textfile_attr_rewind(void *priv_data)
144 {
145 struct map_rect_priv *mr=priv_data;
146 mr->attr_pos=0;
147 mr->attr_last=attr_none;
148 }
149
150 static void
151 textfile_encode_attr(char *attr_val, enum attr_type attr_type, struct attr *attr)
152 {
153 if (attr_type >= attr_type_int_begin && attr_type <= attr_type_int_end)
154 {
155 attr->u.num=atoi(attr_val);
156 }
157 else
158 {
159 attr->u.str=attr_val;
160 }
161 }
162
163 static int
164 textfile_attr_get(void *priv_data, enum attr_type attr_type, struct attr *attr)
165 {
166 struct map_rect_priv *mr=priv_data;
167 char *str=NULL;
168 //dbg(1,"textfile_attr_get mr=%p attrs='%s' ", mr, mr->attrs);
169 if (attr_type != mr->attr_last)
170 {
171 //dbg(1,"reset attr_pos\n");
172 mr->attr_pos=0;
173 mr->attr_last=attr_type;
174 }
175
176 if (attr_type == attr_any)
177 {
178 //dbg(1,"attr_any");
179 if (attr_from_line(mr->attrs,NULL,&mr->attr_pos,mr->attr, mr->attr_name))
180 {
181 attr_type=attr_from_name(mr->attr_name);
182 //dbg(1,"found attr '%s' 0x%x\n", mr->attr_name, attr_type);
183 attr->type=attr_type;
184 textfile_encode_attr(mr->attr, attr_type, attr);
185 return 1;
186 }
187 }
188 else
189 {
190 str=attr_to_name(attr_type);
191 //dbg(1,"attr='%s' ",str);
192 if (attr_from_line(mr->attrs,str,&mr->attr_pos,mr->attr, NULL))
193 {
194 textfile_encode_attr(mr->attr, attr_type, attr);
195 //dbg(1,"found\n");
196 return 1;
197 }
198 }
199
200 //dbg(1,"not found\n");
201 return 0;
202 }
203
204 static struct item_methods methods_textfile = {
205 textfile_coord_rewind,
206 textfile_coord_get,
207 textfile_attr_rewind,
208 textfile_attr_get,
209 };
210
211 static struct map_rect_priv *
212 map_rect_new_textfile(struct map_priv *map, struct map_selection *sel)
213 {
214 struct map_rect_priv *mr;
215
216 //dbg(1,"map_rect_new_textfile\n");
217 mr=g_new0(struct map_rect_priv, 1);
218 mr->m=map;
219 mr->sel=sel;
220 if (map->flags & 1)
221 {
222 mr->item.id_hi=1;
223 }
224 else
225 {
226 mr->item.id_hi=0;
227 }
228
229 mr->item.id_lo=0;
230 mr->item.meth=&methods_textfile;
231 mr->item.priv_data=mr;
232
233 if (map->is_pipe)
234 {
235 #ifdef HAVE_POPEN
236 char *oargs,*args=g_strdup(map->filename),*sep=" ";
237 enum layer_type lay;
238 g_free(mr->args);
239 while (sel) {
240 oargs=args;
241 args=g_strdup_printf("%s 0x%x 0x%x 0x%x 0x%x", oargs, sel->u.c_rect.lu.x, sel->u.c_rect.lu.y, sel->u.c_rect.rl.x, sel->u.c_rect.rl.y);
242 g_free(oargs);
243 for (lay=layer_town ; lay < layer_end ; lay++) {
244 oargs=args;
245 args=g_strdup_printf("%s%s%d", oargs, sep, sel->order);
246 g_free(oargs);
247 sep=",";
248 }
249 sel=sel->next;
250 }
251 //dbg(1,"popen args %s\n", args);
252 mr->args=args;
253 mr->f=popen(mr->args, "r");
254 mr->pos=0;
255 mr->lastlen=0;
256 #else
257 dbg(0,"map_rect_new_textfile is unable to work with pipes %s\n",map->filename);
258 #endif
259 }
260 else
261 {
262 mr->f=fopen(map->filename, "r");
263 }
264
265 if(!mr->f)
266 {
267 printf("map_rect_new_textfile unable to open textfile %s\n",map->filename);
268 }
269
270
271 get_line(mr);
272 return mr;
273 }
274
275
276 static void
277 map_rect_destroy_textfile(struct map_rect_priv *mr)
278 {
279 if (mr->f)
280 {
281 if (mr->m->is_pipe)
282 {
283 #ifdef HAVE_POPEN
284 pclose(mr->f);
285 #endif
286 }
287 else
288 {
289 fclose(mr->f);
290 }
291 }
292 g_free(mr);
293 }
294
295 static struct item *
296 map_rect_get_item_textfile(struct map_rect_priv *mr)
297 {
298 char *p,type[SIZE];
299 //dbg(1,"map_rect_get_item_textfile id_hi=%d line=%s", mr->item.id_hi, mr->line);
300 if (!mr->f)
301 {
302 return NULL;
303 }
304
305 while (mr->more)
306 {
307 struct coord c;
308 textfile_coord_get(mr, &c, 1);
309 }
310
311 for(;;)
312 {
313 if (feof(mr->f))
314 {
315 //dbg(1,"map_rect_get_item_textfile: eof %d\n",mr->item.id_hi);
316 if (mr->m->flags & 1)
317 {
318 if (!mr->item.id_hi)
319 return NULL;
320 mr->item.id_hi=0;
321 }
322 else
323 {
324 if (mr->item.id_hi)
325 return NULL;
326 mr->item.id_hi=1;
327 }
328
329 if (mr->m->is_pipe)
330 {
331 #ifdef HAVE_POPEN
332 pclose(mr->f);
333 mr->f=popen(mr->args, "r");
334 mr->pos=0;
335 mr->lastlen=0;
336 #endif
337 }
338 else
339 {
340 fseek(mr->f, 0, SEEK_SET);
341 clearerr(mr->f);
342 }
343
344 get_line(mr);
345 }
346
347 if ((p=strchr(mr->line,'\n')))
348 {
349 *p='\0';
350 }
351
352 if (mr->item.id_hi)
353 {
354 mr->attrs[0]='\0';
355
356 if (!parse_line(mr, 1))
357 {
358 get_line(mr);
359 continue;
360 }
361 //dbg(1,"map_rect_get_item_textfile: point found\n");
362 mr->eoc=0;
363 mr->item.id_lo=mr->pos;
364 }
365 else
366 {
367 if (parse_line(mr, 1))
368 {
369 get_line(mr);
370 continue;
371 }
372
373 //dbg(1,"map_rect_get_item_textfile: line found\n");
374
375 if (! mr->line[0])
376 {
377 get_line(mr);
378 continue;
379 }
380
381 mr->item.id_lo=mr->pos;
382 strcpy(mr->attrs, mr->line);
383 get_line(mr);
384 //dbg(1,"mr=%p attrs=%s\n", mr, mr->attrs);
385 }
386
387 //dbg(1,"get_attrs %s\n", mr->attrs);
388
389 if (attr_from_line(mr->attrs,"type",NULL,type,NULL))
390 {
391 //dbg(1,"type='%s'\n", type);
392 mr->item.type=item_from_name(type);
393
394 if (mr->item.type == type_none)
395 printf("Warning: type '%s' unknown\n", type);
396
397 }
398 else
399 {
400 get_line(mr);
401 continue;
402 }
403
404 mr->attr_last=attr_none;
405 mr->more=1;
406 //dbg(1,"return attr='%s'\n", mr->attrs);
407 return &mr->item;
408 }
409 }
410
411 static struct item *
412 map_rect_get_item_byid_textfile(struct map_rect_priv *mr, int id_hi, int id_lo)
413 {
414 if (mr->m->is_pipe)
415 {
416 #ifndef _MSC_VER
417 pclose(mr->f);
418 mr->f=popen(mr->args, "r");
419 mr->pos=0;
420 mr->lastlen=0;
421 #endif /* _MSC_VER */
422 }
423 else
424 {
425 fseek(mr->f, id_lo, SEEK_SET);
426 }
427
428 get_line(mr);
429 mr->item.id_hi=id_hi;
430
431 return map_rect_get_item_textfile(mr);
432 }
433
434 static struct map_methods map_methods_textfile = {
435 projection_mg,
436 "iso8859-1",
437 map_destroy_textfile,
438 map_rect_new_textfile,
439 map_rect_destroy_textfile,
440 map_rect_get_item_textfile,
441 map_rect_get_item_byid_textfile,
442 };
443
444 struct map_priv *
445 map_new_textfile(struct map_methods *meth, struct attr **attrs, struct callback_list *cbl)
446 {
447 struct map_priv *m;
448 struct attr *data=attr_search(attrs, NULL, attr_data);
449 struct attr *charset=attr_search(attrs, NULL, attr_charset);
450 struct attr *flags=attr_search(attrs, NULL, attr_flags);
451 struct file_wordexp *wexp;
452 int len,is_pipe=0;
453 char *wdata;
454 char **wexp_data;
455
456 if (! data)
457 {
458 return NULL;
459 }
460
461 // ** dbg(1,"map_new_textfile %s\n", data->u.str);
462
463 //wdata=g_strdup(data->u.str);
464 //len=strlen(wdata);
465 //if (len && wdata[len-1] == '|') {
466 // wdata[len-1]='\0';
467 // is_pipe=1;
468 //}
469 //wexp=file_wordexp_new(wdata);
470 //wexp_data=file_wordexp_get_array(wexp);
471 *meth=map_methods_textfile;
472
473 m=g_new0(struct map_priv, 1);
474 m->id=++map_id;
475 m->filename=g_strdup(data->u.str);
476 m->is_pipe=is_pipe;
477
478 if (flags)
479 {
480 m->flags=flags->u.num;
481 }
482 // dbg(1,"map_new_textfile %s %s\n", m->filename, wdata);
483
484 if (charset)
485 {
486 m->charset=g_strdup(charset->u.str);
487 meth->charset=m->charset;
488 }
489
490 //file_wordexp_destroy(wexp);
491 //g_free(wdata);
492
493 return m;
494 }
495
496 #ifdef PLUGSSS
497 void
498 plugin_init(void)
499 {
500 //dbg(1,"textfile: plugin_init\n");
501 plugin_register_map_type("textfile", map_new_textfile);
502 }
503 #endif
504

   
Visit the ZANavi Wiki