Obj JsonToGap(const gmp_value& v)
{ if (v.is<picojson::null>()) { return Fail;
} elseif (v.is<bool>()) { if(v.get<bool>()) returnTrue; else returnFalse;
} elseif (v.is<gap_val>()) { return v.get<gap_val>().obj;
} elseif (v.is<std::string>()) {
Obj str; constchar* c_str = v.get<std::string>().c_str(); Int len = v.get<std::string>().size();
str = NEW_STRING(len);
memcpy(CHARS_STRING(str), c_str, len); return str;
} elseif (v.is<gmp_value::array>()) { const gmp_value::array& a = v.get<gmp_value::array>();
Obj list = NEW_PLIST(T_PLIST_DENSE, a.size());
SET_LEN_PLIST(list, a.size()); for(int i = 1; i <= a.size(); ++i)
{
Obj val = JsonToGap(a[i-1]);
SET_ELM_PLIST(list, i, val);
CHANGED_BAG(list);
} return list;
} elseif (v.is<gmp_value::object>()) { const gmp_value::object& o = v.get<gmp_value::object>();
Obj rec = NEW_PREC(0); for (gmp_value::object::const_iterator i = o.begin(); i != o.end(); ++i) {
Obj res = JsonToGap(i->second);
AssPRec(rec, RNamName(i->first.c_str()), res);
CHANGED_BAG(rec);
} return rec;
} return Fail;
}
// Add function prototype missing from string.h extern Obj CopyToStringRep(Obj string);
staticInt numberOfBytes(UInt c)
{ if (c < 128) return 1; if (c < 224) return 2; if (c < 240) return 3; return 4;
}
staticInt getChar(Obj list, Int pos)
{
Obj c = ELM_LIST(list, pos); if (c == NULL) return 0; else return *((UChar *)ADDR_OBJ(c));
}
staticInt getUTF8Char(Obj list, Int * basepos)
{ Int pos = *basepos;
UInt val = getChar(list, pos);
UInt singlebyte_val = val; Int len = numberOfBytes(val);
pos++;
if (len == 1) {
*basepos = pos; return val;
}
switch (len) { case 2:
val = val & 0x3F; if (val & 0x20) goto invalid; break; case 3:
val = val & 0x1F; if (val & 0x10) goto invalid; break; case 4:
val = val & 0x0F; if (val & 0x08) goto invalid; break; default:
abort();
}
val = val & 0x3F;
for (Int i = 1; i < len; ++i) {
UInt c = getChar(list, pos); if ((c & 0xC0) != 0x80) goto invalid;
val = (val << 6) | (c & 0x3F);
pos++;
}
// Too high if (val > 0x10ffff) goto invalid;
// UTF-16 Surrogate pair if (val >= 0xd800 && val <= 0xdfff) goto invalid;
*basepos = pos; return val;
// Hope this is Latin-1
invalid:
*basepos = *basepos + 1; return singlebyte_val;
}
// Call this at the start
ConvString(string);
AppendCStr(string, "[", 1); for(int i = 1; i <= len; ++i) { if(i != 1) {
AppendCStr(string, ",", 1);
}
Obj val = ELM_LIST(list, i); if(IS_INTOBJ(val)) {
snprintf(buf, sizeof(buf), "%ld", INT_INTOBJ(val));
AppendCStr(string, buf, strlen(buf));
} elseif(IS_LIST(val) && !(IS_STRING(val))) {
FuncGAP_LIST_TO_JSON_STRING(self, string, stream, val);
} else {
CALL_2ARGS(_GapToJsonStreamInternal, stream, val); // Convert back in case this call modified the string
ConvString(string);
}
}
AppendCStr(string, "]", 1);
return 0;
}
// WARNING: This class is only complete enough to work with // picojson's iterator support. struct GapStreamToInputIterator
{
Obj stream; enum State {
notread, failed, cached
};
State state; char store;
// making an object of this type will ensure when the scope is exited // we clean up any GAP objects we cached struct CleanupCacheGuard
{
~CleanupCacheGuard()
{ callGAPFunction(ClearGAPCacheFunction); }
};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.