[mw-devel] MW3 r885 - branches/portable/src

pwb at sucs.org pwb at sucs.org
Thu May 4 23:55:35 BST 2006


Author: pwb
Date: 2006-05-04 23:55:30 +0100 (Thu, 04 May 2006)
New Revision: 885

Modified:
   branches/portable/src/iconv.c
   branches/portable/src/iconv.h
Log:
merge trunk changes (conversion funcs as wrappers, UTF-8 conversion) into portable


Modified: branches/portable/src/iconv.c
===================================================================
--- branches/portable/src/iconv.c	2006-05-04 22:27:55 UTC (rev 884)
+++ branches/portable/src/iconv.c	2006-05-04 22:55:30 UTC (rev 885)
@@ -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,13 +26,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, "any2wcs: No source character set specified!\n");
 	return NULL;
     }
 
-    conv = iconv_open("WCHAR_T", local_charset);
+    conv = iconv_open("WCHAR_T", charset);
     if (conv == (iconv_t)-1) {
 	if (errno == EINVAL)
 	    fprintf(stderr, "Conversion not supported\n");
@@ -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: branches/portable/src/iconv.h
===================================================================
--- branches/portable/src/iconv.h	2006-05-04 22:27:55 UTC (rev 884)
+++ branches/portable/src/iconv.h	2006-05-04 22:55:30 UTC (rev 885)
@@ -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