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. 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
);
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
);
198 /** Type of this BEnocde.
202 /** Value of this BEncode.
217 /** Dictionary value.
223 /** \brief Exception that is raised by BEncode class.
225 class BEncodeException
: public Tairon::Core::Exception
228 /** Standard constructor.
230 BEncodeException(const String
&desc
) : Tairon::Core::Exception(desc
) {};
232 /** Default destructor.
234 ~BEncodeException() {};
239 }; // namespace Tairent
243 // vim: ai sw=4 ts=4 noet fdm=marker