2 * Claws-Mail-- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2005 Andrej Kacian <andrej@kacian.sk>
5 * - a strreplace function (something like sed's s/foo/bar/g)
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.
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.
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.
31 /* Claws Mail includes */
32 #include <common/utils.h>
35 /* (shouldn't be any) */
37 gchar *rssyl_strreplace(gchar *source, gchar *pattern,
40 gchar *new, *w_new = NULL, *c;
41 guint count = 0, final_length;
42 size_t len_pattern, len_replacement;
45 debug_print("RSSyl: ======= strreplace: '%s': '%s'->'%s'\n", source, pattern,
49 if( source == NULL || pattern == NULL ) {
50 debug_print("RSSyl: source or pattern is NULL!!!\n");
54 if( !g_utf8_validate(source, -1, NULL) ) {
55 debug_print("RSSyl: source is not an UTF-8 encoded text\n");
59 if( !g_utf8_validate(pattern, -1, NULL) ) {
60 debug_print("RSSyl: pattern is not an UTF-8 encoded text\n");
64 len_pattern = strlen(pattern);
65 len_replacement = strlen(replacement);
68 while( ( c = g_strstr_len(c, strlen(c), pattern) ) ) {
74 debug_print("RSSyl: ==== count = %d\n", count);
77 final_length = strlen(source)
78 - ( count * len_pattern )
79 + ( count * len_replacement );
81 new = malloc(final_length + 1);
83 memset(new, '\0', final_length + 1);
88 if( !memcmp(c, pattern, len_pattern) ) {
89 gboolean break_after_rep = FALSE;
91 if (*(c + len_pattern) == '\0')
92 break_after_rep = TRUE;
93 for (i = 0; i < len_replacement; i++) {
94 *w_new = replacement[i];
109 typedef struct _RSSyl_HTMLSymbol RSSyl_HTMLSymbol;
110 struct _RSSyl_HTMLSymbol
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[] = {
138 static RSSyl_HTMLSymbol tag_list[] = {
153 static gchar *rssyl_replace_chrefs(gchar *string)
155 char *new = g_malloc0(strlen(string) + 1), *ret;
156 char buf[16], tmp[6];
157 int i, ii, j, n, len;
159 gboolean valid, replaced;
163 for (i = 0; i < strlen(string); ++i) {
164 if (string[i] == '&') {
168 while (string[j] != '\0' && n < 16) {
169 if (string[j] != ';') {
170 buf[n++] = string[j];
179 if (strlen(buf) > 0 && valid) {
182 if (buf[0] == '#' && (c = atoi(buf+1)) > 0) {
183 len = g_unichar_to_utf8(c, tmp);
185 g_strlcat(new, tmp, strlen(string));
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);
199 new[ii++] = '&'; /* & */
200 g_strlcat(new, buf, strlen(string));
206 new[ii++] = string[i];
209 new[ii++] = string[i];
218 gchar *rssyl_replace_html_stuff(gchar *text,
219 gboolean symbols, gboolean tags)
221 gchar *tmp = NULL, *wtext = NULL;
224 g_return_val_if_fail(text != NULL, NULL);
227 wtext = rssyl_replace_chrefs(text);
229 wtext = g_strdup(text);
232 /* TODO: rewrite this part to work similarly to rssyl_replace_chrefs() */
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);
238 wtext = g_strdup(tmp);
247 static gchar *rssyl_sanitize_string(gchar *str, gboolean strip_nl)
249 gchar *new = NULL, *c = str, *n = NULL;
254 n = new = malloc(strlen(str) + 1);
255 memset(new, '\0', strlen(str) + 1);
257 while( *c != '\0' ) {
258 if( !isspace(*c) || *c == ' ' || (!strip_nl && *c == '\n') ) {
268 /* rssyl_format_string()
269 * - return value needs to be freed
271 gchar *rssyl_format_string(gchar *str, gboolean replace_html,
274 gchar *res = NULL, *tmp = NULL;
276 g_return_val_if_fail(str != NULL, NULL);
279 tmp = rssyl_replace_html_stuff(str, TRUE, TRUE);
283 res = rssyl_sanitize_string(tmp, strip_nl);
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).
299 gchar **strsplit_no_copy(gchar *str, char delimiter)
301 gchar **array = g_new(gchar *, 1);
303 gchar *cur = str, *next;
307 while ((next = strchr(cur, delimiter)) != NULL) {
309 array = g_realloc(array, (sizeof(gchar *)) * (i + 1));
314 array = g_realloc(array, (sizeof(gchar *)) * (i + 1));
319 /* This is a very dumb function - it just strips <, > and everything between
321 void strip_html(gchar *str)
324 gboolean intag = FALSE;
332 if (*p == '<' || *p == '>' || intag)
333 memmove(p, p + 1, strlen(p));
339 gchar *my_normalize_url(const gchar *url)
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);
348 myurl = g_strdup(url);