libktorrent  2.2.0
bnode.h
1 /***************************************************************************
2  * Copyright (C) 2005 by Joris Guisson *
3  * joris.guisson@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 #ifndef BTBNODE_H
21 #define BTBNODE_H
22 
23 #include <QList>
24 #include <QVariant>
25 #include <QByteArray>
26 #include <util/constants.h>
27 #include <ktorrent_export.h>
28 #include "value.h"
29 
30 
31 namespace bt
32 {
33  class BListNode;
34 
42  class KTORRENT_EXPORT BNode
43  {
44  public:
45  enum Type
46  {
47  VALUE,DICT,LIST
48  };
49 
56  BNode(Type type,Uint32 off);
57  virtual ~BNode();
58 
60  Type getType() const {return type;}
61 
63  Uint32 getOffset() const {return off;}
64 
66  Uint32 getLength() const {return len;}
67 
69  void setLength(Uint32 l) {len = l;}
70 
72  virtual void printDebugInfo() = 0;
73  private:
74  Type type;
75  Uint32 off,len;
76  };
77 
84  class KTORRENT_EXPORT BValueNode : public BNode
85  {
86  Value value;
87  public:
88  BValueNode(const Value & v,Uint32 off);
89  ~BValueNode() override;
90 
91  const Value & data() const {return value;}
92  void printDebugInfo() override;
93  };
94 
100  class KTORRENT_EXPORT BDictNode : public BNode
101  {
102  struct DictEntry
103  {
104  QByteArray key;
105  BNode* node;
106  };
107  QList<DictEntry> children;
108  public:
109  BDictNode(Uint32 off);
110  ~BDictNode() override;
111 
113  QList<QByteArray> keys() const;
114 
120  void insert(const QByteArray & key,BNode* node);
121 
127  BNode* getData(const QByteArray & key);
128 
134  BListNode* getList(const QByteArray& key);
135 
141  BDictNode* getDict(const QByteArray& key);
142 
148  BValueNode* getValue(const QByteArray& key);
149 
151  int getInt(const QByteArray& key);
152 
154  qint64 getInt64(const QByteArray& key);
155 
157  QString getString(const QByteArray& key,QTextCodec* tc);
158 
160  QByteArray getByteArray(const QByteArray& key);
161 
162  void printDebugInfo() override;
163  };
164 
170  class KTORRENT_EXPORT BListNode : public BNode
171  {
172  QList<BNode*> children;
173  public:
174  BListNode(Uint32 off);
175  ~BListNode() override;
176 
181  void append(BNode* node);
182  void printDebugInfo() override;
183 
185  Uint32 getNumChildren() const {return children.count();}
186 
192  BNode* getChild(Uint32 idx) {return children.at(idx);}
193 
200  BListNode* getList(Uint32 idx);
201 
208  BDictNode* getDict(Uint32 idx);
209 
216  BValueNode* getValue(Uint32 idx);
217 
219  int getInt(Uint32 idx);
220 
222  qint64 getInt64(Uint32 idx);
223 
225  QString getString(Uint32 idx,QTextCodec* tc);
226 
228  QByteArray getByteArray(Uint32 idx);
229  };
230 }
231 
232 #endif
bt::BListNode::append
void append(BNode *node)
bt::BNode::setLength
void setLength(Uint32 l)
Set the length.
Definition: bnode.h:69
bt::BValueNode::printDebugInfo
void printDebugInfo() override
Print some debugging info.
bt::BListNode::getString
QString getString(Uint32 idx, QTextCodec *tc)
Same as getValue, except directly returns a QString, if something goes wrong, an error will be thrown...
bt::BNode
Base class for a node in a b-encoded piece of data.
Definition: bnode.h:43
bt::BNode::getType
Type getType() const
Get the type of node.
Definition: bnode.h:60
bt::BListNode::getByteArray
QByteArray getByteArray(Uint32 idx)
Same as getValue, except directly returns an QByteArray, if something goes wrong, an error will be th...
bt::BDictNode::printDebugInfo
void printDebugInfo() override
Print some debugging info.
bt::BDictNode::keys
QList< QByteArray > keys() const
Get a list of keys.
bt::BNode::getLength
Uint32 getLength() const
Get the length this node takes up in the bytearray.
Definition: bnode.h:66
bt::BDictNode::getString
QString getString(const QByteArray &key, QTextCodec *tc)
Same as getValue, except directly returns a QString, if something goes wrong, an error will be thrown...
bt::BListNode::getValue
BValueNode * getValue(Uint32 idx)
bt::BNode::BNode
BNode(Type type, Uint32 off)
bt::BListNode::getNumChildren
Uint32 getNumChildren() const
Get the number of nodes in the list.
Definition: bnode.h:185
bt::BListNode::getInt64
qint64 getInt64(Uint32 idx)
Same as getValue, except directly returns a qint64, if something goes wrong, an error will be thrown.
bt::BDictNode::getList
BListNode * getList(const QByteArray &key)
bt::BListNode::getChild
BNode * getChild(Uint32 idx)
Definition: bnode.h:192
bt::BListNode::getList
BListNode * getList(Uint32 idx)
bt::BDictNode::getData
BNode * getData(const QByteArray &key)
bt::BListNode::getDict
BDictNode * getDict(Uint32 idx)
bt::BDictNode::getInt64
qint64 getInt64(const QByteArray &key)
Same as getValue, except directly returns a qint64, if something goes wrong, an error will be thrown.
bt::BValueNode
Represents a value (string,bytearray or int) in bencoded data.
Definition: bnode.h:85
bt::BNode::printDebugInfo
virtual void printDebugInfo()=0
Print some debugging info.
bt::BDictNode::getDict
BDictNode * getDict(const QByteArray &key)
bt::BListNode::getInt
int getInt(Uint32 idx)
Same as getValue, except directly returns an int, if something goes wrong, an error will be thrown.
bt::BListNode
Represents a list in bencoded data.
Definition: bnode.h:171
bt::BDictNode::insert
void insert(const QByteArray &key, BNode *node)
bt::BDictNode
Represents a dictionary in bencoded data.
Definition: bnode.h:101
bt::BDictNode::getInt
int getInt(const QByteArray &key)
Same as getValue, except directly returns an int, if something goes wrong, an error will be thrown.
bt::Value
Definition: value.h:34
bt::BDictNode::getValue
BValueNode * getValue(const QByteArray &key)
bt::BDictNode::getByteArray
QByteArray getByteArray(const QByteArray &key)
Same as getValue, except directly returns an QByteArray, if something goes wrong, an error will be th...
bt::BNode::getOffset
Uint32 getOffset() const
Get the offset in the bytearray where this node starts.
Definition: bnode.h:63
bt::BListNode::printDebugInfo
void printDebugInfo() override
Print some debugging info.