Make serializeStructToString use an ostringstream

This commit is contained in:
ShadowNinja 2014-03-15 15:12:11 -04:00
parent 93729b09d5
commit 23be6450a1
2 changed files with 27 additions and 44 deletions

@ -385,23 +385,20 @@ fail:
} }
bool serializeStructToString(std::string *outstr, bool serializeStructToString(std::string *out,
std::string format, void *value) std::string format, void *value)
{ {
char sbuf[2048]; std::ostringstream os;
int sbuflen = sizeof(sbuf) - 1;
sbuf[sbuflen] = 0;
std::string str; std::string str;
int pos = 0;
size_t fpos;
char *f; char *f;
size_t strpos;
char *bufpos = (char *)value; char *bufpos = (char *) value;
char *fmtpos, *fmt = &format[0]; char *fmtpos, *fmt = &format[0];
while ((f = strtok_r(fmt, ",", &fmtpos))) { while ((f = strtok_r(fmt, ",", &fmtpos))) {
fmt = NULL; fmt = NULL;
bool is_unsigned = false; bool is_unsigned = false;
int width = 0, nprinted = 0; int width = 0;
char valtype = *f; char valtype = *f;
width = (int)strtol(f + 1, &f, 10); width = (int)strtol(f + 1, &f, 10);
@ -415,79 +412,65 @@ bool serializeStructToString(std::string *outstr,
case 'i': case 'i':
if (width == 16) { if (width == 16) {
bufpos += PADDING(bufpos, u16); bufpos += PADDING(bufpos, u16);
nprinted = snprintf(sbuf + pos, sbuflen, os << *((u16 *) bufpos);
is_unsigned ? "%u, " : "%d, ",
*((u16 *)bufpos));
bufpos += sizeof(u16); bufpos += sizeof(u16);
} else if (width == 32) { } else if (width == 32) {
bufpos += PADDING(bufpos, u32); bufpos += PADDING(bufpos, u32);
nprinted = snprintf(sbuf + pos, sbuflen, os << *((u32 *) bufpos);
is_unsigned ? "%u, " : "%d, ",
*((u32 *)bufpos));
bufpos += sizeof(u32); bufpos += sizeof(u32);
} else if (width == 64) { } else if (width == 64) {
bufpos += PADDING(bufpos, u64); bufpos += PADDING(bufpos, u64);
nprinted = snprintf(sbuf + pos, sbuflen, os << *((u64 *) bufpos);
is_unsigned ? "%llu, " : "%lli, ",
(unsigned long long)*((u64 *)bufpos));
bufpos += sizeof(u64); bufpos += sizeof(u64);
} }
break; break;
case 'b': case 'b':
bufpos += PADDING(bufpos, bool); bufpos += PADDING(bufpos, bool);
nprinted = snprintf(sbuf + pos, sbuflen, "%s, ", os << std::boolalpha << *((bool *) bufpos);
*((bool *)bufpos) ? "true" : "false");
bufpos += sizeof(bool); bufpos += sizeof(bool);
break; break;
case 'f': case 'f':
bufpos += PADDING(bufpos, float); bufpos += PADDING(bufpos, float);
nprinted = snprintf(sbuf + pos, sbuflen, "%f, ", os << *((float *) bufpos);
*((float *)bufpos));
bufpos += sizeof(float); bufpos += sizeof(float);
break; break;
case 's': case 's':
bufpos += PADDING(bufpos, std::string *); bufpos += PADDING(bufpos, std::string *);
str = **((std::string **)bufpos); str = **((std::string **) bufpos);
fpos = 0; strpos = 0;
while ((fpos = str.find('"', fpos)) != std::string::npos) { while ((strpos = str.find('"', strpos)) != std::string::npos) {
str.insert(fpos, 1, '\\'); str.insert(strpos, 1, '\\');
fpos += 2; strpos += 2;
} }
nprinted = snprintf(sbuf + pos, sbuflen, "\"%s\", ", os << str;
(*((std::string **)bufpos))->c_str());
bufpos += sizeof(std::string *); bufpos += sizeof(std::string *);
break; break;
case 'v': case 'v':
if (width == 2) { if (width == 2) {
bufpos += PADDING(bufpos, v2f); bufpos += PADDING(bufpos, v2f);
v2f *v = (v2f *)bufpos; v2f *v = (v2f *) bufpos;
nprinted = snprintf(sbuf + pos, sbuflen, os << '(' << v->X << ", " << v->Y << ')';
"(%f, %f), ", v->X, v->Y);
bufpos += sizeof(v2f); bufpos += sizeof(v2f);
} else { } else {
bufpos += PADDING(bufpos, v3f); bufpos += PADDING(bufpos, v3f);
v3f *v = (v3f *)bufpos; v3f *v = (v3f *) bufpos;
nprinted = snprintf(sbuf + pos, sbuflen, os << '(' << v->X << ", " << v->Y << ", " << v->Z << ')';
"(%f, %f, %f), ", v->X, v->Y, v->Z);
bufpos += sizeof(v3f); bufpos += sizeof(v3f);
} }
break; break;
default: default:
return false; return false;
} }
if (nprinted < 0) //error, buffer too small os << ", ";
return false;
pos += nprinted;
sbuflen -= nprinted;
} }
*out = os.str();
// this is to trim off the trailing comma // Trim off the trailing comma and space
if (pos >= 2) if (out->size() >= 2) {
sbuf[pos - 2] = 0; out->resize(out->size() - 2);
}
*outstr = sbuf;
return true; return true;
} }

@ -403,7 +403,7 @@ std::string deSerializeJsonString(std::istream &is);
// Creates a string containing comma delimited values of a struct whose layout is // Creates a string containing comma delimited values of a struct whose layout is
// described by the parameter format // described by the parameter format
bool serializeStructToString(std::string *outstr, bool serializeStructToString(std::string *out,
std::string format, void *value); std::string format, void *value);
// Reads a comma delimited string of values into a struct whose layout is // Reads a comma delimited string of values into a struct whose layout is