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 * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Dependencies must be just on Glib
22 * see ui_utils for thing that depend on Gtk
23 * see vikutils for things that further depend on other Viking types
29 #include <glib/gstdio.h>
30 #include <glib/gi18n.h>
31 #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);
65 * split_string_from_file_on_equals:
67 * @buf: the input string
68 * @key: newly allocated string that is before the '='
69 * @val: newly allocated string after the '='
71 * Designed for file line processing, so it ignores strings beginning with special
72 * characters, such as '#'; returns false in such situations.
73 * Also returns false if no equals character is found
75 * e.g. if buf = "GPS.parameter=42"
76 * key = "GPS.parameter"
79 gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
81 // comments, special characters in viking file format
82 if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
85 if ( ! strchr ( buf, '=' ) )
88 gchar **strv = g_strsplit ( buf, "=", 2 );
91 gchar *str = strv[gi];
94 *key = g_strdup ( str );
96 *val = g_strdup ( str );
103 // Remove newline from val and also any other whitespace
104 *key = g_strstrip ( *key );
105 *val = g_strstrip ( *val );
109 static GSList* deletion_list = NULL;
112 * util_add_to_deletion_list:
114 * Add a name of a file into the list that is to be deleted on program exit
115 * Normally this is for files that get used asynchronously,
116 * so we don't know when it's time to delete them - other than at this program's end
118 void util_add_to_deletion_list ( const gchar* filename )
120 deletion_list = g_slist_append ( deletion_list, g_strdup (filename) );
124 * util_remove_all_in_deletion_list:
126 * Delete all the files in the deletion list
127 * This should only be called on program exit
129 void util_remove_all_in_deletion_list ( void )
131 while ( deletion_list )
133 if ( g_remove ( (const char*)deletion_list->data ) )
134 g_warning ( "%s: Failed to remove %s", __FUNCTION__, (char*)deletion_list->data );
135 g_free ( deletion_list->data );
136 deletion_list = g_slist_delete_link ( deletion_list, deletion_list );
141 * Removes characters from a string, in place.
143 * @param string String to search.
144 * @param chars Characters to remove.
146 * @return @a string - return value is only useful when nesting function calls, e.g.:
147 * @code str = utils_str_remove_chars(g_strdup("f_o_o"), "_"); @endcode
149 * @see @c g_strdelimit.
151 gchar *util_str_remove_chars(gchar *string, const gchar *chars)
156 g_return_val_if_fail(string, NULL);
157 if (G_UNLIKELY(EMPTY(chars)))
160 foreach_str(r, string)
162 if (!strchr(chars, *r))
170 * In 'extreme' debug mode don't remove temporary files
171 * thus the contents can be inspected if things go wrong
172 * with the trade off the user may need to delete tmp files manually
173 * Only use this for 'occasional' downloaded temporary files that need interpretation,
174 * rather than large volume items such as Bing attributions.
176 int util_remove ( const gchar *filename )
178 if ( vik_debug && vik_verbose ) {
179 g_warning ( "Not removing file: %s", filename );
183 return g_remove ( filename );
187 * Stream write buffer to a temporary file (in one go)
189 * @param buffer The buffer to write
190 * @param count Size of the buffer to write
192 * @return the filename of the buffer that was written
194 gchar* util_write_tmp_file_from_bytes ( const void *buffer, gsize count )
197 GError *error = NULL;
198 gchar *tmpname = NULL;
200 #if GLIB_CHECK_VERSION(2,32,0)
201 GFile *gf = g_file_new_tmp ( "vik-tmp.XXXXXX", &gios, &error );
202 tmpname = g_file_get_path (gf);
204 gint fd = g_file_open_tmp ( "vik-tmp.XXXXXX", &tmpname, &error );
206 g_warning ( "%s", error->message );
207 g_error_free ( error );
210 gios = g_file_open_readwrite ( g_file_new_for_path (tmpname), NULL, &error );
212 g_warning ( "%s", error->message );
213 g_error_free ( error );
218 gios = g_file_open_readwrite ( g_file_new_for_path (tmpname), NULL, &error );
220 g_warning ( "%s", error->message );
221 g_error_free ( error );
225 GOutputStream *gos = g_io_stream_get_output_stream ( G_IO_STREAM(gios) );
226 if ( g_output_stream_write ( gos, buffer, count, NULL, &error ) < 0 ) {
227 g_critical ( "Couldn't write tmp %s file due to %s", tmpname, error->message );
232 g_output_stream_close ( gos, NULL, &error );
233 g_object_unref ( gios );