From ab7e0b08fa8c31cf1d468ab4b3c660e2b1836811 Mon Sep 17 00:00:00 2001 From: Fishbone Date: Sun, 2 Jun 2013 16:19:21 +0000 Subject: Added WhatsApp-protocol git-svn-id: http://svn.miranda-ng.org/main/trunk@4861 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- .../WhatsApp/src/WhatsAPI++/ProtocolTreeNode.cpp | 135 +++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 protocols/WhatsApp/src/WhatsAPI++/ProtocolTreeNode.cpp (limited to 'protocols/WhatsApp/src/WhatsAPI++/ProtocolTreeNode.cpp') diff --git a/protocols/WhatsApp/src/WhatsAPI++/ProtocolTreeNode.cpp b/protocols/WhatsApp/src/WhatsAPI++/ProtocolTreeNode.cpp new file mode 100644 index 0000000000..0ed60edbb8 --- /dev/null +++ b/protocols/WhatsApp/src/WhatsAPI++/ProtocolTreeNode.cpp @@ -0,0 +1,135 @@ +/* + * ProtocolTreeNode.cpp + * + * Created on: 26/06/2012 + * Author: Antonio + */ +#include "stdafx.h" +#include "WAException.h" +#include "ProtocolTreeNode.h" + +ProtocolTreeNode::ProtocolTreeNode(const string& tag, map *attributes, vector* data, vector *children) { + this->tag = tag; + this->data = data; + this->attributes = attributes; + this->children = children; +} + +ProtocolTreeNode::ProtocolTreeNode(const string& tag, map *attributes, ProtocolTreeNode* child) { + this->tag = tag; + this->data = NULL; + this->attributes = attributes; + this->children = new std::vector(1); + (*this->children)[0] = child; +} + +ProtocolTreeNode::~ProtocolTreeNode() { + if (this->attributes != NULL) + delete this->attributes; + if (this->children != NULL) { + for (size_t i = 0; i < this->children->size(); i++) + if (this->children->at(i) != NULL) + delete this->children->at(i); + delete this->children; + } + if (this->data != NULL) + delete data; +} + + +string ProtocolTreeNode::toString() { + string out; + out += "<" + this->tag; + if (this->attributes != NULL) { + map::iterator ii; + for (ii = attributes->begin(); ii != attributes->end(); ii++) + out += "" + ii->first + "=\"" + ii->second + "\""; + } + out += ">\n"; + std::string* data = getDataAsString(); + out += (this->data != NULL? *data:""); + delete data; + + if (this->children != NULL) { + vector::iterator ii; + + for (ii = children->begin(); ii != children->end(); ii++) + out += (*ii)->toString(); + } + + out += "tag + ">\n"; + + return out; +} + +ProtocolTreeNode* ProtocolTreeNode::getChild(const string& id) { + if (this->children == NULL || this->children->size() == 0) + return NULL; + + for (std::size_t i = 0; i < this->children->size(); i++) + if (id.compare((*children)[i]->tag) == 0) + return (*children)[i]; + + return NULL; +} + +ProtocolTreeNode* ProtocolTreeNode::getChild(size_t id) { + if (this->children == NULL || this->children->size() == 0) + return NULL; + + if (children->size() > id) + return (*children)[id]; + + return NULL; +} + +string* ProtocolTreeNode::getAttributeValue(const string& attribute) { + if (this->attributes == NULL) + return NULL; + + map::iterator it = attributes->find(attribute); + if (it == attributes->end()) + return NULL; + + return &it->second; +} + +vector* ProtocolTreeNode::getAllChildren() { + vector* ret = new vector(); + + if (this->children == NULL) + return ret; + + return this->children; +} + +std::string* ProtocolTreeNode::getDataAsString() { + if (this->data == NULL) + return NULL; + return new std::string(this->data->begin(), this->data->end()); +} + +vector* ProtocolTreeNode::getAllChildren(const string& tag) { + vector* ret = new vector(); + + if (this->children == NULL) + return ret; + + for (size_t i = 0; i < this->children->size(); i++) + if (tag.compare((*children)[i]->tag) == 0) + ret->push_back((*children)[i]); + + return ret; +} + + +bool ProtocolTreeNode::tagEquals(ProtocolTreeNode *node, const string& tag) { + return (node != NULL && node->tag.compare(tag) == 0); +} + +void ProtocolTreeNode::require(ProtocolTreeNode *node, const string& tag) { + if (!tagEquals(node, tag)) + throw WAException("failed require. node:" + node->toString() + "tag: " + tag, WAException::CORRUPT_STREAM_EX, 0); +} + + -- cgit v1.2.3