I'm trying to parse a binary file where some of its strings are in CP437 encoding. I figure iconv would be suitable because it's cross platform and CP437 to UTF-8 isn't exactly widening characters, but rather decoding them. I haven't found a suitable solution on Google search anywhere, so I winged it. Sure enough, there were problems. The output got truncated to about 2 bytes. What am I doing wrong in this?
Code:
std::string ReadStr(IOStream *ios)
{
static iconv_t cnv = iconv_open("UTF-8", "CP437"); // might as well last the whole execution of the parser program
std::vector<char> s, s2; // not sure if a reserved std::string would work here or not
while (true) // strings in this format are null terminated, which by then we'll return a value
{
char c = 0;
ios->Read(&c, 1, 1); // buffer, size, count
if (c == 0)
{
auto insz = s.size();
auto *inp = &s[0];
auto outsz = insz * 2; // in case there is any extra data in the output
auto *outp = &s2[0];
s2.reserve(outsz);
iconv(cnv, &inp, &insz, &outp, &outsz);
s2.shrink_to_fit(); // don't need extra memory anymore
return std::string(s2.begin(), s2.end()); // again, not sure if vector is needed here?
}
else
s.push_back(c);
}
}