Add missing #include to src/main/torrentclient.h.
[tairent.git] / src / core / bencode.h
blob3e2abbd19b1fde217ed19f4cf5f6f99198910ab3
1 /***************************************************************************
2 * *
3 * Copyright (C) <year> <author> *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation and appearing *
8 * in the file LICENSE.GPL included in the packaging of this file. *
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 GNU *
13 * General Public License for more details. *
14 * *
15 ***************************************************************************/
17 #ifndef _core_bencode_h
18 #define _core_bencode_h
20 #include <stdint.h>
21 #include <list>
22 #include <map>
24 #include <tairon/core/exceptions.h>
26 using Tairon::Core::String;
28 namespace Tairent
31 namespace Core
34 /** \brief Class that is used to hold bencoded informations.
36 class BEncode
38 public:
39 /** List type.
41 typedef std::list<BEncode> List;
43 /** Dictionary type.
45 typedef std::map<String, BEncode> Map;
47 /** BEncode types.
49 enum Type {
50 NONE, VALUE, STRING, LIST, MAP
53 public:
54 /** Constructs BEncode object of type NONE.
56 BEncode();
58 /** Constructs BEncode object of type VALUE.
60 * \param v Value to store in object.
62 BEncode(const int64_t v);
64 /** Constructs BEncode object of type STRING.
66 * \param s String to store in object.
68 BEncode(const String &s);
70 /** Copy constructor.
72 * \param b BEncode object to copy.
74 BEncode(const BEncode &b);
76 /** Explicitly constructs BEncode object of given type.
78 * \param t Type of the object to construct.
80 explicit BEncode(Type t);
82 /** Destroys the object.
84 ~BEncode();
86 /** Converts this BEncode object to LIST and returns it as a reference.
88 BEncode::List &asList();
90 /** Converts this BEncode object to MAP and returns it as a reference.
92 BEncode::Map &asMap();
94 /** Converts this BEncode object to STRING and returns it as a
95 * reference.
97 String &asString();
99 /** Converts this BEncode object to VALUE and returns it as a
100 * reference.
102 int64_t &asValue();
104 /** Converts this BEncode object to LIST and returns a constant
105 * reference to it.
107 const BEncode::List &asList() const;
109 /** Converts this BEncode object to MAP and returns a constant
110 * reference to it.
112 const BEncode::Map &asMap() const;
114 /** Converts this BEncode object to STRING and returns a constant
115 * reference to it.
117 const String &asString() const;
119 /** Converts this BEncode object to VALUE and returns a constant
120 * reference to it.
122 const int64_t &asValue() const;
124 /** Clears all data from this object and its type sets to NONE.
126 void clear();
128 /** Returns true if this object is LIST; otherwise returns false.
130 bool isList() const;
132 /** Returns true if this object is MAP; otherwise returns false.
134 bool isMap() const;
136 /** Returns true if this object is STRING; otheriwse returns false.
138 bool isString() const;
140 /** Returns true if this object is VALUE; otherwise returns false.
142 bool isValue() const;
144 /** Returns type of this object.
146 Type getType() const;
148 /** Computes SHA1 hash of string representing this object.
150 String computeSHA1() const;
152 /** Assigns value of given object to this object and returns a
153 * reference to
154 * it.
156 * \param b Object to set the value of this object to.
158 BEncode &operator=(const BEncode &b);
160 /** If this object is MAP then it returns reference to BEncode object
161 * belonging to given key. If there is no such object then new one is
162 * created with type BEncode::NONE.
164 * \param key Key of the object to return.
166 BEncode &operator[](const String &key);
168 /** If this object is MAP then it returns constant reference to BEncode
169 * object belonging to given key; otherwise raises an exception.
171 * \param key Key of the object to return.
173 const BEncode &operator[](const String &key) const;
175 /** Loads encoded data from stream and stores them to object.
177 * \param s Input stream to read from.
178 * \param b BEncode object to store data into.
180 friend std::istream &operator>>(std::istream &s, BEncode &b);
182 /** Stores given object to stream.
184 * \param s Output stream to write to.
185 * \param b Object to store.
187 friend std::ostream &operator<<(std::ostream &s, const BEncode &b);
189 private:
190 /** Reads a string from a stream.
192 * \param s Input stream from which the data will be read.
193 * \param str String object in which the data will be stored.
195 static bool readString(std::istream &s, String &str);
197 private:
198 /** Type of this BEnocde.
200 Type type;
202 /** Value of this BEncode.
204 union {
205 /** Integer value.
207 int64_t value;
209 /** String value.
211 String *string;
213 /** List value.
215 BEncode::List *list;
217 /** Dictionary value.
219 BEncode::Map *map;
223 /** \brief Exception that is raised by BEncode class.
225 class BEncodeException : public Tairon::Core::Exception
227 public:
228 /** Standard constructor.
230 BEncodeException(const String &desc) : Tairon::Core::Exception(desc) {};
232 /** Default destructor.
234 ~BEncodeException() {};
237 }; // namespace Core
239 }; // namespace Tairent
241 #endif
243 // vim: ai sw=4 ts=4 noet fdm=marker