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