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>
36 /* (shouldn't be any) */
38 gchar *rssyl_strreplace(gchar *source, gchar *pattern,
41 gchar *new, *w_new = NULL, *c;
42 guint count = 0, final_length;
43 size_t len_pattern, len_replacement;
46 debug_print("RSSyl: ======= strreplace: '%s': '%s'->'%s'\n", source, pattern,
50 if( source == NULL || pattern == NULL ) {
51 debug_print("RSSyl: source or pattern is NULL!!!\n");
55 if( !g_utf8_validate(source, -1, NULL) ) {
56 debug_print("RSSyl: source is not an UTF-8 encoded text\n");
60 if( !g_utf8_validate(pattern, -1, NULL) ) {
61 debug_print("RSSyl: pattern is not an UTF-8 encoded text\n");
65 len_pattern = strlen(pattern);
66 len_replacement = strlen(replacement);
69 while( ( c = g_strstr_len(c, strlen(c), pattern) ) ) {
75 debug_print("RSSyl: ==== count = %d\n", count);
78 final_length = strlen(source)
79 - ( count * len_pattern )
80 + ( count * len_replacement );
82 new = malloc(final_length + 1);
83 memset(new, '\0', final_length + 1);
85 /* 'c' will be our iterator over original string
86 * 'w_new' our iterator over the new string */
90 /* Go until either end of string is reached, or until the
91 * remaining text is shorter than the pattern. */
92 while( *c != '\0' && strlen(c) <= len_pattern) {
93 if( !memcmp(c, pattern, len_pattern) ) {
95 for (i = 0; i < len_replacement; i++) {
96 *w_new = replacement[i];
107 /* We broke off the above cycle because remaining text was not
108 * long enough for the pattern, so now we need to append the
109 * remaining text to the new string. */
111 strncat(new, c, final_length - strlen(new));
117 typedef struct _RSSyl_HTMLSymbol RSSyl_HTMLSymbol;
118 struct _RSSyl_HTMLSymbol
124 static RSSyl_HTMLSymbol tag_list[] = {
139 static gchar *rssyl_replace_chrefs(gchar *string)
141 char *new = g_malloc0(strlen(string) + 1), *ret;
147 for (i = 0; i < strlen(string); ++i) {
148 if (string[i] == '&') {
149 entity = entity_decode(&(string[i]));
150 if (entity != NULL) {
151 g_strlcat(new, entity, strlen(string));
152 ii += strlen(entity);
155 while (string[++i] != ';');
156 --i; /* loop will inc it again */
158 new[ii++] = string[i];
161 new[ii++] = string[i];
170 gchar *rssyl_replace_html_stuff(gchar *text,
171 gboolean symbols, gboolean tags)
173 gchar *tmp = NULL, *wtext = NULL;
176 g_return_val_if_fail(text != NULL, NULL);
179 wtext = rssyl_replace_chrefs(text);
181 wtext = g_strdup(text);
184 /* TODO: rewrite this part to work similarly to rssyl_replace_chrefs() */
186 for( i = 0; tag_list[i].key != NULL; i++ ) {
187 if( g_strstr_len(text, strlen(text), tag_list[i].key) ) {
188 tmp = rssyl_strreplace(wtext, tag_list[i].key, tag_list[i].val);
190 wtext = g_strdup(tmp);
199 static gchar *rssyl_sanitize_string(gchar *str, gboolean strip_nl)
201 gchar *new = NULL, *c = str, *n = NULL;
206 n = new = malloc(strlen(str) + 1);
207 memset(new, '\0', strlen(str) + 1);
209 while( *c != '\0' ) {
210 if( !isspace(*c) || *c == ' ' || (!strip_nl && *c == '\n') ) {
220 /* rssyl_format_string()
221 * - return value needs to be freed
223 gchar *rssyl_format_string(gchar *str, gboolean replace_html,
226 gchar *res = NULL, *tmp = NULL;
228 g_return_val_if_fail(str != NULL, NULL);
231 tmp = rssyl_replace_html_stuff(str, TRUE, TRUE);
235 res = rssyl_sanitize_string(tmp, strip_nl);
243 /* this functions splits a string into an array of string, by
244 * returning an array of pointers to positions of the delimiter
245 * in the original string and replacing this delimiter with a
246 * NULL. It does not duplicate memory, hence you should only
247 * free the array and not its elements, and you should not
248 * free the original string before you're done with the array.
249 * maybe could be part of the core (utils.c).
251 gchar **strsplit_no_copy(gchar *str, char delimiter)
253 gchar **array = g_new(gchar *, 1);
255 gchar *cur = str, *next;
259 while ((next = strchr(cur, delimiter)) != NULL) {
261 array = g_realloc(array, (sizeof(gchar *)) * (i + 1));
266 array = g_realloc(array, (sizeof(gchar *)) * (i + 1));
271 /* This is a very dumb function - it just strips <, > and everything between
273 void strip_html(gchar *str)
276 gboolean intag = FALSE;
284 if (*p == '<' || *p == '>' || intag)
285 memmove(p, p + 1, strlen(p));
291 gchar *my_normalize_url(const gchar *url)
295 if (!strncmp(url, "feed://", 7))
296 myurl = g_strdup(url+7);
297 else if (!strncmp(url, "feed:", 5))
298 myurl = g_strdup(url+5);
300 myurl = g_strdup(url);