mirror of https://github.com/aria2/aria2
2008-12-14 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added missing #ifdef guard. Added uc() function for String and removeKey() for Dict. * src/bencode.cc * src/bencode.h * test/BencodeTest.ccpull/1/head
parent
a67ed743a2
commit
b618ada28a
|
@ -1,3 +1,11 @@
|
||||||
|
2008-12-14 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
|
Added missing #ifdef guard. Added uc() function for String and
|
||||||
|
removeKey() for Dict.
|
||||||
|
* src/bencode.cc
|
||||||
|
* src/bencode.h
|
||||||
|
* test/BencodeTest.cc
|
||||||
|
|
||||||
2008-12-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
2008-12-10 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
Use BDE instead of Dictionary/List/Data.
|
Use BDE instead of Dictionary/List/Data.
|
||||||
|
|
|
@ -120,6 +120,15 @@ const std::string& BDE::s() const throw(RecoverableException)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const unsigned char* BDE::uc() const throw(RecoverableException)
|
||||||
|
{
|
||||||
|
if(isString()) {
|
||||||
|
return reinterpret_cast<const unsigned char*>(_string->data());
|
||||||
|
} else {
|
||||||
|
throw RecoverableException("Not String");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Dictionary Interface
|
// Dictionary Interface
|
||||||
|
|
||||||
bool BDE::isDict() const throw()
|
bool BDE::isDict() const throw()
|
||||||
|
@ -160,6 +169,14 @@ bool BDE::containsKey(const std::string& key) const throw(RecoverableException)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BDE::removeKey(const std::string& key) const throw(RecoverableException)
|
||||||
|
{
|
||||||
|
if(isDict()) {
|
||||||
|
_dict->erase(key);
|
||||||
|
} else {
|
||||||
|
throw RecoverableException("Not Dict");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
BDE::Dict::iterator BDE::dictBegin() throw(RecoverableException)
|
BDE::Dict::iterator BDE::dictBegin() throw(RecoverableException)
|
||||||
{
|
{
|
||||||
|
|
|
@ -32,6 +32,9 @@
|
||||||
* files in the program, then also delete it here.
|
* files in the program, then also delete it here.
|
||||||
*/
|
*/
|
||||||
/* copyright --> */
|
/* copyright --> */
|
||||||
|
#ifndef _D_BENCODE_H_
|
||||||
|
#define _D_BENCODE_H_
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -110,6 +113,10 @@ public:
|
||||||
// Returns std::string. Requires this object to be String
|
// Returns std::string. Requires this object to be String
|
||||||
const std::string& s() const throw(RecoverableException);
|
const std::string& s() const throw(RecoverableException);
|
||||||
|
|
||||||
|
// Returns std::string.data() casted to unsigned char*.
|
||||||
|
// Use s().size() to get length.
|
||||||
|
const unsigned char* uc() const throw(RecoverableException);
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
// Dictionary Interface
|
// Dictionary Interface
|
||||||
|
|
||||||
|
@ -133,6 +140,10 @@ public:
|
||||||
// Requires this object to be Dict.
|
// Requires this object to be Dict.
|
||||||
bool containsKey(const std::string& key) const throw(RecoverableException);
|
bool containsKey(const std::string& key) const throw(RecoverableException);
|
||||||
|
|
||||||
|
// Removes specified key from dict.
|
||||||
|
// Requires this object to be Dict.
|
||||||
|
void removeKey(const std::string& key) const throw(RecoverableException);
|
||||||
|
|
||||||
// Returns a read/write iterator that points to the first pair in the dict.
|
// Returns a read/write iterator that points to the first pair in the dict.
|
||||||
// Requires this object to be Dict.
|
// Requires this object to be Dict.
|
||||||
Dict::iterator dictBegin() throw(RecoverableException);
|
Dict::iterator dictBegin() throw(RecoverableException);
|
||||||
|
@ -212,3 +223,5 @@ std::string encode(const BDE& bde) throw();
|
||||||
} // namespace bencode
|
} // namespace bencode
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
||||||
|
#endif // _D_BENCODE_H_
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
|
|
||||||
#include <cppunit/extensions/HelperMacros.h>
|
#include <cppunit/extensions/HelperMacros.h>
|
||||||
|
|
||||||
|
#include "Util.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
class BencodeTest:public CppUnit::TestFixture {
|
class BencodeTest:public CppUnit::TestFixture {
|
||||||
|
@ -47,6 +49,11 @@ void BencodeTest::testString()
|
||||||
|
|
||||||
bencode::BDE zero("");
|
bencode::BDE zero("");
|
||||||
CPPUNIT_ASSERT_EQUAL(std::string(""), zero.s());
|
CPPUNIT_ASSERT_EQUAL(std::string(""), zero.s());
|
||||||
|
|
||||||
|
const unsigned char uc[] = { 0x08, 0x19, 0x2a, 0x3b };
|
||||||
|
bencode::BDE data(uc, sizeof(uc));
|
||||||
|
CPPUNIT_ASSERT_EQUAL(Util::toHex(uc, sizeof(uc)),
|
||||||
|
Util::toHex(data.uc(), data.s().size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BencodeTest::testInteger()
|
void BencodeTest::testInteger()
|
||||||
|
@ -77,6 +84,10 @@ void BencodeTest::testDict()
|
||||||
ref["kn2"]; // This doesn't add kn2 key.
|
ref["kn2"]; // This doesn't add kn2 key.
|
||||||
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), ref.size());
|
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), ref.size());
|
||||||
CPPUNIT_ASSERT(!ref.containsKey("kn2"));
|
CPPUNIT_ASSERT(!ref.containsKey("kn2"));
|
||||||
|
|
||||||
|
dict.removeKey("kn");
|
||||||
|
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), dict.size());
|
||||||
|
CPPUNIT_ASSERT(!dict.containsKey("kn"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BencodeTest::testDictIter()
|
void BencodeTest::testDictIter()
|
||||||
|
|
Loading…
Reference in New Issue