/[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 - (hide annotations) (download)
Wed Mar 4 14:00:54 2015 UTC (4 years, 7 months ago) by zoff99
File MIME type: text/plain
File size: 9727 byte(s)
new market version, lots of fixes
1 zoff99 2 /**
2 zoff99 34 * 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 zoff99 2 * 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 zoff99 34 if(mr->f)
63     {
64     if (!mr->m->is_pipe)
65     {
66 zoff99 2 mr->pos=ftell(mr->f);
67 zoff99 34 }
68 zoff99 2 else
69 zoff99 34 {
70 zoff99 2 mr->pos+=mr->lastlen;
71 zoff99 34 }
72    
73 zoff99 2 fgets(mr->line, SIZE, mr->f);
74     mr->lastlen=strlen(mr->line)+1;
75 zoff99 34
76     if (strlen(mr->line) >= SIZE-1)
77     {
78 zoff99 2 printf("line too long\n");
79 zoff99 34 }
80 zoff99 2 }
81     }
82    
83     static void
84     map_destroy_textfile(struct map_priv *m)
85     {
86 zoff99 28 // dbg(1,"map_destroy_textfile\n");
87 zoff99 2 g_free(m->filename);
88 zoff99 34 if(m->charset)
89     {
90 zoff99 2 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 zoff99 34 if (pos < strlen(mr->line) && attr)
107     {
108 zoff99 2 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 zoff99 28 // dbg(1,"textfile_coord_get %d\n",count);
119 zoff99 34 while (count--)
120     {
121 zoff99 28 if (mr->f && !feof(mr->f) && (!mr->item.id_hi || !mr->eoc) && parse_line(mr, mr->item.id_hi))
122     {
123 zoff99 2 *c=mr->c;
124 zoff99 28 //dbg(1,"c=0x%x,0x%x\n", c->x, c->y);
125 zoff99 2 c++;
126     ret++;
127     get_line(mr);
128     if (mr->item.id_hi)
129 zoff99 34 {
130 zoff99 2 mr->eoc=1;
131 zoff99 34 }
132 zoff99 28 }
133     else
134     {
135 zoff99 2 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 zoff99 34 if (attr_type >= attr_type_int_begin && attr_type <= attr_type_int_end)
154     {
155 zoff99 2 attr->u.num=atoi(attr_val);
156 zoff99 34 }
157 zoff99 2 else
158 zoff99 34 {
159 zoff99 2 attr->u.str=attr_val;
160 zoff99 34 }
161 zoff99 2 }
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 zoff99 28 //dbg(1,"textfile_attr_get mr=%p attrs='%s' ", mr, mr->attrs);
169 zoff99 34 if (attr_type != mr->attr_last)
170     {
171 zoff99 28 //dbg(1,"reset attr_pos\n");
172 zoff99 2 mr->attr_pos=0;
173     mr->attr_last=attr_type;
174     }
175 zoff99 34
176     if (attr_type == attr_any)
177     {
178 zoff99 28 //dbg(1,"attr_any");
179 zoff99 34 if (attr_from_line(mr->attrs,NULL,&mr->attr_pos,mr->attr, mr->attr_name))
180     {
181 zoff99 2 attr_type=attr_from_name(mr->attr_name);
182 zoff99 28 //dbg(1,"found attr '%s' 0x%x\n", mr->attr_name, attr_type);
183 zoff99 2 attr->type=attr_type;
184     textfile_encode_attr(mr->attr, attr_type, attr);
185     return 1;
186     }
187 zoff99 34 }
188     else
189     {
190 zoff99 2 str=attr_to_name(attr_type);
191 zoff99 28 //dbg(1,"attr='%s' ",str);
192 zoff99 34 if (attr_from_line(mr->attrs,str,&mr->attr_pos,mr->attr, NULL))
193     {
194 zoff99 2 textfile_encode_attr(mr->attr, attr_type, attr);
195 zoff99 28 //dbg(1,"found\n");
196 zoff99 2 return 1;
197     }
198     }
199 zoff99 34
200 zoff99 28 //dbg(1,"not found\n");
201 zoff99 2 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 zoff99 28 //dbg(1,"map_rect_new_textfile\n");
217 zoff99 2 mr=g_new0(struct map_rect_priv, 1);
218     mr->m=map;
219     mr->sel=sel;
220     if (map->flags & 1)
221 zoff99 34 {
222 zoff99 2 mr->item.id_hi=1;
223 zoff99 34 }
224 zoff99 2 else
225 zoff99 34 {
226 zoff99 2 mr->item.id_hi=0;
227 zoff99 34 }
228    
229 zoff99 2 mr->item.id_lo=0;
230     mr->item.meth=&methods_textfile;
231     mr->item.priv_data=mr;
232 zoff99 34
233     if (map->is_pipe)
234     {
235 zoff99 2 #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 zoff99 28 //dbg(1,"popen args %s\n", args);
252 zoff99 2 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 zoff99 34 }
260     else
261     {
262 zoff99 2 mr->f=fopen(map->filename, "r");
263     }
264 zoff99 34
265 zoff99 28 if(!mr->f)
266     {
267 zoff99 2 printf("map_rect_new_textfile unable to open textfile %s\n",map->filename);
268     }
269 zoff99 34
270    
271 zoff99 2 get_line(mr);
272     return mr;
273     }
274    
275    
276     static void
277     map_rect_destroy_textfile(struct map_rect_priv *mr)
278     {
279 zoff99 28 if (mr->f)
280     {
281     if (mr->m->is_pipe)
282     {
283 zoff99 2 #ifdef HAVE_POPEN
284     pclose(mr->f);
285     #endif
286     }
287 zoff99 28 else
288     {
289 zoff99 2 fclose(mr->f);
290     }
291     }
292 zoff99 28 g_free(mr);
293 zoff99 2 }
294    
295     static struct item *
296     map_rect_get_item_textfile(struct map_rect_priv *mr)
297     {
298     char *p,type[SIZE];
299 zoff99 28 //dbg(1,"map_rect_get_item_textfile id_hi=%d line=%s", mr->item.id_hi, mr->line);
300 zoff99 34 if (!mr->f)
301     {
302 zoff99 2 return NULL;
303     }
304 zoff99 34
305     while (mr->more)
306     {
307 zoff99 2 struct coord c;
308     textfile_coord_get(mr, &c, 1);
309     }
310 zoff99 34
311     for(;;)
312     {
313     if (feof(mr->f))
314     {
315 zoff99 28 //dbg(1,"map_rect_get_item_textfile: eof %d\n",mr->item.id_hi);
316 zoff99 34 if (mr->m->flags & 1)
317     {
318 zoff99 2 if (!mr->item.id_hi)
319     return NULL;
320     mr->item.id_hi=0;
321 zoff99 34 }
322     else
323     {
324 zoff99 2 if (mr->item.id_hi)
325     return NULL;
326     mr->item.id_hi=1;
327     }
328 zoff99 34
329     if (mr->m->is_pipe)
330     {
331 zoff99 2 #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 zoff99 34 }
338     else
339     {
340 zoff99 2 fseek(mr->f, 0, SEEK_SET);
341     clearerr(mr->f);
342     }
343 zoff99 34
344 zoff99 2 get_line(mr);
345     }
346 zoff99 34
347     if ((p=strchr(mr->line,'\n')))
348     {
349 zoff99 2 *p='\0';
350 zoff99 34 }
351    
352     if (mr->item.id_hi)
353     {
354 zoff99 2 mr->attrs[0]='\0';
355 zoff99 34
356     if (!parse_line(mr, 1))
357     {
358 zoff99 2 get_line(mr);
359     continue;
360     }
361 zoff99 28 //dbg(1,"map_rect_get_item_textfile: point found\n");
362 zoff99 2 mr->eoc=0;
363     mr->item.id_lo=mr->pos;
364 zoff99 34 }
365     else
366     {
367     if (parse_line(mr, 1))
368     {
369 zoff99 2 get_line(mr);
370     continue;
371     }
372 zoff99 34
373 zoff99 28 //dbg(1,"map_rect_get_item_textfile: line found\n");
374 zoff99 34
375     if (! mr->line[0])
376     {
377 zoff99 2 get_line(mr);
378     continue;
379     }
380 zoff99 34
381 zoff99 2 mr->item.id_lo=mr->pos;
382     strcpy(mr->attrs, mr->line);
383     get_line(mr);
384 zoff99 28 //dbg(1,"mr=%p attrs=%s\n", mr, mr->attrs);
385 zoff99 2 }
386 zoff99 34
387 zoff99 28 //dbg(1,"get_attrs %s\n", mr->attrs);
388 zoff99 34
389     if (attr_from_line(mr->attrs,"type",NULL,type,NULL))
390     {
391 zoff99 28 //dbg(1,"type='%s'\n", type);
392 zoff99 2 mr->item.type=item_from_name(type);
393 zoff99 34
394 zoff99 2 if (mr->item.type == type_none)
395     printf("Warning: type '%s' unknown\n", type);
396 zoff99 34
397     }
398     else
399     {
400 zoff99 2 get_line(mr);
401     continue;
402     }
403 zoff99 34
404 zoff99 2 mr->attr_last=attr_none;
405     mr->more=1;
406 zoff99 28 //dbg(1,"return attr='%s'\n", mr->attrs);
407 zoff99 2 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 zoff99 34 if (mr->m->is_pipe)
415     {
416 zoff99 2 #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 zoff99 34 }
423     else
424     {
425 zoff99 2 fseek(mr->f, id_lo, SEEK_SET);
426 zoff99 34 }
427    
428 zoff99 2 get_line(mr);
429     mr->item.id_hi=id_hi;
430 zoff99 34
431 zoff99 2 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 zoff99 40 struct map_priv *
445 zoff99 2 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 zoff99 34
456 zoff99 2 if (! data)
457 zoff99 31 {
458 zoff99 2 return NULL;
459     }
460 zoff99 31
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 zoff99 2 *meth=map_methods_textfile;
472    
473     m=g_new0(struct map_priv, 1);
474     m->id=++map_id;
475 zoff99 31 m->filename=g_strdup(data->u.str);
476 zoff99 2 m->is_pipe=is_pipe;
477 zoff99 34
478 zoff99 31 if (flags)
479     {
480 zoff99 2 m->flags=flags->u.num;
481 zoff99 31 }
482     // dbg(1,"map_new_textfile %s %s\n", m->filename, wdata);
483 zoff99 34
484 zoff99 31 if (charset)
485     {
486 zoff99 2 m->charset=g_strdup(charset->u.str);
487     meth->charset=m->charset;
488     }
489 zoff99 34
490 zoff99 31 //file_wordexp_destroy(wexp);
491     //g_free(wdata);
492 zoff99 34
493 zoff99 2 return m;
494     }
495    
496 zoff99 40 #ifdef PLUGSSS
497 zoff99 2 void
498     plugin_init(void)
499     {
500 zoff99 28 //dbg(1,"textfile: plugin_init\n");
501 zoff99 2 plugin_register_map_type("textfile", map_new_textfile);
502     }
503 zoff99 40 #endif
504 zoff99 2

   
Visit the ZANavi Wiki