changeset 21862:ae58ffd5e929

Create a purple_gai_strerror() function similar to g_str_error() which calls gai_strerror() then tries to convert the result into UTF-8. I believe this fixes #3003
author Mark Doliner <mark@kingant.net>
date Tue, 18 Dec 2007 08:42:41 +0000
parents eb9f928a369d
children 7e833250a0ef
files libpurple/dnsquery.c libpurple/network.c libpurple/util.c libpurple/util.h
diffstat 4 files changed, 57 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/dnsquery.c	Tue Dec 18 07:39:57 2007 +0000
+++ b/libpurple/dnsquery.c	Tue Dec 18 08:42:41 2007 +0000
@@ -547,7 +547,7 @@
 	{
 #ifdef HAVE_GETADDRINFO
 		g_snprintf(message, sizeof(message), _("Error resolving %s:\n%s"),
-				query_data->hostname, gai_strerror(err));
+				query_data->hostname, purple_gai_strerror(err));
 #else
 		g_snprintf(message, sizeof(message), _("Error resolving %s: %d"),
 				query_data->hostname, err);
@@ -695,7 +695,7 @@
 		}
 		freeaddrinfo(tmp);
 	} else {
-		query_data->error_message = g_strdup_printf(_("Error resolving %s:\n%s"), query_data->hostname, gai_strerror(rc));
+		query_data->error_message = g_strdup_printf(_("Error resolving %s:\n%s"), query_data->hostname, purple_gai_strerror(rc));
 	}
 #else
 	if ((hp = gethostbyname(query_data->hostname))) {
--- a/libpurple/network.c	Tue Dec 18 07:39:57 2007 +0000
+++ b/libpurple/network.c	Tue Dec 18 08:42:41 2007 +0000
@@ -289,7 +289,7 @@
 	errnum = getaddrinfo(NULL /* any IP */, serv, &hints, &res);
 	if (errnum != 0) {
 #ifndef _WIN32
-		purple_debug_warning("network", "getaddrinfo: %s\n", gai_strerror(errnum));
+		purple_debug_warning("network", "getaddrinfo: %s\n", purple_gai_strerror(errnum));
 		if (errnum == EAI_SYSTEM)
 			purple_debug_warning("network", "getaddrinfo: system error: %s\n", g_strerror(errno));
 #else
--- a/libpurple/util.c	Tue Dec 18 07:39:57 2007 +0000
+++ b/libpurple/util.c	Tue Dec 18 08:42:41 2007 +0000
@@ -4257,6 +4257,49 @@
 	return g_string_free(workstr, FALSE);
 }
 
+G_CONST_RETURN gchar *
+purple_gai_strerror(gint errnum)
+{
+	static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
+	char *msg;
+	int saved_errno = errno;
+
+	const char *msg_locale;
+
+	msg_locale = gai_strerror(errnum);
+	if (g_get_charset(NULL))
+	{
+		/* This string is already UTF-8--great! */
+		errno = saved_errno;
+		return msg_locale;
+	}
+	else
+	{
+		gchar *msg_utf8 = g_locale_to_utf8(msg_locale, -1, NULL, NULL, NULL);
+		if (msg_utf8)
+		{
+			/* Stick in the quark table so that we can return a static result */
+			GQuark msg_quark = g_quark_from_string(msg_utf8);
+			g_free(msg_utf8);
+
+			msg_utf8 = (gchar *)g_quark_to_string(msg_quark);
+			errno = saved_errno;
+			return msg_utf8;
+		}
+	}
+
+	msg = g_static_private_get(&msg_private);
+	if (!msg)
+	{
+		msg = g_new(gchar, 64);
+		g_static_private_set(&msg_private, msg, g_free);
+	}
+
+	sprintf(msg, "unknown error (%d)", errnum);
+
+	errno = saved_errno;
+	return msg;
+}
 
 char *
 purple_utf8_ncr_encode(const char *str)
--- a/libpurple/util.h	Tue Dec 18 07:39:57 2007 +0000
+++ b/libpurple/util.h	Tue Dec 18 08:42:41 2007 +0000
@@ -1110,6 +1110,17 @@
 gchar *purple_utf8_salvage(const char *str);
 
 /**
+ * Return the UTF-8 version of gai_strerror().  It calls gai_strerror()
+ * then converts the result to UTF-8.  This function is analogous to
+ * g_strerror().
+ *
+ * @param errnum The error code.
+ *
+ * @return The UTF-8 error message.
+ */
+G_CONST_RETURN gchar *purple_gai_strerror(gint errnum);
+
+/**
  * Compares two UTF-8 strings case-insensitively.  This string is
  * more expensive than a simple g_utf8_collate() comparison because
  * it calls g_utf8_casefold() on each string, which allocates new