]> git.street.me.uk Git - andy/viking.git/blob - src/util.c
Fix recent commit: 4dc72a1d407b81853d0093871cff45ef1f47d1b8
[andy/viking.git] / src / util.c
1 /*
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>
5  *    Based on:
6  *    Copyright (C) 2003-2007, Leandro A. F. Pereira <leandro@linuxmag.com.br>
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  */
21  /*
22   * Dependencies must be just on Glib
23   * see ui_utils for thing that depend on Gtk
24   * see vikutils for things that further depend on other Viking types
25   */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <glib/gstdio.h>
31 #include <glib/gi18n.h>
32 #include <glib/gprintf.h>
33 #include <gio/gio.h>
34
35 #include "util.h"
36 #include "globals.h"
37
38 #ifdef WINDOWS
39 #include <windows.h>
40 #else
41 #include <unistd.h>
42 #endif
43
44 guint 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
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  */
80 gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
81 {
82   // comments, special characters in viking file format
83   if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
84     return FALSE;
85
86   if ( ! strchr ( buf, '=' ) )
87     return FALSE;
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 );
107   return TRUE;
108 }
109
110 static 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  */
119 void 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  */
130 void util_remove_all_in_deletion_list ( void )
131 {
132         while ( deletion_list )
133         {
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 );
138         }
139 }
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  **/
152 gchar *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 }
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  */
177 int 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 }
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  */
195 gchar* 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 }