1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/**
* Navit, a modular navigation system.
* Copyright (C) 2005-2008 Navit Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <glib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "config.h"
#include "debug.h"
#include "plugin.h"
#include "projection.h"
#include "item.h"
#include "map.h"
#include "maptype.h"
#include "attr.h"
#include "transform.h"
#include "file.h"
#include "textfile.h"
static int map_id;
static void
get_line(struct map_rect_priv *mr)
{
if(mr->f) {
if (!mr->m->is_pipe)
mr->pos=ftell(mr->f);
else
mr->pos+=mr->lastlen;
fgets(mr->line, SIZE, mr->f);
mr->lastlen=strlen(mr->line)+1;
if (strlen(mr->line) >= SIZE-1)
printf("line too long\n");
}
}
static void
map_destroy_textfile(struct map_priv *m)
{
dbg(1,"map_destroy_textfile\n");
g_free(m->filename);
if(m->charset) {
g_free(m->charset);
}
g_free(m);
}
static void
textfile_coord_rewind(void *priv_data)
{
}
static int
parse_line(struct map_rect_priv *mr, int attr)
{
int pos;
pos=coord_parse(mr->line, projection_mg, &mr->c);
if (pos < strlen(mr->line) && attr) {
strcpy(mr->attrs, mr->line+pos);
}
return pos;
}
static int
textfile_coord_get(void *priv_data, struct coord *c, int count)
{
struct map_rect_priv *mr=priv_data;
int ret=0;
dbg(1,"textfile_coord_get %d\n",count);
while (count--) {
if (mr->f && !feof(mr->f) && (!mr->item.id_hi || !mr->eoc) && parse_line(mr, mr->item.id_hi)) {
*c=mr->c;
dbg(1,"c=0x%x,0x%x\n", c->x, c->y);
c++;
ret++;
get_line(mr);
if (mr->item.id_hi)
mr->eoc=1;
} else {
mr->more=0;
break;
}
}
return ret;
}
static void
textfile_attr_rewind(void *priv_data)
{
struct map_rect_priv *mr=priv_data;
mr->attr_pos=0;
mr->attr_last=attr_none;
}
static void
textfile_encode_attr(char *attr_val, enum attr_type attr_type, struct attr *attr)
{
if (attr_type >= attr_type_int_begin && attr_type <= attr_type_int_end)
attr->u.num=atoi(attr_val);
else
attr->u.str=attr_val;
}
static int
textfile_attr_get(void *priv_data, enum attr_type attr_type, struct attr *attr)
{
struct map_rect_priv *mr=priv_data;
char *str=NULL;
dbg(1,"textfile_attr_get mr=%p attrs='%s' ", mr, mr->attrs);
if (attr_type != mr->attr_last) {
dbg(1,"reset attr_pos\n");
mr->attr_pos=0;
mr->attr_last=attr_type;
}
if (attr_type == attr_any) {
dbg(1,"attr_any");
if (attr_from_line(mr->attrs,NULL,&mr->attr_pos,mr->attr, mr->attr_name)) {
attr_type=attr_from_name(mr->attr_name);
dbg(1,"found attr '%s' 0x%x\n", mr->attr_name, attr_type);
attr->type=attr_type;
textfile_encode_attr(mr->attr, attr_type, attr);
return 1;
}
} else {
str=attr_to_name(attr_type);
dbg(1,"attr='%s' ",str);
if (attr_from_line(mr->attrs,str,&mr->attr_pos,mr->attr, NULL)) {
textfile_encode_attr(mr->attr, attr_type, attr);
dbg(1,"found\n");
return 1;
}
}
dbg(1,"not found\n");
return 0;
}
static struct item_methods methods_textfile = {
textfile_coord_rewind,
textfile_coord_get,
textfile_attr_rewind,
textfile_attr_get,
};
static struct map_rect_priv *
map_rect_new_textfile(struct map_priv *map, struct map_selection *sel)
{
struct map_rect_priv *mr;
dbg(1,"map_rect_new_textfile\n");
mr=g_new0(struct map_rect_priv, 1);
mr->m=map;
mr->sel=sel;
if (map->flags & 1)
mr->item.id_hi=1;
else
mr->item.id_hi=0;
mr->item.id_lo=0;
mr->item.meth=&methods_textfile;
mr->item.priv_data=mr;
if (map->is_pipe) {
#ifdef HAVE_POPEN
char *oargs,*args=g_strdup(map->filename),*sep=" ";
enum layer_type lay;
g_free(mr->args);
while (sel) {
oargs=args;
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);
g_free(oargs);
for (lay=layer_town ; lay < layer_end ; lay++) {
oargs=args;
args=g_strdup_printf("%s%s%d", oargs, sep, sel->order);
g_free(oargs);
sep=",";
}
sel=sel->next;
}
dbg(1,"popen args %s\n", args);
mr->args=args;
mr->f=popen(mr->args, "r");
mr->pos=0;
mr->lastlen=0;
#else
dbg(0,"map_rect_new_textfile is unable to work with pipes %s\n",map->filename);
#endif
} else {
mr->f=fopen(map->filename, "r");
}
if(!mr->f) {
printf("map_rect_new_textfile unable to open textfile %s\n",map->filename);
}
get_line(mr);
return mr;
}
static void
map_rect_destroy_textfile(struct map_rect_priv *mr)
{
if (mr->f) {
if (mr->m->is_pipe) {
#ifdef HAVE_POPEN
pclose(mr->f);
#endif
}
else {
fclose(mr->f);
}
}
g_free(mr);
}
static struct item *
map_rect_get_item_textfile(struct map_rect_priv *mr)
{
char *p,type[SIZE];
dbg(1,"map_rect_get_item_textfile id_hi=%d line=%s", mr->item.id_hi, mr->line);
if (!mr->f) {
return NULL;
}
while (mr->more) {
struct coord c;
textfile_coord_get(mr, &c, 1);
}
for(;;) {
if (feof(mr->f)) {
dbg(1,"map_rect_get_item_textfile: eof %d\n",mr->item.id_hi);
if (mr->m->flags & 1) {
if (!mr->item.id_hi)
return NULL;
mr->item.id_hi=0;
} else {
if (mr->item.id_hi)
return NULL;
mr->item.id_hi=1;
}
if (mr->m->is_pipe) {
#ifdef HAVE_POPEN
pclose(mr->f);
mr->f=popen(mr->args, "r");
mr->pos=0;
mr->lastlen=0;
#endif
} else {
fseek(mr->f, 0, SEEK_SET);
clearerr(mr->f);
}
get_line(mr);
}
if ((p=strchr(mr->line,'\n')))
*p='\0';
if (mr->item.id_hi) {
mr->attrs[0]='\0';
if (!parse_line(mr, 1)) {
get_line(mr);
continue;
}
dbg(1,"map_rect_get_item_textfile: point found\n");
mr->eoc=0;
mr->item.id_lo=mr->pos;
} else {
if (parse_line(mr, 1)) {
get_line(mr);
continue;
}
dbg(1,"map_rect_get_item_textfile: line found\n");
if (! mr->line[0]) {
get_line(mr);
continue;
}
mr->item.id_lo=mr->pos;
strcpy(mr->attrs, mr->line);
get_line(mr);
dbg(1,"mr=%p attrs=%s\n", mr, mr->attrs);
}
dbg(1,"get_attrs %s\n", mr->attrs);
if (attr_from_line(mr->attrs,"type",NULL,type,NULL)) {
dbg(1,"type='%s'\n", type);
mr->item.type=item_from_name(type);
if (mr->item.type == type_none)
printf("Warning: type '%s' unknown\n", type);
} else {
get_line(mr);
continue;
}
mr->attr_last=attr_none;
mr->more=1;
dbg(1,"return attr='%s'\n", mr->attrs);
return &mr->item;
}
}
static struct item *
map_rect_get_item_byid_textfile(struct map_rect_priv *mr, int id_hi, int id_lo)
{
if (mr->m->is_pipe) {
#ifndef _MSC_VER
pclose(mr->f);
mr->f=popen(mr->args, "r");
mr->pos=0;
mr->lastlen=0;
#endif /* _MSC_VER */
} else
fseek(mr->f, id_lo, SEEK_SET);
get_line(mr);
mr->item.id_hi=id_hi;
return map_rect_get_item_textfile(mr);
}
static struct map_methods map_methods_textfile = {
projection_mg,
"iso8859-1",
map_destroy_textfile,
map_rect_new_textfile,
map_rect_destroy_textfile,
map_rect_get_item_textfile,
map_rect_get_item_byid_textfile,
};
static struct map_priv *
map_new_textfile(struct map_methods *meth, struct attr **attrs, struct callback_list *cbl)
{
struct map_priv *m;
struct attr *data=attr_search(attrs, NULL, attr_data);
struct attr *charset=attr_search(attrs, NULL, attr_charset);
struct attr *flags=attr_search(attrs, NULL, attr_flags);
struct file_wordexp *wexp;
int len,is_pipe=0;
char *wdata;
char **wexp_data;
if (! data)
return NULL;
dbg(1,"map_new_textfile %s\n", data->u.str);
wdata=g_strdup(data->u.str);
len=strlen(wdata);
if (len && wdata[len-1] == '|') {
wdata[len-1]='\0';
is_pipe=1;
}
wexp=file_wordexp_new(wdata);
wexp_data=file_wordexp_get_array(wexp);
*meth=map_methods_textfile;
m=g_new0(struct map_priv, 1);
m->id=++map_id;
m->filename=g_strdup(wexp_data[0]);
m->is_pipe=is_pipe;
if (flags)
m->flags=flags->u.num;
dbg(1,"map_new_textfile %s %s\n", m->filename, wdata);
if (charset) {
m->charset=g_strdup(charset->u.str);
meth->charset=m->charset;
}
file_wordexp_destroy(wexp);
g_free(wdata);
return m;
}
void
plugin_init(void)
{
dbg(1,"textfile: plugin_init\n");
plugin_register_map_type("textfile", map_new_textfile);
}
|