1 /***************************************************************************
3 * Copyright (C) <year> <author> *
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. *
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. *
15 ***************************************************************************/
17 #ifndef _core_bencode_h
18 #define _core_bencode_h
24 #include <tairon/core/exceptions.h>
26 using Tairon::Core::String
;
34 /** \brief Class that is used to hold bencoded informations.
41 typedef std::list
<BEncode
> List
;
45 typedef std::map
<String
, BEncode
> Map
;
50 NONE
, VALUE
, STRING
, LIST
, MAP
54 /** Constructs BEncode object of type NONE.
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
);
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.
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
99 /** Converts this BEncode object to VALUE and returns it as a
104 /** Converts this BEncode object to LIST and returns a constant
107 const BEncode::List
&asList() const;
109 /** Converts this BEncode object to MAP and returns a constant
112 const BEncode::Map
&asMap() const;
114 /** Converts this BEncode object to STRING and returns a constant
117 const String
&asString() const;
119 /** Converts this BEncode object to VALUE and returns a constant
122 const int64_t &asValue() const;
124 /** Clears all data from this object and its type sets to NONE.
128 /** Returns true if this object is LIST; otherwise returns false.
132 /** Returns true if this object is MAP; otherwise returns false.
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
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; otherwise raises an exception.
163 * \param key Key of the object to return.
165 BEncode
&operator[](const String
&key
);
167 /** If this object is MAP then it returns constant reference to BEncode
168 * object belonging to given key; otherwise raises an exception.
170 * \param key Key of the object to return.
172 const BEncode
&operator[](const String
&key
) const;
174 /** Loads encoded data from stream and stores them to object.
176 * \param s Input stream to read from.
177 * \param b BEncode object to store data into.
179 friend std::istream
&operator>>(std::istream
&s
, BEncode
&b
);
181 /** Stores given object to stream.
183 * \param s Output stream to write to.
184 * \param b Object to store.
186 friend std::ostream
&operator<<(std::ostream
&s
, const BEncode
&b
);
189 /** Reads a string from a stream.
191 * \param s Input stream from which the data will be read.
192 * \param str String object in which the data will be stored.
194 static bool readString(std::istream
&s
, String
&str
);
197 /** Type of this BEnocde.
201 /** Value of this BEncode.
216 /** Dictionary value.
222 /** \brief Exception that is raised by BEncode class.
224 class BEncodeException
: public Tairon::Core::Exception
227 /** Standard constructor.
229 BEncodeException(const String
&desc
) : Tairon::Core::Exception(desc
) {};
231 /** Default destructor.
233 ~BEncodeException() {};
238 }; // namespace Tairent
242 // vim: ai sw=4 ts=4 noet fdm=marker