]> git.street.me.uk Git - andy/viking.git/blame - src/babel.c
Alex's GPSBabel acquire 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>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include "viking.h"
23#include "gpx.h"
24#include "babel.h"
25#include "sys/wait.h"
26
27gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb )
28{
29 int fd_src;
30 FILE *f;
31 gchar *name_src;
32 gboolean ret = FALSE;
33 gchar *bargs = g_strconcat(babelargs, " -i gpx");
34
35 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) < 0) {
36 ret = FALSE;
37 } else {
38 f = fdopen(fd_src, "w");
39 a_gpx_write_file(vt, f);
40 fclose(f);
41 ret = a_babel_convert_from ( vt, bargs, cb, name_src );
42 }
43
44 g_free(bargs);
45 remove(name_src);
46 g_free(name_src);
47 return ret;
48}
49
50gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, const char *from )
51{
52 int fd_dst;
53 FILE *f;
54 gchar *name_dst;
55 gchar cmd[1024];
56 gboolean ret = FALSE;
57 gchar **args;
58 GPid pid;
59
60 gint babel_stdin, babel_stdout, babel_stderr;
61
62 if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) < 0) {
63 ret = FALSE;
64 } else {
65 close(fd_dst);
66
67 g_stpcpy(cmd, "/usr/local/bin/gpsbabel ");
68 g_strlcat(cmd, babelargs, sizeof(cmd));
69 g_strlcat(cmd, " -o gpx ", sizeof(cmd));
70 g_strlcat(cmd, from, sizeof(cmd));
71 g_strlcat(cmd, " ", sizeof(cmd));
72 g_strlcat(cmd, name_dst, sizeof(cmd));
73
74 args = g_strsplit(cmd, " ", 0);
75
76 if (!g_spawn_async_with_pipes (NULL, args, NULL, 0, NULL, NULL, &pid, &babel_stdin, &babel_stdout, &babel_stderr, NULL)) {
77 // if (!g_spawn_async_with_pipes (NULL, args, NULL, 0, NULL, NULL, NULL, &babel_stdin, &babel_stdout, NULL, NULL)) {
78 ret = FALSE;
79 } else {
80 gchar line[512];
81 FILE *diag;
82 diag = fdopen(babel_stdout, "r");
83 setvbuf(diag, NULL, _IONBF, 0);
84
85 while (fgets(line, sizeof(line), diag)) {
86 cb(BABEL_DIAG_OUTPUT, line);
87 }
88 cb(BABEL_DONE, NULL);
89 fclose(diag);
90 waitpid(pid, NULL, 0);
91 g_spawn_close_pid(pid);
92
93 f = fopen(name_dst, "r");
94 a_gpx_read_file ( vt, f );
95 fclose(f);
96 ret = TRUE;
97 }
98
99 g_strfreev(args);
100 }
101
102 remove(name_dst);
103 g_free(name_dst);
104 return ret;
105}