Make serializeStructToString use an ostringstream
authorShadowNinja <shadowninja@minetest.net>
Sat, 15 Mar 2014 19:12:11 +0000 (15:12 -0400)
committerShadowNinja <shadowninja@minetest.net>
Sat, 15 Mar 2014 19:13:02 +0000 (15:13 -0400)
src/util/serialize.cpp
src/util/serialize.h

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