[mw-devel] MW3 r883 - trunk/src

pwb at sucs.org pwb at sucs.org
Thu May 4 23:09:17 BST 2006


Author: pwb
Date: 2006-05-04 23:09:16 +0100 (Thu, 04 May 2006)
New Revision: 883

Modified:
   trunk/src/iconv.c
   trunk/src/iconv.h
Log:
Turned the string conversion functions into wrappers around more generic functions, and added a couple more wrappers to deal with UTF-8 strings. Also check UTF-8 is available to make sure these work.

This is in preparation for changing the IPC to use UTF-8. wchar_t would probably be preferable except that its representation is allowed to be locale-dependent (though it happens to be UCS-4 for glibc).


Modified: trunk/src/iconv.c
===================================================================
--- trunk/src/iconv.c	2006-04-30 15:35:27 UTC (rev 882)
+++ trunk/src/iconv.c	2006-05-04 22:09:16 UTC (rev 883)
@@ -16,9 +16,9 @@
 
 static char * local_charset = NULL;
 
-/* convert string in local encoding to wide string */
+/* convert string in given encoding to wide string */
 wchar_t *
-local2wcs(char * s)
+any2wcs(char * s, char * charset)
 {
     char * local, * localcpy;
     char * wcs; /* needs to be char * for passing to iconv */
@@ -26,9 +26,8 @@
     size_t localbytesleft, wcsbytesleft;
     iconv_t conv;
 
-    if (!local_charset || !*local_charset) {
-	/* oops, this is a bug */
-	fprintf(stderr, "No local character set specified!\n");
+    if (!charset || !*charset) {
+	fprintf(stderr, "any2wcs: No source character set specified!\n");
 	return NULL;
     }
 
@@ -75,9 +74,9 @@
     return wcscpy;
 }
 
-/* convert wide string to local encoding */
+/* convert wide string to string in given encoding */
 char *
-wcs2local(wchar_t * ws)
+wcs2any(wchar_t * ws, char * charset)
 {
     char * wcs; /* needs to be char * for passing to iconv */
     wchar_t * wcscpy; /* the string as itself */
@@ -85,13 +84,12 @@
     size_t localbytesleft, wcsbytesleft;
     iconv_t conv;
 
-    if (!local_charset || !*local_charset) {
-	/* oops, this is a bug */
-	fprintf(stderr, "No local character set specified!\n");
+    if (!charset || !*charset) {
+	fprintf(stderr, "No target character set specified!\n");
 	return NULL;
     }
 
-    conv = iconv_open(local_charset, "WCHAR_T");
+    conv = iconv_open(charset, "WCHAR_T");
     if (conv == (iconv_t)-1) {
 	if (errno == EINVAL)
 	    fprintf(stderr, "Conversion not supported\n");
@@ -133,6 +131,34 @@
     return localcpy;
 }
 
+/* convert string in local encoding to wide string */
+wchar_t *
+local2wcs(char * s)
+{
+    return any2wcs(s, local_charset);
+}
+
+/* convert wide string to local encoding */
+char *
+wcs2local(wchar_t * ws)
+{
+    return wcs2any(ws, local_charset);
+}
+
+/* convert UTF-8 encoded string to wide string */
+wchar_t *
+utf82wcs(char * s)
+{
+    return any2wcs(s, "UTF-8");
+}
+
+/* convert wide string to UTF-8 encoded string */
+char *
+wcs2utf8(wchar_t * ws)
+{
+    return wcs2any(ws, "UTF-8");
+}
+
 /* set local charset independently of locale */
 int set_local_charset(char * set)
 {
@@ -215,6 +241,14 @@
 	if (locale_mesgs(locale)) {
 	    fprintf(stderr, "Please change your locale environment (e.g. LANG) to something which this system supports.\n");
 	    exit(1);
+	} else {
+	    /* check this environment supports UTF-8 */
+	    iconv_t conv = iconv_open("WCHAR_T", "UTF-8");
+	    if (conv == (iconv_t)-1) {
+		fprintf(stderr, "This environment doesn't seem to support UTF-8. Milliways requires UTF-8 support to work.\n");
+		exit(1);
+	    } else
+		iconv_close(conv);
 	}
 }
 

Modified: trunk/src/iconv.h
===================================================================
--- trunk/src/iconv.h	2006-04-30 15:35:27 UTC (rev 882)
+++ trunk/src/iconv.h	2006-05-04 22:09:16 UTC (rev 883)
@@ -1,3 +1,11 @@
+#include <wchar.h>
+
+/* convert string in given encoding to wide string */
+wchar_t * any2wcs(char * s, char * charset);
+
+/* convert wide string to string in given encoding */
+char * wcs2any(wchar_t * ws, char * charset);
+
 /* convert string in local encoding to wide string */
 extern wchar_t * local2wcs(char * s);
 
@@ -4,6 +12,12 @@
 /* convert wide string to local encoding */
 extern char * wcs2local(wchar_t * ws);
 
+/* convert UTF-8 encoded string to wide string */
+wchar_t * utf82wcs(char * s);
+
+/* convert wide string to UTF-8 encoded string */
+char * wcs2utf8(wchar_t * ws)
+
 /* set local charset independently of locale */
 extern int set_local_charset(char * set);
 




More information about the mw-devel mailing list