-
Notifications
You must be signed in to change notification settings - Fork 127
/
gdb.h
214 lines (180 loc) · 7.09 KB
/
gdb.h
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
/*
Garmin GPS Database Reader/Writer
Copyright (C) 2005-2008 Olaf Klein, o.b.klein@gpsbabel.org
Mainly based on mapsource.c,
Copyright (C) 2005 Robert Lipe, robertlipe+source@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.
A format description obtained from reverse-engineering is at
https://www.memotech.franken.de/FileFormats/Garmin_MPS_GDB_and_GFI_Format.pdf
*/
#ifndef GDB_H_INCLUDED_
#define GDB_H_INCLUDED_
#include <QList> // for QList
#include <QHash> // for QHash<>::const_iterator, QHash<>::key_iterator, qHash, qHashMulti, QHash
#include <QString> // for QString
#include <QStringView> // for QStringView
#include <QVector> // for QVector
#include "defs.h" // for arglist_t, Waypoint, route_head, ARGTYPE_BOOL, ARGTYPE_INT, ARG_NOMINMAX, bounds, FF_CAP_RW_ALL, ff_cap, ff_type, ff_type_file
#include "format.h" // for Format
#include "garmin_fs.h" // for garmin_fs_t
#include "garmin_tables.h" // for gt_waypt_classes_e
#include "gbfile.h" // for gbfile
#include "mkshort.h" // for MakeShort
#include "option.h" // for OptionBool, OptionString
class GdbFormat : public Format
{
public:
QVector<arglist_t>* get_args() override
{
return &gdb_args;
}
ff_type get_type() const override
{
return ff_type_file;
}
QVector<ff_cap> get_cap() const override
{
return FF_CAP_RW_ALL;
}
void rd_init(const QString& fname) override;
void read() override;
void rd_deinit() override;
void wr_init(const QString& fname) override;
void write() override;
void wr_deinit() override;
/* Types */
// see https://www.kdab.com/how-to-declare-a-qhash-overload/
class WptNamePosnKey;
using WptNamePosnHash = QHash<WptNamePosnKey, Waypoint*>;
class WptNamePosnKey {
public:
WptNamePosnKey(const QString& name, double lt, double ln) : shortname(name), lat(lt), lon(ln) {}
friend size_t qHash(const WptNamePosnKey &c, size_t seed = 0) noexcept
{
return qHashMulti(seed, c.shortname.toUpper(), c.lat, c.lon);
}
QString shortname;
double lat{};
double lon{};
};
class WptNameKey;
using WptNameHash = QHash<WptNameKey, Waypoint*>;
class WptNameKey {
public:
WptNameKey(const QString& name) : shortname(name) {} /* converting constructor */
friend size_t qHash(const WptNameKey &c, size_t seed = 0) noexcept
{
return qHash(c.shortname.toUpper(), seed);
}
QString shortname;
};
private:
/* Constants */
static constexpr int kGDBVer1 = 1;
static constexpr int kGDBVer2 = 2;
static constexpr int kGDBVer3 = 3;
static constexpr int kGDBVerUTF8 = kGDBVer3;
static constexpr int kGDBVerMin = kGDBVer1;
static constexpr int kGDBVerMax = kGDBVer3;
static constexpr int kGDBDefIcon = 18;
static constexpr int kGDBNameBufferLen = 1024;
/* Member Functions */
static void gdb_flush_waypt_queue(WptNamePosnHash& Q);
void disp_summary(const gbfile* f) const;
QString fread_cstr() const;
static char* gdb_fread_cstr(gbfile* file_in);
QString gdb_fread_strlist() const;
static Waypoint* gdb_find_wayptq(const WptNameHash& Q, const Waypoint* wpt);
static Waypoint* gdb_find_wayptq(const WptNamePosnHash& Q, const Waypoint* wpt);
Waypoint* gdb_reader_find_waypt(const Waypoint* wpt, bool exact) const;
Waypoint* gdb_add_route_waypt(route_head* rte, Waypoint* ref, int wpt_class) const;
static QString gdb_to_ISO8601_duration(unsigned int seconds);
void gdb_write_cstr(QStringView a = QStringView()) const;
void gdb_write_cstr_list(QStringView str = QStringView()) const;
void gdb_write_dbl(double value, double def) const;
void gdb_write_time(int time) const;
void read_file_header();
Waypoint* read_waypoint(gt_waypt_classes_e* waypt_class_out);
route_head* read_route();
route_head* read_track();
void reset_short_handle(const char* defname);
void write_header();
static void gdb_check_waypt(Waypoint* wpt);
void write_waypoint(const Waypoint* wpt, const QString& shortname, const garmin_fs_t* gmsd, int icon, int display);
static void route_compute_bounds(const route_head* rte, bounds* bounds);
void route_write_bounds(bounds* bounds) const;
void write_route(const route_head* rte, const QString& rte_name);
void write_track(const route_head* trk, const QString& trk_name);
void finalize_item(gbfile* origin, char identifier);
void write_waypoint_cb(const Waypoint* refpt);
void write_route_cb(const route_head* rte);
void write_track_cb(const route_head* trk);
/* Data Members */
gbfile* fin{}, *fout{}, *ftmp{};
int gdb_ver{}, gdb_category{};
bool gdb_roadbook{};
bool gdb_hide_wpt{};
bool gdb_hide_rpt{};
WptNamePosnHash waypt_nameposn_in_hash;
WptNameHash waypt_name_in_hash;
WptNamePosnHash waypt_nameposn_in_hidden_hash;
WptNameHash waypt_name_in_hidden_hash;
WptNamePosnHash waypt_nameposn_out_hash;
MakeShort* short_h{};
OptionInt gdb_opt_category;
OptionInt gdb_opt_ver;
OptionBool gdb_opt_via;
OptionBool gdb_opt_roadbook;
OptionInt gdb_opt_bitcategory{false, 0};
OptionBool gdb_opt_drop_hidden_wpt;
int waypt_flag{};
int route_flag{};
int waypt_ct{}; /* informational: total number of waypoints in/out */
int waypth_ct{}; /* informational: total number of hidden waypoints in/out */
int rtept_ct{}; /* informational: total number of route points in/out */
int trkpt_ct{}; /* informational: total number of track points in/out */
int rte_ct{}; /* informational: total number of routes in/out */
int trk_ct{}; /* informational: total number of tracks in/out */
QVector<arglist_t> gdb_args = {
{
"cat", &gdb_opt_category,
"Default category on output (1..16)",
nullptr, ARGTYPE_INT, "1", "16", nullptr
},
{
"bitscategory", &gdb_opt_bitcategory, "Bitmap of categories",
nullptr, ARGTYPE_INT, "1", "65535", nullptr
},
{
"ver", &gdb_opt_ver,
"Version of gdb file to generate (1..3)",
"2", ARGTYPE_INT, "1", "3", nullptr
},
{
"via", &gdb_opt_via,
"Drop route points that do not have an equivalent waypoint (hidden points)",
nullptr, ARGTYPE_BOOL, ARG_NOMINMAX, nullptr
},
{
"dropwpt", &gdb_opt_drop_hidden_wpt,
"Don't create waypoints for non-user points",
nullptr, ARGTYPE_BOOL, ARG_NOMINMAX, nullptr
},
{
"roadbook", &gdb_opt_roadbook,
"Include major turn points (with description) from calculated route",
nullptr, ARGTYPE_BOOL, ARG_NOMINMAX, nullptr
}
};
};
#endif // GDB_H_INCLUDED_