summaryrefslogtreecommitdiffstats
path: root/libktorrent/torrent/bnode.h
blob: 685291c723e20b9188cf9f60023f550748055f25 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   joris.guisson@gmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#ifndef BTBNODE_H
#define BTBNODE_H

#include <qptrlist.h>
#include <qvaluelist.h>
#include <util/constants.h>
#include "value.h"


namespace bt
{
	class BListNode;

	/**
	 * @author Joris Guisson
	 * @brief Base class for a node in a b-encoded piece of data
	 * 
	 * There are 3 possible pieces of data in b-encoded piece of data.
	 * This is the base class for all those 3 things.
	*/
	class BNode
	{
	public:
		enum Type
		{
			VALUE,DICT,LIST
		};
		
		/**
		 * Constructor, sets the Type, and the offset into 
		 * the data.
		 * @param type Type of node
		 * @param off The offset into the data
		 */
		BNode(Type type,Uint32 off);
		virtual ~BNode();

		/// Get the type of node
		Type getType() const {return type;}

		/// Get the offset in the bytearray where this node starts.
		Uint32 getOffset() const {return off;}

		/// Get the length this node takes up in the bytearray.
		Uint32 getLength() const {return len;}

		/// Set the length
		void setLength(Uint32 l) {len = l;}

		/// Print some debugging info
		virtual void printDebugInfo() = 0;
	private:
		Type type;
		Uint32 off,len;
	};

	/**
	 * @author Joris Guisson
	 * @brief Represents a value (string,bytearray or int) in bencoded data
	 *
	 * @todo Use QVariant
	 */
	class BValueNode : public BNode
	{
		Value v;
	public:
		BValueNode(const Value & v,Uint32 off);
		virtual ~BValueNode();
		
		const Value & data() const {return v;}
		void printDebugInfo();
	};

	/**
	 * @author Joris Guisson
	 * @brief Represents a dictionary in bencoded data
	 *
	 */
	class BDictNode : public BNode
	{
		struct DictEntry
		{
			QByteArray key;
			BNode* node;
		};
		QValueList<DictEntry> children;
	public:
		BDictNode(Uint32 off);
		virtual ~BDictNode();
		
		/**
		 * Insert a BNode in the dictionary.
		 * @param key The key
		 * @param node The node
		 */
		void insert(const QByteArray & key,BNode* node);
		
		/**
		 * Get a BNode.
		 * @param key The key
		 * @return The node or 0 if there is no node with has key @a key 
		 */
		BNode* getData(const QString & key);

		/**
		 * Get a BListNode.
		 * @param key The key
		 * @return The node or 0 if there is no list node with has key @a key
		 */
		BListNode* getList(const QString & key);

		/**
		 * Get a BDictNode.
		 * @param key The key
		 * @return The node or 0 if there is no dict node with has key @a key
		 */
		BDictNode* getDict(const QString & key);
		
		/**
		 * Get a BDictNode.
		 * @param key The key
		 * @return The node or 0 if there is no dict node with has key @a key
		 */
		BDictNode* getDict(const QByteArray & key);

		/**
		 * Get a BValueNode.
		 * @param key The key
		 * @return The node or 0 if there is no value node with has key @a key
		 */
		BValueNode* getValue(const QString & key);
		
		void printDebugInfo();
	};

	/**
	 * @author Joris Guisson
	 * @brief Represents a list in bencoded data
	 *
	 */
	class BListNode : public BNode
	{
		QPtrList<BNode> children;
	public:
		BListNode(Uint32 off);
		virtual ~BListNode();

		/**
		 * Append a node to the list.
		 * @param node The node
		 */
		void append(BNode* node);
		void printDebugInfo();

		/// Get the number of nodes in the list.
		Uint32 getNumChildren() const {return children.count();}
		
		/**
		 * Get a node from the list
		 * @param idx The index
		 * @return The node or 0 if idx is out of bounds
		 */
		BNode* getChild(Uint32 idx) {return children.at(idx);}

		/**
		 * Get a BListNode.
		 * @param idx The index
		 * @return The node or 0 if the index is out of bounds or the element
		 * 	at postion @a idx isn't a BListNode.
		 */
		BListNode* getList(Uint32 idx);

		/**
		 * Get a BDictNode.
		 * @param idx The index
		 * @return The node or 0 if the index is out of bounds or the element
		 * 	at postion @a idx isn't a BDictNode.
		 */
		BDictNode* getDict(Uint32 idx);

		/**
		 * Get a BValueNode.
		 * @param idx The index
		 * @return The node or 0 if the index is out of bounds or the element
		 * 	at postion @a idx isn't a BValueNode.
		 */
		BValueNode* getValue(Uint32 idx);
	};
}

#endif