]> git.street.me.uk Git - andy/viking.git/blame - src/util.c
Add capability to save layer defaults from outside the layer defaults code.
[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"
7d02a0b0 35
ba4a5e11
GB
36gchar *uri_escape(gchar *str)
37{
38 gchar *esc_str = g_malloc(3*strlen(str));
39 gchar *dst = esc_str;
40 gchar *src;
41
42 for (src = str; *src; src++) {
43 if (*src == ' ')
44 *dst++ = '+';
45 else if (g_ascii_isalnum(*src))
46 *dst++ = *src;
47 else {
7705a76f 48 g_sprintf(dst, "%%%02hhX", *src);
ba4a5e11
GB
49 dst += 3;
50 }
51 }
52 *dst = '\0';
53
54 return(esc_str);
55}
56
70434be3
GB
57
58GList * str_array_to_glist(gchar* data[])
59{
60 GList *gl = NULL;
61 gpointer * p;
62 for (p = (gpointer)data; *p; p++)
63 gl = g_list_prepend(gl, *p);
64 return g_list_reverse(gl);
65}
9106934d 66
0da89d90
RN
67/**
68 * split_string_from_file_on_equals:
69 *
70 * @buf: the input string
71 * @key: newly allocated string that is before the '='
72 * @val: newly allocated string after the '='
73 *
74 * Designed for file line processing, so it ignores strings beginning with special
75 * characters, such as '#'; returns false in such situations.
76 * Also returns false if no equals character is found
77 *
78 * e.g. if buf = "GPS.parameter=42"
79 * key = "GPS.parameter"
80 * val = "42"
81 */
82gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
9106934d 83{
9106934d
RN
84 // comments, special characters in viking file format
85 if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
86 return FALSE;
0da89d90
RN
87
88 if ( ! strchr ( buf, '=' ) )
9106934d 89 return FALSE;
0da89d90
RN
90
91 gchar **strv = g_strsplit ( buf, "=", 2 );
92
93 gint gi = 0;
94 gchar *str = strv[gi];
95 while ( str ) {
96 if ( gi == 0 )
97 *key = g_strdup ( str );
98 else
99 *val = g_strdup ( str );
100 gi++;
101 str = strv[gi];
102 }
103
104 g_strfreev ( strv );
105
106 // Remove newline from val and also any other whitespace
107 *key = g_strstrip ( *key );
108 *val = g_strstrip ( *val );
109
9106934d
RN
110 return TRUE;
111}
44871dd1
RN
112
113static GSList* deletion_list = NULL;
114
115/**
116 * util_add_to_deletion_list:
117 *
118 * Add a name of a file into the list that is to be deleted on program exit
119 * Normally this is for files that get used asynchronously,
120 * so we don't know when it's time to delete them - other than at this program's end
121 */
122void util_add_to_deletion_list ( const gchar* filename )
123{
124 deletion_list = g_slist_append ( deletion_list, g_strdup (filename) );
125}
126
127/**
128 * util_remove_all_in_deletion_list:
129 *
130 * Delete all the files in the deletion list
131 * This should only be called on program exit
132 */
133void util_remove_all_in_deletion_list ( void )
134{
135 while ( deletion_list )
136 {
137 g_remove ( deletion_list->data );
138 g_free ( deletion_list->data );
139 deletion_list = g_slist_delete_link ( deletion_list, deletion_list );
140 }
141}
6a280b85
RN
142
143/**
144 * Removes characters from a string, in place.
145 *
146 * @param string String to search.
147 * @param chars Characters to remove.
148 *
149 * @return @a string - return value is only useful when nesting function calls, e.g.:
150 * @code str = utils_str_remove_chars(g_strdup("f_o_o"), "_"); @endcode
151 *
152 * @see @c g_strdelimit.
153 **/
154gchar *util_str_remove_chars(gchar *string, const gchar *chars)
155{
156 const gchar *r;
157 gchar *w = string;
158
159 g_return_val_if_fail(string, NULL);
160 if (G_UNLIKELY(EMPTY(chars)))
161 return string;
162
163 foreach_str(r, string)
164 {
165 if (!strchr(chars, *r))
166 *w++ = *r;
167 }
168 *w = 0x0;
169 return string;
170}