From 1b54e64d34f87dca00a9d93bf110207a1098a2a5 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 9 Dec 2008 14:43:11 +0000 Subject: 2008-12-09 Tatsuhiro Tsujikawa Added bencode helper functions and BDE class. They will replace MetaFileUtil and Dictionary/List/Data classes. * src/Makefile.am * src/bencode.cc * src/bencode.h * test/BencodeTest.cc * test/Makefile.am --- src/bencode.h | 214 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 214 insertions(+) create mode 100644 src/bencode.h (limited to 'src/bencode.h') diff --git a/src/bencode.h b/src/bencode.h new file mode 100644 index 00000000..2a7fcea3 --- /dev/null +++ b/src/bencode.h @@ -0,0 +1,214 @@ +/* */ +#include "common.h" + +#include +#include +#include +#include + +#include "SharedHandle.h" +#include "RecoverableException.h" + +namespace aria2 { + +namespace bencode { + +class BDE; + +class BDE { +public: + typedef std::map Dict; + typedef std::deque List; + typedef int64_t Integer; +private: + enum TYPE{ + TYPE_NONE, + TYPE_INTEGER, + TYPE_STRING, + TYPE_DICT, + TYPE_LIST, + }; + + TYPE _type; + SharedHandle _dict; + SharedHandle _list; + SharedHandle _string; + SharedHandle _integer; + +public: + BDE() throw(); + + static BDE dict() throw(); + + static BDE list() throw(); + + static const BDE none; + + // Test for Null data + // Return true if the type of this object is None. + bool isNone() const throw(); + + ////////////////////////////////////////////////////////////////////////////// + // Integer Interface + + BDE(Integer integer) throw(); + + // Returns true if the type of this object is Integer. + bool isInteger() const throw(); + + // Returns Integer. Requires this object to be Integer. + Integer i() const throw(RecoverableException); + + ////////////////////////////////////////////////////////////////////////////// + // String Interface + + BDE(const std::string& string) throw(); + + // Made explicit to avoid ambiguity with BDE(Integer). + explicit BDE(const char* cstring) throw(); + + BDE(const char* data, size_t length) throw(); + + BDE(const unsigned char* data, size_t length) throw(); + + // Returns true if the type of this object is String. + bool isString() const throw(); + + // Returns std::string. Requires this object to be String + const std::string& s() const throw(RecoverableException); + + ////////////////////////////////////////////////////////////////////////////// + // Dictionary Interface + + // Returns true if the type of this object is Dict. + bool isDict() const throw(); + + // Returns the reference to BDE object associated with given key. + // If the key is not found, new pair with that key is created using default + // values, which is then returned. In other words, this is the same behavior + // of std::map's operator[]. + // Requires this object to be Dict. + BDE& operator[](const std::string& key) throw(RecoverableException); + + // Returns the const reference to BDE ojbect associated with given key. + // If the key is not found, BDE::none is returned. + // Requires this object to be Dict. + const BDE& operator[](const std::string& key) const + throw(RecoverableException); + + // Returns true if the given key is found in dict. + // Requires this object to be Dict. + bool containsKey(const std::string& key) const throw(RecoverableException); + + // Returns a read/write iterator that points to the first pair in the dict. + // Requires this object to be Dict. + Dict::iterator dictBegin() throw(RecoverableException); + + // Returns a read/write read-only iterator that points to the first pair in + // the dict. + // Requires this object to be Dict. + Dict::const_iterator dictBegin() const throw(RecoverableException); + + // Returns a read/write read-only iterator that points to one past the last + // pair in the dict. + // Requires this object to be Dict. + Dict::iterator dictEnd() throw(RecoverableException); + + // Returns a read/write read-only iterator that points to one past the last + // pair in the dict. + // Requires this object to be Dict. + Dict::const_iterator dictEnd() const throw(RecoverableException); + + ////////////////////////////////////////////////////////////////////////////// + // List Interface + + // Returns true if the type of this object is List. + bool isList() const throw(); + + // Appends given bde to list. Required the type of this object to be List. + void append(const BDE& bde) throw(RecoverableException); + + // Alias for append() + void operator<<(const BDE& bde) throw(RecoverableException); + + // Returns the reference of the object at the given index. Required this + // object to be List. + BDE& operator[](size_t index) throw(RecoverableException); + + // Returns the const reference of the object at the given index. + // Required this object to be List. + const BDE& operator[](size_t index) const throw(RecoverableException); + + // Returns a read/write iterator that points to the first object in list. + // Required this object to be List. + List::iterator listBegin() throw(RecoverableException); + + // Returns a read/write read-only iterator that points to the first object + // in list. Required this object to be List. + List::const_iterator listBegin() const throw(RecoverableException); + + // Returns a read/write iterator that points to the one past the last object + // in list. Required this object to be List. + List::iterator listEnd() throw(RecoverableException); + + // Returns a read/write read-only iterator that points to the one past the + // last object in list. Required this object to be List. + List::const_iterator listEnd() const throw(RecoverableException); + + // For List type: Returns size of list. + // For Dict type: Returns size of dict. + size_t size() const throw(RecoverableException); + + // For List type: Returns true if size of list is 0. + // For Dict type: Returns true if size of dict is 0. + bool empty() const throw(RecoverableException); +}; + +BDE decode(std::istream& in) throw(RecoverableException); + +// Decode the data in s. +BDE decode(const std::string& s) throw(RecoverableException); + +BDE decode(const unsigned char* data, size_t length) + throw(RecoverableException); + +BDE decodeFromFile(const std::string& filename) throw(RecoverableException); + +std::string encode(const BDE& bde) throw(); + +} // namespace bencode + +} // namespace aria2 -- cgit