comparison 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
comparison
equal deleted inserted replaced
141:fd4297884319 142:2189a6b8134e
41 // UTF8 <-> UTF16 <-> ISO8859 Character set conversion functions and (ack) their globals 41 // UTF8 <-> UTF16 <-> ISO8859 Character set conversion functions and (ack) their globals
42 42
43 static int unicode_up = 0; 43 static int unicode_up = 0;
44 static iconv_t i16to8; 44 static iconv_t i16to8;
45 static const char *target_charset = NULL; 45 static const char *target_charset = NULL;
46 static int target_open = 0;
46 static iconv_t i8totarget; 47 static iconv_t i8totarget;
47 48
48 49
49 void unicode_init() 50 void unicode_init()
50 { 51 {
59 60
60 61
61 void unicode_close() 62 void unicode_close()
62 { 63 {
63 iconv_close(i16to8); 64 iconv_close(i16to8);
64 if (target_charset) { 65 if (target_open) {
65 iconv_close(i8totarget); 66 iconv_close(i8totarget);
66 free((char *)target_charset); 67 free((char *)target_charset);
67 target_charset = NULL; 68 target_charset = NULL;
69 target_open = 0;
68 } 70 }
69 unicode_up = 0; 71 unicode_up = 0;
70 } 72 }
71 73
72 74
128 size_t inbytesleft = iblen; 130 size_t inbytesleft = iblen;
129 size_t icresult = (size_t)-1; 131 size_t icresult = (size_t)-1;
130 size_t outbytesleft = 0; 132 size_t outbytesleft = 0;
131 char *outbuf = NULL; 133 char *outbuf = NULL;
132 134
133 if (!target_charset || (target_charset && strcasecmp(target_charset, charset))) { 135 if (!target_charset || strcasecmp(target_charset, charset)) {
134 if (target_charset) { 136 if (target_open) {
135 iconv_close(i8totarget); 137 iconv_close(i8totarget);
136 free((char *)target_charset); 138 free((char *)target_charset);
137 } 139 }
138 target_charset = strdup(charset); 140 target_charset = strdup(charset);
141 target_open = 1;
139 i8totarget = iconv_open(target_charset, "UTF-8"); 142 i8totarget = iconv_open(target_charset, "UTF-8");
140 if (i8totarget == (iconv_t)-1) { 143 if (i8totarget == (iconv_t)-1) {
144 target_open = 0;
141 fprintf(stderr, "Couldn't open iconv descriptor for UTF-8 to %s.\n", target_charset); 145 fprintf(stderr, "Couldn't open iconv descriptor for UTF-8 to %s.\n", target_charset);
142 return (size_t)-1; 146 return (size_t)-1;
143 } 147 }
144 } 148 }
149
150 if (!target_open) return (size_t)-1; // previous failure to open the target
145 151
146 if (2 > dest->blen) vbresize(dest, 2); 152 if (2 > dest->blen) vbresize(dest, 2);
147 dest->dlen = 0; 153 dest->dlen = 0;
148 154
149 do { 155 do {