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>
44 guint util_get_number_of_cpus ()
46 #if GLIB_CHECK_VERSION (2, 36, 0)
47 return g_get_num_processors();
53 nprocs = info.dwNumberOfProcessors;
55 #ifdef _SC_NPROCESSORS_ONLN
56 nprocs = sysconf(_SC_NPROCESSORS_ONLN);
65 gchar *uri_escape(gchar *str)
67 gchar *esc_str = g_malloc(3*strlen(str));
71 for (src = str; *src; src++) {
74 else if (g_ascii_isalnum(*src))
77 g_sprintf(dst, "%%%02hhX", *src);
87 GList * str_array_to_glist(gchar* data[])
91 for (p = (gpointer)data; *p; p++)
92 gl = g_list_prepend(gl, *p);
93 return g_list_reverse(gl);
97 * split_string_from_file_on_equals:
99 * @buf: the input string
100 * @key: newly allocated string that is before the '='
101 * @val: newly allocated string after the '='
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
107 * e.g. if buf = "GPS.parameter=42"
108 * key = "GPS.parameter"
111 gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
113 // comments, special characters in viking file format
114 if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
117 if ( ! strchr ( buf, '=' ) )
120 gchar **strv = g_strsplit ( buf, "=", 2 );
123 gchar *str = strv[gi];
126 *key = g_strdup ( str );
128 *val = g_strdup ( str );
135 // Remove newline from val and also any other whitespace
136 *key = g_strstrip ( *key );
137 *val = g_strstrip ( *val );
141 static GSList* deletion_list = NULL;
144 * util_add_to_deletion_list:
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
150 void util_add_to_deletion_list ( const gchar* filename )
152 deletion_list = g_slist_append ( deletion_list, g_strdup (filename) );
156 * util_remove_all_in_deletion_list:
158 * Delete all the files in the deletion list
159 * This should only be called on program exit
161 void util_remove_all_in_deletion_list ( void )
163 while ( deletion_list )
165 if ( g_remove ( (const char*)deletion_list->data ) )
166 g_warning ( "%s: Failed to remove %s", __FUNCTION__, (char*)deletion_list->data );
167 g_free ( deletion_list->data );
168 deletion_list = g_slist_delete_link ( deletion_list, deletion_list );
173 * Removes characters from a string, in place.
175 * @param string String to search.
176 * @param chars Characters to remove.
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
181 * @see @c g_strdelimit.
183 gchar *util_str_remove_chars(gchar *string, const gchar *chars)
188 g_return_val_if_fail(string, NULL);
189 if (G_UNLIKELY(EMPTY(chars)))
192 foreach_str(r, string)
194 if (!strchr(chars, *r))
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.
208 int util_remove ( const gchar *filename )
210 if ( vik_debug && vik_verbose ) {
211 g_warning ( "Not removing file: %s", filename );
215 return g_remove ( filename );
219 * Stream write buffer to a temporary file (in one go)
221 * @param buffer The buffer to write
222 * @param count Size of the buffer to write
224 * @return the filename of the buffer that was written
226 gchar* util_write_tmp_file_from_bytes ( const void *buffer, gsize count )
229 GError *error = NULL;
230 gchar *tmpname = NULL;
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);
236 gint fd = g_file_open_tmp ( "vik-tmp.XXXXXX", &tmpname, &error );
238 g_warning ( "%s", error->message );
239 g_error_free ( error );
242 gios = g_file_open_readwrite ( g_file_new_for_path (tmpname), NULL, &error );
244 g_warning ( "%s", error->message );
245 g_error_free ( error );
250 gios = g_file_open_readwrite ( g_file_new_for_path (tmpname), NULL, &error );
252 g_warning ( "%s", error->message );
253 g_error_free ( error );
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 );
264 g_output_stream_close ( gos, NULL, &error );
265 g_object_unref ( gios );