2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
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.
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.
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.
29 #include "projection.h"
34 #include "transform.h"
42 remove_comment_line(char* line){
43 if (line==TEXTFILE_COMMENT_CHAR){
49 get_line(struct map_rect_priv *mr)
56 fgets(mr->line, TEXTFILE_LINE_SIZE, mr->f);
57 dbg(1,"read textfile line: %s\n", mr->line);
58 remove_comment_line(mr->line);
59 mr->lastlen=strlen(mr->line)+1;
60 if (strlen(mr->line) >= TEXTFILE_LINE_SIZE-1)
61 dbg(0, "line too long: %s\n", mr->line);
66 map_destroy_textfile(struct map_priv *m)
76 textfile_coord_rewind(void *priv_data)
81 parse_line(struct map_rect_priv *mr, int attr)
85 pos=coord_parse(mr->line, projection_mg, &mr->c);
86 if (pos < strlen(mr->line) && attr) {
87 strcpy(mr->attrs, mr->line+pos);
93 textfile_coord_get(void *priv_data, struct coord *c, int count)
95 struct map_rect_priv *mr=priv_data;
97 dbg(1,"enter, count: %d\n",count);
99 if (mr->f && !feof(mr->f) && (!mr->item.id_hi || !mr->eoc) && parse_line(mr, mr->item.id_hi)) {
101 dbg(1,"c=0x%x,0x%x\n", c->x, c->y);
116 textfile_attr_rewind(void *priv_data)
118 struct map_rect_priv *mr=priv_data;
120 mr->attr_last=attr_none;
124 textfile_encode_attr(char *attr_val, enum attr_type attr_type, struct attr *attr)
126 if (attr_type >= attr_type_int_begin && attr_type <= attr_type_int_end)
127 attr->u.num=atoi(attr_val);
129 attr->u.str=attr_val;
133 textfile_attr_get(void *priv_data, enum attr_type attr_type, struct attr *attr)
135 struct map_rect_priv *mr=priv_data;
137 dbg(1,"mr=%p attrs='%s' ", mr, mr->attrs);
138 if (attr_type != mr->attr_last) {
139 dbg(1,"reset attr_pos\n");
141 mr->attr_last=attr_type;
143 if (attr_type == attr_any) {
145 if (attr_from_line(mr->attrs,NULL,&mr->attr_pos,mr->attr, mr->attr_name)) {
146 attr_type=attr_from_name(mr->attr_name);
147 dbg(1,"found attr '%s' 0x%x\n", mr->attr_name, attr_type);
148 attr->type=attr_type;
149 textfile_encode_attr(mr->attr, attr_type, attr);
153 str=attr_to_name(attr_type);
154 dbg(1,"attr='%s' ",str);
155 if (attr_from_line(mr->attrs,str,&mr->attr_pos,mr->attr, NULL)) {
156 textfile_encode_attr(mr->attr, attr_type, attr);
161 dbg(1,"not found\n");
165 static struct item_methods methods_textfile = {
166 textfile_coord_rewind,
168 textfile_attr_rewind,
172 static struct map_rect_priv *
173 map_rect_new_textfile(struct map_priv *map, struct map_selection *sel)
175 struct map_rect_priv *mr;
178 mr=g_new0(struct map_rect_priv, 1);
186 mr->item.meth=&methods_textfile;
187 mr->item.priv_data=mr;
190 char *oargs,*args=g_strdup(map->filename),*sep=" ";
195 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);
197 for (lay=layer_town ; lay < layer_end ; lay++) {
199 args=g_strdup_printf("%s%s%d", oargs, sep, sel->order);
205 dbg(1,"popen args %s\n", args);
207 mr->f=popen(mr->args, "r");
211 dbg(0,"unable to work with pipes %s\n",map->filename);
214 mr->f=fopen(map->filename, "r");
217 dbg(0, "error opening textfile %s: %s\n",map->filename, strerror(errno));
225 map_rect_destroy_textfile(struct map_rect_priv *mr)
228 if (mr->m->is_pipe) {
241 map_rect_get_item_textfile(struct map_rect_priv *mr)
243 char *p,type[TEXTFILE_LINE_SIZE];
244 dbg(1,"map_rect_get_item_textfile id_hi=%d line=%s", mr->item.id_hi, mr->line);
250 textfile_coord_get(mr, &c, 1);
254 dbg(1,"map_rect_get_item_textfile: eof %d\n",mr->item.id_hi);
255 if (mr->m->flags & 1) {
264 if (mr->m->is_pipe) {
267 mr->f=popen(mr->args, "r");
272 fseek(mr->f, 0, SEEK_SET);
277 if ((p=strchr(mr->line,'\n')))
279 if (mr->item.id_hi) {
281 if (!parse_line(mr, 1)) {
285 dbg(1,"map_rect_get_item_textfile: point found\n");
287 mr->item.id_lo=mr->pos;
289 if (parse_line(mr, 1)) {
293 dbg(1,"map_rect_get_item_textfile: line found\n");
298 mr->item.id_lo=mr->pos;
299 strcpy(mr->attrs, mr->line);
301 dbg(1,"mr=%p attrs=%s\n", mr, mr->attrs);
303 dbg(1,"get_attrs %s\n", mr->attrs);
304 if (attr_from_line(mr->attrs,"type",NULL,type,NULL)) {
305 dbg(1,"type='%s'\n", type);
306 mr->item.type=item_from_name(type);
307 if (mr->item.type == type_none)
308 dbg(0, "Warning: type '%s' unknown\n", type);
313 mr->attr_last=attr_none;
315 dbg(1,"return attr='%s'\n", mr->attrs);
321 map_rect_get_item_byid_textfile(struct map_rect_priv *mr, int id_hi, int id_lo)
323 if (mr->m->is_pipe) {
326 mr->f=popen(mr->args, "r");
329 #endif /* _MSC_VER */
331 fseek(mr->f, id_lo, SEEK_SET);
333 mr->item.id_hi=id_hi;
334 return map_rect_get_item_textfile(mr);
337 static struct map_methods map_methods_textfile = {
340 map_destroy_textfile,
341 map_rect_new_textfile,
342 map_rect_destroy_textfile,
343 map_rect_get_item_textfile,
344 map_rect_get_item_byid_textfile,
347 static struct map_priv *
348 map_new_textfile(struct map_methods *meth, struct attr **attrs, struct callback_list *cbl)
351 struct attr *data=attr_search(attrs, NULL, attr_data);
352 struct attr *charset=attr_search(attrs, NULL, attr_charset);
353 struct attr *flags=attr_search(attrs, NULL, attr_flags);
354 struct file_wordexp *wexp;
360 dbg(1,"map_new_textfile %s\n", data->u.str);
361 wdata=g_strdup(data->u.str);
363 if (len && wdata[len-1] == '|') {
367 wexp=file_wordexp_new(wdata);
368 wexp_data=file_wordexp_get_array(wexp);
369 *meth=map_methods_textfile;
371 m=g_new0(struct map_priv, 1);
373 m->filename=g_strdup(wexp_data[0]);
376 m->flags=flags->u.num;
377 dbg(1,"map_new_textfile %s %s\n", m->filename, wdata);
379 m->charset=g_strdup(charset->u.str);
380 meth->charset=m->charset;
382 file_wordexp_destroy(wexp);
390 dbg(1,"textfile: plugin_init\n");
391 plugin_register_map_type("textfile", map_new_textfile);