iconv() is returning strange results and does not appear to convert anything

This commit is contained in:
Josh Holtrop 2010-03-29 20:46:44 -04:00
parent 3097feb9fb
commit f0af347ad8

View File

@ -1,6 +1,7 @@
#include "serialize.h" #include "serialize.h"
#include <string.h> #include <string.h>
#include <iconv.h>
using namespace std; using namespace std;
refptr< vector<unichar_t> > deserialize(const char * encoding, istream & in) refptr< vector<unichar_t> > deserialize(const char * encoding, istream & in)
@ -11,7 +12,7 @@ refptr< vector<unichar_t> > deserialize(const char * encoding, istream & in)
char * inbuf_ptr = (char *) &inbuf[0]; char * inbuf_ptr = (char *) &inbuf[0];
unichar_t outbuf[buf_size]; unichar_t outbuf[buf_size];
char * outbuf_ptr; char * outbuf_ptr;
size_t bytes_converted, inbytes = 0, outbytes; size_t chars_converted, inbytesleft = 0, outbytesleft;
refptr< vector<unichar_t> > ucs = new vector<unichar_t>(); refptr< vector<unichar_t> > ucs = new vector<unichar_t>();
iconv_t cd = iconv_open(encoding, "UTF-32"); iconv_t cd = iconv_open(encoding, "UTF-32");
@ -23,25 +24,30 @@ refptr< vector<unichar_t> > deserialize(const char * encoding, istream & in)
for (;;) for (;;)
{ {
in.read(inbuf_ptr, sizeof(inbuf) - inbytes); in.read(inbuf_ptr, sizeof(inbuf) - inbytesleft);
num_read = in.gcount(); num_read = in.gcount();
cout << "num_read: " << num_read << endl;
if (num_read <= 0) if (num_read <= 0)
break; break;
inbytesleft += num_read;
outbuf_ptr = (char *) &outbuf[0]; outbuf_ptr = (char *) &outbuf[0];
outbytes = sizeof(outbuf); outbytesleft = sizeof(outbuf);
bytes_converted = iconv(cd, &inbuf_ptr, &inbytes, cout << "before inbytesleft: " << inbytesleft << ", outbytesleft: " << outbytesleft << endl;
&outbuf_ptr, &outbytes); chars_converted = iconv(cd, &inbuf_ptr, &inbytesleft,
if (inbytes > 0) &outbuf_ptr, &outbytesleft);
cout << "chars_converted: " << chars_converted << endl;
cout << "after inbytesleft: " << inbytesleft << ", outbytesleft: " << outbytesleft << endl;
if (inbytesleft > 0)
{ {
memmove(&inbuf[0], inbuf_ptr, inbytes); memmove(&inbuf[0], inbuf_ptr, inbytesleft);
} }
inbuf_ptr = ((char *) &inbuf[0]) + inbytes; inbuf_ptr = ((char *) &inbuf[0]) + inbytesleft;
for (int i = 0; i < (bytes_converted / sizeof(outbuf[0])); i++) for (int i = 0;
i < ((sizeof(outbuf) - outbytesleft) / sizeof(outbuf[0]));
i++)
{ {
ucs->push_back(outbuf[i]); ucs->push_back(outbuf[i]);
} }
if (bytes_converted & 0x3)
cerr << "Warning: bytes_converted = " << bytes_converted << endl;
if (in.eof()) if (in.eof())
break; break;
} }