diff src/vbuf.c @ 142:2189a6b8134e

improve character set handling - don't try to convert utf-8 to single byte for fields that were not originally unicode. if the conversion fails, leave the data in utf-8.
author Carl Byington <carl@five-ten-sg.com>
date Mon, 23 Feb 2009 20:40:51 -0800
parents fc11b1d1ad34
children cda7c812ec01
line wrap: on
line diff
--- a/src/vbuf.c	Sat Feb 14 11:02:37 2009 -0800
+++ b/src/vbuf.c	Mon Feb 23 20:40:51 2009 -0800
@@ -43,6 +43,7 @@
 static int unicode_up = 0;
 static iconv_t i16to8;
 static const char *target_charset = NULL;
+static int         target_open = 0;
 static iconv_t    i8totarget;
 
 
@@ -61,10 +62,11 @@
 void unicode_close()
 {
     iconv_close(i16to8);
-    if (target_charset) {
+    if (target_open) {
         iconv_close(i8totarget);
         free((char *)target_charset);
         target_charset = NULL;
+        target_open    = 0;
     }
     unicode_up = 0;
 }
@@ -130,19 +132,23 @@
     size_t outbytesleft = 0;
     char *outbuf        = NULL;
 
-    if (!target_charset || (target_charset && strcasecmp(target_charset, charset))) {
-        if (target_charset) {
+    if (!target_charset || strcasecmp(target_charset, charset)) {
+        if (target_open) {
             iconv_close(i8totarget);
             free((char *)target_charset);
         }
         target_charset = strdup(charset);
+        target_open    = 1;
         i8totarget = iconv_open(target_charset, "UTF-8");
         if (i8totarget == (iconv_t)-1) {
+            target_open = 0;
             fprintf(stderr, "Couldn't open iconv descriptor for UTF-8 to %s.\n", target_charset);
             return (size_t)-1;
         }
     }
 
+    if (!target_open) return (size_t)-1;    // previous failure to open the target
+
     if (2 > dest->blen) vbresize(dest, 2);
     dest->dlen = 0;