2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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.
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.
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
27 /* in the future we could have support for other shells (change command strings), or not use a shell at all */
28 #define BASH_LOCATION "/bin/bash"
30 gboolean
a_babel_convert( VikTrwLayer
*vt
, const char *babelargs
, BabelStatusFunc cb
, gpointer user_data
)
36 gchar
*bargs
= g_strconcat(babelargs
, " -i gpx", NULL
);
38 if ((fd_src
= g_file_open_tmp("tmp-viking.XXXXXX", &name_src
, NULL
)) < 0) {
41 f
= fdopen(fd_src
, "w");
42 a_gpx_write_file(vt
, f
);
44 ret
= a_babel_convert_from ( vt
, bargs
, cb
, name_src
, user_data
);
53 gboolean
babel_general_convert_from( VikTrwLayer
*vt
, BabelStatusFunc cb
, gchar
**args
, const gchar
*name_dst
, gpointer user_data
)
57 gint babel_stdin
, babel_stdout
, babel_stderr
;
61 if (!g_spawn_async_with_pipes (NULL
, args
, NULL
, 0, NULL
, NULL
, &pid
, &babel_stdin
, &babel_stdout
, &babel_stderr
, NULL
)) {
62 // if (!g_spawn_async_with_pipes (NULL, args, NULL, 0, NULL, NULL, NULL, &babel_stdin, &babel_stdout, NULL, NULL)) {
67 diag
= fdopen(babel_stdout
, "r");
68 setvbuf(diag
, NULL
, _IONBF
, 0);
70 while (fgets(line
, sizeof(line
), diag
)) {
72 cb(BABEL_DIAG_OUTPUT
, line
, user_data
);
75 cb(BABEL_DONE
, NULL
, user_data
);
77 waitpid(pid
, NULL
, 0);
78 g_spawn_close_pid(pid
);
80 f
= fopen(name_dst
, "r");
81 a_gpx_read_file ( vt
, f
);
89 gboolean
a_babel_convert_from( VikTrwLayer
*vt
, const char *babelargs
, BabelStatusFunc cb
, const char *from
, gpointer user_data
)
97 if ((fd_dst
= g_file_open_tmp("tmp-viking.XXXXXX", &name_dst
, NULL
)) < 0) {
103 gpsbabel_loc
= g_find_program_in_path("gpsbabel");
106 gchar
*unbuffer_loc
= g_find_program_in_path("unbuffer");
107 cmd
= g_strdup_printf ( "%s%s%s %s -o gpx %s %s",
108 unbuffer_loc
? unbuffer_loc
: "",
109 unbuffer_loc
? " " : "",
116 g_free ( unbuffer_loc
);
118 args
= g_strsplit(cmd
, " ", 0);
119 ret
= babel_general_convert_from ( vt
, cb
, args
, name_dst
, user_data
);
130 gboolean
a_babel_convert_from_shellcommand ( VikTrwLayer
*vt
, const char *input_cmd
, const char *input_type
, BabelStatusFunc cb
, gpointer user_data
)
134 gboolean ret
= FALSE
;
137 if ((fd_dst
= g_file_open_tmp("tmp-viking.XXXXXX", &name_dst
, NULL
)) < 0) {
140 gchar
*shell_command
= g_strdup_printf("%s | gpsbabel -i %s -f - -o gpx -F %s", input_cmd
, input_type
, name_dst
);
141 g_debug("%s", shell_command
);
144 args
= g_malloc(sizeof(gchar
*)*4);
145 args
[0] = BASH_LOCATION
;
147 args
[2] = shell_command
;
150 ret
= babel_general_convert_from ( vt
, cb
, args
, name_dst
, user_data
);
152 g_free ( shell_command
);
160 gboolean
babel_general_convert_to( VikTrwLayer
*vt
, BabelStatusFunc cb
, gchar
**args
, const gchar
*name_src
, gpointer user_data
)
164 gint babel_stdin
, babel_stdout
, babel_stderr
;
166 if (!a_file_export(vt
, name_src
, FILE_TYPE_GPX
)) {
167 g_warning("babel_general_convert_to(): error exporting to %s\n", name_src
);
171 if (!g_spawn_async_with_pipes (NULL
, args
, NULL
, 0, NULL
, NULL
, &pid
, &babel_stdin
, &babel_stdout
, &babel_stderr
, NULL
)) {
176 diag
= fdopen(babel_stdout
, "r");
177 setvbuf(diag
, NULL
, _IONBF
, 0);
179 while (fgets(line
, sizeof(line
), diag
)) {
181 cb(BABEL_DIAG_OUTPUT
, line
, user_data
);
184 cb(BABEL_DONE
, NULL
, user_data
);
186 waitpid(pid
, NULL
, 0);
187 g_spawn_close_pid(pid
);
195 gboolean
a_babel_convert_to( VikTrwLayer
*vt
, const char *babelargs
, BabelStatusFunc cb
, const char *to
, gpointer user_data
)
200 gboolean ret
= FALSE
;
203 if ((fd_src
= g_file_open_tmp("tmp-viking.XXXXXX", &name_src
, NULL
)) < 0) {
209 gpsbabel_loc
= g_find_program_in_path("gpsbabel");
212 gchar
*unbuffer_loc
= g_find_program_in_path("unbuffer");
213 cmd
= g_strdup_printf ( "%s%s%s %s -i gpx %s %s",
214 unbuffer_loc
? unbuffer_loc
: "",
215 unbuffer_loc
? " " : "",
222 g_free ( unbuffer_loc
);
224 g_debug("cmd=%s\n", cmd
);
226 args
= g_strsplit(cmd
, " ", 0);
227 ret
= babel_general_convert_to ( vt
, cb
, args
, name_src
, user_data
);