]> git.street.me.uk Git - andy/viking.git/blame - src/util.c
Remove definition of a non existant function
[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
GB
5 *
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
60efaeb1
RN
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
7d02a0b0
GB
10 *
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.
15 *
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
19 */
5ab2942c 20 /*
6a280b85
RN
21 * Dependencies must be just on Glib
22 * see ui_utils for thing that depend on Gtk
5ab2942c
RN
23 * see vikutils for things that further depend on other Viking types
24 */
4c77d5e0
GB
25#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
28
91c46f90 29#include <glib/gstdio.h>
4c77d5e0 30#include <glib/gi18n.h>
c612e922 31#include <glib/gprintf.h>
73def8cb 32#include <gio/gio.h>
7d02a0b0 33
8e562a70 34#include "util.h"
81bf47ee 35#include "globals.h"
872840c0 36#include "fileutils.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
0da89d90
RN
65/**
66 * split_string_from_file_on_equals:
67 *
68 * @buf: the input string
69 * @key: newly allocated string that is before the '='
70 * @val: newly allocated string after the '='
71 *
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
75 *
76 * e.g. if buf = "GPS.parameter=42"
77 * key = "GPS.parameter"
78 * val = "42"
79 */
80gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
9106934d 81{
9106934d
RN
82 // comments, special characters in viking file format
83 if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
84 return FALSE;
0da89d90
RN
85
86 if ( ! strchr ( buf, '=' ) )
9106934d 87 return FALSE;
0da89d90
RN
88
89 gchar **strv = g_strsplit ( buf, "=", 2 );
90
91 gint gi = 0;
92 gchar *str = strv[gi];
93 while ( str ) {
94 if ( gi == 0 )
95 *key = g_strdup ( str );
96 else
97 *val = g_strdup ( str );
98 gi++;
99 str = strv[gi];
100 }
101
102 g_strfreev ( strv );
103
104 // Remove newline from val and also any other whitespace
105 *key = g_strstrip ( *key );
106 *val = g_strstrip ( *val );
9106934d
RN
107 return TRUE;
108}
44871dd1
RN
109
110static GSList* deletion_list = NULL;
111
112/**
113 * util_add_to_deletion_list:
114 *
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
118 */
119void util_add_to_deletion_list ( const gchar* filename )
120{
121 deletion_list = g_slist_append ( deletion_list, g_strdup (filename) );
122}
123
124/**
125 * util_remove_all_in_deletion_list:
126 *
127 * Delete all the files in the deletion list
128 * This should only be called on program exit
129 */
130void util_remove_all_in_deletion_list ( void )
131{
132 while ( deletion_list )
133 {
fd437981
RN
134 if ( g_remove ( (const char*)deletion_list->data ) )
135 g_warning ( "%s: Failed to remove %s", __FUNCTION__, (char*)deletion_list->data );
44871dd1
RN
136 g_free ( deletion_list->data );
137 deletion_list = g_slist_delete_link ( deletion_list, deletion_list );
138 }
139}
6a280b85
RN
140
141/**
142 * Removes characters from a string, in place.
143 *
144 * @param string String to search.
145 * @param chars Characters to remove.
146 *
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
149 *
150 * @see @c g_strdelimit.
151 **/
152gchar *util_str_remove_chars(gchar *string, const gchar *chars)
153{
154 const gchar *r;
155 gchar *w = string;
156
157 g_return_val_if_fail(string, NULL);
158 if (G_UNLIKELY(EMPTY(chars)))
159 return string;
160
161 foreach_str(r, string)
162 {
163 if (!strchr(chars, *r))
164 *w++ = *r;
165 }
166 *w = 0x0;
167 return string;
168}
81bf47ee
RN
169
170/**
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.
176 */
177int util_remove ( const gchar *filename )
178{
179 if ( vik_debug && vik_verbose ) {
180 g_warning ( "Not removing file: %s", filename );
181 return 0;
182 }
183 else
184 return g_remove ( filename );
185}
73def8cb
RN
186
187/**
188 * Stream write buffer to a temporary file (in one go)
189 *
190 * @param buffer The buffer to write
191 * @param count Size of the buffer to write
192 *
193 * @return the filename of the buffer that was written
194 */
195gchar* util_write_tmp_file_from_bytes ( const void *buffer, gsize count )
196{
197 GFileIOStream *gios;
198 GError *error = NULL;
199 gchar *tmpname = NULL;
200
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);
204#else
205 gint fd = g_file_open_tmp ( "vik-tmp.XXXXXX", &tmpname, &error );
206 if ( error ) {
207 g_warning ( "%s", error->message );
208 g_error_free ( error );
209 return NULL;
210 }
211 gios = g_file_open_readwrite ( g_file_new_for_path (tmpname), NULL, &error );
212 if ( error ) {
213 g_warning ( "%s", error->message );
214 g_error_free ( error );
215 return NULL;
216 }
217#endif
218
219 gios = g_file_open_readwrite ( g_file_new_for_path (tmpname), NULL, &error );
220 if ( error ) {
221 g_warning ( "%s", error->message );
222 g_error_free ( error );
223 return NULL;
224 }
225
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 );
229 g_free (tmpname);
230 tmpname = NULL;
231 }
232
233 g_output_stream_close ( gos, NULL, &error );
234 g_object_unref ( gios );
235
236 return tmpname;
237}
872840c0
RN
238
239/**
240 * util_make_absolute_filename:
241 *
242 * Returns a newly allocated string of the absolute filename or
243 * NULL if name is already absolute (or dirpath is unusable)
244 */
245gchar* util_make_absolute_filename ( const gchar *filename, const gchar *dirpath )
246{
247 if ( !dirpath ) return NULL;
248
249 // Is it ready absolute?
250 if ( g_path_is_absolute ( filename ) ) {
251 return NULL;
252 }
253 else {
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
257 g_free ( full );
258 return absolute;
259 }
260}
261
262/**
263 * util_make_absolute_filenames:
264 *
265 * Ensures the supplied list of filenames are all absolute
266 */
267void util_make_absolute_filenames ( GList *filenames, const gchar *dirpath )
268{
269 if ( !filenames ) return;
270 if ( !dirpath ) return;
271
272 GList *gl;
273 foreach_list ( gl, filenames ) {
274 gchar *fn = util_make_absolute_filename ( gl->data, dirpath );
275 if ( fn ) {
276 g_free ( gl->data );
277 gl->data = fn;
278 }
279 }
280}