/
hsa_ndv.c
563 lines (477 loc) · 12.2 KB
/
hsa_ndv.c
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
Copyright (C) 2004 HSA Systems, Sven Dowideit <sven@hsa.com.au>
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 "cet_util.h"
#if HAVE_LIBEXPAT
#include <expat.h>
static XML_Parser psr;
#endif
static char *cdatastr;
static int in_Route = 0;
static int in_ChartWork = 0;
static int in_Object = 0;
static waypoint *wpt_tmp;
static char *routeName = "ROUTENAME";
#define REPLACEMENT_SIRIUS_ATTR_SEPARATOR ';'
#define ATTR_USRMRK "usrmrk"
#define ATTR_OBJECTNAME "OBJNAM"
#define ATTR_SHIPNAME "shpnam"
static void readVersion4(gbfile* pFile);
static void getAttr(const char *data, const char *attr, char **val, char seperator);
static gbfile *fd;
static gbfile *ofd;
static
arglist_t hsa_ndv_args[] = {
ARG_TERMINATOR
};
#define MYNAME "HsaNdv"
#define MY_CBUF 4096
#define TRUE 1
#define FALSE 0
#if ! HAVE_LIBEXPAT
static void
hsa_ndv_rd_init(const char *fname)
{
fatal(MYNAME ": This build excluded HSA Endeavour support because expat was not installed.\n");
}
static void
hsa_ndv_read(void)
{
}
#else
static void
hsa_ndv_start(void *data, const XML_Char *xml_el, const XML_Char **attr)
{
const char *el = xml_convert_to_char_string(xml_el);
// printf("<%s>\n", el);
if (strcmp(el, "Export") == 0)
{//should only be one
}
else if (strcmp(el, "Route") == 0)
{
in_Route++;
}
else if (strcmp(el, "Chartwork") == 0)
{
in_ChartWork++;
}
else if (strcmp(el, "Object") == 0)
{
wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1);
wpt_tmp->altitude = unknown_alt;
in_Object++;
}
//reset data :)
memset(cdatastr,0, MY_CBUF);
xml_free_converted_string(el);
}
static void
hsa_ndv_end(void *data, const XML_Char *xml_el)
{
const char *el = xml_convert_to_char_string(xml_el);
if (in_Route)
{
if (strcmp(el, "Version") == 0)
{//don't really care
}
else if (strcmp(el, "Name") == 0)
{
routeName = xstrdup(cdatastr);
}
else if (strcmp(el, "LastModified") == 0)
{//don't really care
}
if (in_Object)
{
if (strcmp(el, "ClassName") == 0)
{
}
else if (strcmp(el, "Attr") == 0)
{
getAttr(cdatastr, ATTR_OBJECTNAME, &wpt_tmp->shortname, REPLACEMENT_SIRIUS_ATTR_SEPARATOR);
getAttr(cdatastr, ATTR_USRMRK, &wpt_tmp->description, REPLACEMENT_SIRIUS_ATTR_SEPARATOR);
}
else if (strcmp(el, "LegAttr") == 0)
{
}
else if (strcmp(el, "NumberOfVertexs") == 0)
{
}
else if (strcmp(el, "Latitude") == 0)
{
wpt_tmp->latitude = atof(cdatastr);
}
else if (strcmp(el, "Longitude") == 0)
{
wpt_tmp->longitude = atof(cdatastr);
}
}
}
if (in_ChartWork)
{
if (strcmp(el, "Version") == 0)
{//don't really care
}
if (in_Object)
{
if (strcmp(el, "ClassName") == 0)
{
// className = xstrdup(cdatastr);
}
else if (strcmp(el, "Attr") == 0)
{
//getAttr(cdatastr, ATTR_OBJECTNAME, &wpt_tmp->shortname, REPLACEMENT_SIRIUS_ATTR_SEPARATOR);
//getAttr(cdatastr, ATTR_SHIPNAME, &wpt_tmp->description, REPLACEMENT_SIRIUS_ATTR_SEPARATOR);
}
else if (strcmp(el, "NumberOfVertexs") == 0)
{
}
else if (strcmp(el, "Latitude") == 0)
{
wpt_tmp->latitude = atof(cdatastr);
}
else if (strcmp(el, "Longitude") == 0)
{
wpt_tmp->longitude = atof(cdatastr);
}
else if (strcmp(el, "Time") == 0)
{
wpt_tmp->creation_time = atoi(cdatastr);
}
}
}
//ignore everything else for now..
memset(cdatastr,0, MY_CBUF);
if (strcmp(el, "Object") == 0)
{
if (in_Route)
{
waypt_add(wpt_tmp);
}
else if (in_ChartWork)
{
//TODO: not sure how i want to handle this..
}
in_Object--;
}
else if (strcmp(el, "Route") == 0)
{
in_Route--;
}
else if (strcmp(el, "Chartwork") == 0)
{
in_ChartWork--;
}
xml_free_converted_string(el);
}
static void
hsa_ndv_cdata(void *dta, const XML_Char *s, int len)
{
char *estr;
estr = cdatastr + strlen(cdatastr);
memcpy(estr, s, len);
}
static void
hsa_ndv_rd_init(const char *fname)
{
fd = gbfopen(fname, "r", MYNAME);
psr = XML_ParserCreate(NULL);
if (!psr) {
fatal(MYNAME ":Cannot create XML parser\n");
}
XML_SetUnknownEncodingHandler(psr, cet_lib_expat_UnknownEncodingHandler, NULL);
XML_SetElementHandler(psr, hsa_ndv_start, hsa_ndv_end);
cdatastr = xcalloc(MY_CBUF,1);
XML_SetCharacterDataHandler(psr, hsa_ndv_cdata);
}
static void
hsa_ndv_read(void)
{
int len;
char buf[MY_CBUF + 1];
while ((len = gbfread(buf, 1, sizeof(buf) - 1, fd)))
{
char *bad;
buf[len] = '\0';
if (NULL != strstr(buf, "nver=1"))
{//its the older format, not xml
gbfseek(fd, 0, SEEK_SET);
readVersion4(fd);
break;
}
//grumble - have to remove \x1f's from sirius attributes
bad = buf;
while (NULL != (bad = strchr(bad, '\x1f')))
{
*bad = REPLACEMENT_SIRIUS_ATTR_SEPARATOR;
}
if (!XML_Parse(psr, buf, len, gbfeof(fd))) {
fatal(MYNAME ":Parse error at %d: %s\n",
(int) XML_GetCurrentLineNumber(psr),
XML_ErrorString(XML_GetErrorCode(psr)));
}
}
XML_ParserFree(psr);
}
#endif
static void getAttr(const char *data, const char *attr, char **val, char seperator)
{
char *start;
if ((start = strstr(data, attr)) != NULL)
{
char *end;
int len;
end = strchr(start, seperator);
if (end == NULL)
{
end = start + strlen(start);//assume we are teh last attr
}
len = end-start - strlen(attr);
*val = xcalloc(len+1, 1);
memcpy(*val, start+strlen(attr), len);
(*val)[len] = '\0';
}
else
{
*val = xcalloc(1, 1);
(*val)[0] = '\0';
}
}
static void
hsa_ndv_rd_deinit(void)
{
if ( cdatastr ) {
xfree(cdatastr);
}
gbfclose(fd);
}
static void
hsa_ndv_wr_init(const char *fname)
{
ofd = gbfopen(fname, "w", MYNAME);
}
static void
hsa_ndv_wr_deinit(void)
{
gbfclose(ofd);
}
static int legNum = 0;
static void
hsa_ndv_waypt_pr(const waypoint *waypointp)
{
gbfprintf(ofd, "\t\t<Object>\n");
gbfprintf(ofd, "\t\t\t<ClassName>waypnt</ClassName>\n");
//ignore these for now, they are s57 specific
// fprintf(ofd, "\t\t\t<FeatureNameAgency>0</FeatureNameAgency>\n");
// fprintf(ofd, "\t\t\t<FeatureNameSubDiv>1</FeatureNameSubDiv>\n");
// fprintf(ofd, "\t\t\t<FeatureNameNumber>1089009023</FeatureNameNumber>\n");
gbfprintf(ofd, "\t\t\t<Attr><![CDATA[attr=grpnam%s\x1ftrnrad50\x1fOBJNAM%s\x1flegnum%i\x1fusrmrk%s\x1fselect2]]></Attr>\n", routeName, waypointp->shortname, legNum, waypointp->description);
gbfprintf(ofd, "\t\t\t<LegAttr><![CDATA[attr=grpnam%s\x1f]]></LegAttr>\n", routeName);
gbfprintf(ofd, "\t\t\t<NumberOfVertexs>1</NumberOfVertexs>\n");
gbfprintf(ofd, "\t\t\t<Latitude>%lf</Latitude>\n", waypointp->latitude);
gbfprintf(ofd, "\t\t\t<Longitude>%lf</Longitude>\n", waypointp->longitude);
gbfprintf(ofd, "\t\t</Object>\n");
legNum++;
}
static void
hsa_ndv_write(void)
{
gbfprintf(ofd, "<?xml version=\"1.0\"?>\n");
gbfprintf(ofd, "<Export>\n");
gbfprintf(ofd, "\t<Route>\n");
gbfprintf(ofd, "\t\t<Version>1.0000000</Version>\n");
gbfprintf(ofd, "\t\t<Name>ROUTENAME</Name>\n"); /*TODO: used filename? */
gbfprintf(ofd, "\t\t<LastModified>0</LastModified>\n");
waypt_disp_all(hsa_ndv_waypt_pr);
gbfprintf(ofd, "\t</Route>\n");
//later we'll import past tracks and chart objects?
// fprintf(ofd, "\t<Chartwork>\n");
// fprintf(ofd, "\t\t<Version>1.0000000</Version>\n");
// track_disp_all(hsa_ndv_track_pr);
// fprintf(ofd, "\t</Chartwork>\n");
gbfprintf(ofd, "</Export>\n");
}
ff_vecs_t HsaEndeavourNavigator_vecs = {
ff_type_file,
FF_CAP_RW_WPT,
hsa_ndv_rd_init,
hsa_ndv_wr_init,
hsa_ndv_rd_deinit,
hsa_ndv_wr_deinit,
hsa_ndv_read,
hsa_ndv_write,
NULL,
hsa_ndv_args,
CET_CHARSET_ASCII, 0 /* CET-REVIEW */
};
//////////////////////////////////////////////////////////////////////////
// older style Endeavour route export file
//read DEC2000 NDV export files
#define EF_RECORD_DELIMTER 0
#define ED_REC_NAME_SIZE 5
#define EF_NVER_REC "nver="
#define EF_LAT_REC "lati="
#define EF_LONG_REC "long="
#define EF_TIME_REC "time="
#define EF_ATTR_REC "attr="
#define EF_CLNM_REC "clnm="
#define INVALID_TIME -1L
#define SOUNDARRAY_CHAR 'S'
static int readRecord(gbfile* pFile, const char* pRecName, char *recData);
static int readPositionRecord(gbfile* pFile, double* lat, double* lng, long* timeStamp);
static void readVersion4(gbfile* pFile)
{
while( TRUE )
{
char recData[256] = {0};
// get the position
double lat2, lng2 = 0;
// set the pointer to the time stamp depending
// on whether we have a sounding array or not
long ts1, ts2;
long* pts1 = 0;
long* pts2 = 0;
int soundArray = FALSE;
int numberOfVerticies = 0;
char className[256];
char attr[1024];
int Vertex;
memset(attr, 0, sizeof(attr));
wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1);
wpt_tmp->altitude = unknown_alt;
// read the first record
if( !readRecord( pFile, EF_NVER_REC, recData) )
// no first record then finished
break;
// get the type
sscanf( (const char*)recData, "%d", &numberOfVerticies);
// do we have a sounding array
if( *((const char *)recData + strlen(recData) - 1) == SOUNDARRAY_CHAR )
{
soundArray = TRUE;
}
if( soundArray )
{
pts1 = &ts1;
pts2 = &ts2;
}
// go through the vertices
for( Vertex = 0; Vertex < numberOfVerticies; Vertex++)
{
// read vertex position
if( !readPositionRecord( pFile, &lat2, &lng2, pts2) ) {
xfree(wpt_tmp);
return;
}
wpt_tmp->longitude = lng2;
wpt_tmp->latitude = lat2;
break;//TODO: ignore more points for now
}
// read the class name
if( !readRecord( pFile, EF_CLNM_REC, className) ) {
xfree( wpt_tmp );
return;
}
// read the attributes name
if( !readRecord( pFile, EF_ATTR_REC, attr) ) {
xfree( wpt_tmp );
return;
}
getAttr(attr, ATTR_OBJECTNAME, &wpt_tmp->shortname, '\x1f');
getAttr(attr, ATTR_USRMRK, &wpt_tmp->description, '\x1f');
{
char *bad;
//remove \n and \x1f from description data
while (NULL != (bad = strchr(wpt_tmp->description, '\x1f')))
{
*bad = REPLACEMENT_SIRIUS_ATTR_SEPARATOR;
}
while (NULL != (bad = strchr(wpt_tmp->description, '\n')))
{
*bad = ' ';
}
while (NULL != (bad = strchr(wpt_tmp->description, '\r')))
{
*bad = ' ';
}
}
waypt_add(wpt_tmp);
}
gbfclose(pFile);
return;
}
// read a record to a file
static int readRecord(gbfile* pFile, const char* pRecName, char *recData)
{
// get the rec name
int len;
char recName[ED_REC_NAME_SIZE+1];
char arrRecData[256];
for( len = 0; len < ED_REC_NAME_SIZE; len++)
{
int c = gbfgetc(pFile);
// if we hit EOF failed
if( c == EOF )
return FALSE;
recName[len] = c;
}
// if the record name is not the reqiured type then error
if( strncmp( recName, pRecName, ED_REC_NAME_SIZE) != 0 )
return FALSE;
// get the rec data
for( len = 0; TRUE; len++)
{
int c = gbfgetc( pFile);
// if we hit EOF failed
if( c == EOF )
return FALSE;
// hit end of line
if( c == EF_RECORD_DELIMTER )
break;
arrRecData[len] = c;
}
// get the rec data to a string
strncpy(recData, arrRecData, len);
return TRUE;
}
// read position
static int readPositionRecord(gbfile* pFile, double* lat, double* lng,
long* timeStamp)
{
// read the lat record
char recData[256] = {0};
if( !readRecord( pFile, EF_LAT_REC, recData) )
// no lat record then finished
return FALSE;
// read the latitude
sscanf( (const char*)recData, "%lf", lat);
// read the lng record
if( !readRecord( pFile, EF_LONG_REC, recData) )
// no first record then finished
return FALSE;
// read the latitude
sscanf( (const char*)recData, "%lf", lng);
// if we are to read a time record
if( timeStamp )
{
// read the lng record
if( !readRecord( pFile, EF_TIME_REC, recData) )
// no first record then finished
return FALSE;
// read the latitude
sscanf( (const char*)recData, "%ld", timeStamp);
}
return TRUE;
}