libktorrent  2.1.1
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 <QList>
26 #include <QByteArray>
27 #include <util/constants.h>
28 #include <ktorrent_export.h>
29 #include "value.h"
30 
31 
32 namespace bt
33 {
34  class BListNode;
35 
43  class KTORRENT_EXPORT BNode
44  {
45  public:
46  enum Type
47  {
48  VALUE,DICT,LIST
49  };
50 
57  BNode(Type type,Uint32 off);
58  virtual ~BNode();
59 
61  Type getType() const {return type;}
62 
64  Uint32 getOffset() const {return off;}
65 
67  Uint32 getLength() const {return len;}
68 
70  void setLength(Uint32 l) {len = l;}
71 
73  virtual void printDebugInfo() = 0;
74  private:
75  Type type;
76  Uint32 off,len;
77  };
78 
85  class KTORRENT_EXPORT BValueNode : public BNode
86  {
87  Value value;
88  public:
89  BValueNode(const Value & v,Uint32 off);
90  ~BValueNode() override;
91 
92  const Value & data() const {return value;}
93  void printDebugInfo() override;
94  };
95 
101  class KTORRENT_EXPORT BDictNode : public BNode
102  {
103  struct DictEntry
104  {
105  QByteArray key;
106  BNode* node;
107  };
108  QList<DictEntry> children;
109  public:
110  BDictNode(Uint32 off);
111  ~BDictNode() override;
112 
114  QList<QByteArray> keys() const;
115 
121  void insert(const QByteArray & key,BNode* node);
122 
128  BNode* getData(const QByteArray & key);
129 
135  BListNode* getList(const QByteArray& key);
136 
142  BDictNode* getDict(const QByteArray& key);
143 
149  BValueNode* getValue(const QByteArray& key);
150 
152  int getInt(const QByteArray& key);
153 
155  qint64 getInt64(const QByteArray& key);
156 
158  QString getString(const QByteArray& key,QTextCodec* tc);
159 
161  QByteArray getByteArray(const QByteArray& key);
162 
163  void printDebugInfo() override;
164  };
165 
171  class KTORRENT_EXPORT BListNode : public BNode
172  {
173  QList<BNode*> children;
174  public:
175  BListNode(Uint32 off);
176  ~BListNode() override;
177 
182  void append(BNode* node);
183  void printDebugInfo() override;
184 
186  Uint32 getNumChildren() const {return children.count();}
187 
193  BNode* getChild(Uint32 idx) {return children.at(idx);}
194 
201  BListNode* getList(Uint32 idx);
202 
209  BDictNode* getDict(Uint32 idx);
210 
217  BValueNode* getValue(Uint32 idx);
218 
220  int getInt(Uint32 idx);
221 
223  qint64 getInt64(Uint32 idx);
224 
226  QString getString(Uint32 idx,QTextCodec* tc);
227 
229  QByteArray getByteArray(Uint32 idx);
230  };
231 }
232 
233 #endif
bt::BNode
Base class for a node in a b-encoded piece of data.
Definition: bnode.h:62
bt::BValueNode
Represents a value (string,bytearray or int) in bencoded data.
Definition: bnode.h:104
bt::BListNode
Represents a list in bencoded data.
Definition: bnode.h:190
bt::BDictNode
Represents a dictionary in bencoded data.
Definition: bnode.h:120
bt::Value
Definition: value.h:52