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