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 static RSSyl_HTMLSymbol symbol_list[] = {
126 { "™", "(TM)" },
127 { "™", "(TM)" },
129 { "…", "..." },
130 { "…", "..." },
135 static RSSyl_HTMLSymbol tag_list[] = {
150 gchar *rssyl_replace_html_stuff(gchar *text,
151 gboolean symbols, gboolean tags)
153 gchar *tmp = NULL, *wtext = NULL;
156 g_return_val_if_fail(text != NULL, NULL);
158 wtext = g_strdup(text);
160 /* Ugly, needlessly traverses the string again and again. Probably
161 * could use a rewrite. */
163 for( i = 0; symbol_list[i].key != NULL; i++ ) {
164 if( g_strstr_len(text, strlen(text), symbol_list[i].key) ) {
165 tmp = rssyl_strreplace(wtext, symbol_list[i].key, symbol_list[i].val);
166 wtext = g_strdup(tmp);
173 for( i = 0; tag_list[i].key != NULL; i++ ) {
174 if( g_strstr_len(text, strlen(text), symbol_list[i].key) ) {
175 tmp = rssyl_strreplace(wtext, tag_list[i].key, tag_list[i].val);
176 wtext = g_strdup(tmp);
185 static gchar *rssyl_sanitize_string(gchar *str, gboolean strip_nl)
187 gchar *new = NULL, *c = str, *n = NULL;
192 n = new = malloc(strlen(str) + 1);
193 memset(new, '\0', strlen(str) + 1);
195 while( *c != '\0' ) {
196 if( !isspace(*c) || *c == ' ' || (!strip_nl && *c == '\n') ) {
206 /* rssyl_format_string()
207 * - return value needs to be freed
209 gchar *rssyl_format_string(gchar *str, gboolean replace_html,
212 gchar *res = NULL, *tmp = NULL;
214 g_return_val_if_fail(str != NULL, NULL);
217 tmp = rssyl_replace_html_stuff(str, TRUE, TRUE);
221 res = rssyl_sanitize_string(tmp, strip_nl);
229 /* this functions splits a string into an array of string, by
230 * returning an array of pointers to positions of the delimiter
231 * in the original string and replacing this delimiter with a
232 * NULL. It does not duplicate memory, hence you should only
233 * free the array and not its elements, and you should not
234 * free the original string before you're done with the array.
235 * maybe could be part of the core (utils.c).
237 gchar **strsplit_no_copy(gchar *str, char delimiter)
239 gchar **array = g_new(gchar *, 1);
241 gchar *cur = str, *next;
245 while ((next = strchr(cur, delimiter)) != NULL) {
247 array = g_realloc(array, (sizeof(gchar *)) * (i + 1));
252 array = g_realloc(array, (sizeof(gchar *)) * (i + 1));
257 /* This is a very dumb function - it just strips <, > and everything between
259 void strip_html(gchar *str)
262 gboolean intag = FALSE;
270 if (*p == '<' || *p == '>' || intag)
271 memmove(p, p + 1, strlen(p));
277 gchar *my_normalize_url(const gchar *url)
281 if (!strncmp(url, "feed://", 7))
282 myurl = g_strdup(url+7);
283 else if (!strncmp(url, "feed:", 5))
284 myurl = g_strdup(url+5);
286 myurl = g_strdup(url);