]> git.street.me.uk Git - andy/viking.git/blame - src/util.c
CID#34561+CID#34569: Check return values
[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 5 * Based on:
a482007a 6 * Copyright (C) 2003-2007, Leandro A. F. Pereira <leandro@linuxmag.com.br>
7d02a0b0
GB
7 *
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.
11 *
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.
16 *
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
20 */
5ab2942c 21 /*
6a280b85
RN
22 * Dependencies must be just on Glib
23 * see ui_utils for thing that depend on Gtk
5ab2942c
RN
24 * see vikutils for things that further depend on other Viking types
25 */
4c77d5e0
GB
26#ifdef HAVE_CONFIG_H
27#include "config.h"
28#endif
29
91c46f90 30#include <glib/gstdio.h>
4c77d5e0 31#include <glib/gi18n.h>
c612e922 32#include <glib/gprintf.h>
7d02a0b0 33
8e562a70 34#include "util.h"
81bf47ee 35#include "globals.h"
7d02a0b0 36
6b6225cb
RN
37#ifdef WINDOWS
38#include <windows.h>
39#else
40#include <unistd.h>
41#endif
42
43guint util_get_number_of_cpus ()
44{
45#if GLIB_CHECK_VERSION (2, 36, 0)
46 return g_get_num_processors();
47#else
48 long nprocs = 1;
49#ifdef WINDOWS
50 SYSTEM_INFO info;
51 GetSystemInfo(&info);
52 nprocs = info.dwNumberOfProcessors;
53#else
54#ifdef _SC_NPROCESSORS_ONLN
55 nprocs = sysconf(_SC_NPROCESSORS_ONLN);
56 if (nprocs < 1)
57 nprocs = 1;
58#endif
59#endif
60 return nprocs;
61#endif
62}
63
ba4a5e11
GB
64gchar *uri_escape(gchar *str)
65{
66 gchar *esc_str = g_malloc(3*strlen(str));
67 gchar *dst = esc_str;
68 gchar *src;
69
70 for (src = str; *src; src++) {
71 if (*src == ' ')
72 *dst++ = '+';
73 else if (g_ascii_isalnum(*src))
74 *dst++ = *src;
75 else {
7705a76f 76 g_sprintf(dst, "%%%02hhX", *src);
ba4a5e11
GB
77 dst += 3;
78 }
79 }
80 *dst = '\0';
81
82 return(esc_str);
83}
84
70434be3
GB
85
86GList * str_array_to_glist(gchar* data[])
87{
88 GList *gl = NULL;
89 gpointer * p;
90 for (p = (gpointer)data; *p; p++)
91 gl = g_list_prepend(gl, *p);
92 return g_list_reverse(gl);
93}
9106934d 94
0da89d90
RN
95/**
96 * split_string_from_file_on_equals:
97 *
98 * @buf: the input string
99 * @key: newly allocated string that is before the '='
100 * @val: newly allocated string after the '='
101 *
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
105 *
106 * e.g. if buf = "GPS.parameter=42"
107 * key = "GPS.parameter"
108 * val = "42"
109 */
110gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
9106934d 111{
9106934d
RN
112 // comments, special characters in viking file format
113 if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
114 return FALSE;
0da89d90
RN
115
116 if ( ! strchr ( buf, '=' ) )
9106934d 117 return FALSE;
0da89d90
RN
118
119 gchar **strv = g_strsplit ( buf, "=", 2 );
120
121 gint gi = 0;
122 gchar *str = strv[gi];
123 while ( str ) {
124 if ( gi == 0 )
125 *key = g_strdup ( str );
126 else
127 *val = g_strdup ( str );
128 gi++;
129 str = strv[gi];
130 }
131
132 g_strfreev ( strv );
133
134 // Remove newline from val and also any other whitespace
135 *key = g_strstrip ( *key );
136 *val = g_strstrip ( *val );
9106934d
RN
137 return TRUE;
138}
44871dd1
RN
139
140static GSList* deletion_list = NULL;
141
142/**
143 * util_add_to_deletion_list:
144 *
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
148 */
149void util_add_to_deletion_list ( const gchar* filename )
150{
151 deletion_list = g_slist_append ( deletion_list, g_strdup (filename) );
152}
153
154/**
155 * util_remove_all_in_deletion_list:
156 *
157 * Delete all the files in the deletion list
158 * This should only be called on program exit
159 */
160void util_remove_all_in_deletion_list ( void )
161{
162 while ( deletion_list )
163 {
fd437981
RN
164 if ( g_remove ( (const char*)deletion_list->data ) )
165 g_warning ( "%s: Failed to remove %s", __FUNCTION__, (char*)deletion_list->data );
44871dd1
RN
166 g_free ( deletion_list->data );
167 deletion_list = g_slist_delete_link ( deletion_list, deletion_list );
168 }
169}
6a280b85
RN
170
171/**
172 * Removes characters from a string, in place.
173 *
174 * @param string String to search.
175 * @param chars Characters to remove.
176 *
177 * @return @a string - return value is only useful when nesting function calls, e.g.:
178 * @code str = utils_str_remove_chars(g_strdup("f_o_o"), "_"); @endcode
179 *
180 * @see @c g_strdelimit.
181 **/
182gchar *util_str_remove_chars(gchar *string, const gchar *chars)
183{
184 const gchar *r;
185 gchar *w = string;
186
187 g_return_val_if_fail(string, NULL);
188 if (G_UNLIKELY(EMPTY(chars)))
189 return string;
190
191 foreach_str(r, string)
192 {
193 if (!strchr(chars, *r))
194 *w++ = *r;
195 }
196 *w = 0x0;
197 return string;
198}
81bf47ee
RN
199
200/**
201 * In 'extreme' debug mode don't remove temporary files
202 * thus the contents can be inspected if things go wrong
203 * with the trade off the user may need to delete tmp files manually
204 * Only use this for 'occasional' downloaded temporary files that need interpretation,
205 * rather than large volume items such as Bing attributions.
206 */
207int util_remove ( const gchar *filename )
208{
209 if ( vik_debug && vik_verbose ) {
210 g_warning ( "Not removing file: %s", filename );
211 return 0;
212 }
213 else
214 return g_remove ( filename );
215}