+std::string serializeJsonStringIfNeeded(const std::string &s)
+{
+ for (size_t i = 0; i < s.size(); ++i) {
+ if (s[i] <= 0x1f || s[i] >= 0x7f || s[i] == ' ' || s[i] == '\"')
+ return serializeJsonString(s);
+ }
+ return s;
+}
+
+std::string deSerializeJsonStringIfNeeded(std::istream &is)
+{
+ std::ostringstream tmp_os;
+ bool expect_initial_quote = true;
+ bool is_json = false;
+ bool was_backslash = false;
+ for (;;) {
+ char c = is.get();
+ if (is.eof())
+ break;
+
+ if (expect_initial_quote && c == '"') {
+ tmp_os << c;
+ is_json = true;
+ } else if(is_json) {
+ tmp_os << c;
+ if (was_backslash)
+ was_backslash = false;
+ else if (c == '\\')
+ was_backslash = true;
+ else if (c == '"')
+ break; // Found end of string
+ } else {
+ if (c == ' ') {
+ // Found end of word
+ is.unget();
+ break;
+ } else {
+ tmp_os << c;
+ }
+ }
+ expect_initial_quote = false;
+ }
+ if (is_json) {
+ std::istringstream tmp_is(tmp_os.str(), std::ios::binary);
+ return deSerializeJsonString(tmp_is);
+ } else
+ return tmp_os.str();
+}
+
+////
+//// String/Struct conversions
+////
+
+bool deSerializeStringToStruct(std::string valstr,
+ std::string format, void *out, size_t olen)
+{
+ size_t len = olen;
+ std::vector<std::string *> strs_alloced;
+ std::string *str;
+ char *f, *snext;
+ size_t pos;
+
+ char *s = &valstr[0];
+ char *buf = new char[len];
+ char *bufpos = buf;
+
+ char *fmtpos, *fmt = &format[0];
+ while ((f = strtok_r(fmt, ",", &fmtpos)) && s) {
+ fmt = NULL;
+
+ bool is_unsigned = false;
+ int width = 0;
+ char valtype = *f;
+
+ width = (int)strtol(f + 1, &f, 10);
+ if (width && valtype == 's')
+ valtype = 'i';
+
+ switch (valtype) {
+ case 'u':
+ is_unsigned = true;
+ /* FALLTHROUGH */
+ case 'i':
+ if (width == 16) {
+ bufpos += PADDING(bufpos, u16);
+ if ((bufpos - buf) + sizeof(u16) <= len) {
+ if (is_unsigned)
+ *(u16 *)bufpos = (u16)strtoul(s, &s, 10);
+ else
+ *(s16 *)bufpos = (s16)strtol(s, &s, 10);
+ }
+ bufpos += sizeof(u16);
+ } else if (width == 32) {
+ bufpos += PADDING(bufpos, u32);
+ if ((bufpos - buf) + sizeof(u32) <= len) {
+ if (is_unsigned)
+ *(u32 *)bufpos = (u32)strtoul(s, &s, 10);
+ else
+ *(s32 *)bufpos = (s32)strtol(s, &s, 10);
+ }
+ bufpos += sizeof(u32);
+ } else if (width == 64) {
+ bufpos += PADDING(bufpos, u64);
+ if ((bufpos - buf) + sizeof(u64) <= len) {
+ if (is_unsigned)
+ *(u64 *)bufpos = (u64)strtoull(s, &s, 10);
+ else
+ *(s64 *)bufpos = (s64)strtoll(s, &s, 10);
+ }
+ bufpos += sizeof(u64);
+ }
+ s = strchr(s, ',');
+ break;
+ case 'b':
+ snext = strchr(s, ',');
+ if (snext)
+ *snext++ = 0;
+
+ bufpos += PADDING(bufpos, bool);
+ if ((bufpos - buf) + sizeof(bool) <= len)
+ *(bool *)bufpos = is_yes(std::string(s));
+ bufpos += sizeof(bool);
+
+ s = snext;
+ break;
+ case 'f':
+ bufpos += PADDING(bufpos, float);
+ if ((bufpos - buf) + sizeof(float) <= len)
+ *(float *)bufpos = strtof(s, &s);
+ bufpos += sizeof(float);
+
+ s = strchr(s, ',');
+ break;
+ case 's':
+ while (*s == ' ' || *s == '\t')
+ s++;
+ if (*s++ != '"') //error, expected string
+ goto fail;
+ snext = s;
+
+ while (snext[0] && !(snext[-1] != '\\' && snext[0] == '"'))
+ snext++;
+ *snext++ = 0;
+
+ bufpos += PADDING(bufpos, std::string *);
+
+ str = new std::string(s);
+ pos = 0;
+ while ((pos = str->find("\\\"", pos)) != std::string::npos)
+ str->erase(pos, 1);
+
+ if ((bufpos - buf) + sizeof(std::string *) <= len)
+ *(std::string **)bufpos = str;
+ bufpos += sizeof(std::string *);
+ strs_alloced.push_back(str);
+
+ s = *snext ? snext + 1 : NULL;
+ break;
+ case 'v':
+ while (*s == ' ' || *s == '\t')
+ s++;
+ if (*s++ != '(') //error, expected vector
+ goto fail;