tReplaced calls to strerror() with a function that automatically converts from the locale's charset to UTF-8 where necessary. - vaccinewars - be a doctor and try to vaccinate the world
(HTM) git clone git://src.adamsgaard.dk/vaccinewars
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 3b5c2ce242bbfb0726bf0fffe006e5e5f607eec1
(DIR) parent d16c72301fc42e813bc8500815d96ec6f4f23a48
(HTM) Author: Ben Webb <ben@salilab.org>
Date: Mon, 29 Apr 2002 10:33:53 +0000
Replaced calls to strerror() with a function that automatically converts
from the locale's charset to UTF-8 where necessary.
Diffstat:
M src/error.c | 42 ++++++++++++++++++++++++++++++-
M src/error.h | 2 ++
M src/gui_client/gtk_client.c | 1 +
M src/gui_client/optdialog.c | 4 +++-
4 files changed, 47 insertions(+), 2 deletions(-)
---
(DIR) diff --git a/src/error.c b/src/error.c
t@@ -37,6 +37,44 @@
#include "error.h"
#include "nls.h"
+static gboolean err_utf8_encoding = FALSE;
+
+/*
+ * If "want" is TRUE, we want all error texts (via. strerror) in UTF8,
+ * even if the locale's charset is not UTF8.
+ */
+void WantUTF8Errors(gboolean want)
+{
+ err_utf8_encoding = want;
+}
+
+/*
+ * Returns the strerror() error string for the given error code,
+ * possibly translated to UTF8. N.B. Unlike strerror(), this string
+ * must be g_free'd by the caller when no longer needed.
+ */
+gchar *ErrStrFromErrno(int errcode)
+{
+ gchar *untran = strerror(errcode);
+
+#ifdef HAVE_GLIB2
+ if (err_utf8_encoding) {
+ gchar *utf8str;
+
+ utf8str = g_locale_to_utf8(untran, strlen(untran), NULL, NULL, NULL);
+ if (utf8str) {
+ return utf8str;
+ } else {
+ g_strdup(_("(Error cannot be displayed in UTF-8)"));
+ }
+ } else {
+ return g_strdup(untran);
+ }
+#else
+ return g_strdup(untran);
+#endif
+}
+
void FreeError(LastError *error)
{
if (!error)
t@@ -103,7 +141,9 @@ ErrorType *ET_CUSTOM = &ETCustom;
*/
void ErrnoAppendError(GString *str, LastError *error)
{
- g_string_append(str, strerror(error->code));
+ gchar *errstr = ErrStrFromErrno(error->code);
+ g_string_append(str, errstr);
+ g_free(errstr);
}
static ErrorType ETErrno = { ErrnoAppendError, NULL };
(DIR) diff --git a/src/error.h b/src/error.h
t@@ -58,6 +58,8 @@ typedef struct _ErrTable {
gchar *string;
} ErrTable;
+void WantUTF8Errors(gboolean want);
+gchar *ErrStrFromErrno(int errcode);
void FreeError(LastError *error);
LastError *NewError(ErrorType *type, gint code, gpointer data);
void SetError(LastError **error, ErrorType *type, gint code,
(DIR) diff --git a/src/gui_client/gtk_client.c b/src/gui_client/gtk_client.c
t@@ -2205,6 +2205,7 @@ gboolean GtkLoop(int *argc, char **argv[], gboolean ReturnOnFail)
bind_textdomain_codeset(PACKAGE, "UTF-8");
ConvertToUTF8();
+ WantUTF8Errors(TRUE);
#endif
/* Set up message handlers */
(DIR) diff --git a/src/gui_client/optdialog.c b/src/gui_client/optdialog.c
t@@ -552,7 +552,9 @@ static void UpdateLocalConfig(void)
}
if (!fp) {
- g_warning(_("Could not open file %s: %s"), cfgfile, strerror(errno));
+ gchar *errstr = ErrStrFromErrno(errno);
+ g_warning(_("Could not open file %s: %s"), cfgfile, errstr);
+ g_free(errstr);
g_free(cfgfile);
return;
}