forked from GPSBabel/gpsbabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
garmin_device_xml.cc
163 lines (137 loc) · 4.37 KB
/
garmin_device_xml.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
/*
Parse 'GarminDevice.xml' on a Garmin mass storage device (e.g. Zumo,
Nuvi, Colorado, etc. and return key device info.
Copyright (C) 2008 Robert Lipe, [email protected]
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.
*/
// References:
// http://developer.garmin.com/web-device/garmin-mass-storage-mode-devices/
// http://developer.garmin.com/schemas/device/v2/
#include "defs.h"
#include "garmin_device_xml.h"
#include "xmlgeneric.h"
#include <QXmlStreamAttributes>
#include <QFile>
#include <cstdio>
#define MYNAME "whatever"
static gdx_info* my_gdx_info;
static int type;
static char* mountpoint, *base, *path, *ext;
static xg_callback device_s, id_s, path_s, ext_s, base_s, dir_s;
jmp_buf gdx_jmp_buf;
static void type_s(xg_string args, const QXmlStreamAttributes*)
{
type = args.compare(u"GPSData");
}
void device_s(xg_string args, const QXmlStreamAttributes*)
{
if (my_gdx_info) {
fatal(MYNAME ": More than one device type found in file.\n");
}
my_gdx_info = (gdx_info*) xcalloc(sizeof *my_gdx_info, 1);
my_gdx_info->device_desc = xstrdup(args);
}
void id_s(xg_string args, const QXmlStreamAttributes*)
{
my_gdx_info->device_id = xstrdup(args);
}
void path_s(xg_string args, const QXmlStreamAttributes*)
{
path = xstrdup(args);
}
void ext_s(xg_string args, const QXmlStreamAttributes*)
{
ext = xstrdup(args);
}
void base_s(xg_string args, const QXmlStreamAttributes*)
{
base = xstrdup(args);
}
void dir_s(xg_string args, const QXmlStreamAttributes*)
{
if (type) {
return;
}
if (args == "OutputFromUnit") {
xasprintf(&my_gdx_info->from_device.path, "%s%c%s",
mountpoint, GB_PATHSEP, path);
my_gdx_info->from_device.basename = xstrdup(base);
my_gdx_info->from_device.extension = xstrdup(ext);
xasprintf(&my_gdx_info->from_device.canon, "%s/%s.%s",
my_gdx_info->from_device.path,
my_gdx_info->from_device.basename,
my_gdx_info->from_device.extension);
} else if (args == "InputToUnit") {
xasprintf(&my_gdx_info->to_device.path, "%s%c%s",
mountpoint, GB_PATHSEP, path);
my_gdx_info->to_device.basename = xstrdup(base);
my_gdx_info->to_device.extension = xstrdup(ext);
} else {
fatal(MYNAME ":Unknown direction '%s'\n", qPrintable(args));
}
if (base) {
xfree(base) ;
}
base = nullptr;
if (ext) {
xfree(ext) ;
}
ext = nullptr;
if (path) {
xfree(path) ;
}
path = nullptr;
}
static xg_tag_mapping gdx_map[] = {
{ device_s, cb_cdata, "/Device/Model/Description" },
{ id_s, cb_cdata, "/Device/Id" },
{ path_s, cb_cdata, "/Device/MassStorageMode/DataType/File/Location/Path" },
{ type_s, cb_cdata, "/Device/MassStorageMode/DataType/Name" },
{ ext_s, cb_cdata, "/Device/MassStorageMode/DataType/File/Location/FileExtension" },
{ base_s, cb_cdata, "/Device/MassStorageMode/DataType/File/Location/BaseName" },
{ dir_s, cb_cdata, "/Device/MassStorageMode/DataType/File/TransferDirection" },
{ nullptr, (xg_cb_type) 0, nullptr }
};
const gdx_info*
gdx_read(const char* fname)
{
// Test file open-able before gb_open gets a chance to fatal().
if (QFile(fname).open(QIODevice::ReadOnly)) {
xml_init(fname, gdx_map, nullptr);
xml_read();
xml_deinit();
}
return my_gdx_info;
}
// Look for the Device in the incoming NULL-terminated list of directories
const gdx_info*
gdx_find_file(char** dirlist)
{
while (dirlist && *dirlist) {
char* tbuf;
xasprintf(&tbuf, "%s/%s", *dirlist, "/Garmin/GarminDevice.xml");
mountpoint = *dirlist;
const gdx_info* gdx = gdx_read(tbuf);
xfree(tbuf);
if (gdx) {
longjmp(gdx_jmp_buf, 1);
}
dirlist++;
}
return nullptr;
}
const gdx_info*
gdx_get_info()
{
return my_gdx_info;
}