File: google.c

package info (click to toggle)
gpsbabel 1.3.2-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 15,956 kB
  • ctags: 9,083
  • sloc: ansic: 87,514; xml: 13,640; pascal: 6,981; sh: 3,770; makefile: 811; perl: 754; tcl: 74; objc: 7
file content (371 lines) | stat: -rw-r--r-- 8,273 bytes parent folder | download
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
/* 
    Copyright (C) 2002 Robert Lipe, robertlipe@usa.net

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    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., 59 Temple Place - Suite 330, Boston, MA 02111 USA

 */
#include "defs.h"
#include "xmlgeneric.h"

static char *encoded_points = NULL;
static char *encoded_levels = NULL;
static char *script = NULL;
static route_head *routehead;
static short_handle desc_handle;

static int serial = 0;

#define MYNAME "google"
#define MY_CBUF 4096

#if ! HAVE_LIBEXPAT
static void
google_rd_init(const char *fname)
{
	fatal(MYNAME ": This build excluded Google Maps support because expat was not installed.\n");
}

static void
google_read(void)
{
}
#else

static xg_callback      goog_points, goog_levels, goog_poly_e, goog_script;
static xg_callback	goog_segment_s, goog_segment, goog_td_s, goog_td_b;
static xg_callback	goog_td_e;

static 
xg_tag_mapping google_map[] = {
	{ goog_points,  cb_cdata,       "/page/directions/polyline/points" },
	{ goog_levels,  cb_cdata,       "/page/directions/polyline/levels" },
	{ goog_poly_e,  cb_end,         "/page/directions/polyline" },
	{ goog_script,  cb_cdata,       "/html/head/script" },
	{ goog_segment_s, cb_start,      "/page/directions/segments/segment" },
	{ goog_segment, cb_cdata,      "/page/directions/segments/segment" },
	{ goog_td_s,    cb_start,      "/div/table/tr/td" },
	{ goog_td_b,      cb_cdata,      "/div/table/tr/td/b" },
	{ goog_td_e,    cb_end,        "/div/table/tr/td" },
	{ NULL,         0,              NULL }
};

void goog_script( const char *args, const char **unused ) 
{
	if (args)
	{
		if ( script ) 
		{
			script = xstrappend( script, args );
		}
		else
		{
			script = xstrdup( args );
		}
	}
}			

void goog_points( const char *args, const char **unused )
{
	if (args)  
	{
		if ( encoded_points )
		{
			encoded_points = xstrappend( encoded_points, args );
		}
		else 
		{
			encoded_points = xstrdup(args);
		}
	}
}

void goog_levels( const char *args, const char **unused )
{
	if (args)  
	{
		if ( encoded_levels )
		{
			encoded_levels = xstrappend( encoded_levels, args );
		}
		else 
		{
			encoded_levels = xstrdup(args);
		}
	}
}

static char goog_segname[7];
static char *goog_realname = NULL;

/*
 * The segments contain an index into the points array.  We use that
 * index to find the waypoint and insert a better name for it.
 */
void goog_segment_s( const char *args, const char **attrv )
{
        const char **avp = &attrv[0];
        while (*avp) {
                if (0 == strcmp(avp[0], "pointIndex")) {
			snprintf(goog_segname, sizeof(goog_segname), "\\%5.5x", atoi(avp[1]));
		}
		avp += 2;
	}

}

void goog_segment( const char *args, const char **unused )
{
	waypoint *wpt_tmp;

	wpt_tmp = route_find_waypt_by_name( routehead, goog_segname);
	if (wpt_tmp) {
		xfree(wpt_tmp->shortname);
		wpt_tmp->shortname = mkshort(desc_handle,args);
		wpt_tmp->description = xstrdup(args);
	}
}

void goog_td_s( const char *args, const char **attrv )
{
	const char **avp = &attrv[0];
	int isdesc = 0;
	while (*avp) {
		if ( 0 == strcmp(avp[0], "class" )) {
			isdesc = !strcmp(avp[1], "desc" );
		}
		else if ( isdesc && (0 == strcmp( avp[0], "id" ))) {
			snprintf( goog_segname, sizeof(goog_segname),
				"\\%5.5x",
				atoi(avp[1] + 6 ));
		}
		avp += 2;
	}
}
	
void goog_td_b( const char *args, const char **attrv ) {
	if ( goog_segname[0] == '\\' && !strchr( args, '\xa0')) {
		if ( goog_realname ) {
			xfree( goog_realname );
			goog_realname = NULL;
		}
		goog_realname = xmalloc( strlen(args)+1);
		strcpy( goog_realname, args );
	}
}
void goog_td_e( const char *args, const char **attrv ) 
{
	if ( goog_segname[0] == '\\' && goog_realname ) {
		goog_segment( goog_realname, attrv );
	}
	goog_segname[0] = '\0';
	if ( goog_realname ) {
		xfree( goog_realname );
		goog_realname = NULL;
	}
}

static long decode_goog64( char **str )
{
	long result = 0;
	unsigned char c = 0;
	unsigned char shift = 0;

        if ( !(**str)) {
		return 0;
	}
	
	do 
	{
		c = (unsigned char)(*(*str)++)-'?';
		result |= (c & 31)<<shift;
		shift += 5;
	} while ( c & ~31 );
	
	if ( result & 1 ) 
	{
		result = ~result;
	}
	return result/2;
}

void goog_poly_e( const char *args, const char **unused )
{
	long lat = 0;
	long lon = 0;
	long level = 0;
	long level1 = -9999;
	long level2 = -9999;
        char *str = encoded_points;
	char *lstr = encoded_levels;

	routehead = route_head_alloc();
	route_add_head(routehead);

	while ( str && *str ) 
	{
		lat += decode_goog64( &str );
		lon += decode_goog64( &str );
		
		level = -1;
		level2 = level1;
		if ( lstr && *lstr ) 
		{
			level1 = -decode_goog64( &lstr );
		}
		else 
		{
			level1 = -9999;
		}
		level = (level1<level2)?level1:level2;
		
		/* level of -9999 happens for endpoints */
	        if ( level == -9999 ) 
			level = 99999;	
		
		{
			waypoint *wpt_tmp = waypt_new();
			wpt_tmp->latitude = lat / 100000.0;
			wpt_tmp->longitude = lon / 100000.0;
			wpt_tmp->route_priority=level;
			wpt_tmp->shortname = (char *) xmalloc(7);
			sprintf( wpt_tmp->shortname, "\\%5.5x", serial++ );
			route_add_wpt(routehead, wpt_tmp);
		}
	}
	
}

static void
google_rd_init(const char *fname)
{
	desc_handle = mkshort_new_handle();
	setshort_length(desc_handle, 12);

	xml_init(fname, google_map, "ISO-8859-1" );
}

static void
google_read(void)
{
	xml_read();
	if ( encoded_points ) 
	{
		xfree( encoded_points );
		encoded_points = NULL;
	}
	if ( encoded_levels )
	{
		xfree( encoded_levels );
		encoded_levels = NULL;
	}
	if ( script ) 
	{
		char *xml = strchr( script, '\'' );
		char *dict = strstr( script, "({" );
		
		char *end = NULL;
		
		if ( xml && (!dict || (xml < dict ))) {
			xml++;
			end = strchr( xml+1, '\'' );
			if ( end ) {
				*end = '\0';
				xml_deinit();
				xml_init( NULL, google_map, NULL );
				xml_readstring( xml );
				if ( encoded_points ) 
				{
					xfree( encoded_points );
					encoded_points = NULL;
				}
				if ( encoded_levels )
				{
					xfree( encoded_levels );
					encoded_levels = NULL;
				}
			}
		}
		else if ( dict ) {
		  char *panel = strstr( dict, "panel: '" );
		  encoded_points = strstr( dict, "points: '" );
		  encoded_levels = strstr( dict, "levels: '" );
		  
		  if ( encoded_points && encoded_levels ) {
	            encoded_points += 9;
		    encoded_levels += 9;
		    end = strchr( encoded_points, '\'' );
		    if ( end ) {
	              *end = '\0';
		      end = encoded_points;
		      while ( (end = strstr(end, "\\\\" ))) {
			memmove( end, end+1, strlen(end)+1 );
			end++;
		      }
		      end = strchr( encoded_levels, '\'' );
		      if ( end ) {
			*end = '\0';
		        end = encoded_levels;
  		        while ( (end = strstr(end, "\\\\" ))) {
			  memmove( end, end+1, strlen(end)+1 );
			  end++;
		        }
			goog_poly_e( NULL, NULL );
		      }
		    }		      
		  }
	          if ( panel ) {
		    panel += 8;
		    end = strstr( panel, "/table><div class=\\\"legal" );
		    if ( end ) {
	              strcpy(end,"/table></div>");
		      end = panel;
		      while ( (end = strstr( end, "\\\"" ))) {
		        memmove( end, end+1, strlen(end)+1 );
		      }
		      end = panel;
		      while ( (end = strstr( end, "\\'" ))) {
			memmove( end, end+1, strlen(end)+1 );
		      }
		      xml_deinit();
		      xml_init( NULL, google_map, NULL );
		      xml_readstring( panel );
		    }
		  }
		}
		xfree( script );
	}
}
#endif

static void
google_rd_deinit(void)
{
	xml_deinit();
	mkshort_del_handle(&desc_handle);
}

ff_vecs_t google_vecs = {
	ff_type_file,
        { ff_cap_none, ff_cap_read, ff_cap_none},
	google_rd_init,	
	NULL,
	google_rd_deinit,
	NULL,
	google_read,
	NULL,
	NULL, 
	NULL,
	CET_CHARSET_UTF8, 1	/* CET-REVIEW */
};