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>
36 #include "fileutils.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);
66 * split_string_from_file_on_equals:
68 * @buf: the input string
69 * @key: newly allocated string that is before the '='
70 * @val: newly allocated string after the '='
72 * Designed for file line processing, so it ignores strings beginning with special
73 * characters, such as '#'; returns false in such situations.
74 * Also returns false if no equals character is found
76 * e.g. if buf = "GPS.parameter=42"
77 * key = "GPS.parameter"
80 gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
82 // comments, special characters in viking file format
83 if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
86 if ( ! strchr ( buf, '=' ) )
89 gchar **strv = g_strsplit ( buf, "=", 2 );
92 gchar *str = strv[gi];
95 *key = g_strdup ( str );
97 *val = g_strdup ( str );
104 // Remove newline from val and also any other whitespace
105 *key = g_strstrip ( *key );
106 *val = g_strstrip ( *val );
110 static GSList* deletion_list = NULL;
113 * util_add_to_deletion_list:
115 * Add a name of a file into the list that is to be deleted on program exit
116 * Normally this is for files that get used asynchronously,
117 * so we don't know when it's time to delete them - other than at this program's end
119 void util_add_to_deletion_list ( const gchar* filename )
121 deletion_list = g_slist_append ( deletion_list, g_strdup (filename) );
125 * util_remove_all_in_deletion_list:
127 * Delete all the files in the deletion list
128 * This should only be called on program exit
130 void util_remove_all_in_deletion_list ( void )
132 while ( deletion_list )
134 if ( g_remove ( (const char*)deletion_list->data ) )
135 g_warning ( "%s: Failed to remove %s", __FUNCTION__, (char*)deletion_list->data );
136 g_free ( deletion_list->data );
137 deletion_list = g_slist_delete_link ( deletion_list, deletion_list );
142 * Removes characters from a string, in place.
144 * @param string String to search.
145 * @param chars Characters to remove.
147 * @return @a string - return value is only useful when nesting function calls, e.g.:
148 * @code str = utils_str_remove_chars(g_strdup("f_o_o"), "_"); @endcode
150 * @see @c g_strdelimit.
152 gchar *util_str_remove_chars(gchar *string, const gchar *chars)
157 g_return_val_if_fail(string, NULL);
158 if (G_UNLIKELY(EMPTY(chars)))
161 foreach_str(r, string)
163 if (!strchr(chars, *r))
171 * In 'extreme' debug mode don't remove temporary files
172 * thus the contents can be inspected if things go wrong
173 * with the trade off the user may need to delete tmp files manually
174 * Only use this for 'occasional' downloaded temporary files that need interpretation,
175 * rather than large volume items such as Bing attributions.
177 int util_remove ( const gchar *filename )
179 if ( vik_debug && vik_verbose ) {
180 g_warning ( "Not removing file: %s", filename );
184 return g_remove ( filename );
188 * Stream write buffer to a temporary file (in one go)
190 * @param buffer The buffer to write
191 * @param count Size of the buffer to write
193 * @return the filename of the buffer that was written
195 gchar* util_write_tmp_file_from_bytes ( const void *buffer, gsize count )
198 GError *error = NULL;
199 gchar *tmpname = NULL;
201 #if GLIB_CHECK_VERSION(2,32,0)
202 GFile *gf = g_file_new_tmp ( "vik-tmp.XXXXXX", &gios, &error );
203 tmpname = g_file_get_path (gf);
205 gint fd = g_file_open_tmp ( "vik-tmp.XXXXXX", &tmpname, &error );
207 g_warning ( "%s", error->message );
208 g_error_free ( error );
211 gios = g_file_open_readwrite ( g_file_new_for_path (tmpname), NULL, &error );
213 g_warning ( "%s", error->message );
214 g_error_free ( error );
219 gios = g_file_open_readwrite ( g_file_new_for_path (tmpname), NULL, &error );
221 g_warning ( "%s", error->message );
222 g_error_free ( error );
226 GOutputStream *gos = g_io_stream_get_output_stream ( G_IO_STREAM(gios) );
227 if ( g_output_stream_write ( gos, buffer, count, NULL, &error ) < 0 ) {
228 g_critical ( "Couldn't write tmp %s file due to %s", tmpname, error->message );
233 g_output_stream_close ( gos, NULL, &error );
234 g_object_unref ( gios );
240 * util_make_absolute_filename:
242 * Returns a newly allocated string of the absolute filename or
243 * NULL if name is already absolute (or dirpath is unusable)
245 gchar* util_make_absolute_filename ( const gchar *filename, const gchar *dirpath )
247 if ( !dirpath ) return NULL;
249 // Is it ready absolute?
250 if ( g_path_is_absolute ( filename ) ) {
254 // Otherwise create the absolute filename from the given directory and filename
255 gchar *full = g_strconcat ( dirpath, G_DIR_SEPARATOR_S, filename, NULL );
256 gchar *absolute = file_realpath_dup ( full ); // resolved into the canonical name
263 * util_make_absolute_filenames:
265 * Ensures the supplied list of filenames are all absolute
267 void util_make_absolute_filenames ( GList *filenames, const gchar *dirpath )
269 if ( !filenames ) return;
270 if ( !dirpath ) return;
273 foreach_list ( gl, filenames ) {
274 gchar *fn = util_make_absolute_filename ( gl->data, dirpath );