Fix memory leak when freeing RSSyl's FeedItem struct.
[claws.git] / src / plugins / rssyl / strutils.c
1 /*
2  * Claws-Mail-- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2005 Andrej Kacian <andrej@kacian.sk>
4  *
5  * - a strreplace function (something like sed's s/foo/bar/g)
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 /* Global includes */
27 #include <glib.h>
28 #include <stdlib.h>
29 #include <ctype.h>
30
31 /* Claws Mail includes */
32 #include <common/utils.h>
33
34 /* Local includes */
35 /* (shouldn't be any) */
36
37 gchar *rssyl_strreplace(gchar *source, gchar *pattern,
38                 gchar *replacement)
39 {
40         gchar *new, *w_new = NULL, *c;
41         guint count = 0, final_length;
42         size_t len_pattern, len_replacement;
43
44         /*
45         debug_print("RSSyl: ======= strreplace: '%s': '%s'->'%s'\n", source, pattern,
46                         replacement);
47         */
48
49         if( source == NULL || pattern == NULL ) {
50                 debug_print("RSSyl: source or pattern is NULL!!!\n");
51                 return source;
52         }
53
54         if( !g_utf8_validate(source, -1, NULL) ) {
55                 debug_print("RSSyl: source is not an UTF-8 encoded text\n");
56                 return source;
57         }
58
59         if( !g_utf8_validate(pattern, -1, NULL) ) {
60                 debug_print("RSSyl: pattern is not an UTF-8 encoded text\n");
61                 return source;
62         }
63
64         len_pattern = strlen(pattern);
65         len_replacement = strlen(replacement);
66
67         c = source;
68         while( ( c = g_strstr_len(c, strlen(c), pattern) ) ) {
69                 count++;
70                 c += len_pattern;
71         }
72
73         /*
74         debug_print("RSSyl: ==== count = %d\n", count);
75         */
76
77         final_length = strlen(source)
78                 - ( count * len_pattern )
79                 + ( count * len_replacement );
80
81         new = malloc(final_length + 1);
82         w_new = new;
83         memset(new, '\0', final_length + 1);
84
85         c = source;
86
87         while( *c != '\0' ) {
88                 if( !memcmp(c, pattern, len_pattern) ) {
89                         gboolean break_after_rep = FALSE;
90                         int i;
91                         if (*(c + len_pattern) == '\0')
92                                 break_after_rep = TRUE;
93                         for (i = 0; i < len_replacement; i++) {
94                                 *w_new = replacement[i];
95                                 w_new++;
96                         }
97                         if (break_after_rep)
98                                 break;
99                         c = c + len_pattern;
100                 } else {
101                         *w_new = *c;
102                         w_new++;
103                         c++;
104                 }
105         }
106         return new;
107 }
108
109 typedef struct _RSSyl_HTMLSymbol RSSyl_HTMLSymbol;
110 struct _RSSyl_HTMLSymbol
111 {
112         gchar *const key;
113         gchar *const val;
114 };
115
116 /* TODO: find a way to offload this to a library which knows all the
117  * defined named entities (over 200). */
118 static RSSyl_HTMLSymbol symbol_list[] = {
119         { "lt", "<" },
120         { "gt", ">" },
121         { "amp", "&" },
122         { "apos", "'" },
123         { "quot", "\"" },
124         { "lsquo",  "‘" },
125         { "rsquo",  "’" },
126         { "ldquo",  "“" },
127         { "rdquo",  "”" },
128         { "nbsp", " " },
129         { "trade", "™" },
130         { "copy", "©" },
131         { "reg", "®" },
132         { "hellip", "…" },
133         { "mdash", "—" },
134         { "euro", "€" },
135         { NULL, NULL }
136 };
137
138 static RSSyl_HTMLSymbol tag_list[] = {
139         { "<cite>", "\"" },
140         { "</cite>", "\"" },
141         { "<i>", "" },
142         { "</i>", "" },
143         { "<em>", "" },
144         { "</em>", "" },
145         { "<b>", "" },
146         { "</b>", "" },
147         { "<nobr>", "" },
148         { "</nobr>", "" },
149         { "<wbr>", "" },
150         { NULL, NULL }
151 };
152
153 static gchar *rssyl_replace_chrefs(gchar *string)
154 {
155         char *new = g_malloc0(strlen(string) + 1), *ret;
156         char buf[16], tmp[6];
157         int i, ii, j, n, len;
158         gunichar c;
159         gboolean valid, replaced;
160
161         /* &xx; */
162         ii = 0;
163         for (i = 0; i < strlen(string); ++i) {
164                 if (string[i] == '&') {
165                         j = i+1;
166                         n = 0;
167                         valid = FALSE;
168                         while (string[j] != '\0' && n < 16) {
169                                 if (string[j] != ';') {
170                                         buf[n++] = string[j];
171                                 } else {
172                                         /* End of entity */
173                                         valid = TRUE;
174                                         buf[n] = '\0';
175                                         break;
176                                 }
177                                 j++;
178                         }
179                         if (strlen(buf) > 0 && valid) {
180                                 replaced = FALSE;
181
182                                 if (buf[0] == '#' && (c = atoi(buf+1)) > 0) {
183                                         len = g_unichar_to_utf8(c, tmp);
184                                         tmp[len] = '\0';
185                                         g_strlcat(new, tmp, strlen(string));
186                                         ii += len;
187                                         replaced = TRUE;
188                                 } else {
189                                         for (c = 0; symbol_list[c].key != NULL; c++) {
190                                                 if (!strcmp(buf, symbol_list[c].key)) {
191                                                         g_strlcat(new, symbol_list[c].val, strlen(string));
192                                                         ii += strlen(symbol_list[c].val);
193                                                         replaced = TRUE;
194                                                         break;
195                                                 }
196                                         }
197                                 }
198                                 if (!replaced) {
199                                         new[ii++] = '&'; /* & */
200                                         g_strlcat(new, buf, strlen(string));
201                                         ii += strlen(buf);
202                                         new[ii++] = ';';
203                                 }
204                                 i = j;
205                         } else {
206                                 new[ii++] = string[i];
207                         }
208                 } else {
209                         new[ii++] = string[i];
210                 }
211         }
212
213         ret = g_strdup(new);
214         g_free(new);
215         return ret;
216 }
217
218 gchar *rssyl_replace_html_stuff(gchar *text,
219                 gboolean symbols, gboolean tags)
220 {
221         gchar *tmp = NULL, *wtext = NULL;
222         gint i;
223
224         g_return_val_if_fail(text != NULL, NULL);
225
226         if( symbols ) {
227                 wtext = rssyl_replace_chrefs(text);
228         } else {
229                 wtext = g_strdup(text);
230         }
231
232         /* TODO: rewrite this part to work similarly to rssyl_replace_chrefs() */
233         if( tags ) {
234                 for( i = 0; tag_list[i].key != NULL; i++ ) {
235                         if( g_strstr_len(text, strlen(text), symbol_list[i].key) ) {
236                                 tmp = rssyl_strreplace(wtext, tag_list[i].key, tag_list[i].val);
237                                 g_free(wtext);
238                                 wtext = g_strdup(tmp);
239                                 g_free(tmp);
240                         }
241                 }
242         }
243
244         return wtext;
245 }
246
247 static gchar *rssyl_sanitize_string(gchar *str, gboolean strip_nl)
248 {
249         gchar *new = NULL, *c = str, *n = NULL;
250
251         if( str == NULL )
252                 return NULL;
253
254         n = new = malloc(strlen(str) + 1);
255         memset(new, '\0', strlen(str) + 1);
256
257         while( *c != '\0' ) {
258                 if( !isspace(*c) || *c == ' ' || (!strip_nl && *c == '\n') ) {
259                         *n = *c;
260                         n++;
261                 }
262                 c++;
263         }
264
265         return new;
266 }
267
268 /* rssyl_format_string()
269  * - return value needs to be freed
270  */
271 gchar *rssyl_format_string(gchar *str, gboolean replace_html,
272                 gboolean strip_nl)
273 {
274         gchar *res = NULL, *tmp = NULL;
275
276         g_return_val_if_fail(str != NULL, NULL);
277
278         if (replace_html)
279                 tmp = rssyl_replace_html_stuff(str, TRUE, TRUE);
280         else
281                 tmp = g_strdup(str);
282
283         res = rssyl_sanitize_string(tmp, strip_nl);
284         g_free(tmp);
285
286         g_strstrip(res);
287
288         return res;
289 }
290
291 /* this functions splits a string into an array of string, by 
292  * returning an array of pointers to positions of the delimiter
293  * in the original string and replacing this delimiter with a
294  * NULL. It does not duplicate memory, hence you should only
295  * free the array and not its elements, and you should not
296  * free the original string before you're done with the array.
297  * maybe could be part of the core (utils.c).
298  */
299 gchar **strsplit_no_copy(gchar *str, char delimiter)
300 {
301         gchar **array = g_new(gchar *, 1);
302         int i = 0;
303         gchar *cur = str, *next;
304         
305         array[i] = cur;
306         i++;
307         while ((next = strchr(cur, delimiter)) != NULL) {
308                 *(next) = '\0';
309                 array = g_realloc(array, (sizeof(gchar *)) * (i + 1));
310                 array[i] = next + 1;
311                 cur = next + 1;
312                 i++;
313         }
314         array = g_realloc(array, (sizeof(gchar *)) * (i + 1));
315         array[i] = NULL;
316         return array;
317 }
318
319 /* This is a very dumb function - it just strips <, > and everything between
320  * them. */
321 void strip_html(gchar *str)
322 {
323         gchar *p = str;
324         gboolean intag = FALSE;
325
326         while (*p) {
327                 if (*p == '<')
328                         intag = TRUE;
329                 else if (*p == '>')
330                         intag = FALSE;
331
332                 if (*p == '<' || *p == '>' || intag)
333                         memmove(p, p + 1, strlen(p));
334                 else
335                         p++;
336         }
337 }
338
339 gchar *my_normalize_url(const gchar *url)
340 {
341         gchar *myurl = NULL;
342
343         if (!strncmp(url, "feed://", 7))
344                 myurl = g_strdup(url+7);
345         else if (!strncmp(url, "feed:", 5))
346                 myurl = g_strdup(url+5);
347         else
348                 myurl = g_strdup(url);
349
350         return myurl;
351 }