forked from GPSBabel/gpsbabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cst.cc
337 lines (281 loc) · 7.86 KB
/
cst.cc
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
/*
Support for CarteSurTable data file,
Copyright (C) 2005 Olaf Klein, o.b.klein@gpsbabel.org
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"
#include <stdio.h>
#include <stdlib.h>
#define MYNAME "cst"
#undef CST_DEBUG
#define CST_UNKNOWN 0
#define CST_HEADER 1
#define CST_ROUTE 2
#define CST_NOTES 3
#define CST_REFERENCE 4
#define CST_VERSION 5
static gbfile* fin;
static route_head* temp_route;
/* placeholders for options */
static
arglist_t cst_args[] = {
ARG_TERMINATOR
};
/* helpers */
static void
cst_add_wpt(const route_head* track, Waypoint* wpt)
{
if ((wpt == NULL) || (track == NULL)) {
return;
}
if (wpt->shortname != NULL) {
waypt_add(new Waypoint(*wpt));
// Rather than creating a new waypt on each read, tis format bizarrely
// recycles the same one, relying on new waypoint(*) above and then manually
// resetting fields. Weird.
wpt->url_link_list_.clear();
if (temp_route == NULL) {
temp_route = route_head_alloc();
route_add_head(temp_route);
}
route_add_wpt(temp_route, new Waypoint(*wpt));
}
track_add_wpt((route_head*)track, (Waypoint*)wpt);
}
static char*
cst_make_url(char* str)
{
int len = strlen(str);
char* res;
if (len < 3) {
return NULL;
}
if (strstr(str, "://") > str) {
return xstrdup(str);
} else if (strstr(str, ":\\") == str+1) { /* DOS 0.01++ file format */
res = xstrdup("file://*:");
res[7] = *str++;
res[8] = *str++;
res = xstrappend(res, str);
{
char* c;
int i;
c = res; /* replace all backslashes with a slash */
while ((c = strchr(c, '\\'))) {
*c++ = '/';
}
c = res; /* enumerate number of spaces within filename */
i = 0;
while ((c = strchr(c, ' '))) {
c++;
i++;
}
if (i > 0) { /* .. and replace them with "%20" */
char* src, *dest, *last;
last = src = res;
res = dest = (char*) xcalloc(strlen(src) + (2*i) + 1, 1);
while ((c = strchr(src, ' '))) {
if (c != src) {
strncpy(dest, src, c - src);
}
strcat(dest, "%20");
c++;
src = c;
dest = res + strlen(res);
}
while (*src != '\0') {
*dest++ = *src++;
}
xfree(last);
}
}
return res;
} else {
return NULL;
}
}
/* --------------------------------------------------------------------------- */
static void
cst_rd_init(const QString& fname)
{
fin = gbfopen(fname, "rb", MYNAME);
temp_route = NULL;
}
static void
cst_rd_deinit(void)
{
gbfclose(fin);
}
/* --------------------------------------------------------------------------- */
static void
cst_data_read(void)
{
char* buff;
int line = 0;
int data_lines = -1;
int line_of_count = -1;
int valid = 0;
int section = CST_UNKNOWN;
int cst_version;
int cst_points = -1;
route_head* track = NULL;
Waypoint* wpt = NULL;
while ((buff = gbfgetstr(fin))) {
char* cin = buff;
if ((line++ == 0) && fin->unicode) {
cet_convert_init(CET_CHARSET_UTF8, 1);
}
cin = lrtrim(buff);
if (strlen(cin) == 0) {
continue;
}
if (strncmp(cin, "; ", 2) == 0) {
continue;
}
if (*cin == '#') {
section = CST_UNKNOWN;
if (strcmp(cin+1, "ROUTE") == 0) {
section = CST_ROUTE;
} else if (strcmp(cin+1, "VERSION") == 0) {
section = CST_VERSION;
} else if (strcmp(cin+1, "NOTES") == 0) {
section = CST_NOTES;
} else if (strcmp(cin+1, "REFERENCE") == 0) {
section = CST_REFERENCE;
} else if (strcmp(cin+1, "CARTE SUR TABLE DATA FILE") == 0) {
section = CST_HEADER;
valid = 1;
} else {
warning(MYNAME ": Unknown section \"%s\".\n", cin+1);
}
continue;
}
if (valid == 0) {
continue;
}
switch (section) {
case CST_ROUTE:
if (*cin == ';') {
int data = 0;
if (*(cin+1) != '\xA4') {
continue;
}
if (strncmp(cin + 2, "bitmap", 6) == 0) {
cin = lrtrim(cin + 8);
if (*cin != '\0') {
char* url = cst_make_url(cin);
wpt->AddUrlLink(url);
if (url) {
xfree(url);
}
}
}
while ((buff = gbfgetstr(fin))) {
line++;
cin = lrtrim(buff);
if (strcmp(cin + 2, "note") == 0) {
buff = gbfgetstr(fin);
if (buff == NULL) {
buff = (char*) "";
}
line++;
cin = lrtrim(buff);
if (*cin != '\0') {
wpt->notes = QString::fromLatin1(cin);
}
} else if (strcmp(cin + 2, "end") == 0) {
data = 1;
break;
}
}
if (data == 0) {
fatal(MYNAME ": Unexpected end of file!\n");
}
} else {
int interp, i;
char name[256];
if (data_lines < 0) {
if ((2 != sscanf(cin, "%d %128s", &i, name)) ||
(case_ignore_strcmp(name, "Points") != 0)) {
fatal(MYNAME "-line %d: Number of points expected!\n", line);
}
line_of_count = line;
data_lines = 0;
cst_points = i;
continue;
}
cst_add_wpt(track, wpt);
wpt = NULL;
wpt = new Waypoint;
if (5 != sscanf(cin, "%lf %lf %lf %d %s",
&wpt->longitude,
&wpt->latitude,
&wpt->altitude,
&interp, name)) {
fatal(MYNAME ": Could not interprete line %d!\n", line);
}
data_lines++;
if (strcmp(name, "1") == 0) {
track = route_head_alloc();
track_add_head(track);
} else if (strncmp(name, "NAME:", 5) == 0) {
wpt->shortname = QString::fromLatin1(((char*)&name) + 5);
}
QString time_string(cin);
int caret = time_string.indexOf('^');
if (caret > -1) {
QString dts = time_string.mid(caret + 1).trimmed();
QDateTime dt = QDateTime::fromString(dts, "yyyy MM dd hh:mm:ss");
dt.setTimeSpec(Qt::UTC);
wpt->SetCreationTime(dt);
}
wpt->latitude /= 100000.0;
wpt->longitude /= 100000.0;
}
break;
case CST_VERSION:
cst_version = atoi(cin);
if (cst_version != 40) {
warning(MYNAME ": Not tested with file version %d.\n", cst_version);
}
break;
case CST_REFERENCE:
if ((strncmp(cin, "DATUM ", 6) == 0) && (strstr(cin, "WGS 84") == NULL)) {
fatal(MYNAME ": Unsupported datum (%s)!\n", cin);
}
break;
case CST_HEADER:
case CST_NOTES:
break;
}
}
cst_add_wpt(track, wpt);
wpt = NULL;
if ((cst_points >= 0) && (data_lines != cst_points)) {
warning(MYNAME ": Loaded %d point(s), but line %d says %d!\n", data_lines, line_of_count, cst_points);
}
}
ff_vecs_t cst_vecs = {
ff_type_file,
{ ff_cap_read, ff_cap_read, ff_cap_read },
cst_rd_init,
NULL, /* cst_wr_init, */
cst_rd_deinit,
NULL, /* cst_wr_deinit, */
cst_data_read,
NULL, /* cst_data_write, */
NULL,
cst_args,
CET_CHARSET_MS_ANSI, 0 /* CET-REVIEW */
};