libktorrent  2.1.1
bencoder.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 BTBENCODER_H
21 #define BTBENCODER_H
22 
23 #include <ktorrent_export.h>
24 #include <util/file.h>
25 
26 class QIODevice;
27 
28 namespace bt
29 {
30  class File;
31 
37  class KTORRENT_EXPORT BEncoderOutput
38  {
39  public:
40  virtual ~BEncoderOutput() {}
46  virtual void write(const char* str,Uint32 len) = 0;
47  };
48 
52  class KTORRENT_EXPORT BEncoderFileOutput : public BEncoderOutput
53  {
54  File* fptr;
55  public:
56  BEncoderFileOutput(File* fptr);
57 
58  void write(const char* str,Uint32 len) override;
59  };
60 
64  class KTORRENT_EXPORT BEncoderBufferOutput : public BEncoderOutput
65  {
66  QByteArray & data;
67  Uint32 ptr;
68  public:
69  BEncoderBufferOutput(QByteArray & data);
70 
71  void write(const char* str,Uint32 len) override;
72  };
73 
74  class KTORRENT_EXPORT BEncoderIODeviceOutput : public BEncoderOutput
75  {
76  QIODevice* dev;
77  public:
78  BEncoderIODeviceOutput(QIODevice* dev);
79 
80  void write(const char* str,Uint32 len) override;
81  };
82 
91  class KTORRENT_EXPORT BEncoder
92  {
93  BEncoderOutput* out;
94  bool del;
95  public:
100  BEncoder(File* fptr);
101 
102 
108 
113  BEncoder(QIODevice* dev);
114 
115  virtual ~BEncoder();
116 
120  void beginDict();
121 
125  void beginList();
126 
127  template <class T>
128  void write(const QByteArray& key,T val)
129  {
130  write(key);
131  write(val);
132  }
133 
138  void write(bool val);
139 
144  void write(float val);
145 
150  void write(Uint32 val);
151 
156  void write(Uint64 val);
157 
158 private:
163  void write(const char* str);
164 public:
165 
170  void write(const QByteArray & data);
171 
177  void write(const Uint8* data,Uint32 size);
178 
182  void end();
183  };
184 
185 }
186 
187 #endif
bt::BEncoderIODeviceOutput::write
void write(const char *str, Uint32 len) override
bt::BEncoder::write
void write(const QByteArray &data)
bt::BEncoder
Helper class to b-encode stuff.
Definition: bencoder.h:92
bt::BEncoder::write
void write(float val)
bt::BEncoder::beginDict
void beginDict()
bt::BEncoderIODeviceOutput
Definition: bencoder.h:75
bt::BEncoder::BEncoder
BEncoder(BEncoderOutput *out)
bt::BEncoder::write
void write(Uint64 val)
bt::BEncoderOutput::write
virtual void write(const char *str, Uint32 len)=0
bt::File
Wrapper class for stdio's FILE.
Definition: file.h:56
bt::BEncoder::write
void write(bool val)
bt::BEncoder::write
void write(const Uint8 *data, Uint32 size)
bt::BEncoderFileOutput
Definition: bencoder.h:53
bt::BEncoder::end
void end()
bt::BEncoderOutput
Definition: bencoder.h:38
bt::BEncoder::beginList
void beginList()
bt::BEncoder::BEncoder
BEncoder(File *fptr)
bt::BEncoderBufferOutput
Definition: bencoder.h:65
bt::BEncoder::write
void write(Uint32 val)
bt::BEncoder::BEncoder
BEncoder(QIODevice *dev)
bt::BEncoderFileOutput::write
void write(const char *str, Uint32 len) override
bt::BEncoderBufferOutput::write
void write(const char *str, Uint32 len) override