2 * Viking - GPS data editor
3 * Copyright (C) 2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
4 * Copyright (C) 2014, Rob Norris <rw_norris@hotmail.com>
6 * Copyright (C) 2003-2007, Leandro A. F. Pereira <leandro@linuxmag.com.br>
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.
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.
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
22 * Dependencies must be just on Glib
23 * see ui_utils for thing that depend on Gtk
24 * see vikutils for things that further depend on other Viking types
30 #include <glib/gstdio.h>
31 #include <glib/gi18n.h>
32 #include <glib/gprintf.h>
43 guint util_get_number_of_cpus ()
45 #if GLIB_CHECK_VERSION (2, 36, 0)
46 return g_get_num_processors();
52 nprocs = info.dwNumberOfProcessors;
54 #ifdef _SC_NPROCESSORS_ONLN
55 nprocs = sysconf(_SC_NPROCESSORS_ONLN);
64 gchar *uri_escape(gchar *str)
66 gchar *esc_str = g_malloc(3*strlen(str));
70 for (src = str; *src; src++) {
73 else if (g_ascii_isalnum(*src))
76 g_sprintf(dst, "%%%02hhX", *src);
86 GList * str_array_to_glist(gchar* data[])
90 for (p = (gpointer)data; *p; p++)
91 gl = g_list_prepend(gl, *p);
92 return g_list_reverse(gl);
96 * split_string_from_file_on_equals:
98 * @buf: the input string
99 * @key: newly allocated string that is before the '='
100 * @val: newly allocated string after the '='
102 * Designed for file line processing, so it ignores strings beginning with special
103 * characters, such as '#'; returns false in such situations.
104 * Also returns false if no equals character is found
106 * e.g. if buf = "GPS.parameter=42"
107 * key = "GPS.parameter"
110 gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
112 // comments, special characters in viking file format
113 if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
116 if ( ! strchr ( buf, '=' ) )
119 gchar **strv = g_strsplit ( buf, "=", 2 );
122 gchar *str = strv[gi];
125 *key = g_strdup ( str );
127 *val = g_strdup ( str );
134 // Remove newline from val and also any other whitespace
135 *key = g_strstrip ( *key );
136 *val = g_strstrip ( *val );
140 static GSList* deletion_list = NULL;
143 * util_add_to_deletion_list:
145 * Add a name of a file into the list that is to be deleted on program exit
146 * Normally this is for files that get used asynchronously,
147 * so we don't know when it's time to delete them - other than at this program's end
149 void util_add_to_deletion_list ( const gchar* filename )
151 deletion_list = g_slist_append ( deletion_list, g_strdup (filename) );
155 * util_remove_all_in_deletion_list:
157 * Delete all the files in the deletion list
158 * This should only be called on program exit
160 void util_remove_all_in_deletion_list ( void )
162 while ( deletion_list )
164 g_remove ( deletion_list->data );
165 g_free ( deletion_list->data );
166 deletion_list = g_slist_delete_link ( deletion_list, deletion_list );
171 * Removes characters from a string, in place.
173 * @param string String to search.
174 * @param chars Characters to remove.
176 * @return @a string - return value is only useful when nesting function calls, e.g.:
177 * @code str = utils_str_remove_chars(g_strdup("f_o_o"), "_"); @endcode
179 * @see @c g_strdelimit.
181 gchar *util_str_remove_chars(gchar *string, const gchar *chars)
186 g_return_val_if_fail(string, NULL);
187 if (G_UNLIKELY(EMPTY(chars)))
190 foreach_str(r, string)
192 if (!strchr(chars, *r))
200 * In 'extreme' debug mode don't remove temporary files
201 * thus the contents can be inspected if things go wrong
202 * with the trade off the user may need to delete tmp files manually
203 * Only use this for 'occasional' downloaded temporary files that need interpretation,
204 * rather than large volume items such as Bing attributions.
206 int util_remove ( const gchar *filename )
208 if ( vik_debug && vik_verbose ) {
209 g_warning ( "Not removing file: %s", filename );
213 return g_remove ( filename );