e_iconv_* replaced with g_iconv_*



Hi there,

This patch replaces the e_iconv_open, e_iconv, e_iconv_close usage in
Camel with the g_iconv_open, g_iconv and g_iconv_close APIs

This is a tracker bug for upstream Camel:
http://bugzilla.gnome.org/show_bug.cgi?id=374734

Tinymail mailing list: this patch will probably soon go in the SVN repo.
I will first do a few basic regression tests this evening.

-- 
Philip Van Hoof, software developer
home: me at pvanhoof dot be
gnome: pvanhoof at gnome dot org
work: vanhoof at x-tend dot be
blog: http://pvanhoof.be/blog
Index: camel/camel-mime-utils.c
===================================================================
--- camel/camel-mime-utils.c	(revision 1147)
+++ camel/camel-mime-utils.c	(working copy)
@@ -1002,7 +1002,7 @@
 {
 	const char *inptr = in+2;
 	const char *inend = in+len-2;
-	const char *inbuf;
+	gchar *inbuf;
 	const char *charset;
 	char *encname, *p;
 	int tmplen;
@@ -1013,7 +1013,7 @@
 	char *outbuf;
 	size_t inlen, outlen;
 	gboolean retried = FALSE;
-	iconv_t ic;
+	GIConv ic;
 	
 	d(printf("rfc2047: decoding '%.*s'\n", len, in));
 
@@ -1075,15 +1075,15 @@
 			outbuf = outbase;
 			
 		retry:
-			ic = e_iconv_open ("UTF-8", charset);
-			if (ic != (iconv_t) -1) {
-				ret = e_iconv (ic, &inbuf, &inlen, &outbuf, &outlen);
+			ic = g_iconv_open ("UTF-8", charset);
+			if (ic != (GIConv) -1) {
+				ret = g_iconv (ic, &inbuf, &inlen, &outbuf, &outlen);
 				if (ret != (size_t) -1) {
-					e_iconv (ic, NULL, 0, &outbuf, &outlen);
+					g_iconv (ic, NULL, 0, &outbuf, &outlen);
 					*outbuf = 0;
 					decoded = g_strdup (outbase);
 				}
-				e_iconv_close (ic);
+				g_iconv_close (ic);
 			} else {
 				w(g_warning ("Cannot decode charset, header display may be corrupt: %s: %s",
 					     charset, strerror (errno)));
@@ -1131,35 +1131,34 @@
 }
 
 static int
-append_8bit (GString *out, const char *inbuf, size_t inlen, const char *charset)
+append_8bit (GString *out, gchar *inbuf, size_t inlen, const char *charset)
 {
 	char *outbase, *outbuf;
 	size_t outlen;
-	iconv_t ic;
-	
-	ic = e_iconv_open ("UTF-8", charset);
-	if (ic == (iconv_t) -1)
+	GIConv ic;
+
+	ic = g_iconv_open ("UTF-8", charset);
+	if (ic == (GIConv) -1)
 		return FALSE;
 
 	outlen = inlen * 6 + 16;
 	outbuf = outbase = g_malloc(outlen);
 	
-	if (e_iconv (ic, &inbuf, &inlen, &outbuf, &outlen) == (size_t) -1) {
+	if (g_iconv (ic, &inbuf, &inlen, &outbuf, &outlen) == (size_t) -1) {
 		w(g_warning("Conversion to '%s' failed: %s", charset, strerror (errno)));
 		g_free(outbase);
-		e_iconv_close (ic);
+		g_iconv_close (ic);
 		return FALSE;
 	}
 	
-	e_iconv (ic, NULL, NULL, &outbuf, &outlen);
+	g_iconv (ic, NULL, NULL, &outbuf, &outlen);
 	
 	*outbuf = 0;
 	g_string_append(out, outbase);
 	g_free(outbase);
-	e_iconv_close (ic);
+	g_iconv_close (ic);
 
 	return TRUE;
-	
 }
 
 static GString *
@@ -1185,7 +1184,8 @@
 header_decode_text (const char *in, size_t inlen, int ctext, const char *default_charset)
 {
 	GString *out;
-	const char *inptr, *inend, *start, *chunk, *locale_charset;
+	const char *inptr, *inend, *start, *locale_charset;
+	gchar *chunk;
 	GString *(* append) (GString *, const char *, gssize);
 	char *dword = NULL;
 	guint32 mask;
@@ -1216,7 +1216,7 @@
 		} else if (dword == NULL) {
 			append (out, start, inptr - start);
 		} else {
-			chunk = start;
+			chunk = (gchar*) start;
 		}
 
 		start = inptr;
@@ -1229,7 +1229,7 @@
 			g_free(dword);
 		} else {
 			if (!chunk)
-				chunk = start;
+				chunk = (gchar*) start;
 			
 			if ((default_charset == NULL || !append_8bit (out, chunk, inptr-chunk, default_charset))
 			    && (locale_charset == NULL || !append_8bit(out, chunk, inptr-chunk, locale_charset)))
@@ -1292,10 +1292,11 @@
 static void
 rfc2047_encode_word(GString *outstring, const char *in, size_t len, const char *type, unsigned short safemask)
 {
-	iconv_t ic = (iconv_t) -1;
+	GIConv ic = (GIConv) -1;
 	char *buffer, *out, *ascii;
 	size_t inlen, outlen, enclen, bufflen;
-	const char *inptr, *p;
+	const char *p;
+	gchar *inptr;
 	int first = 1;
 
 	d(printf("Converting [%d] '%.*s' to %s\n", len, len, in, type));
@@ -1304,12 +1305,12 @@
 	bufflen = len * 6 + 16;
 	buffer = g_alloca (bufflen);
 	inlen = len;
-	inptr = in;
+	inptr = (gchar*) in;
 	
 	ascii = g_alloca (bufflen);
 	
 	if (g_ascii_strcasecmp (type, "UTF-8") != 0)
-		ic = e_iconv_open (type, "UTF-8");
+		ic = g_iconv_open (type, "UTF-8");
 	
 	while (inlen) {
 		size_t convlen, proclen;
@@ -1359,13 +1360,13 @@
 			   hopefully-small-enough chunks, and leave it at that */
 			convlen = MIN(inlen, CAMEL_FOLD_PREENCODED);
 			p = inptr;
-			if (e_iconv (ic, &inptr, &convlen, &out, &outlen) == (size_t) -1 && errno != EINVAL) {
+			if (g_iconv (ic, &inptr, &convlen, &out, &outlen) == (size_t) -1 && errno != EINVAL) {
 				w(g_warning("Conversion problem: conversion truncated: %s", strerror (errno)));
 				/* blah, we include it anyway, better than infinite loop ... */
 				inptr += convlen;
 			} else {
 				/* make sure we flush out any shift state */
-				e_iconv (ic, NULL, 0, &out, &outlen);
+				g_iconv (ic, NULL, 0, &out, &outlen);
 			}
 			inlen -= (inptr - p);
 		}
@@ -1389,8 +1390,8 @@
 		}
 	}
 	
-	if (ic != (iconv_t) -1)
-		e_iconv_close (ic);
+	if (ic != (GIConv) -1)
+		g_iconv_close (ic);
 }
 
 
@@ -1983,26 +1984,26 @@
 
 /* Tries to convert @in @from charset @to charset.  Any failure, we get no data out rather than partial conversion */
 static char *
-header_convert(const char *to, const char *from, const char *in, size_t inlen)
+header_convert(const char *to, const char *from, gchar *in, size_t inlen)
 {
-	iconv_t ic;
+	GIConv ic;
 	size_t outlen, ret;
 	char *outbuf, *outbase, *result = NULL;
 
-	ic = e_iconv_open(to, from);
-	if (ic == (iconv_t) -1)
+	ic = g_iconv_open(to, from);
+	if (ic == (GIConv) -1)
 		return NULL;
 
 	outlen = inlen * 6 + 16;
 	outbuf = outbase = g_malloc(outlen);
 			
-	ret = e_iconv(ic, &in, &inlen, &outbuf, &outlen);
+	ret = g_iconv(ic, &in, &inlen, &outbuf, &outlen);
 	if (ret != (size_t) -1) {
-		e_iconv(ic, NULL, 0, &outbuf, &outlen);
+		g_iconv(ic, NULL, 0, &outbuf, &outlen);
 		*outbuf = '\0';
 		result = g_strdup(outbase);
 	}
-	e_iconv_close(ic);
+	g_iconv_close(ic);
 	g_free(outbase);
 
 	return result;
@@ -3222,7 +3223,7 @@
 		return g_strdup((gchar*)in);
 	
 	if (g_ascii_strcasecmp(charset, "UTF-8") != 0) {
-		if ((outbuf = (unsigned char *)header_convert(charset, "UTF-8", (const char*)in, strlen((char*)in))))
+		if ((outbuf = (unsigned char *)header_convert(charset, "UTF-8", (gchar*)in, strlen((char*)in))))
 			inptr = outbuf;
 		else
 			return g_strdup((gchar*)in);
Index: camel/camel-sasl-digest-md5.c
===================================================================
--- camel/camel-sasl-digest-md5.c	(revision 1147)
+++ camel/camel-sasl-digest-md5.c	(working copy)
@@ -705,21 +705,21 @@
 		char *username, *outbuf;
 		const char *charset;
 		size_t len, outlen;
-		const char *inbuf;
-		iconv_t cd;
-		
+		gchar *inbuf;
+		GIConv cd;
+
 		charset = e_iconv_locale_charset ();
 		if (!charset)
 			charset = "iso-8859-1";
 		
-		cd = e_iconv_open (resp->charset, charset);
+		cd = g_iconv_open (resp->charset, charset);
 		
 		len = strlen (resp->username);
 		outlen = 2 * len; /* plenty of space */
 		
 		outbuf = username = g_malloc0 (outlen + 1);
 		inbuf = resp->username;
-		if (cd == (iconv_t) -1 || e_iconv (cd, &inbuf, &len, &outbuf, &outlen) == (size_t) -1) {
+		if (cd == (GIConv) -1 || g_iconv (cd, &inbuf, &len, &outbuf, &outlen) == (size_t) -1) {
 			/* We can't convert to UTF-8 - pretend we never got a charset param? */
 			g_free (resp->charset);
 			resp->charset = NULL;
@@ -730,7 +730,7 @@
 		}
 		
 		if (cd != (iconv_t) -1)
-			e_iconv_close (cd);
+			g_iconv_close (cd);
 		
 		g_byte_array_append (buffer, (guchar *) username, strlen (username));
 		g_free (username);
Index: camel/camel-mime-filter-charset.c
===================================================================
--- camel/camel-mime-filter-charset.c	(revision 1147)
+++ camel/camel-mime-filter-charset.c	(working copy)
@@ -67,7 +67,7 @@
 	g_free(f->from);
 	g_free(f->to);
 	if (f->ic != (iconv_t) -1) {
-		e_iconv_close (f->ic);
+		g_iconv_close (f->ic);
 		f->ic = (iconv_t) -1;
 	}
 }
@@ -83,7 +83,7 @@
 	/* what happens with the output bytes if this resets the state? */
 	if (f->ic != (iconv_t) -1) {
 		buffer = buf;
-		e_iconv (f->ic, NULL, 0, &buffer, &outlen);
+		g_iconv (f->ic, NULL, 0, &buffer, &outlen);
 	}
 }
 
@@ -92,7 +92,7 @@
 {
 	CamelMimeFilterCharset *charset = (CamelMimeFilterCharset *)mf;
 	size_t inleft, outleft, converted = 0;
-	const char *inbuf;
+	gchar *inbuf;
 	char *outbuf;
 	
 	if (charset->ic == (iconv_t) -1)
@@ -107,7 +107,7 @@
 	
 	if (inleft > 0) {
 		do {
-			converted = e_iconv (charset->ic, &inbuf, &inleft, &outbuf, &outleft);
+			converted = g_iconv (charset->ic, &inbuf, &inleft, &outbuf, &outleft);
 			if (converted == (size_t) -1) {
 				if (errno == E2BIG) {
 					/*
@@ -147,7 +147,7 @@
 	}
 	
 	/* flush the iconv conversion */
-	e_iconv (charset->ic, NULL, NULL, &outbuf, &outleft);
+	g_iconv (charset->ic, NULL, NULL, &outbuf, &outleft);
 	
 	*out = mf->outbuf;
 	*outlen = mf->outsize - outleft;
@@ -167,7 +167,7 @@
 {
 	CamelMimeFilterCharset *charset = (CamelMimeFilterCharset *)mf;
 	size_t inleft, outleft, converted = 0;
-	const char *inbuf;
+	gchar *inbuf;
 	char *outbuf;
 	
 	if (charset->ic == (iconv_t) -1)
@@ -181,7 +181,7 @@
 	inleft = len;
 	
 	do {
-		converted = e_iconv (charset->ic, &inbuf, &inleft, &outbuf, &outleft);
+		converted = g_iconv (charset->ic, &inbuf, &inleft, &outbuf, &outleft);
 		if (converted == (size_t) -1) {
 			if (errno == E2BIG || errno == EINVAL)
 				break;
@@ -272,9 +272,12 @@
 	CamelMimeFilterCharset *new;
 	
 	new = CAMEL_MIME_FILTER_CHARSET (camel_object_new (camel_mime_filter_charset_get_type ()));
-	
-	new->ic = e_iconv_open (to_charset, from_charset);
-	if (new->ic == (iconv_t) -1) {
+
+	if (from_charset == NULL || to_charset == NULL)
+		new->ic = (GIConv)-1;
+	else
+		new->ic = g_iconv_open (to_charset, from_charset);
+	if (new->ic == (GIConv) -1) {
 		w(g_warning ("Cannot create charset conversion from %s to %s: %s",
 			     from_charset ? from_charset : "(null)",
 			     to_charset ? to_charset : "(null)",
Index: camel/camel-mime-filter-charset.h
===================================================================
--- camel/camel-mime-filter-charset.h	(revision 1147)
+++ camel/camel-mime-filter-charset.h	(working copy)
@@ -41,7 +41,7 @@
 
 	struct _CamelMimeFilterCharsetPrivate *priv;
 
-	iconv_t ic;
+	GIConv ic;
 	char *from;
 	char *to;
 };
Index: libedataserver/e-iconv.c
===================================================================
--- libedataserver/e-iconv.c	(revision 1147)
+++ libedataserver/e-iconv.c	(working copy)
@@ -444,129 +444,8 @@
 	g_free(ic);
 }
 
-/* This should run pretty quick, its called a lot */
-iconv_t e_iconv_open(const char *oto, const char *ofrom)
-{
-	const char *to, *from;
-	char *tofrom;
-	struct _iconv_cache *ic;
-	struct _iconv_cache_node *in;
-	int errnosav;
-	iconv_t ip;
 
-	if (oto == NULL || ofrom == NULL) {
-		errno = EINVAL;
-		return (iconv_t) -1;
-	}
-	
-	to = e_iconv_charset_name (oto);
-	from = e_iconv_charset_name (ofrom);
-	tofrom = g_alloca (strlen (to) + strlen (from) + 2);
-	sprintf(tofrom, "%s%%%s", to, from);
 
-	LOCK();
-
-	ic = g_hash_table_lookup(iconv_cache, tofrom);
-	if (ic) {
-		e_dlist_remove((EDListNode *)ic);
-	} else {
-		struct _iconv_cache *last = (struct _iconv_cache *)iconv_cache_list.tailpred;
-		struct _iconv_cache *prev;
-
-		prev = last->prev;
-		while (prev && iconv_cache_size > E_ICONV_CACHE_SIZE) {
-			in = (struct _iconv_cache_node *)last->open.head;
-			if (in->next && !in->busy) {
-				cd(printf("Flushing iconv converter '%s'\n", last->conv));
-				e_dlist_remove((EDListNode *)last);
-				g_hash_table_remove(iconv_cache, last->conv);
-				flush_entry(last);
-				iconv_cache_size--;
-			}
-			last = prev;
-			prev = last->prev;
-		}
-
-		iconv_cache_size++;
-		
-		ic = g_malloc(sizeof(*ic));
-		e_dlist_init(&ic->open);
-		ic->conv = g_strdup(tofrom);
-		g_hash_table_insert(iconv_cache, ic->conv, ic);
-
-		cd(printf("Creating iconv converter '%s'\n", ic->conv));
-	}
-	e_dlist_addhead(&iconv_cache_list, (EDListNode *)ic);
-
-	/* If we have a free iconv, use it */
-	in = (struct _iconv_cache_node *)ic->open.tailpred;
-	if (in->prev && !in->busy) {
-		cd(printf("using existing iconv converter '%s'\n", ic->conv));
-		ip = in->ip;
-		if (ip != (iconv_t)-1) {
-			/* work around some broken iconv implementations 
-			 * that die if the length arguments are NULL 
-			 */
-			size_t buggy_iconv_len = 0;
-			char *buggy_iconv_buf = NULL;
-
-			/* resets the converter */
-			iconv(ip, &buggy_iconv_buf, &buggy_iconv_len, &buggy_iconv_buf, &buggy_iconv_len);
-			in->busy = TRUE;
-			e_dlist_remove((EDListNode *)in);
-			e_dlist_addhead(&ic->open, (EDListNode *)in);
-		}
-	} else {
-		cd(printf("creating new iconv converter '%s'\n", ic->conv));
-		ip = iconv_open(to, from);
-		in = g_malloc(sizeof(*in));
-		in->ip = ip;
-		in->parent = ic;
-		e_dlist_addhead(&ic->open, (EDListNode *)in);
-		if (ip != (iconv_t)-1) {
-			g_hash_table_insert(iconv_cache_open, ip, in);
-			in->busy = TRUE;
-		} else {
-			errnosav = errno;
-			g_warning("Could not open converter for '%s' to '%s' charset", from, to);
-			in->busy = FALSE;
-			errno = errnosav;
-		}
-	}
-
-	UNLOCK();
-
-	return ip;
-}
-
-size_t e_iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft, char ** outbuf, size_t *outbytesleft)
-{
-	return iconv(cd, (char **) inbuf, inbytesleft, outbuf, outbytesleft);
-}
-
-void
-e_iconv_close(iconv_t ip)
-{
-	struct _iconv_cache_node *in;
-
-	if (ip == (iconv_t)-1)
-		return;
-
-	LOCK();
-	in = g_hash_table_lookup(iconv_cache_open, ip);
-	if (in) {
-		cd(printf("closing iconv converter '%s'\n", in->parent->conv));
-		e_dlist_remove((EDListNode *)in);
-		in->busy = FALSE;
-		e_dlist_addtail(&in->parent->open, (EDListNode *)in);
-	} else {
-		g_warning("trying to close iconv i dont know about: %p", ip);
-		iconv_close(ip);
-	}
-	UNLOCK();
-
-}
-
 const char *e_iconv_locale_charset(void)
 {
 	e_iconv_init(FALSE);
Index: libedataserver/e-iconv.h
===================================================================
--- libedataserver/e-iconv.h	(revision 1147)
+++ libedataserver/e-iconv.h	(working copy)
@@ -33,9 +33,6 @@
 #endif /* __cplusplus */
 
 const char *e_iconv_charset_name(const char *charset);
-iconv_t e_iconv_open(const char *oto, const char *ofrom);
-size_t e_iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft, char ** outbuf, size_t *outbytesleft);
-void e_iconv_close(iconv_t ip);
 const char *e_iconv_locale_charset(void);
 
 /* languages */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]