Michal Suchanek <hramr...@centrum.cz> wrote:
> FWIW here is a patch that
>
> a) tries to detect if user is running in utf-8 locale by a heuristic
> similar that that used in the autoconf test

setlocale(LC_CTYPE, "") only sets the current LC_CTYPE to the value of
the user environment.

If e.g. the user has LC_ALL=C the program will fail even if there is a
utf-8 locale it could use installed on the computer.


The attached patch first tries to set the locale found in the autoconf
script.

If that fails, it popens 'locale -a' and searches for a working utf-8
locale to use.

Bye
Patric

diff -r 79f840e44c31 src/charset.c
--- a/src/charset.c	Mon Oct 23 23:20:43 2006 +0200
+++ b/src/charset.c	Sat Feb 06 15:17:25 2010 +0100
@@ -34,20 +34,54 @@
 
 #ifdef MULTIBYTE
 iconv_t conv;
+char internal_locale[256];
 #endif
 
 /* ------------------------------------------------ */
 
 #ifdef MULTIBYTE
+static int suffix(const char * str, const char * suffix)
+{
+	if ( strlen(str) < strlen(suffix) ) return 0;
+	if ( ! strcmp(suffix, str + ( strlen(str) - strlen(suffix) ) ) ) return 1;
+	return 0;
+}
+
+static int utf_8_locale(const char * locale)
+{
+	if (!locale) return 0;
+	return suffix(locale,".utf8") || suffix(locale, ".UTF-8");
+}
+
 void init_multibyte()
 {
-	char *ret;
-	ret = setlocale(LC_CTYPE, INTERNAL_LOCALE); 
-  if (ret==NULL) { 
-		fprintf(stderr, "setlocale failed with: %s\n\n", INTERNAL_LOCALE); 
-		exit(1);
+	char *locale_found;
+	if ((locale_found = setlocale(LC_CTYPE, INTERNAL_LOCALE))) {
+		strcpy(internal_locale, INTERNAL_LOCALE);
+	} else {
+		/* read all available locales */
+		FILE *fp = popen("locale -a", "r");
+		if (fp) {
+			while (!feof(fp) && !locale_found) {
+				char buf[256];
+				if (fgets(buf, sizeof(buf), fp) != NULL) {
+					/* remove newline */
+					buf[strlen(buf)-1] = '\0';
+					/* check for a working UTF-8 locale */
+					if (utf_8_locale(buf) &&
+					    (locale_found = setlocale(LC_CTYPE, buf))) {
+						strcpy(internal_locale, buf);
+					}
+				}
+			}
+		}
+
+		if (locale_found == NULL) {
+			fprintf(stderr, "Could not find a UTF-8 locale! Please enable en_US.UTF-8!\n");
+			exit(EXIT_FAILURE);
+		}
+		pclose(fp);
 	}
-  /*else { fprintf(stderr, "%s\n", ret); } */
 }
 #endif
 
diff -r 79f840e44c31 src/fileio.c
--- a/src/fileio.c	Mon Oct 23 23:20:43 2006 +0200
+++ b/src/fileio.c	Sat Feb 06 15:17:25 2010 +0100
@@ -27,6 +27,8 @@
 #ifdef MULTIBYTE
   #include <iconv.h>
   #include <locale.h>
+
+extern char internal_locale;
 #endif
 
 FILE *in, *out;
@@ -100,9 +102,9 @@
 		
 	output[strlen(output)] = '\0';
 
-	ret = setlocale(LC_CTYPE, INTERNAL_LOCALE);
-  if (ret==NULL) { 
-		fprintf(stderr, "setlocale failed with: %s\n\n", INTERNAL_LOCALE);
+	ret = setlocale(LC_CTYPE, internal_locale);
+	if (ret==NULL) { 
+		fprintf(stderr, "setlocale failed with: %s\n\n", internal_locale);
 		exit(1);
 	}
 	mbstowcs(converted_string, output, strlen(output));
-- 
Homepage: http://bhaak.dyndns.org/
E-mail: bh...@gmx.net  Jabber: bh...@jabber.org  ICQ: 23653421
Languages: Deutsch, English
(Not fluently: Français, Italiano, Latinum)

Reply via email to