Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 347 lines (276 sloc) 7.718 kb
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
1 /*
2 Read and write CoPilot files.
3
4 Copyright (C) 2002 Paul Tomblin, ptomblin@xcski.com
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
19
20 */
21
22 #include "defs.h"
e43b1cc Make shapefile and all palm pdb formats deselectable at build time.
robertl authored
23 #if PDBFMTS_ENABLED
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
24 #include "coldsync/palm.h"
25 #include "coldsync/pdb.h"
86a0798 Use macros DEG() and RAD(), defined in grtcirc.h, for conversions betwee...
oliskoli authored
26 #include "grtcirc.h"
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
27
a68e8a7 Paul Tomblin updates CoPilot code..
robertl authored
28 #define MYNAME "CoPilot Waypoint"
29 #define wayp_TYPE 0x77617970 /* wayp */
7eeee29 Paul adds type identifiers to copilot.
robertl authored
30 #define wayu_TYPE 0x77617975 /* wayu */
a68e8a7 Paul Tomblin updates CoPilot code..
robertl authored
31 #define swpu_TYPE 0x73777075 /* swpu */
32 #define GXPU_CREATOR 0x47584255 /* GXBU */
33 #define AP_P_CREATOR 0x41502d50 /* AP-P */
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
34
35
a68e8a7 Paul Tomblin updates CoPilot code..
robertl authored
36 struct record0 {
37 pdb_double latitude; /* PDB double format, */
38 pdb_double longitude; /* similarly, neg = east */
39 pdb_double magvar; /* magnetic variation in degrees, neg = east */
40 udword elevation; /* feet */
41 };
42
43 struct record1 {
44 pdb_double latitude; /* PDB double format, */
45 pdb_double longitude; /* similarly, neg = east */
46 pdb_double magvar; /* magnetic variation in degrees, neg = east */
47 pdb_double elevation; /* feet */
48 };
49
50 struct record3 {
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
51 pdb_double latitude; /* PDB double format, */
52 pdb_double longitude; /* similarly, neg = east */
53 pdb_double magvar; /* magnetic variation in degrees, neg = east */
54 pdb_double elevation; /* feet */
a68e8a7 Paul Tomblin updates CoPilot code..
robertl authored
55 char flags; /* flags */
56 };
57
58 struct record4 {
59 pdb_double latitude; /* PDB double format, */
60 pdb_double longitude; /* similarly, neg = east */
61 pdb_float magvar; /* magnetic variation in degrees, neg = east */
62 pdb_float elevation; /* feet */
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
63 };
64
65 static FILE *file_in;
66 static FILE *file_out;
67 static const char *out_fname;
648659a More aggressively 'static'-ize module internal data.
robertl authored
68 static struct pdb *opdb;
69 static struct pdb_record *opdb_rec;
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
70
71 static void
8aaa13f Massive search-and-replace to eliminate non-unneeded args being passed
robertl authored
72 rd_init(const char *fname)
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
73 {
48cdd0b All files: replace explict calls to fopen with xfopen wrapper that
robertl authored
74 file_in = xfopen(fname, "rb", MYNAME);
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
75 }
76
77 static void
78 rd_deinit(void)
79 {
80 fclose(file_in);
81 }
82
83 static void
8aaa13f Massive search-and-replace to eliminate non-unneeded args being passed
robertl authored
84 wr_init(const char *fname)
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
85 {
48cdd0b All files: replace explict calls to fopen with xfopen wrapper that
robertl authored
86 file_out = xfopen(fname, "wb", MYNAME);
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
87 out_fname = fname;
88 }
89
90 static void
91 wr_deinit(void)
92 {
93 fclose(file_out);
94 }
95
a68e8a7 Paul Tomblin updates CoPilot code..
robertl authored
96 static waypoint*
97 read_version0(ubyte* data)
98 {
99 char *vdata;
100 waypoint *wpt_tmp;
101 struct record0* rec = (struct record0*)data;
102
103 wpt_tmp = waypt_new();
104
105 wpt_tmp->longitude =
106 DEG(-pdb_read_double(&rec->longitude));
107 wpt_tmp->latitude =
108 DEG(pdb_read_double(&rec->latitude));
109 wpt_tmp->altitude = FEET_TO_METERS(be_read32(&rec->elevation));
110
111 vdata = (char *) data + sizeof(*rec);
112
113 wpt_tmp->shortname = xstrdup(vdata);
114 vdata = vdata + strlen(vdata) + 1;
115
116 wpt_tmp->description = xstrdup(vdata);
117 vdata = vdata + strlen(vdata) + 1;
118
119 wpt_tmp->notes = NULL;
120
121 return wpt_tmp;
122 }
123
124 static waypoint*
125 read_version1(ubyte* data)
126 {
127 char *vdata;
128 waypoint *wpt_tmp;
129 struct record1* rec = (struct record1*)data;
130
131 wpt_tmp = waypt_new();
132
133 wpt_tmp->longitude =
134 DEG(-pdb_read_double(&rec->longitude));
135 wpt_tmp->latitude =
136 DEG(pdb_read_double(&rec->latitude));
137 wpt_tmp->altitude =
138 FEET_TO_METERS(pdb_read_double(&rec->elevation));
139
140 vdata = (char *) data + sizeof(*rec);
141
142 wpt_tmp->shortname = xstrdup(vdata);
143 vdata = vdata + strlen(vdata) + 1;
144
145 wpt_tmp->description = xstrdup(vdata);
146 vdata = vdata + strlen(vdata) + 1;
147
148 wpt_tmp->notes = xstrdup(vdata);
149
150 return wpt_tmp;
151 }
152
153 static waypoint*
154 read_version3(ubyte* data)
155 {
156 char *vdata;
157 waypoint *wpt_tmp;
158 struct record3* rec = (struct record3*)data;
159
160 wpt_tmp = waypt_new();
161
162 wpt_tmp->longitude =
163 DEG(-pdb_read_double(&rec->longitude));
164 wpt_tmp->latitude =
165 DEG(pdb_read_double(&rec->latitude));
166 wpt_tmp->altitude =
167 FEET_TO_METERS(pdb_read_double(&rec->elevation));
168
169 vdata = (char *) data + sizeof(*rec);
170
171 wpt_tmp->shortname = xstrdup(vdata);
172 vdata = vdata + strlen(vdata) + 1;
173
174 wpt_tmp->description = xstrdup(vdata);
175 vdata = vdata + strlen(vdata) + 1;
176
177 wpt_tmp->notes = xstrdup(vdata);
178
179 return wpt_tmp;
180 }
181
182 static waypoint*
183 read_version4(ubyte* data)
184 {
185 char *vdata;
186 waypoint *wpt_tmp;
187 struct record4* rec = (struct record4*)data;
188
189 wpt_tmp = waypt_new();
190
191 wpt_tmp->longitude =
192 DEG(-pdb_read_double(&rec->longitude));
193 wpt_tmp->latitude =
194 DEG(pdb_read_double(&rec->latitude));
195 wpt_tmp->altitude =
196 FEET_TO_METERS(pdb_read_float(&rec->elevation));
197
198 vdata = (char *) data + sizeof(*rec);
199
200 wpt_tmp->shortname = xstrdup(vdata);
201 vdata = vdata + strlen(vdata) + 1;
202
203 wpt_tmp->description = xstrdup(vdata);
204 vdata = vdata + strlen(vdata) + 1;
205
206 wpt_tmp->notes = xstrdup(vdata);
207
208 return wpt_tmp;
209 }
210
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
211 static void
212 data_read(void)
213 {
214 struct pdb *pdb;
215 struct pdb_record *pdb_rec;
216
217 if (NULL == (pdb = pdb_Read(fileno(file_in)))) {
218 fatal(MYNAME ": pdb_Read failed\n");
219 }
220
a68e8a7 Paul Tomblin updates CoPilot code..
robertl authored
221 if ((pdb->creator != GXPU_CREATOR && pdb->creator != AP_P_CREATOR) ||
7eeee29 Paul adds type identifiers to copilot.
robertl authored
222 (pdb->type != wayp_TYPE && pdb->type != swpu_TYPE &&
223 pdb->type != wayu_TYPE)) {
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
224 fatal(MYNAME ": Not a CoPilot file.\n");
225 }
a68e8a7 Paul Tomblin updates CoPilot code..
robertl authored
226 if (pdb->version > 4) {
227 fatal(MYNAME ": %d is not a known version.\n", pdb->version);
228 }
229
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
230
231 for(pdb_rec = pdb->rec_index.rec; pdb_rec; pdb_rec=pdb_rec->next) {
232 waypoint *wpt_tmp;
233
a68e8a7 Paul Tomblin updates CoPilot code..
robertl authored
234 switch (pdb->version)
235 {
236 case 0:
237 wpt_tmp = read_version0(pdb_rec->data);
238 break;
239 case 1:
240 case 2:
241 wpt_tmp = read_version1(pdb_rec->data);
242 break;
243 case 3:
244 wpt_tmp = read_version3(pdb_rec->data);
245 break;
246 case 4:
247 wpt_tmp = read_version4(pdb_rec->data);
248 break;
249 default:
250 fatal(MYNAME ": Unknown version %d.\n", pdb->version);
251 }
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
252 waypt_add(wpt_tmp);
253
254 }
255 free_pdb(pdb);
256 }
257
258 static void
259 copilot_writewpt(const waypoint *wpt)
260 {
a68e8a7 Paul Tomblin updates CoPilot code..
robertl authored
261 struct record4 *rec;
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
262 static int ct = 0;
263 char *vdata;
264
265 rec = xcalloc(sizeof(*rec)+1141,1);
266
86a0798 Use macros DEG() and RAD(), defined in grtcirc.h, for conversions betwee...
oliskoli authored
267 pdb_write_double(&rec->latitude, RAD(wpt->latitude));
268 pdb_write_double(&rec->longitude, RAD(-wpt->longitude));
a68e8a7 Paul Tomblin updates CoPilot code..
robertl authored
269 pdb_write_float(&rec->magvar, 0);
270 pdb_write_float(&rec->elevation,
271 METERS_TO_FEET(wpt->altitude));
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
272
273 vdata = (char *)rec + sizeof(*rec);
274 if ( wpt->shortname ) {
275 strncpy( vdata, wpt->shortname, 10 );
276 vdata[9] = '\0';
277 }
278 else {
279 vdata[0] ='\0';
280 }
281 vdata += strlen( vdata ) + 1;
282 if ( wpt->description ) {
283 strncpy( vdata, wpt->description, 100 );
284 vdata[99] = '\0';
285 }
286 else {
287 vdata[0] ='\0';
288 }
289 vdata += strlen( vdata ) + 1;
290
291 if ( wpt->notes ) {
292 strncpy( vdata, wpt->notes, 1000 );
293 vdata[999] = '\0';
294 }
295 else {
296 vdata[0] ='\0';
297 }
298 vdata += strlen( vdata ) + 1;
299
6eb845d Billions of warning fixes for MSVC-like substances.
robertl authored
300 opdb_rec = new_Record (0, 2, ct++, (uword) (vdata-(char *)rec), (const ubyte *)rec);
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
301
302 if (opdb_rec == NULL) {
303 fatal(MYNAME ": libpdb couldn't create record\n");
304 }
305
306 if (pdb_AppendRecord(opdb, opdb_rec)) {
307 fatal(MYNAME ": libpdb couldn't append record\n");
308 }
309 xfree(rec);
310 }
311
312 static void
313 data_write(void)
314 {
315 if (NULL == (opdb = new_pdb())) {
316 fatal (MYNAME ": new_pdb failed\n");
317 }
318
319 strncpy(opdb->name, out_fname, PDB_DBNAMELEN);
320 opdb->name[PDB_DBNAMELEN-1] = 0;
321 opdb->attributes = PDB_ATTR_BACKUP;
94fc983 Wrap calls to time(2) so we can freeze time for test suite.
robertl authored
322 opdb->ctime = opdb->mtime = current_time() + 2082844800U;
a68e8a7 Paul Tomblin updates CoPilot code..
robertl authored
323 opdb->type = wayp_TYPE;
324 opdb->creator = GXPU_CREATOR;
325 opdb->version = 4;
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
326
327 waypt_disp_all(copilot_writewpt);
328
329 pdb_Write(opdb, fileno(file_out));
330 }
331
332
333 ff_vecs_t copilot_vecs = {
affc26b Add a type (with format output '1') to the '%' output.
robertl authored
334 ff_type_file,
40a686a Add member to vector to indicate read/write capability of each format.
robertl authored
335 FF_CAP_RW_WPT,
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
336 rd_init,
337 wr_init,
338 rd_deinit,
339 wr_deinit,
340 data_read,
341 data_write,
6061998 Bring in Olaf's CET character set transformation work.
robertl authored
342 NULL,
343 NULL,
344 CET_CHARSET_ASCII, 0 /* CET-REVIEW */
afc27e8 Add copilot support. Courtesy Paul Tomblin.
robertl authored
345 };
e43b1cc Make shapefile and all palm pdb formats deselectable at build time.
robertl authored
346 #endif
Something went wrong with that request. Please try again.