File: gnav_trl.cc

package info (click to toggle)
gpsbabel 1.7.0%2Bds-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 100,408 kB
  • sloc: cpp: 104,725; xml: 14,055; sh: 4,699; ansic: 2,062; makefile: 960; perl: 681; tcl: 138; javascript: 9
file content (160 lines) | stat: -rw-r--r-- 3,623 bytes parent folder | download | duplicates (2)
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
/*

    Support for Google Navigator tracklines (.trl).

    Copyright (C) 2008 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

 */

#include "defs.h"

#define MYNAME "gnav_trl"

static
QVector<arglist_t> gnav_trl_args = {
};

struct gnav_trl_t {
  uint32_t time;
  float lat;
  float lon;
  uint32_t alt;
};

static gbfile* fin, *fout;

/*******************************************************************************
* %%%        global callbacks called by gpsbabel main process              %%% *
*******************************************************************************/

static void
gnav_trl_rd_init(const QString& fname)
{
  fin = gbfopen_le(fname, "rb", MYNAME);
}

static void
gnav_trl_rw_init(const QString& fname)
{
  fout = gbfopen_le(fname, "wb", MYNAME);
}

static void
gnav_trl_rd_deinit()
{
  gbfclose(fin);
}

static void
gnav_trl_rw_deinit()
{
  gbfclose(fout);
}

static double
read_altitude(void* ptr)
{
  auto* i = (unsigned char*) ptr;
  char buf[sizeof(float)];
  le_write32(&buf, i[2] << 24 | i[1] << 16 | i[0] <<8 | i[3]);
  return le_read_float(&buf);
}

static void
write_altitude(void* ptr, const float alt)
{
  char buf[sizeof(float)];
  auto* i = (unsigned char*) &buf;
  le_write_float(&buf, alt);
  le_write32(ptr, i[0] << 24 | i[3] << 16 | i[2] << 8 | i[1]);
}

static void
gnav_trl_read()
{
  route_head* trk = nullptr;

  while (! gbfeof(fin)) {
    gnav_trl_t rec;

    if (gbfread(&rec, sizeof(rec), 1, fin) != 1) {
      fatal(MYNAME ": Unexpected EOF (end of file)!\n");
    }

    auto* wpt = new Waypoint;

    wpt->SetCreationTime(le_read32(&rec.time));
    wpt->latitude = le_read_float(&rec.lat);
    wpt->longitude = le_read_float(&rec.lon);
    wpt->altitude = read_altitude(&rec.alt);

    if (trk == nullptr) {
      trk = new route_head;
      track_add_head(trk);
    }
    track_add_wpt(trk, wpt);
  }
}

static void
gnav_trl_write_trkpt(const Waypoint* wpt)
{
  gnav_trl_t rec;

  le_write32(&rec.time, wpt->GetCreationTime().toTime_t());
  le_write_float(&rec.lat, wpt->latitude);
  le_write_float(&rec.lon, wpt->longitude);
  if (wpt->altitude != unknown_alt) {
    write_altitude(&rec.alt, wpt->altitude);
  } else {
    write_altitude(&rec.alt, 0);
  }

  gbfwrite(&rec, sizeof(rec), 1, fout);
}

static void
gnav_trl_write()
{
  track_disp_all(nullptr, nullptr, gnav_trl_write_trkpt);
}


/**************************************************************************/

ff_vecs_t gnav_trl_vecs = {
  ff_type_file,
  {
    ff_cap_none			/* waypoints */,
    (ff_cap)(ff_cap_read | ff_cap_write)	/* tracks */,
    ff_cap_none			/* routes */
  },
  gnav_trl_rd_init,
  gnav_trl_rw_init,
  gnav_trl_rd_deinit,
  gnav_trl_rw_deinit,
  gnav_trl_read,
  gnav_trl_write,
  nullptr,
  &gnav_trl_args,
  CET_CHARSET_UTF8, 1	/* CET - do nothing ! */
  , NULL_POS_OPS,
  nullptr

};

/**************************************************************************/