]> git.street.me.uk Git - andy/viking.git/blame - src/util.c
Fix crash if a route requested by the route finder is empty.
[andy/viking.git] / src / util.c
CommitLineData
7d02a0b0
GB
1/*
2 * Viking - GPS data editor
a482007a 3 * Copyright (C) 2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
44871dd1 4 * Copyright (C) 2014, Rob Norris <rw_norris@hotmail.com>
7d02a0b0 5 * Based on:
a482007a 6 * Copyright (C) 2003-2007, Leandro A. F. Pereira <leandro@linuxmag.com.br>
7d02a0b0
GB
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, version 2.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
5ab2942c 21 /*
6a280b85
RN
22 * Dependencies must be just on Glib
23 * see ui_utils for thing that depend on Gtk
5ab2942c
RN
24 * see vikutils for things that further depend on other Viking types
25 */
4c77d5e0
GB
26#ifdef HAVE_CONFIG_H
27#include "config.h"
28#endif
29
91c46f90 30#include <glib/gstdio.h>
4c77d5e0 31#include <glib/gi18n.h>
c612e922 32#include <glib/gprintf.h>
73def8cb 33#include <gio/gio.h>
7d02a0b0 34
8e562a70 35#include "util.h"
81bf47ee 36#include "globals.h"
7d02a0b0 37
6b6225cb
RN
38#ifdef WINDOWS
39#include <windows.h>
40#else
41#include <unistd.h>
42#endif
43
44guint util_get_number_of_cpus ()
45{
46#if GLIB_CHECK_VERSION (2, 36, 0)
47 return g_get_num_processors();
48#else
49 long nprocs = 1;
50#ifdef WINDOWS
51 SYSTEM_INFO info;
52 GetSystemInfo(&info);
53 nprocs = info.dwNumberOfProcessors;
54#else
55#ifdef _SC_NPROCESSORS_ONLN
56 nprocs = sysconf(_SC_NPROCESSORS_ONLN);
57 if (nprocs < 1)
58 nprocs = 1;
59#endif
60#endif
61 return nprocs;
62#endif
63}
64
ba4a5e11
GB
65gchar *uri_escape(gchar *str)
66{
67 gchar *esc_str = g_malloc(3*strlen(str));
68 gchar *dst = esc_str;
69 gchar *src;
70
71 for (src = str; *src; src++) {
72 if (*src == ' ')
73 *dst++ = '+';
74 else if (g_ascii_isalnum(*src))
75 *dst++ = *src;
76 else {
7705a76f 77 g_sprintf(dst, "%%%02hhX", *src);
ba4a5e11
GB
78 dst += 3;
79 }
80 }
81 *dst = '\0';
82
83 return(esc_str);
84}
85
70434be3
GB
86
87GList * str_array_to_glist(gchar* data[])
88{
89 GList *gl = NULL;
90 gpointer * p;
91 for (p = (gpointer)data; *p; p++)
92 gl = g_list_prepend(gl, *p);
93 return g_list_reverse(gl);
94}
9106934d 95
0da89d90
RN
96/**
97 * split_string_from_file_on_equals:
98 *
99 * @buf: the input string
100 * @key: newly allocated string that is before the '='
101 * @val: newly allocated string after the '='
102 *
103 * Designed for file line processing, so it ignores strings beginning with special
104 * characters, such as '#'; returns false in such situations.
105 * Also returns false if no equals character is found
106 *
107 * e.g. if buf = "GPS.parameter=42"
108 * key = "GPS.parameter"
109 * val = "42"
110 */
111gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
9106934d 112{
9106934d
RN
113 // comments, special characters in viking file format
114 if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
115 return FALSE;
0da89d90
RN
116
117 if ( ! strchr ( buf, '=' ) )
9106934d 118 return FALSE;
0da89d90
RN
119
120 gchar **strv = g_strsplit ( buf, "=", 2 );
121
122 gint gi = 0;
123 gchar *str = strv[gi];
124 while ( str ) {
125 if ( gi == 0 )
126 *key = g_strdup ( str );
127 else
128 *val = g_strdup ( str );
129 gi++;
130 str = strv[gi];
131 }
132
133 g_strfreev ( strv );
134
135 // Remove newline from val and also any other whitespace
136 *key = g_strstrip ( *key );
137 *val = g_strstrip ( *val );
9106934d
RN
138 return TRUE;
139}
44871dd1
RN
140
141static GSList* deletion_list = NULL;
142
143/**
144 * util_add_to_deletion_list:
145 *
146 * Add a name of a file into the list that is to be deleted on program exit
147 * Normally this is for files that get used asynchronously,
148 * so we don't know when it's time to delete them - other than at this program's end
149 */
150void util_add_to_deletion_list ( const gchar* filename )
151{
152 deletion_list = g_slist_append ( deletion_list, g_strdup (filename) );
153}
154
155/**
156 * util_remove_all_in_deletion_list:
157 *
158 * Delete all the files in the deletion list
159 * This should only be called on program exit
160 */
161void util_remove_all_in_deletion_list ( void )
162{
163 while ( deletion_list )
164 {
fd437981
RN
165 if ( g_remove ( (const char*)deletion_list->data ) )
166 g_warning ( "%s: Failed to remove %s", __FUNCTION__, (char*)deletion_list->data );
44871dd1
RN
167 g_free ( deletion_list->data );
168 deletion_list = g_slist_delete_link ( deletion_list, deletion_list );
169 }
170}
6a280b85
RN
171
172/**
173 * Removes characters from a string, in place.
174 *
175 * @param string String to search.
176 * @param chars Characters to remove.
177 *
178 * @return @a string - return value is only useful when nesting function calls, e.g.:
179 * @code str = utils_str_remove_chars(g_strdup("f_o_o"), "_"); @endcode
180 *
181 * @see @c g_strdelimit.
182 **/
183gchar *util_str_remove_chars(gchar *string, const gchar *chars)
184{
185 const gchar *r;
186 gchar *w = string;
187
188 g_return_val_if_fail(string, NULL);
189 if (G_UNLIKELY(EMPTY(chars)))
190 return string;
191
192 foreach_str(r, string)
193 {
194 if (!strchr(chars, *r))
195 *w++ = *r;
196 }
197 *w = 0x0;
198 return string;
199}
81bf47ee
RN
200
201/**
202 * In 'extreme' debug mode don't remove temporary files
203 * thus the contents can be inspected if things go wrong
204 * with the trade off the user may need to delete tmp files manually
205 * Only use this for 'occasional' downloaded temporary files that need interpretation,
206 * rather than large volume items such as Bing attributions.
207 */
208int util_remove ( const gchar *filename )
209{
210 if ( vik_debug && vik_verbose ) {
211 g_warning ( "Not removing file: %s", filename );
212 return 0;
213 }
214 else
215 return g_remove ( filename );
216}
73def8cb
RN
217
218/**
219 * Stream write buffer to a temporary file (in one go)
220 *
221 * @param buffer The buffer to write
222 * @param count Size of the buffer to write
223 *
224 * @return the filename of the buffer that was written
225 */
226gchar* util_write_tmp_file_from_bytes ( const void *buffer, gsize count )
227{
228 GFileIOStream *gios;
229 GError *error = NULL;
230 gchar *tmpname = NULL;
231
232#if GLIB_CHECK_VERSION(2,32,0)
233 GFile *gf = g_file_new_tmp ( "vik-tmp.XXXXXX", &gios, &error );
234 tmpname = g_file_get_path (gf);
235#else
236 gint fd = g_file_open_tmp ( "vik-tmp.XXXXXX", &tmpname, &error );
237 if ( error ) {
238 g_warning ( "%s", error->message );
239 g_error_free ( error );
240 return NULL;
241 }
242 gios = g_file_open_readwrite ( g_file_new_for_path (tmpname), NULL, &error );
243 if ( error ) {
244 g_warning ( "%s", error->message );
245 g_error_free ( error );
246 return NULL;
247 }
248#endif
249
250 gios = g_file_open_readwrite ( g_file_new_for_path (tmpname), NULL, &error );
251 if ( error ) {
252 g_warning ( "%s", error->message );
253 g_error_free ( error );
254 return NULL;
255 }
256
257 GOutputStream *gos = g_io_stream_get_output_stream ( G_IO_STREAM(gios) );
258 if ( g_output_stream_write ( gos, buffer, count, NULL, &error ) < 0 ) {
259 g_critical ( "Couldn't write tmp %s file due to %s", tmpname, error->message );
260 g_free (tmpname);
261 tmpname = NULL;
262 }
263
264 g_output_stream_close ( gos, NULL, &error );
265 g_object_unref ( gios );
266
267 return tmpname;
268}