]> git.street.me.uk Git - andy/viking.git/blame - src/util.c
Make a newly loaded file layer more accessible in the layers panel
[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"
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
0da89d90
RN
64/**
65 * split_string_from_file_on_equals:
66 *
67 * @buf: the input string
68 * @key: newly allocated string that is before the '='
69 * @val: newly allocated string after the '='
70 *
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
74 *
75 * e.g. if buf = "GPS.parameter=42"
76 * key = "GPS.parameter"
77 * val = "42"
78 */
79gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
9106934d 80{
9106934d
RN
81 // comments, special characters in viking file format
82 if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
83 return FALSE;
0da89d90
RN
84
85 if ( ! strchr ( buf, '=' ) )
9106934d 86 return FALSE;
0da89d90
RN
87
88 gchar **strv = g_strsplit ( buf, "=", 2 );
89
90 gint gi = 0;
91 gchar *str = strv[gi];
92 while ( str ) {
93 if ( gi == 0 )
94 *key = g_strdup ( str );
95 else
96 *val = g_strdup ( str );
97 gi++;
98 str = strv[gi];
99 }
100
101 g_strfreev ( strv );
102
103 // Remove newline from val and also any other whitespace
104 *key = g_strstrip ( *key );
105 *val = g_strstrip ( *val );
9106934d
RN
106 return TRUE;
107}
44871dd1
RN
108
109static GSList* deletion_list = NULL;
110
111/**
112 * util_add_to_deletion_list:
113 *
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
117 */
118void util_add_to_deletion_list ( const gchar* filename )
119{
120 deletion_list = g_slist_append ( deletion_list, g_strdup (filename) );
121}
122
123/**
124 * util_remove_all_in_deletion_list:
125 *
126 * Delete all the files in the deletion list
127 * This should only be called on program exit
128 */
129void util_remove_all_in_deletion_list ( void )
130{
131 while ( deletion_list )
132 {
fd437981
RN
133 if ( g_remove ( (const char*)deletion_list->data ) )
134 g_warning ( "%s: Failed to remove %s", __FUNCTION__, (char*)deletion_list->data );
44871dd1
RN
135 g_free ( deletion_list->data );
136 deletion_list = g_slist_delete_link ( deletion_list, deletion_list );
137 }
138}
6a280b85
RN
139
140/**
141 * Removes characters from a string, in place.
142 *
143 * @param string String to search.
144 * @param chars Characters to remove.
145 *
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
148 *
149 * @see @c g_strdelimit.
150 **/
151gchar *util_str_remove_chars(gchar *string, const gchar *chars)
152{
153 const gchar *r;
154 gchar *w = string;
155
156 g_return_val_if_fail(string, NULL);
157 if (G_UNLIKELY(EMPTY(chars)))
158 return string;
159
160 foreach_str(r, string)
161 {
162 if (!strchr(chars, *r))
163 *w++ = *r;
164 }
165 *w = 0x0;
166 return string;
167}
81bf47ee
RN
168
169/**
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.
175 */
176int util_remove ( const gchar *filename )
177{
178 if ( vik_debug && vik_verbose ) {
179 g_warning ( "Not removing file: %s", filename );
180 return 0;
181 }
182 else
183 return g_remove ( filename );
184}
73def8cb
RN
185
186/**
187 * Stream write buffer to a temporary file (in one go)
188 *
189 * @param buffer The buffer to write
190 * @param count Size of the buffer to write
191 *
192 * @return the filename of the buffer that was written
193 */
194gchar* util_write_tmp_file_from_bytes ( const void *buffer, gsize count )
195{
196 GFileIOStream *gios;
197 GError *error = NULL;
198 gchar *tmpname = NULL;
199
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);
203#else
204 gint fd = g_file_open_tmp ( "vik-tmp.XXXXXX", &tmpname, &error );
205 if ( error ) {
206 g_warning ( "%s", error->message );
207 g_error_free ( error );
208 return NULL;
209 }
210 gios = g_file_open_readwrite ( g_file_new_for_path (tmpname), NULL, &error );
211 if ( error ) {
212 g_warning ( "%s", error->message );
213 g_error_free ( error );
214 return NULL;
215 }
216#endif
217
218 gios = g_file_open_readwrite ( g_file_new_for_path (tmpname), NULL, &error );
219 if ( error ) {
220 g_warning ( "%s", error->message );
221 g_error_free ( error );
222 return NULL;
223 }
224
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 );
228 g_free (tmpname);
229 tmpname = NULL;
230 }
231
232 g_output_stream_close ( gos, NULL, &error );
233 g_object_unref ( gios );
234
235 return tmpname;
236}