diff --git a/ChangeLog b/ChangeLog index c4ba0b9d..c951a193 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2008-12-04 Tatsuhiro Tsujikawa + + Added Option::blank(). + Use !Option::blank(name) instead of Option::defined(name) for + the options that take filename. + * src/MultiUrlRequestInfo.cc + * src/Option.cc + * src/Option.h + * src/main.cc + * src/option_processing.cc + * test/OptionTest.cc + 2008-12-04 Tatsuhiro Tsujikawa Documented the default value of --check-certificate option in man diff --git a/src/MultiUrlRequestInfo.cc b/src/MultiUrlRequestInfo.cc index 73684514..ddd1a87e 100644 --- a/src/MultiUrlRequestInfo.cc +++ b/src/MultiUrlRequestInfo.cc @@ -107,7 +107,7 @@ int MultiUrlRequestInfo::execute() DownloadEngineFactory().newDownloadEngine(_option, _requestGroups); try { - if(_option->defined(PREF_LOAD_COOKIES)) { + if(!_option->blank(PREF_LOAD_COOKIES)) { File cookieFile(_option->get(PREF_LOAD_COOKIES)); if(cookieFile.isFile()) { e->getCookieStorage()->load(_option->get(PREF_LOAD_COOKIES)); @@ -138,12 +138,12 @@ int MultiUrlRequestInfo::execute() #ifdef ENABLE_SSL SharedHandle tlsContext(new TLSContext()); - if(_option->defined(PREF_CERTIFICATE) && - _option->defined(PREF_PRIVATE_KEY)) { + if(!_option->blank(PREF_CERTIFICATE) && + !_option->blank(PREF_PRIVATE_KEY)) { tlsContext->addClientKeyFile(_option->get(PREF_CERTIFICATE), _option->get(PREF_PRIVATE_KEY)); } - if(_option->defined(PREF_CA_CERTIFICATE)) { + if(!_option->blank(PREF_CA_CERTIFICATE)) { try { tlsContext->addTrustedCACertFile(_option->get(PREF_CA_CERTIFICATE)); } catch(RecoverableException& e) { diff --git a/src/Option.cc b/src/Option.cc index a2d34e6d..b464ec50 100644 --- a/src/Option.cc +++ b/src/Option.cc @@ -48,10 +48,17 @@ void Option::put(const std::string& name, const std::string& value) { table[name] = value; } -bool Option::defined(const std::string& name) const { +bool Option::defined(const std::string& name) const +{ return table.count(name) == 1; } +bool Option::blank(const std::string& name) const +{ + std::map::const_iterator i = table.find(name); + return i == table.end() || (*i).second.empty(); +} + const std::string& Option::get(const std::string& name) const { std::map::const_iterator itr = table.find(name); if(itr == table.end()) { diff --git a/src/Option.h b/src/Option.h index e7e380a5..13818982 100644 --- a/src/Option.h +++ b/src/Option.h @@ -49,7 +49,12 @@ public: ~Option(); void put(const std::string& name, const std::string& value); + // Returns true if name is defined. Otherwise returns false. + // Note that even if the value is a empty string, this method returns true. bool defined(const std::string& name) const; + // Returns true if name is not defined or the value is a empty string. + // Otherwise returns false. + bool blank(const std::string& name) const; const std::string& get(const std::string& name) const; int32_t getAsInt(const std::string& name) const; int64_t getAsLLInt(const std::string& name) const; diff --git a/src/main.cc b/src/main.cc index 26405810..714db62d 100644 --- a/src/main.cc +++ b/src/main.cc @@ -141,7 +141,7 @@ int main(int argc, char* argv[]) int32_t returnValue = 0; std::deque > requestGroups; #ifdef ENABLE_BITTORRENT - if(op->defined(PREF_TORRENT_FILE)) { + if(!op->blank(PREF_TORRENT_FILE)) { if(op->get(PREF_SHOW_FILES) == V_TRUE) { DefaultBtContextHandle btContext(new DefaultBtContext()); btContext->load(op->get(PREF_TORRENT_FILE)); @@ -154,7 +154,7 @@ int main(int argc, char* argv[]) else #endif // ENABLE_BITTORRENT #ifdef ENABLE_METALINK - if(op->defined(PREF_METALINK_FILE)) { + if(!op->blank(PREF_METALINK_FILE)) { if(op->get(PREF_SHOW_FILES) == V_TRUE) { std::deque > metalinkEntries; MetalinkHelper::parseAndQuery(metalinkEntries, @@ -169,7 +169,7 @@ int main(int argc, char* argv[]) } else #endif // ENABLE_METALINK - if(op->defined(PREF_INPUT_FILE)) { + if(!op->blank(PREF_INPUT_FILE)) { createRequestGroupForUriList(requestGroups, op); } else { createRequestGroupForUri(requestGroups, op, args); diff --git a/src/option_processing.cc b/src/option_processing.cc index 169806a8..523658a3 100644 --- a/src/option_processing.cc +++ b/src/option_processing.cc @@ -617,12 +617,12 @@ Option* option_processing(int argc, char* const argv[]) } if( #ifdef ENABLE_BITTORRENT - !op->defined(PREF_TORRENT_FILE) && + op->blank(PREF_TORRENT_FILE) && #endif // ENABLE_BITTORRENT #ifdef ENABLE_METALINK - !op->defined(PREF_METALINK_FILE) && + op->blank(PREF_METALINK_FILE) && #endif // ENABLE_METALINK - !op->defined(PREF_INPUT_FILE)) { + op->blank(PREF_INPUT_FILE)) { if(optind == argc) { std::cerr << MSG_URI_REQUIRED << std::endl; showUsage(TAG_HELP, oparser); diff --git a/test/OptionTest.cc b/test/OptionTest.cc index c219103a..306505a8 100644 --- a/test/OptionTest.cc +++ b/test/OptionTest.cc @@ -10,6 +10,8 @@ class OptionTest:public CppUnit::TestFixture { CPPUNIT_TEST(testPutAndGet); CPPUNIT_TEST(testPutAndGetAsInt); CPPUNIT_TEST(testPutAndGetAsDouble); + CPPUNIT_TEST(testDefined); + CPPUNIT_TEST(testBlank); CPPUNIT_TEST_SUITE_END(); private: @@ -20,6 +22,8 @@ public: void testPutAndGet(); void testPutAndGetAsInt(); void testPutAndGetAsDouble(); + void testDefined(); + void testBlank(); }; @@ -48,4 +52,24 @@ void OptionTest::testPutAndGetAsDouble() { CPPUNIT_ASSERT_EQUAL(10.0, op.getAsDouble("key")); } +void OptionTest::testDefined() +{ + Option op; + op.put("k", "v"); + op.put("k1", ""); + CPPUNIT_ASSERT(op.defined("k")); + CPPUNIT_ASSERT(op.defined("k1")); + CPPUNIT_ASSERT(!op.defined("undefined")); +} + +void OptionTest::testBlank() +{ + Option op; + op.put("k", "v"); + op.put("k1", ""); + CPPUNIT_ASSERT(!op.blank("k")); + CPPUNIT_ASSERT(op.blank("k1")); + CPPUNIT_ASSERT(op.blank("undefined")); +} + } // namespace aria2