From 1f3fcd5c65fcf77cb2fe997a5b0ee63ab8ca8a31 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Wed, 21 Mar 2012 14:54:37 -0700 Subject: [PATCH] Missing from previous commits. Sorry Arthur. --- src/SerializedObject.cpp | 150 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 src/SerializedObject.cpp diff --git a/src/SerializedObject.cpp b/src/SerializedObject.cpp new file mode 100644 index 0000000000..e9805ed832 --- /dev/null +++ b/src/SerializedObject.cpp @@ -0,0 +1,150 @@ + +#include "SerializedObject.h" + +STUObject::STUObject(SOElement* elem, const char *name) : SerializedType(name) +{ + while(elem->e_id!=STI_DONE) + { + type.push_back(elem); + if( (elem->e_type==SOE_IFFLAG) || (elem->e_type==SOE_IFNFLAG) ) + giveObject(new STUObject(elem->e_name)); + else switch(elem->e_id) + { + case STI_UINT16: + giveObject(new STUInt16(elem->e_name)); + break; + case STI_UINT32: + giveObject(new STUInt32(elem->e_name)); + break; + case STI_UINT64: + giveObject(new STUInt64(elem->e_name)); + break; + case STI_HASH160: + giveObject(new STHash160(elem->e_name)); + break; + case STI_HASH256: + giveObject(new STHash256(elem->e_name)); + break; + case STI_VL: + giveObject(new STVariableLength(elem->e_name)); + break; + case STI_TL: + giveObject(new STTaggedList(elem->e_name)); + break; +#if 0 + case STI_ACCOUNT: // CHECKME: Should an account be variable length? + giveObject(new STVariableLength(elem->e_name)); + break; +#endif + default: throw(std::runtime_error("invalid transaction element")); + } + elem++; + } +} + +STUObject::STUObject(SOElement* elem, SerializerIterator& sit, const char *name) : SerializedType(name) +{ + int flags=-1; + while(elem->e_id!=STI_DONE) + { + type.push_back(elem); + bool done=false; + if(elem->e_type==SOE_IFFLAG) + { + assert(flags>=0); + if((flags&elem->e_flags)==0) done=true; + } + else if(elem->e_type==SOE_IFNFLAG) + { + assert(flags>=0); + if((flags&elem->e_flags)!=0) done=true; + } + else if(elem->e_type==SOE_FLAGS) + { + assert(elem->e_id==STI_UINT16); + flags=sit.get16(); + giveObject(new STUInt16(elem->e_name, flags)); + done=true; + } + if(!done) + { + switch(elem->e_id) + { + case STI_UINT16: + giveObject(STUInt16::construct(sit, elem->e_name)); + break; + case STI_UINT32: + giveObject(STUInt32::construct(sit, elem->e_name)); + break; + case STI_UINT64: + giveObject(STUInt64::construct(sit, elem->e_name)); + break; + case STI_HASH160: + giveObject(STHash160::construct(sit, elem->e_name)); + break; + case STI_HASH256: + giveObject(STHash256::construct(sit, elem->e_name)); + break; + case STI_VL: + giveObject(STVariableLength::construct(sit, elem->e_name)); + break; + case STI_TL: + giveObject(STTaggedList::construct(sit, elem->e_name)); + break; + #if 0 + case STI_ACCOUNT: // CHECKME: Should an account be variable length? + giveObject(STVariableLength::construct(sit, elem->e_name)); + break; + #endif + default: throw(std::runtime_error("invalid transaction element")); + } + } + elem++; + } +} + +std::string STUObject::getFullText() const +{ + std::string ret; + if(name!=NULL) + { + ret=name; + ret+=" = {"; + } + else ret="{"; + for(boost::ptr_vector::const_iterator it=data.begin(), end=data.end(); it!=end; ++it) + ret+=it->getFullText(); + ret+="}"; + return ret; +} + +int STUObject::getLength() const +{ + int ret=0; + for(boost::ptr_vector::const_iterator it=data.begin(), end=data.end(); it!=end; ++it) + ret+=it->getLength(); + return ret; +} + +void STUObject::add(Serializer& s) const +{ + for(boost::ptr_vector::const_iterator it=data.begin(), end=data.end(); it!=end; ++it) + it->add(s); +} + +std::string STUObject::getText() const +{ + std::string ret="{"; + bool first=false; + for(boost::ptr_vector::const_iterator it=data.begin(), end=data.end(); it!=end; ++it) + { + if(!first) + { + ret+=", "; + first=false; + } + ret+=it->getText(); + } + ret+="}"; + return ret; +}