File: gpspilot.c

package info (click to toggle)
gpsbabel 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 34,456 kB
  • sloc: ansic: 127,158; xml: 13,850; pascal: 7,470; cpp: 5,668; sh: 4,215; makefile: 1,174; perl: 804; tcl: 138
file content (234 lines) | stat: -rw-r--r-- 5,428 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
/*
    Read and write GPSPilot Tracker files.

    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"
#if PDBFMTS_ENABLED
#include "pdbfile.h"

#define MYNAME "GPSPilot"
#define MYTYPE_POINTS 0x706f696e  	/* poin */
#define MYTYPE_AIRPORT 0x706f3030  	/* po00 */
#define MYTYPE_CITIES  0x706f3031  	/* po01 */
#define MYTYPE_LNDMRKS 0x706f3032  	/* po02 */
#define MYTYPE_NAVAIDS 0x706f3033  	/* po03 */
#define MYCREATOR 0x47704c69 	/* GpLi */

struct record {
  pdb_32 longitude; 	/* Big endian, long * 3.6e6 */
  pdb_32 latitude; 	/* similarly */
  pdb_16 elevation; 	/* meters */
  pdb_16 magvar; 		/* magnetic variation in degrees, neg = west */
};

struct runways {
  pdb_32 be_longitude; 	/* Big endian, long * 3.6e6 */
  pdb_32 be_latitude; 	/* similarly */
  pdb_32 en_longitude; 	/* Big endian, long * 3.6e6 */
  pdb_32 en_latitude; 	/* similarly */
};

static pdbfile* file_in, *file_out;
static const char* out_fname;
static char* dbname = NULL;
static int ct;

static
arglist_t gpspilot_args[] = {
  {"dbname", &dbname, "Database name", NULL, ARGTYPE_STRING, ARG_NOMINMAX},
  ARG_TERMINATOR
};

static void
rd_init(const char* fname)
{
  file_in = pdb_open(fname, MYNAME);
}

static void
rd_deinit(void)
{
  pdb_close(file_in);
  if (dbname) {
    xfree(dbname);
    dbname = NULL;
  }
}

static void
wr_init(const char* fname)
{
  file_out = pdb_create(fname, MYNAME);
  out_fname = fname;
  ct = 0;
}

static void
wr_deinit(void)
{
  pdb_close(file_out);
  if (dbname) {
    xfree(dbname);
    dbname = NULL;
  }
}

static void
data_read(void)
{
  struct record* rec;
  pdbrec_t* pdb_rec;

  if ((file_in->creator != MYCREATOR)) {
    fatal(MYNAME ": Not a gpspilot file.\n");
  }

  switch (file_in->type) {
  case MYTYPE_AIRPORT:
  case MYTYPE_POINTS:
  case MYTYPE_CITIES:
  case MYTYPE_LNDMRKS:
  case MYTYPE_NAVAIDS:
    break;
  default:
    fatal(MYNAME ": Not a gpspilot file.\n");
  }

  for (pdb_rec = file_in->rec_list; pdb_rec; pdb_rec = pdb_rec->next) {
    waypoint* wpt_tmp;
    char* vdata;

    wpt_tmp = waypt_new();

    rec = (struct record*) pdb_rec->data;
    wpt_tmp->longitude = be_read32(&rec->longitude) / 3.6e6;
    wpt_tmp->latitude = be_read32(&rec->latitude) / 3.6e6;
    wpt_tmp->altitude =
      be_read16(&rec->elevation);

    vdata = (char*) pdb_rec->data + sizeof(*rec);

    /*
    * skip runway records if an airport.
    */
    if (pdb_rec->category == 0) {
      int numRunways;
      numRunways = be_read16(vdata);
      vdata += 2;
      vdata += (sizeof(struct runways) * numRunways);
    }

    /*
     * This maping is a bit contrived.
     * Name is up to 36. ID is up to 9.
     * Since 'ID' maps more clearly to "shortname" (and this
     * more likely to be resemble  a wayoint name in another
     * receiver) we use that for shortname and use 'name' as
     * our description.
     */
    wpt_tmp->description = xstrdup(vdata);
    vdata = vdata + strlen(vdata) + 1;

    wpt_tmp->shortname = xstrdup(vdata);
    vdata = vdata + strlen(vdata) + 1;

    wpt_tmp->notes = xstrdup(vdata);

    waypt_add(wpt_tmp);

  }
}


static void
gpspilot_writewpt(const waypoint* wpt)
{
  struct record* rec;
  char* vdata;

  rec = (struct record*) xcalloc(sizeof(*rec)+206,1);

  be_write32(&rec->longitude, si_round(wpt->longitude * 3.6e6));
  be_write32(&rec->latitude, si_round(wpt->latitude * 3.6e6));
  be_write16(&rec->elevation, si_round(wpt->altitude));
  be_write16(&rec->magvar, 0);

  vdata = (char*)rec + sizeof(*rec);
  if (wpt->description) {
    strncpy(vdata, wpt->description, 36);
    vdata[35] = '\0';
  } else {
    vdata[0] ='\0';
  }
  vdata += strlen(vdata) + 1;
  if (wpt->shortname) {
    strncpy(vdata, wpt->shortname, 9);
    vdata[8] = '\0';
  } else {
    vdata[0] ='\0';
  }
  vdata += strlen(vdata) + 1;

  if (wpt->notes) {
    strncpy(vdata, wpt->notes, 161);
    vdata[160] = '\0';
  } else {
    vdata[0] ='\0';
  }
  vdata += strlen(vdata) + 1;

  pdb_write_rec(file_out, 0, 2, ct++, (void*)rec, (char*)vdata - (char*)rec);

  xfree(rec);
}

static void
data_write(void)
{
  if (dbname) {
    strncpy(file_out->name, dbname, PDB_DBNAMELEN);
  } else {
    strncpy(file_out->name, out_fname, PDB_DBNAMELEN);
  }
  file_out->name[PDB_DBNAMELEN-1] = 0;
  file_out->attr = PDB_FLAG_BACKUP;
  file_out->ctime = file_out->mtime = current_time() + 2082844800U;
  file_out->type = MYTYPE_POINTS;
  file_out->creator = MYCREATOR;
  file_out->version = 0;

  waypt_disp_all(gpspilot_writewpt);
}


ff_vecs_t gpspilot_vecs = {
  ff_type_file,
  FF_CAP_RW_WPT,
  rd_init,
  wr_init,
  rd_deinit,
  wr_deinit,
  data_read,
  data_write,
  NULL,
  gpspilot_args,
  CET_CHARSET_ASCII, 0	/* CET-REVIEW */
};
#endif