~ubuntu-branches/ubuntu/lucid/ktorrent/lucid

« back to all changes in this revision

Viewing changes to libbtcore/bcodec/bnode.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-12-08 19:17:41 UTC
  • mfrom: (1.2.1 upstream) (0.7.12 sid)
  • Revision ID: james.westby@ubuntu.com-20091208191741-lqlq0xvnlv8ki19u
Tags: 3.3.1+dfsg.1-1ubuntu1
* Merge with Debian Testing remaining changes:
  - Build-depend directly on libboost-serialization1.40-dev since
    libboost-serialization-dev from boost-defaults is not in Main
  - Add in rules: include /usr/lib/kubuntu-desktop-i18n/debhelper/kubuntu.mk
  - Don't use dpkg-source 3.0 format
  - Add quilt to build-depends

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 ***************************************************************************/
20
20
#include "bnode.h"
21
21
#include <util/log.h>
 
22
#include <util/error.h>
 
23
#include <qtextcodec.h>
22
24
 
23
25
namespace bt
24
26
{
33
35
 
34
36
        ////////////////////////////////////////////////
35
37
 
36
 
        BValueNode::BValueNode(const Value & v,Uint32 off) : BNode(VALUE,off),v(v)
 
38
        BValueNode::BValueNode(const Value & v,Uint32 off) : BNode(VALUE,off),value(v)
37
39
        {}
38
40
        
39
41
        BValueNode::~BValueNode()
41
43
        
42
44
        void BValueNode::printDebugInfo()
43
45
        {
44
 
                if (v.getType() == Value::INT)
45
 
                        Out(SYS_GEN|LOG_DEBUG) << "Value = " << v.toInt() << endl;
46
 
                else
47
 
                        Out(SYS_GEN|LOG_DEBUG) << "Value = " << v.toString() << endl;
 
46
                if (value.getType() == Value::STRING)
 
47
                        Out(SYS_GEN|LOG_DEBUG) << "Value = " << value.toString() << endl;
 
48
                else if (value.getType() == Value::INT)
 
49
                        Out(SYS_GEN|LOG_DEBUG) << "Value = " << value.toInt() << endl;
 
50
                else if (value.getType() == Value::INT64)                    
 
51
                        Out(SYS_GEN|LOG_DEBUG) << "Value = " << value.toInt64() << endl;
48
52
        }
49
53
        
50
54
        ////////////////////////////////////////////////
130
134
                return dynamic_cast<BValueNode*>(n);
131
135
        }
132
136
        
 
137
        int BDictNode::getInt(const QString & key)
 
138
        {
 
139
                BValueNode* v = getValue(key);
 
140
                if (!v)
 
141
                        throw bt::Error("Key not found in dict");
 
142
                
 
143
                if (v->data().getType() != bt::Value::INT)
 
144
                        throw bt::Error("Incompatible type");
 
145
                
 
146
                return v->data().toInt();
 
147
        }
 
148
        
 
149
        qint64 BDictNode::getInt64(const QString & key)
 
150
        {
 
151
                BValueNode* v = getValue(key);
 
152
                if (!v)
 
153
                        throw bt::Error("Key not found in dict");
 
154
                
 
155
                if (v->data().getType() != bt::Value::INT64 && v->data().getType() != bt::Value::INT)
 
156
                        throw bt::Error("Incompatible type");
 
157
                
 
158
                return v->data().toInt64();
 
159
        }
 
160
        
 
161
        QString BDictNode::getString(const QString & key,QTextCodec* tc)
 
162
        {
 
163
                BValueNode* v = getValue(key);
 
164
                if (!v)
 
165
                        throw bt::Error("Key not found in dict");
 
166
                
 
167
                if (v->data().getType() != bt::Value::STRING)
 
168
                        throw bt::Error("Incompatible type");
 
169
                
 
170
                if (!tc)
 
171
                        return v->data().toString();
 
172
                else
 
173
                        return v->data().toString(tc);
 
174
        }
 
175
        
 
176
        QByteArray BDictNode::getByteArray(const QString & key)
 
177
        {
 
178
                BValueNode* v = getValue(key);
 
179
                if (!v)
 
180
                        throw bt::Error("Key not found in dict");
 
181
                
 
182
                if (v->data().getType() != bt::Value::STRING)
 
183
                        throw bt::Error("Incompatible type");
 
184
                
 
185
                return v->data().toByteArray();
 
186
        }
 
187
        
133
188
        void BDictNode::printDebugInfo()
134
189
        {
135
190
                Out(SYS_GEN|LOG_DEBUG) << "DICT" << endl;
181
236
                return dynamic_cast<BValueNode*>(getChild(idx));
182
237
        }
183
238
        
 
239
        int BListNode::getInt(Uint32 idx)
 
240
        {
 
241
                BValueNode* v = getValue(idx);
 
242
                if (!v)
 
243
                        throw bt::Error("Key not found in dict");
 
244
                
 
245
                if (v->data().getType() != bt::Value::INT)
 
246
                        throw bt::Error("Incompatible type");
 
247
                
 
248
                return v->data().toInt();
 
249
        }
 
250
        
 
251
        qint64 BListNode::getInt64(Uint32 idx)
 
252
        {
 
253
                BValueNode* v = getValue(idx);
 
254
                if (!v)
 
255
                        throw bt::Error("Key not found in dict");
 
256
                
 
257
                if (v->data().getType() != bt::Value::INT64 && v->data().getType() != bt::Value::INT)
 
258
                        throw bt::Error("Incompatible type");
 
259
                
 
260
                return v->data().toInt64();
 
261
        }
 
262
        
 
263
        QString BListNode::getString(Uint32 idx,QTextCodec* tc)
 
264
        {
 
265
                BValueNode* v = getValue(idx);
 
266
                if (!v)
 
267
                        throw bt::Error("Key not found in dict");
 
268
                
 
269
                if (v->data().getType() != bt::Value::STRING)
 
270
                        throw bt::Error("Incompatible type");
 
271
                
 
272
                if (!tc)
 
273
                        return v->data().toString();
 
274
                else
 
275
                        return v->data().toString(tc);
 
276
        }
 
277
        
 
278
        QByteArray BListNode::getByteArray(Uint32 idx)
 
279
        {
 
280
                BValueNode* v = getValue(idx);
 
281
                if (!v)
 
282
                        throw bt::Error("Key not found in dict");
 
283
                
 
284
                if (v->data().getType() != bt::Value::STRING)
 
285
                        throw bt::Error("Incompatible type");
 
286
                
 
287
                return v->data().toByteArray();
 
288
        }
 
289
        
184
290
        void BListNode::printDebugInfo()
185
291
        {
186
292
                Out(SYS_GEN|LOG_DEBUG) << "LIST " <<  children.count() << endl;