tConvert config variable strings from UTF-8 to the locale's codeset when writing the configuration file, so that the GTK+2.0 client works consistently in non-UTF8 locales. - 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 5df905e8ef3931c4f1c3e66b6c1d5ce333617f2b
(DIR) parent aae2d4fec33b36d3dd3cee90a40092ed5685bd32
(HTM) Author: Ben Webb <ben@salilab.org>
Date: Wed, 31 Jul 2002 16:31:01 +0000
Convert config variable strings from UTF-8 to the locale's codeset when
writing the configuration file, so that the GTK+2.0 client works consistently
in non-UTF8 locales.
Diffstat:
M src/configfile.c | 30 +++++++++++++++++++++++++++++-
1 file changed, 29 insertions(+), 1 deletion(-)
---
(DIR) diff --git a/src/configfile.c b/src/configfile.c
t@@ -77,6 +77,30 @@ static void PrintEscaped(FILE *fp, gchar *str)
}
/*
+ * Converts the given string from UTF-8 to the locale's codeset. If the
+ * locale codeset already is UTF-8, returns a copy of the original
+ * string. The returned string is dynamically allocated, and should be
+ * later g_free'd by the caller.
+ */
+static gchar *ToLocaleCodeset(const gchar *origstr)
+{
+#ifdef HAVE_GLIB2
+ if (!g_get_charset(NULL)) {
+ gchar *convstr = g_locale_from_utf8(origstr, -1, NULL, NULL, NULL);
+ if (convstr) {
+ return convstr;
+ } else {
+ return g_strdup("[Could not convert string from UTF8]");
+ }
+ } else {
+ return g_strdup(origstr);
+ }
+#else
+ return g_strdup(origstr);
+#endif
+}
+
+/*
* Writes a single configuration file variable (identified by GlobalIndex
* and StructIndex) to the specified file, in a format suitable for reading
* back in (via. ParseNextConfig and friends).
t@@ -106,8 +130,12 @@ static void WriteConfigValue(FILE *fp, int GlobalIndex, int StructIndex)
fprintf(fp, "%s = %s\n", GlobalName, prstr);
g_free(prstr);
} else if (Globals[GlobalIndex].StringVal) {
+ gchar *convstr;
+
fprintf(fp, "%s = \"", GlobalName);
- PrintEscaped(fp, *GetGlobalString(GlobalIndex, StructIndex));
+ convstr = ToLocaleCodeset(*GetGlobalString(GlobalIndex, StructIndex));
+ PrintEscaped(fp, convstr);
+ g_free(convstr);
fprintf(fp, "\"\n");
} else if (Globals[GlobalIndex].StringList) {
int i;