diff src/util.c @ 8341:fec4c1fb2ac8

[gaim-migrate @ 9065] Alright, I had to lay down a little bit of smack. Here goes: -Work around the rate-limit problem caused by Gaim auto-requesting away messages too quickly. Basically there is now a 1.2sec gap between each request. The downside is that it takes a bit longer for Gaim to get everyone's away message initially. Adium shouldn't need to do anything to take advantage of this. Fire (they use libfaim, right?) will need to add a callback for AIM_CB_LOC_REQUESTINFOTIMEOUT. Just search oscar.c for gaim_reqinfo_timeout() and copy what that thing does. -Attempt to do a better job showing away messages in tooltips. Hopefully & and greater than and less than will show up correctly now. I don't think there should be any side effects, but if you mouse over someone and it crashes or you get a pango error let me know. -Remove/combine some silly functions in util.c that few things use. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Thu, 26 Feb 2004 08:29:32 +0000
parents da57fb60680a
children 33cc36f5a7a6
line wrap: on
line diff
--- a/src/util.c	Thu Feb 26 07:22:47 2004 +0000
+++ b/src/util.c	Thu Feb 26 08:29:32 2004 +0000
@@ -1642,84 +1642,32 @@
 	return (cpy);
 }
 
-/*
- * rcg10312000 This could be more robust, but it works for my current
- *  goal: to remove those annoying <BR> tags.  :)
- * dtf12162000 made the loop more readable. i am a neat freak. ;) */
-void
-gaim_strncpy_nohtml(char *dest, const char *src, size_t destsize)
-{
-	char *ptr;
-
-	g_return_if_fail(dest != NULL);
-	g_return_if_fail(src  != NULL);
-	g_return_if_fail(destsize > 0);
-
-	g_snprintf(dest, destsize, "%s", src);
-
-	while ((ptr = strstr(dest, "<BR>")) != NULL) {
-		/* replace <BR> with a newline. */
-		*ptr = '\n';
-		memmove(ptr + 1, ptr + 4, strlen(ptr + 4) + 1);
-	}
-}
-
-void
-gaim_strncpy_withhtml(gchar *dest, const gchar *src, size_t destsize)
+gchar *
+gaim_strdup_withhtml(const gchar *src)
 {
-	gchar *end;
-
-	g_return_if_fail(dest != NULL);
-	g_return_if_fail(src  != NULL);
-	g_return_if_fail(destsize > 0);
-
-	end = dest + destsize;
-
-	while (dest < end) {
-		if (*src == '\n' && dest < end - 5) {
-			strcpy(dest, "<BR>");
-			src++;
-			dest += 4;
-		} else if(*src == '\r') {
-			src++;
-		} else {
-			*dest++ = *src;
-			if (*src == '\0')
-				return;
-			else
-				src++;
-		}
-	}
-}
-
-/*
- * Like strncpy_withhtml (above), but malloc()'s the necessary space
- *
- * The caller is responsible for freeing the space pointed to by the
- * return value.
- */
-char *
-gaim_strdup_withhtml(const char *src)
-{
-	char *sp, *dest;
-	gulong destsize;
+	gulong destsize, i, j;
+	gchar *dest;
 
 	g_return_val_if_fail(src != NULL, NULL);
 
-	/*
-	 * All we need do is multiply the number of newlines by 3 (the
-	 * additional length of "<BR>" over "\n"), account for the
-	 * terminator, malloc the space and call strncpy_withhtml.
-	 */
-	for(destsize = 0, sp = (gchar *)src;
-		(sp = strchr(sp, '\n')) != NULL;
-		++sp, ++destsize)
-		;
+	/* New length is (length of src) + (number of \n's * 3) + 1 */
+	for (i = 0, j = 0; src[i] != '\0'; i++)
+		if (src[i] == '\n')
+			j++;
+
+	destsize = i + (j * 3) + 1;
+	dest = g_malloc(destsize);
 
-	destsize *= 3;
-	destsize += strlen(src) + 1;
-	dest = g_malloc(destsize);
-	gaim_strncpy_withhtml(dest, src, destsize);
+	/* Copy stuff, ignoring \r's, because they are dumb */
+	for (i = 0, j = 0; src[i] != '\0'; i++) {
+		if (src[i] == '\n') {
+			strcpy(&dest[j], "<BR>");
+			j += 4;
+		} else if (src[i] != '\r')
+			dest[j++] = src[i];
+	}
+
+	dest[destsize-1] = '\0';
 
 	return dest;
 }
@@ -1799,7 +1747,7 @@
 	g_free(text2);
 }
 
-char *
+gchar *
 gaim_strreplace(const char *string, const char *delimiter,
 				const char *replacement)
 {
@@ -1817,6 +1765,54 @@
 	return ret;
 }
 
+gchar *
+gaim_strcasereplace(const char *string, const char *delimiter,
+					const char *replacement)
+{
+	gchar *ret;
+	int length_del, length_rep, i, j;
+
+	g_return_val_if_fail(string      != NULL, NULL);
+	g_return_val_if_fail(delimiter   != NULL, NULL);
+	g_return_val_if_fail(replacement != NULL, NULL);
+
+	length_del = strlen(delimiter);
+	length_rep = strlen(replacement);
+
+	/* Count how many times the delimiter appears */
+	i = 0; /* position in the source string */
+	j = 0; /* number of occurences of "delimiter" */
+	while (string[i] != '\0') {
+		if (!strncasecmp(&string[i], delimiter, length_del)) {
+			i += length_del;
+			j += length_rep;
+		} else {
+			i++;
+			j++;
+		}
+	}
+
+	ret = g_malloc(j+1);
+
+	i = 0; /* position in the source string */
+	j = 0; /* position in the destination string */
+	while (string[i] != '\0') {
+		if (!strncasecmp(&string[i], delimiter, length_del)) {
+			strncpy(&ret[j], replacement, length_rep);
+			i += length_del;
+			j += length_rep;
+		} else {
+			ret[j] = string[i];
+			i++;
+			j++;
+		}
+	}
+
+	ret[j] = '\0';
+
+	return ret;
+}
+
 const char *
 gaim_strcasestr(const char *haystack, const char *needle)
 {