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