]>
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> | |
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 | ||
8cf048bd EB |
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" | |
29 | ||
7b3479e3 | 30 | gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, gpointer user_data ) |
1d1bc3c1 EB |
31 | { |
32 | int fd_src; | |
33 | FILE *f; | |
34 | gchar *name_src; | |
35 | gboolean ret = FALSE; | |
36 | gchar *bargs = g_strconcat(babelargs, " -i gpx"); | |
37 | ||
38 | if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) < 0) { | |
39 | ret = FALSE; | |
40 | } else { | |
41 | f = fdopen(fd_src, "w"); | |
42 | a_gpx_write_file(vt, f); | |
43 | fclose(f); | |
7b3479e3 | 44 | ret = a_babel_convert_from ( vt, bargs, cb, name_src, user_data ); |
1d1bc3c1 EB |
45 | } |
46 | ||
47 | g_free(bargs); | |
48 | remove(name_src); | |
49 | g_free(name_src); | |
50 | return ret; | |
51 | } | |
52 | ||
7b3479e3 | 53 | gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_dst, gpointer user_data ) |
8cf048bd EB |
54 | { |
55 | gboolean ret; | |
56 | GPid pid; | |
57 | gint babel_stdin, babel_stdout, babel_stderr; | |
58 | FILE *f; | |
59 | ||
60 | ||
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)) { | |
63 | ret = FALSE; | |
64 | } else { | |
65 | gchar line[512]; | |
66 | FILE *diag; | |
67 | diag = fdopen(babel_stdout, "r"); | |
68 | setvbuf(diag, NULL, _IONBF, 0); | |
69 | ||
70 | while (fgets(line, sizeof(line), diag)) { | |
7b3479e3 EB |
71 | if ( cb ) |
72 | cb(BABEL_DIAG_OUTPUT, line, user_data); | |
8cf048bd | 73 | } |
7b3479e3 EB |
74 | if ( cb ) |
75 | cb(BABEL_DONE, NULL, user_data); | |
8cf048bd EB |
76 | fclose(diag); |
77 | waitpid(pid, NULL, 0); | |
78 | g_spawn_close_pid(pid); | |
79 | ||
80 | f = fopen(name_dst, "r"); | |
81 | a_gpx_read_file ( vt, f ); | |
82 | fclose(f); | |
83 | ret = TRUE; | |
84 | } | |
85 | ||
86 | return ret; | |
87 | } | |
88 | ||
7b3479e3 | 89 | gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, const char *from, gpointer user_data ) |
1d1bc3c1 EB |
90 | { |
91 | int fd_dst; | |
1d1bc3c1 EB |
92 | gchar *name_dst; |
93 | gchar cmd[1024]; | |
94 | gboolean ret = FALSE; | |
95 | gchar **args; | |
1d1bc3c1 EB |
96 | |
97 | if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) < 0) { | |
98 | ret = FALSE; | |
99 | } else { | |
100 | close(fd_dst); | |
101 | ||
102 | g_stpcpy(cmd, "/usr/local/bin/gpsbabel "); | |
103 | g_strlcat(cmd, babelargs, sizeof(cmd)); | |
104 | g_strlcat(cmd, " -o gpx ", sizeof(cmd)); | |
105 | g_strlcat(cmd, from, sizeof(cmd)); | |
106 | g_strlcat(cmd, " ", sizeof(cmd)); | |
107 | g_strlcat(cmd, name_dst, sizeof(cmd)); | |
108 | ||
109 | args = g_strsplit(cmd, " ", 0); | |
7b3479e3 | 110 | ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data ); |
1d1bc3c1 EB |
111 | g_strfreev(args); |
112 | } | |
113 | ||
114 | remove(name_dst); | |
115 | g_free(name_dst); | |
116 | return ret; | |
117 | } | |
8cf048bd | 118 | |
7b3479e3 | 119 | gboolean a_babel_convert_from_shellcommand ( VikTrwLayer *vt, const char *input_cmd, const char *input_type, BabelStatusFunc cb, gpointer user_data ) |
8cf048bd EB |
120 | { |
121 | int fd_dst; | |
122 | gchar *name_dst; | |
123 | gboolean ret = FALSE; | |
124 | gchar **args; | |
125 | ||
126 | if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) < 0) { | |
127 | ret = FALSE; | |
128 | } else { | |
129 | gchar *shell_command = g_strdup_printf("%s | gpsbabel -i %s -f - -o gpx -F %s", input_cmd, input_type, name_dst); | |
130 | ||
131 | close(fd_dst); | |
132 | ||
133 | args = g_malloc(sizeof(gchar *)*4); | |
134 | args[0] = BASH_LOCATION; | |
135 | args[1] = "-c"; | |
136 | args[2] = shell_command; | |
137 | args[3] = NULL; | |
138 | ||
7b3479e3 | 139 | ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data ); |
8cf048bd EB |
140 | g_free ( args ); |
141 | g_free ( shell_command ); | |
142 | } | |
143 | ||
144 | remove(name_dst); | |
145 | g_free(name_dst); | |
146 | return ret; | |
147 | } | |
148 |