diff src/util.c @ 10258:357d4fa1bfbe

[gaim-migrate @ 11400] This is the IRC fallback encoding patch and gaim_utf8_salvage function that just hit oldstatus. If CVS didn't suck, I wouldn't have to generate two commits for this. :-P committer: Tailor Script <tailor@pidgin.im>
author Ethan Blanton <elb@pidgin.im>
date Wed, 24 Nov 2004 06:39:47 +0000
parents 7ff9b8b22e7d
children 8d42237564f6
line wrap: on
line diff
--- a/src/util.c	Wed Nov 24 02:16:36 2004 +0000
+++ b/src/util.c	Wed Nov 24 06:39:47 2004 +0000
@@ -3057,6 +3057,33 @@
 	return NULL;
 }
 
+#define utf8_first(x) ((x & 0x80) == 0 || (x & 0xe0) == 0xc0 \
+		       || (x & 0xf0) == 0xe0 || (x & 0xf8) == 0xf)
+gchar *
+gaim_utf8_salvage(const char *str)
+{
+	GString *workstr;
+	const char *end;
+
+	g_return_val_if_fail(str != NULL, NULL);
+
+	workstr = g_string_sized_new(strlen(str));
+
+	do {
+		g_utf8_validate(str, -1, &end);
+		workstr = g_string_append_len(workstr, str, end - str);
+		str = end;
+		if (*str == '\0')
+			break;
+		do {
+			workstr = g_string_append_c(workstr, '?');
+			str++;
+		} while (!utf8_first(*str));
+	} while (*str != '\0');
+
+	return g_string_free(workstr, FALSE);
+}
+
 char *
 gaim_utf8_ncr_decode(const char *in)
 {