Rewritten parseIndexPath. Renamed createIndexPathMap as createIndexPaths.

pull/2/head
Tatsuhiro Tsujikawa 2011-11-02 00:18:39 +09:00
parent 701a2d6ff8
commit f884ad8339
4 changed files with 21 additions and 21 deletions

View File

@ -189,10 +189,10 @@ createBtRequestGroup(const std::string& torrentFilePath,
sgl.normalize(); sgl.normalize();
dctx->setFileFilter(sgl); dctx->setFileFilter(sgl);
std::istringstream indexOutIn(option->get(PREF_INDEX_OUT)); std::istringstream indexOutIn(option->get(PREF_INDEX_OUT));
std::map<size_t, std::string> indexPathMap = std::vector<std::pair<size_t, std::string> > indexPaths =
util::createIndexPathMap(indexOutIn); util::createIndexPaths(indexOutIn);
for(std::map<size_t, std::string>::const_iterator i = indexPathMap.begin(), for(std::vector<std::pair<size_t, std::string> >::const_iterator i =
eoi = indexPathMap.end(); i != eoi; ++i) { indexPaths.begin(), eoi = indexPaths.end(); i != eoi; ++i) {
dctx->setFilePathWithIndex dctx->setFilePathWithIndex
((*i).first, util::applyDir(option->get(PREF_DIR), (*i).second)); ((*i).first, util::applyDir(option->get(PREF_DIR), (*i).second));
} }

View File

@ -1370,7 +1370,7 @@ std::string htmlEscape(const std::string& src)
return dest; return dest;
} }
std::map<size_t, std::string>::value_type std::pair<size_t, std::string>
parseIndexPath(const std::string& line) parseIndexPath(const std::string& line)
{ {
std::pair<std::string, std::string> p; std::pair<std::string, std::string> p;
@ -1380,17 +1380,17 @@ parseIndexPath(const std::string& line)
throw DL_ABORT_EX(fmt("Path with index=%u is empty.", throw DL_ABORT_EX(fmt("Path with index=%u is empty.",
static_cast<unsigned int>(index))); static_cast<unsigned int>(index)));
} }
return std::map<size_t, std::string>::value_type(index, p.second); return std::make_pair(index, p.second);
} }
std::map<size_t, std::string> createIndexPathMap(std::istream& i) std::vector<std::pair<size_t, std::string> > createIndexPaths(std::istream& i)
{ {
std::map<size_t, std::string> indexPathMap; std::vector<std::pair<size_t, std::string> > indexPaths;
std::string line; std::string line;
while(getline(i, line)) { while(getline(i, line)) {
indexPathMap.insert(indexPathMap.begin(), parseIndexPath(line)); indexPaths.push_back(parseIndexPath(line));
} }
return indexPathMap; return indexPaths;
} }
namespace { namespace {

View File

@ -347,10 +347,10 @@ std::string joinPath(InputIterator first, InputIterator last)
// Parses INDEX=PATH format string. INDEX must be an unsigned // Parses INDEX=PATH format string. INDEX must be an unsigned
// integer. // integer.
std::map<size_t, std::string>::value_type std::pair<size_t, std::string>
parseIndexPath(const std::string& line); parseIndexPath(const std::string& line);
std::map<size_t, std::string> createIndexPathMap(std::istream& i); std::vector<std::pair<size_t, std::string> > createIndexPaths(std::istream& i);
/** /**
* Take a string src which is a delimited list and add its elements * Take a string src which is a delimited list and add its elements

View File

@ -62,7 +62,7 @@ class UtilTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testHtmlEscape); CPPUNIT_TEST(testHtmlEscape);
CPPUNIT_TEST(testJoinPath); CPPUNIT_TEST(testJoinPath);
CPPUNIT_TEST(testParseIndexPath); CPPUNIT_TEST(testParseIndexPath);
CPPUNIT_TEST(testCreateIndexPathMap); CPPUNIT_TEST(testCreateIndexPaths);
CPPUNIT_TEST(testGenerateRandomData); CPPUNIT_TEST(testGenerateRandomData);
CPPUNIT_TEST(testFromHex); CPPUNIT_TEST(testFromHex);
CPPUNIT_TEST(testParsePrioritizePieceRange); CPPUNIT_TEST(testParsePrioritizePieceRange);
@ -122,7 +122,7 @@ public:
void testHtmlEscape(); void testHtmlEscape();
void testJoinPath(); void testJoinPath();
void testParseIndexPath(); void testParseIndexPath();
void testCreateIndexPathMap(); void testCreateIndexPaths();
void testGenerateRandomData(); void testGenerateRandomData();
void testFromHex(); void testFromHex();
void testParsePrioritizePieceRange(); void testParsePrioritizePieceRange();
@ -1015,7 +1015,7 @@ void UtilTest::testJoinPath()
void UtilTest::testParseIndexPath() void UtilTest::testParseIndexPath()
{ {
std::map<size_t, std::string>::value_type p = util::parseIndexPath("1=foo"); std::pair<size_t, std::string> p = util::parseIndexPath("1=foo");
CPPUNIT_ASSERT_EQUAL((size_t)1, p.first); CPPUNIT_ASSERT_EQUAL((size_t)1, p.first);
CPPUNIT_ASSERT_EQUAL(std::string("foo"), p.second); CPPUNIT_ASSERT_EQUAL(std::string("foo"), p.second);
try { try {
@ -1032,17 +1032,17 @@ void UtilTest::testParseIndexPath()
} }
} }
void UtilTest::testCreateIndexPathMap() void UtilTest::testCreateIndexPaths()
{ {
std::stringstream in std::stringstream in
("1=/tmp/myfile\n" ("1=/tmp/myfile\n"
"100=/myhome/mypicture.png\n"); "100=/myhome/mypicture.png\n");
std::map<size_t, std::string> m = util::createIndexPathMap(in); std::vector<std::pair<size_t, std::string> > m = util::createIndexPaths(in);
CPPUNIT_ASSERT_EQUAL((size_t)2, m.size()); CPPUNIT_ASSERT_EQUAL((size_t)2, m.size());
CPPUNIT_ASSERT(m.find(1) != m.end()); CPPUNIT_ASSERT_EQUAL((size_t)1, m[0].first);
CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), m[1]); CPPUNIT_ASSERT_EQUAL(std::string("/tmp/myfile"), m[0].second);
CPPUNIT_ASSERT(m.find(100) != m.end()); CPPUNIT_ASSERT_EQUAL((size_t)100, m[1].first);
CPPUNIT_ASSERT_EQUAL(std::string("/myhome/mypicture.png"), m[100]); CPPUNIT_ASSERT_EQUAL(std::string("/myhome/mypicture.png"), m[1].second);
} }
void UtilTest::testGenerateRandomData() void UtilTest::testGenerateRandomData()