Prechádzať zdrojové kódy

2009-08-19 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

	Get comment, comment.utf-8, created by and creation date from
	.torrent file and print them in -S output.
	* src/bittorrent_helper.cc
	* src/bittorrent_helper.h
	* test/BittorrentHelperTest.cc
	* test/test.torrent
	* test/utf8.torrent
Tatsuhiro Tsujikawa 16 rokov pred
rodič
commit
9077d5a985

+ 10 - 0
ChangeLog

@@ -1,3 +1,13 @@
+2009-08-19  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
+
+	Get comment, comment.utf-8, created by and creation date from
+	.torrent file and print them in -S output.
+	* src/bittorrent_helper.cc
+	* src/bittorrent_helper.h
+	* test/BittorrentHelperTest.cc
+	* test/test.torrent
+	* test/utf8.torrent
+
 2009-08-18  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 
 	Show exact file size along in -S option output.

+ 48 - 0
src/bittorrent_helper.cc

@@ -83,6 +83,14 @@ static const std::string C_ANNOUNCE_LIST("announce-list");
 
 static const std::string C_NODES("nodes");
 
+static const std::string C_CREATION_DATE("creation date");
+
+static const std::string C_COMMENT("comment");
+
+static const std::string C_COMMENT_UTF8("comment.utf-8");
+
+static const std::string C_CREATED_BY("created by");
+
 static const std::string DEFAULT_PEER_ID_PREFIX("-aria2-");
 
 const std::string INFO_HASH("infoHash");
@@ -103,6 +111,12 @@ const std::string NAME("name");
 
 const std::string URL_LIST("urlList");
 
+const std::string CREATION_DATE("creationDate");
+
+const std::string COMMENT("comment");
+
+const std::string CREATED_BY("createdBy");
+
 const std::string BITTORRENT("bittorrent");
 
 const std::string MULTI("multi");
@@ -402,6 +416,24 @@ static void processRootDictionary
   // retrieve nodes
   extractNodes(torrent, rootDict[C_NODES]);
 
+  const BDE& creationDate = rootDict[C_CREATION_DATE];
+  if(creationDate.isInteger()) {
+    torrent[CREATION_DATE] = creationDate;
+  }
+  const BDE& commentUtf8 = rootDict[C_COMMENT_UTF8];
+  if(commentUtf8.isString()) {
+    torrent[COMMENT] = commentUtf8;
+  } else {
+    const BDE& comment = rootDict[C_COMMENT];
+    if(comment.isString()) {
+      torrent[COMMENT] = comment;
+    }
+  }
+  const BDE& createdBy = rootDict[C_CREATED_BY];
+  if(createdBy.isString()) {
+    torrent[CREATED_BY] = createdBy;
+  }
+
   ctx->setAttribute(BITTORRENT, torrent);
 }
 
@@ -498,6 +530,22 @@ void print(std::ostream& o, const SharedHandle<DownloadContext>& dctx)
 {
   const BDE& torrentAttrs = dctx->getAttribute(BITTORRENT);
   o << "*** BitTorrent File Information ***" << "\n";
+  if(torrentAttrs.containsKey(COMMENT)) {
+    o << "Comment: " << torrentAttrs[COMMENT].s() << "\n";
+  }
+  if(torrentAttrs.containsKey(CREATION_DATE)) {
+    struct tm* staticNowtmPtr;
+    char buf[26];
+    time_t t = torrentAttrs[CREATION_DATE].i();
+    if((staticNowtmPtr = localtime(&t)) != 0 &&
+       asctime_r(staticNowtmPtr, buf) != 0) {
+      // buf includes "\n"
+      o << "Creation Date: " << buf;
+    }
+  }
+  if(torrentAttrs.containsKey(CREATED_BY)) {
+    o << "Created By: " << torrentAttrs[CREATED_BY].s() << "\n";
+  }
   o << "Mode: " << torrentAttrs[MODE].s() << "\n";
   o << "Announce:" << "\n";
   const BDE& announceList = torrentAttrs[ANNOUNCE_LIST];

+ 6 - 0
src/bittorrent_helper.h

@@ -71,6 +71,12 @@ extern const std::string NAME;
 
 extern const std::string URL_LIST;
 
+extern const std::string CREATION_DATE;
+
+extern const std::string COMMENT;
+
+extern const std::string CREATED_BY;
+
 extern const std::string SINGLE;
 
 extern const std::string MULTI;

+ 16 - 0
test/BittorrentHelperTest.cc

@@ -51,6 +51,7 @@ class BittorrentHelperTest:public CppUnit::TestFixture {
   CPPUNIT_TEST(testSetFileFilter_single);
   CPPUNIT_TEST(testSetFileFilter_multi);
   CPPUNIT_TEST(testUTF8Torrent);
+  CPPUNIT_TEST(testMetaData);
   CPPUNIT_TEST_SUITE_END();
 public:
   void setUp() {
@@ -86,6 +87,7 @@ public:
   void testSetFileFilter_single();
   void testSetFileFilter_multi();
   void testUTF8Torrent();
+  void testMetaData();
 };
 
 
@@ -625,6 +627,20 @@ void BittorrentHelperTest::testUTF8Torrent()
   CPPUNIT_ASSERT_EQUAL(std::string("name in utf-8"), getName(dctx));
   CPPUNIT_ASSERT_EQUAL(std::string("./name in utf-8/path in utf-8"),
 		       dctx->getFirstFileEntry()->getPath());
+  CPPUNIT_ASSERT_EQUAL(std::string("This is utf8 comment."),
+		       dctx->getAttribute(BITTORRENT)[COMMENT].s());
+}
+
+void BittorrentHelperTest::testMetaData()
+{
+  SharedHandle<DownloadContext> dctx(new DownloadContext());
+  load("test.torrent", dctx);
+  CPPUNIT_ASSERT_EQUAL(std::string("REDNOAH.COM RULES"),
+		       dctx->getAttribute(BITTORRENT)[COMMENT].s());
+  CPPUNIT_ASSERT_EQUAL(std::string("aria2"),
+		       dctx->getAttribute(BITTORRENT)[CREATED_BY].s());
+  CPPUNIT_ASSERT_EQUAL((int64_t)1123456789,
+		       dctx->getAttribute(BITTORRENT)[CREATION_DATE].i());
 }
 
 } // namespace bittorrent

+ 1 - 1
test/test.torrent

@@ -1 +1 @@
-d8:announce36:http://aria.rednoah.com/announce.php13:announce-listll16:http://tracker1 el15:http://tracker2el15:http://tracker3ee7:privatei1e7:comment17:REDNOAH.COM RULES13:creation datei1123456789e4:infod5:filesld6:lengthi284e4:pathl5:aria23:src6:aria2ceed6:lengthi100e4:pathl19:aria2-0.2.2.tar.bz2eee4:name10:aria2-test12:piece lengthi128e6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCee
+d8:announce36:http://aria.rednoah.com/announce.php13:announce-listll16:http://tracker1 el15:http://tracker2el15:http://tracker3ee7:privatei1e7:comment17:REDNOAH.COM RULES13:creation datei1123456789e10:created by5:aria24:infod5:filesld6:lengthi284e4:pathl5:aria23:src6:aria2ceed6:lengthi100e4:pathl19:aria2-0.2.2.tar.bz2eee4:name10:aria2-test12:piece lengthi128e6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCee

+ 1 - 1
test/utf8.torrent

@@ -1 +1 @@
-d8:announce36:http://aria.rednoah.com/announce.php13:announce-listll16:http://tracker1 el15:http://tracker2el15:http://tracker3ee7:privatei1e7:comment17:REDNOAH.COM RULES13:creation datei1123456789e4:infod5:filesld6:lengthi284e4:pathl5:aria23:src6:aria2ce10:path.utf-8l13:path in utf-8eed6:lengthi100e4:pathl19:aria2-0.2.2.tar.bz2eee4:name10:aria2-test10:name.utf-813:name in utf-812:piece lengthi128e6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCee
+d8:announce36:http://aria.rednoah.com/announce.php13:announce-listll16:http://tracker1 el15:http://tracker2el15:http://tracker3ee7:privatei1e7:comment17:REDNOAH.COM RULES13:comment.utf-821:This is utf8 comment.13:creation datei1123456789e4:infod5:filesld6:lengthi284e4:pathl5:aria23:src6:aria2ce10:path.utf-8l13:path in utf-8eed6:lengthi100e4:pathl19:aria2-0.2.2.tar.bz2eee4:name10:aria2-test10:name.utf-813:name in utf-812:piece lengthi128e6:pieces60:AAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCee