Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

TextMsgEvent.cc

Go to the documentation of this file.
00001 #include "TextMsgEvent.h"
00002 #include <sstream>
00003 #include <libxml/tree.h>
00004 
00005 const EventBase::classTypeID_t TextMsgEvent::autoRegisterTextMsgEvent=getTypeRegistry().registerType<TextMsgEvent>(makeClassTypeID("TXTM"));
00006 
00007 std::string
00008 TextMsgEvent::getDescription(bool showTypeSpecific/*=true*/, unsigned int verbosity/*=0*/) const {
00009   if(!showTypeSpecific)
00010     return EventBase::getDescription(showTypeSpecific,verbosity);
00011   std::ostringstream logdata;
00012   logdata << EventBase::getDescription(showTypeSpecific,verbosity) << '\t' << _text;
00013   return logdata.str();
00014 }
00015 
00016 unsigned int
00017 TextMsgEvent::getBinSize() const {
00018   unsigned int used=EventBase::getBinSize();
00019   if(saveFormat==XML)
00020     return used; //if using XML, the XMLLoadSave::getBinSize (called by EventBase::getBinSize) is all we need
00021   //otherwise need to add our own fields
00022   used+=creatorSize("EventBase::TextMsgEvent");
00023   used+=getSerializedSize(_text);
00024   //used+=sizeof(_token);
00025   return used;
00026 }
00027 
00028 unsigned int
00029 TextMsgEvent::loadBinaryBuffer(const char buf[], unsigned int len) {
00030   unsigned int origlen=len;
00031   if(!checkInc(EventBase::loadBinaryBuffer(buf,len),buf,len)) return 0;
00032   if(!checkCreatorInc("EventBase::TextMsgEvent",buf,len,true)) return 0;
00033   if(!decodeInc(_text,buf,len)) return 0;
00034   //if(!decodeInc(_token,buf,len)) return 0;
00035   //len-=used; buf+=used;
00036   return origlen-len; 
00037 }
00038 
00039 unsigned int
00040 TextMsgEvent::saveBinaryBuffer(char buf[], unsigned int len) const {
00041   unsigned int origlen=len;
00042   if(!checkInc(EventBase::saveBinaryBuffer(buf,len),buf,len)) return 0;
00043   if(!saveCreatorInc("EventBase::TextMsgEvent",buf,len)) return 0;
00044   if(!encodeInc(_text,buf,len)) return 0;
00045   //if(!encodeInc(_token,buf,len)) return 0;
00046   return origlen-len;
00047 }
00048 
00049 void TextMsgEvent::loadXML(xmlNode* node) {
00050   if(node==NULL)
00051     return;
00052   
00053   EventBase::loadXML(node);
00054   
00055   for(xmlNode* cur = skipToElement(node->children); cur!=NULL; cur = skipToElement(cur->next)) {
00056     if(xmlStrcmp(cur->name, (const xmlChar *)"param"))
00057       continue;
00058     
00059     xmlChar * name = xmlGetProp(cur,(const xmlChar*)"name");
00060     if(name==NULL)
00061       throw bad_format(cur,"property missing name");
00062     
00063     xmlChar * val = xmlGetProp(cur,(const xmlChar*)"value");
00064     if(val==NULL)
00065       throw bad_format(cur,"property missing value");
00066     
00067     if(xmlStrcmp(name, (const xmlChar *)"text")==0)
00068       _text=(const char*)val;
00069     
00070     xmlFree(val);
00071     xmlFree(name);
00072   }
00073 }
00074 
00075 void TextMsgEvent::saveXML(xmlNode * node) const {
00076   if(node==NULL)
00077     return;
00078   EventBase::saveXML(node);
00079   
00080   //clear old params first
00081   for(xmlNode* cur = skipToElement(node->children); cur!=NULL; ) {
00082     if(xmlStrcmp(cur->name, (const xmlChar *)"param")==0) {
00083       xmlUnlinkNode(cur);
00084       xmlFreeNode(cur);
00085       cur = skipToElement(node->children); //restart the search (boo)
00086     } else
00087       cur = skipToElement(cur->next);
00088   }
00089   
00090   xmlNode* cur=xmlNewChild(node,NULL,(const xmlChar*)"param",NULL);
00091   if(cur==NULL)
00092     throw bad_format(node,"Error: TextMsgEvent xml error on saving param");
00093   xmlSetProp(cur,(const xmlChar*)"name",(const xmlChar*)"text");
00094   xmlSetProp(cur,(const xmlChar*)"value",(const xmlChar*)_text.c_str());
00095 }
00096 
00097 /*! @file
00098  * @brief Implements TextMsgEvent, which extends EventBase to also include actual message text
00099  * @author ejt (Creator)
00100  */

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:51 2016 by Doxygen 1.6.3