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
pull/1/head
Tatsuhiro Tsujikawa 2008-12-14 03:59:24 +00:00
parent a67ed743a2
commit b618ada28a
4 changed files with 49 additions and 0 deletions

View File

@ -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>
Use BDE instead of Dictionary/List/Data.

View File

@ -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
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)
{

View File

@ -32,6 +32,9 @@
* files in the program, then also delete it here.
*/
/* copyright --> */
#ifndef _D_BENCODE_H_
#define _D_BENCODE_H_
#include "common.h"
#include <string>
@ -110,6 +113,10 @@ public:
// Returns std::string. Requires this object to be String
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
@ -133,6 +140,10 @@ public:
// Requires this object to be Dict.
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.
// Requires this object to be Dict.
Dict::iterator dictBegin() throw(RecoverableException);
@ -212,3 +223,5 @@ std::string encode(const BDE& bde) throw();
} // namespace bencode
} // namespace aria2
#endif // _D_BENCODE_H_

View File

@ -5,6 +5,8 @@
#include <cppunit/extensions/HelperMacros.h>
#include "Util.h"
namespace aria2 {
class BencodeTest:public CppUnit::TestFixture {
@ -47,6 +49,11 @@ void BencodeTest::testString()
bencode::BDE zero("");
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()
@ -77,6 +84,10 @@ void BencodeTest::testDict()
ref["kn2"]; // This doesn't add kn2 key.
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), ref.size());
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()