]>
Commit | Line | Data |
---|---|---|
1d1bc3c1 EB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net> | |
a482007a | 5 | * Copyright (C) 2006, Quy Tonthat <qtonthat@gmail.com> |
1d1bc3c1 EB |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | */ | |
22 | ||
072cbb13 GB |
23 | /** |
24 | * SECTION:babel | |
25 | * @short_description: running external programs and redirecting to TRWLayers. | |
26 | * | |
28c82d8b | 27 | * GPSBabel may not be necessary for everything -- for instance, |
072cbb13 | 28 | * use a_babel_convert_from_shellcommand() with input_file_type == %NULL |
28c82d8b EB |
29 | * for an external program that outputs GPX. |
30 | */ | |
31 | ||
45acf79e MA |
32 | #ifdef HAVE_CONFIG_H |
33 | #include "config.h" | |
34 | #endif | |
35 | ||
1d1bc3c1 EB |
36 | #include "viking.h" |
37 | #include "gpx.h" | |
38 | #include "babel.h" | |
8c060406 | 39 | #include <stdio.h> |
45acf79e MA |
40 | #ifdef HAVE_UNISTD_H |
41 | #include <unistd.h> | |
42 | #endif | |
79e0f36e | 43 | #include <string.h> |
45acf79e MA |
44 | #include <glib.h> |
45 | #include <glib/gstdio.h> | |
1d1bc3c1 | 46 | |
072cbb13 | 47 | /* TODO in the future we could have support for other shells (change command strings), or not use a shell at all */ |
8cf048bd EB |
48 | #define BASH_LOCATION "/bin/bash" |
49 | ||
18ec873d GB |
50 | /** |
51 | * Path to gpsbabel | |
52 | */ | |
53 | static gchar *gpsbabel_loc = NULL; | |
54 | ||
55 | /** | |
56 | * Path to unbuffer | |
57 | */ | |
58 | static gchar *unbuffer_loc = NULL; | |
59 | ||
79e0f36e GB |
60 | /** |
61 | * List of file formats supported by gpsbabel. | |
62 | */ | |
63 | GList *a_babel_file_list; | |
64 | ||
65 | /** | |
66 | * List of device supported by gpsbabel. | |
67 | */ | |
68 | GList *a_babel_device_list; | |
69 | ||
072cbb13 GB |
70 | /** |
71 | * a_babel_convert: | |
72 | * @vt: The TRW layer to modify. All data will be deleted, and replaced by what gpsbabel outputs. | |
73 | * @babelargs: A string containing gpsbabel command line filter options. No file types or names should | |
74 | * be specified. | |
75 | * @cb: A callback function. | |
ed691ed1 RN |
76 | * @user_data: passed along to cb |
77 | * @not_used: Must use NULL | |
072cbb13 GB |
78 | * |
79 | * This function modifies data in a trw layer using gpsbabel filters. This routine is synchronous; | |
80 | * that is, it will block the calling program until the conversion is done. To avoid blocking, call | |
81 | * this routine from a worker thread. | |
82 | * | |
83 | * Returns: %TRUE on success | |
84 | */ | |
ed691ed1 | 85 | gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, gpointer user_data, gpointer not_used ) |
1d1bc3c1 EB |
86 | { |
87 | int fd_src; | |
88 | FILE *f; | |
18ec873d | 89 | gchar *name_src = NULL; |
1d1bc3c1 | 90 | gboolean ret = FALSE; |
3adc68b0 | 91 | gchar *bargs = g_strconcat(babelargs, " -i gpx", NULL); |
1d1bc3c1 | 92 | |
2ecf5998 | 93 | if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) { |
e45b3da1 | 94 | g_debug ("%s: temporary file: %s", __FUNCTION__, name_src); |
1d1bc3c1 | 95 | f = fdopen(fd_src, "w"); |
208d2084 | 96 | a_gpx_write_file(vt, f, NULL); |
1d1bc3c1 | 97 | fclose(f); |
8c060406 | 98 | f = NULL; |
ed691ed1 | 99 | ret = a_babel_convert_from ( vt, bargs, name_src, cb, user_data, not_used ); |
18ec873d GB |
100 | g_remove(name_src); |
101 | g_free(name_src); | |
1d1bc3c1 EB |
102 | } |
103 | ||
104 | g_free(bargs); | |
1d1bc3c1 EB |
105 | return ret; |
106 | } | |
107 | ||
c3ac9df8 RN |
108 | /** |
109 | * Perform any cleanup actions once GPSBabel has completed running | |
110 | */ | |
111 | static void babel_watch ( GPid pid, | |
112 | gint status, | |
113 | gpointer user_data ) | |
70548902 | 114 | { |
c3ac9df8 | 115 | g_spawn_close_pid ( pid ); |
70548902 | 116 | } |
c3ac9df8 RN |
117 | |
118 | /** | |
119 | * babel_general_convert: | |
120 | * @args: The command line arguments passed to GPSBabel | |
121 | * @cb: callback that is run for each line of GPSBabel output and at completion of the run | |
122 | * callback may be NULL | |
123 | * @user_data: passed along to cb | |
124 | * | |
125 | * The function to actually invoke the GPSBabel external command | |
126 | * | |
127 | * Returns: %TRUE on successful invocation of GPSBabel command | |
128 | */ | |
883e5ed6 | 129 | static gboolean babel_general_convert( BabelStatusFunc cb, gchar **args, gpointer user_data ) |
8cf048bd | 130 | { |
2ecf5998 | 131 | gboolean ret = FALSE; |
8cf048bd | 132 | GPid pid; |
6c641b1a MA |
133 | GError *error = NULL; |
134 | gint babel_stdout; | |
8cf048bd | 135 | |
c3ac9df8 | 136 | if (!g_spawn_async_with_pipes (NULL, args, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL, &babel_stdout, NULL, &error)) { |
f2131761 | 137 | g_warning ("Async command failed: %s", error->message); |
6c641b1a | 138 | g_error_free(error); |
8cf048bd EB |
139 | ret = FALSE; |
140 | } else { | |
2b756ea0 | 141 | |
8cf048bd EB |
142 | gchar line[512]; |
143 | FILE *diag; | |
144 | diag = fdopen(babel_stdout, "r"); | |
145 | setvbuf(diag, NULL, _IONBF, 0); | |
146 | ||
147 | while (fgets(line, sizeof(line), diag)) { | |
7b3479e3 EB |
148 | if ( cb ) |
149 | cb(BABEL_DIAG_OUTPUT, line, user_data); | |
8cf048bd | 150 | } |
7b3479e3 EB |
151 | if ( cb ) |
152 | cb(BABEL_DONE, NULL, user_data); | |
8cf048bd | 153 | fclose(diag); |
8c060406 | 154 | diag = NULL; |
c3ac9df8 RN |
155 | |
156 | g_child_watch_add ( pid, (GChildWatchFunc) babel_watch, NULL ); | |
8cf048bd | 157 | |
2634ad33 GB |
158 | ret = TRUE; |
159 | } | |
160 | ||
161 | return ret; | |
162 | } | |
2634ad33 GB |
163 | |
164 | /** | |
165 | * babel_general_convert_from: | |
733949a2 RN |
166 | * @vtl: The TrackWaypoint Layer to save the data into |
167 | * If it is null it signifies that no data is to be processed, | |
168 | * however the gpsbabel command is still ran as it can be for non-data related options eg: | |
169 | * for use with the power off command - 'command_off' | |
2634ad33 GB |
170 | * @cb: callback that is run upon new data from STDOUT (?) |
171 | * (TODO: STDERR would be nice since we usually redirect STDOUT) | |
172 | * @user_data: passed along to cb | |
173 | * | |
174 | * Runs args[0] with the arguments and uses the GPX module | |
175 | * to import the GPX data into layer vt. Assumes that upon | |
176 | * running the command, the data will appear in the (usually | |
177 | * temporary) file name_dst. | |
178 | * | |
179 | * Returns: %TRUE on success | |
180 | */ | |
181 | static gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_dst, gpointer user_data ) | |
182 | { | |
183 | gboolean ret = FALSE; | |
184 | FILE *f = NULL; | |
185 | ||
883e5ed6 | 186 | if (babel_general_convert(cb, args, user_data)) { |
2634ad33 | 187 | |
733949a2 RN |
188 | /* No data actually required but still need to have run gpsbabel anyway |
189 | - eg using the device power command_off */ | |
190 | if ( vt == NULL ) | |
191 | return TRUE; | |
192 | ||
8c060406 MA |
193 | f = g_fopen(name_dst, "r"); |
194 | if (f) { | |
efe9a9c3 | 195 | ret = a_gpx_read_file ( vt, f ); |
8c060406 MA |
196 | fclose(f); |
197 | f = NULL; | |
2ecf5998 | 198 | } |
8cf048bd EB |
199 | } |
200 | ||
201 | return ret; | |
202 | } | |
203 | ||
072cbb13 GB |
204 | /** |
205 | * a_babel_convert_from: | |
206 | * @vt: The TRW layer to place data into. Duplicate items will be overwritten. | |
207 | * @babelargs: A string containing gpsbabel command line options. In addition to any filters, this string | |
208 | * must include the input file type (-i) option. | |
209 | * @cb: Optional callback function. Same usage as in a_babel_convert(). | |
ce4f6212 | 210 | * @user_data: passed along to cb |
ed691ed1 | 211 | * @not_used: Must use NULL |
072cbb13 GB |
212 | * |
213 | * Loads data into a trw layer from a file, using gpsbabel. This routine is synchronous; | |
214 | * that is, it will block the calling program until the conversion is done. To avoid blocking, call | |
215 | * this routine from a worker thread. | |
216 | * | |
217 | * Returns: %TRUE on success | |
218 | */ | |
ed691ed1 | 219 | gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, const char *from, BabelStatusFunc cb, gpointer user_data, gpointer not_used ) |
1d1bc3c1 | 220 | { |
f3cd9987 | 221 | int i,j; |
1d1bc3c1 | 222 | int fd_dst; |
18ec873d | 223 | gchar *name_dst = NULL; |
1d1bc3c1 | 224 | gboolean ret = FALSE; |
f3cd9987 | 225 | gchar *args[64]; |
1d1bc3c1 | 226 | |
2ecf5998 | 227 | if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) { |
e45b3da1 | 228 | g_debug ("%s: temporary file: %s", __FUNCTION__, name_dst); |
1d1bc3c1 EB |
229 | close(fd_dst); |
230 | ||
92255687 | 231 | if (gpsbabel_loc ) { |
f3cd9987 QT |
232 | gchar **sub_args = g_strsplit(babelargs, " ", 0); |
233 | ||
234 | i = 0; | |
235 | if (unbuffer_loc) | |
236 | args[i++] = unbuffer_loc; | |
237 | args[i++] = gpsbabel_loc; | |
e208e3c0 QT |
238 | for (j = 0; sub_args[j]; j++) { |
239 | /* some version of gpsbabel can not take extra blank arg */ | |
240 | if (sub_args[j][0] != '\0') | |
241 | args[i++] = sub_args[j]; | |
242 | } | |
f3cd9987 QT |
243 | args[i++] = "-o"; |
244 | args[i++] = "gpx"; | |
e208e3c0 | 245 | args[i++] = "-f"; |
8398d878 | 246 | args[i++] = (char *)from; |
e208e3c0 | 247 | args[i++] = "-F"; |
f3cd9987 QT |
248 | args[i++] = name_dst; |
249 | args[i] = NULL; | |
250 | ||
251 | ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data ); | |
252 | ||
f3cd9987 | 253 | g_strfreev(sub_args); |
374b7f45 | 254 | } else |
24ff9c7b | 255 | g_critical("gpsbabel not found in PATH"); |
18ec873d GB |
256 | g_remove(name_dst); |
257 | g_free(name_dst); | |
1d1bc3c1 EB |
258 | } |
259 | ||
1d1bc3c1 EB |
260 | return ret; |
261 | } | |
8cf048bd | 262 | |
072cbb13 GB |
263 | /** |
264 | * a_babel_convert_from_shellcommand: | |
ce4f6212 GB |
265 | * @vt: The #VikTrwLayer where to insert the collected data |
266 | * @input_cmd: the command to run | |
267 | * @cb: Optional callback function. Same usage as in a_babel_convert(). | |
268 | * @user_data: passed along to cb | |
ed691ed1 | 269 | * @not_used: Must use NULL |
072cbb13 GB |
270 | * |
271 | * Runs the input command in a shell (bash) and optionally uses GPSBabel to convert from input_file_type. | |
272 | * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX (or Geocaching *.loc) | |
28c82d8b | 273 | * |
072cbb13 | 274 | * Uses babel_general_convert_from() to actually run the command. This function |
28c82d8b EB |
275 | * prepares the command and temporary file, and sets up the arguments for bash. |
276 | */ | |
ed691ed1 | 277 | gboolean a_babel_convert_from_shellcommand ( VikTrwLayer *vt, const char *input_cmd, const char *input_file_type, BabelStatusFunc cb, gpointer user_data, gpointer not_used ) |
8cf048bd EB |
278 | { |
279 | int fd_dst; | |
18ec873d | 280 | gchar *name_dst = NULL; |
8cf048bd EB |
281 | gboolean ret = FALSE; |
282 | gchar **args; | |
283 | ||
2ecf5998 | 284 | if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) { |
e45b3da1 | 285 | g_debug ("%s: temporary file: %s", __FUNCTION__, name_dst); |
28c82d8b EB |
286 | gchar *shell_command; |
287 | if ( input_file_type ) | |
18ec873d GB |
288 | shell_command = g_strdup_printf("%s | %s -i %s -f - -o gpx -F %s", |
289 | input_cmd, gpsbabel_loc, input_file_type, name_dst); | |
28c82d8b EB |
290 | else |
291 | shell_command = g_strdup_printf("%s > %s", input_cmd, name_dst); | |
292 | ||
29a5a545 | 293 | g_debug("%s: %s", __FUNCTION__, shell_command); |
8cf048bd EB |
294 | close(fd_dst); |
295 | ||
296 | args = g_malloc(sizeof(gchar *)*4); | |
297 | args[0] = BASH_LOCATION; | |
298 | args[1] = "-c"; | |
299 | args[2] = shell_command; | |
300 | args[3] = NULL; | |
301 | ||
7b3479e3 | 302 | ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data ); |
8cf048bd EB |
303 | g_free ( args ); |
304 | g_free ( shell_command ); | |
18ec873d GB |
305 | g_remove(name_dst); |
306 | g_free(name_dst); | |
8cf048bd EB |
307 | } |
308 | ||
8cf048bd EB |
309 | return ret; |
310 | } | |
311 | ||
ce4f6212 GB |
312 | /** |
313 | * a_babel_convert_from_url: | |
314 | * @vt: The #VikTrwLayer where to insert the collected data | |
315 | * @url: the URL to fetch | |
316 | * @cb: Optional callback function. Same usage as in a_babel_convert(). | |
317 | * @user_data: passed along to cb | |
ed691ed1 | 318 | * @options: download options. Maybe NULL. |
ce4f6212 | 319 | * |
cc2e600e GB |
320 | * Download the file pointed by the URL and optionally uses GPSBabel to convert from input_file_type. |
321 | * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX. | |
ed691ed1 RN |
322 | * |
323 | * Returns: %TRUE on successful invocation of GPSBabel or read of the GPX | |
324 | * | |
ce4f6212 | 325 | */ |
ed691ed1 | 326 | gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char *input_type, BabelStatusFunc cb, gpointer user_data, DownloadMapOptions *options ) |
533bbf34 | 327 | { |
ed691ed1 RN |
328 | // If no download options specified, use defaults: |
329 | DownloadMapOptions myoptions = { FALSE, FALSE, NULL, 0, NULL, NULL }; | |
330 | if ( options ) | |
331 | myoptions = *options; | |
533bbf34 | 332 | gint fd_src; |
8bd9fe17 GB |
333 | int fetch_ret; |
334 | gboolean ret = FALSE; | |
18ec873d GB |
335 | gchar *name_src = NULL; |
336 | gchar *babelargs = NULL; | |
533bbf34 MA |
337 | |
338 | g_debug("%s: input_type=%s url=%s", __FUNCTION__, input_type, url); | |
339 | ||
8bd9fe17 | 340 | if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) { |
e45b3da1 | 341 | g_debug ("%s: temporary file: %s", __FUNCTION__, name_src); |
533bbf34 MA |
342 | close(fd_src); |
343 | g_remove(name_src); | |
344 | ||
ed691ed1 | 345 | fetch_ret = a_http_download_get_url(url, "", name_src, &myoptions, NULL); |
cc2e600e GB |
346 | if (fetch_ret == 0) { |
347 | if (input_type != NULL) { | |
348 | babelargs = g_strdup_printf(" -i %s", input_type); | |
ed691ed1 | 349 | ret = a_babel_convert_from( vt, babelargs, name_src, NULL, NULL, NULL ); |
cc2e600e GB |
350 | } else { |
351 | /* Process directly the retrieved file */ | |
352 | g_debug("%s: directly read GPX file %s", __FUNCTION__, name_src); | |
353 | FILE *f = g_fopen(name_src, "r"); | |
354 | if (f) { | |
355 | ret = a_gpx_read_file ( vt, f ); | |
356 | fclose(f); | |
357 | f = NULL; | |
358 | } | |
359 | } | |
360 | } | |
533bbf34 MA |
361 | g_remove(name_src); |
362 | g_free(babelargs); | |
363 | g_free(name_src); | |
364 | } | |
365 | ||
366 | return ret; | |
367 | } | |
368 | ||
6f94db6d | 369 | static gboolean babel_general_convert_to( VikTrwLayer *vt, VikTrack *trk, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data ) |
70548902 | 370 | { |
208d2084 RN |
371 | // Now strips out invisible tracks and waypoints |
372 | if (!a_file_export(vt, name_src, FILE_TYPE_GPX, trk, FALSE)) { | |
24ff9c7b | 373 | g_critical("Error exporting to %s", name_src); |
18ec873d | 374 | return FALSE; |
70548902 MA |
375 | } |
376 | ||
883e5ed6 | 377 | return babel_general_convert (cb, args, user_data); |
b364d6bc QT |
378 | } |
379 | ||
6f94db6d RN |
380 | /** |
381 | * a_babel_convert_to: | |
ce4f6212 GB |
382 | * @vt: The TRW layer from which data is taken. |
383 | * @track: Operate on the individual track if specified. Use NULL when operating on a TRW layer | |
384 | * @babelargs: A string containing gpsbabel command line options. In addition to any filters, this string | |
6f94db6d | 385 | * must include the input file type (-i) option. |
ce4f6212 GB |
386 | * @to: Filename or device the data is written to. |
387 | * @cb: Optional callback function. Same usage as in a_babel_convert. | |
388 | * @user_data: passed along to cb | |
6f94db6d RN |
389 | * |
390 | * Exports data using gpsbabel. This routine is synchronous; | |
391 | * that is, it will block the calling program until the conversion is done. To avoid blocking, call | |
392 | * this routine from a worker thread. | |
393 | * | |
394 | * Returns: %TRUE on successful invocation of GPSBabel command | |
395 | */ | |
396 | gboolean a_babel_convert_to( VikTrwLayer *vt, VikTrack *track, const char *babelargs, const char *to, BabelStatusFunc cb, gpointer user_data ) | |
b364d6bc | 397 | { |
f3cd9987 | 398 | int i,j; |
b364d6bc | 399 | int fd_src; |
18ec873d | 400 | gchar *name_src = NULL; |
b364d6bc | 401 | gboolean ret = FALSE; |
f3cd9987 | 402 | gchar *args[64]; |
b364d6bc | 403 | |
2ecf5998 | 404 | if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) { |
e45b3da1 | 405 | g_debug ("%s: temporary file: %s", __FUNCTION__, name_src); |
b364d6bc QT |
406 | close(fd_src); |
407 | ||
b364d6bc | 408 | if (gpsbabel_loc ) { |
f3cd9987 QT |
409 | gchar **sub_args = g_strsplit(babelargs, " ", 0); |
410 | ||
411 | i = 0; | |
412 | if (unbuffer_loc) | |
413 | args[i++] = unbuffer_loc; | |
414 | args[i++] = gpsbabel_loc; | |
415 | args[i++] = "-i"; | |
416 | args[i++] = "gpx"; | |
417 | for (j = 0; sub_args[j]; j++) | |
e208e3c0 QT |
418 | /* some version of gpsbabel can not take extra blank arg */ |
419 | if (sub_args[j][0] != '\0') | |
420 | args[i++] = sub_args[j]; | |
421 | args[i++] = "-f"; | |
f3cd9987 | 422 | args[i++] = name_src; |
e208e3c0 | 423 | args[i++] = "-F"; |
8398d878 | 424 | args[i++] = (char *)to; |
f3cd9987 QT |
425 | args[i] = NULL; |
426 | ||
6f94db6d | 427 | ret = babel_general_convert_to ( vt, track, cb, args, name_src, user_data ); |
f3cd9987 | 428 | |
f3cd9987 | 429 | g_strfreev(sub_args); |
374b7f45 | 430 | } else |
24ff9c7b | 431 | g_critical("gpsbabel not found in PATH"); |
18ec873d GB |
432 | g_remove(name_src); |
433 | g_free(name_src); | |
b364d6bc QT |
434 | } |
435 | ||
b364d6bc QT |
436 | return ret; |
437 | } | |
18ec873d | 438 | |
79e0f36e GB |
439 | static void set_mode(BabelMode mode, gchar *smode) |
440 | { | |
441 | mode.waypointsRead = smode[0] == 'r'; | |
442 | mode.waypointsWrite = smode[1] == 'w'; | |
443 | mode.tracksRead = smode[2] == 'r'; | |
444 | mode.tracksWrite = smode[3] == 'w'; | |
445 | mode.routesRead = smode[4] == 'r'; | |
446 | mode.routesWrite = smode[5] == 'w'; | |
447 | } | |
448 | ||
449 | /** | |
ce4f6212 | 450 | * load_feature_parse_line: |
79e0f36e GB |
451 | * |
452 | * Load a single feature stored in the given line. | |
453 | */ | |
454 | static void load_feature_parse_line (gchar *line) | |
455 | { | |
456 | gchar **tokens = g_strsplit ( line, "\t", 0 ); | |
457 | if ( tokens != NULL | |
458 | && tokens[0] != NULL ) { | |
459 | if ( strcmp("serial", tokens[0]) == 0 ) { | |
460 | if ( tokens[1] != NULL | |
461 | && tokens[2] != NULL | |
462 | && tokens[3] != NULL | |
463 | && tokens[4] != NULL ) { | |
464 | BabelDevice *device = g_malloc ( sizeof (BabelDevice) ); | |
465 | set_mode (device->mode, tokens[1]); | |
466 | device->name = g_strdup (tokens[2]); | |
431624c5 | 467 | device->label = g_strndup (tokens[4], 50); // Limit really long label text |
79e0f36e GB |
468 | a_babel_device_list = g_list_append (a_babel_device_list, device); |
469 | g_debug ("New gpsbabel device: %s", device->name); | |
470 | } else { | |
471 | g_warning ( "Unexpected gpsbabel format string: %s", line); | |
472 | } | |
473 | } else if ( strcmp("file", tokens[0]) == 0 ) { | |
474 | if ( tokens[1] != NULL | |
475 | && tokens[2] != NULL | |
476 | && tokens[3] != NULL | |
477 | && tokens[4] != NULL ) { | |
478 | BabelFile *file = g_malloc ( sizeof (BabelFile) ); | |
479 | set_mode (file->mode, tokens[1]); | |
480 | file->name = g_strdup (tokens[2]); | |
481 | file->ext = g_strdup (tokens[3]); | |
482 | file->label = g_strdup (tokens[4]); | |
483 | a_babel_file_list = g_list_append (a_babel_file_list, file); | |
484 | g_debug ("New gpsbabel file: %s", file->name); | |
485 | } else { | |
486 | g_warning ( "Unexpected gpsbabel format string: %s", line); | |
487 | } | |
488 | } /* else: ignore */ | |
489 | } else { | |
490 | g_warning ( "Unexpected gpsbabel format string: %s", line); | |
491 | } | |
492 | g_strfreev ( tokens ); | |
493 | } | |
494 | ||
495 | static void load_feature_cb (BabelProgressCode code, gpointer line, gpointer user_data) | |
496 | { | |
497 | if (line != NULL) | |
498 | load_feature_parse_line (line); | |
499 | } | |
500 | ||
501 | static gboolean load_feature () | |
502 | { | |
503 | int i; | |
504 | gboolean ret = FALSE; | |
505 | gchar *args[4]; | |
506 | ||
507 | if ( gpsbabel_loc ) { | |
508 | i = 0; | |
509 | if ( unbuffer_loc ) | |
510 | args[i++] = unbuffer_loc; | |
511 | args[i++] = gpsbabel_loc; | |
512 | args[i++] = "-^3"; | |
513 | args[i] = NULL; | |
514 | ||
515 | ret = babel_general_convert (load_feature_cb, args, NULL); | |
516 | } else | |
24ff9c7b | 517 | g_critical("gpsbabel not found in PATH"); |
79e0f36e GB |
518 | |
519 | return ret; | |
520 | } | |
18ec873d | 521 | |
ce4f6212 GB |
522 | /** |
523 | * a_babel_init: | |
524 | * | |
525 | * Initialises babel module. | |
526 | * Mainly check existence of gpsbabel progam | |
527 | * and load all features available in ths version. | |
528 | */ | |
18ec873d GB |
529 | void a_babel_init () |
530 | { | |
531 | /* TODO allow to set gpsbabel path via command line */ | |
532 | gpsbabel_loc = g_find_program_in_path( "gpsbabel" ); | |
533 | if ( !gpsbabel_loc ) | |
24ff9c7b | 534 | g_critical( "gpsbabel not found in PATH" ); |
3684f362 RN |
535 | |
536 | // Unlikely to package unbuffer on Windows so ATM don't even bother trying | |
537 | // Highly unlikely unbuffer is available on a Windows system otherwise | |
538 | #ifndef WINDOWS | |
18ec873d GB |
539 | unbuffer_loc = g_find_program_in_path( "unbuffer" ); |
540 | if ( !unbuffer_loc ) | |
541 | g_warning( "unbuffer not found in PATH" ); | |
3684f362 | 542 | #endif |
18ec873d | 543 | |
79e0f36e | 544 | load_feature (); |
18ec873d GB |
545 | } |
546 | ||
ce4f6212 GB |
547 | /** |
548 | * a_babel_uninit: | |
549 | * | |
550 | * Free resources acquired by a_babel_init. | |
551 | */ | |
18ec873d GB |
552 | void a_babel_uninit () |
553 | { | |
554 | g_free ( gpsbabel_loc ); | |
555 | g_free ( unbuffer_loc ); | |
67c32f00 RN |
556 | |
557 | if ( a_babel_file_list ) { | |
558 | GList *gl; | |
559 | for (gl = a_babel_file_list; gl != NULL; gl = g_list_next(gl)) { | |
560 | BabelFile *file = gl->data; | |
561 | g_free ( file->name ); | |
562 | g_free ( file->ext ); | |
563 | g_free ( file->label ); | |
564 | g_free ( gl->data ); | |
565 | } | |
566 | g_list_free ( a_babel_file_list ); | |
567 | } | |
568 | ||
569 | if ( a_babel_device_list ) { | |
570 | GList *gl; | |
571 | for (gl = a_babel_device_list; gl != NULL; gl = g_list_next(gl)) { | |
572 | BabelDevice *device = gl->data; | |
573 | g_free ( device->name ); | |
574 | g_free ( device->label ); | |
575 | g_free ( gl->data ); | |
576 | } | |
577 | g_list_free ( a_babel_device_list ); | |
578 | } | |
579 | ||
18ec873d | 580 | } |