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::BNode
Base class for a node in a b-encoded piece of data.
Definition: bnode.h:61
bt::BValueNode
Represents a value (string,bytearray or int) in bencoded data.
Definition: bnode.h:103
bt::BListNode
Represents a list in bencoded data.
Definition: bnode.h:189
bt::BDictNode
Represents a dictionary in bencoded data.
Definition: bnode.h:119
bt::Value
Definition: value.h:52