summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/WhatsAPI++/ProtocolTreeNode.h
blob: aeb1b6e18e66b3dea833f3de721253a32f512645 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* ProtocolTreeNode.h
*
* Created on: 26/06/2012
* Author: Antonio
*/

#if !defined(PROTOCOLNODE_H)
#define PROTOCOLNODE_H

#include <string>
#include <vector>
#include <map>

using namespace std;

struct XATTR
{
	__forceinline XATTR(const char *_name, const char *_value) :
		name(_name), value(_value)
	{}

	__forceinline XATTR(const char *_name, const std::string &_value) :
		name(_name), value(_value.c_str())
	{}

	__forceinline XATTR(const std::string &_name, const std::string &_value) :
		name(_name.c_str()), value(_value.c_str())
	{}

	const char *name, *value;
};

struct XATTRI
{
	__forceinline XATTRI(const char *_name, int _value) :
		name(_name), value(_value)
	{}

	__forceinline XATTRI(const std::string &_name, int _value) :
		name(_name.c_str()), value(_value)
	{}

	const char *name;
	int value;
};

class ProtocolTreeNode
{
	ProtocolTreeNode(const ProtocolTreeNode&); // to prevent copying

public:
	vector<unsigned char>* data;
   string tag;
   map<string, string> *attributes;
   vector<ProtocolTreeNode*> *children;

   ProtocolTreeNode(const string& tag, ProtocolTreeNode* child);
   ProtocolTreeNode(const string& tag, vector<unsigned char>* data = NULL, vector<ProtocolTreeNode*> *children = NULL);
	~ProtocolTreeNode();

   string toString() const; 
   ProtocolTreeNode* getChild(const string& id);
   ProtocolTreeNode* getChild(size_t id);
   const string& getAttributeValue(const string& attribute);

   vector<ProtocolTreeNode*> getAllChildren();
   vector<ProtocolTreeNode*> getAllChildren(const string& tag);
	std::string getDataAsString() const;

   static bool tagEquals(ProtocolTreeNode *node, const string& tag);
   static void require(ProtocolTreeNode *node, const string& tag);
};

ProtocolTreeNode& operator<<(ProtocolTreeNode&, const XATTR&);
ProtocolTreeNode* operator<<(ProtocolTreeNode*, const XATTR&);

ProtocolTreeNode& operator<<(ProtocolTreeNode&, const XATTRI&);
ProtocolTreeNode* operator<<(ProtocolTreeNode*, const XATTRI&);

#endif /* PROTOCOLNODE_H_ */