2008-12-04 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

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
pull/1/head
Tatsuhiro Tsujikawa 2008-12-04 13:05:05 +00:00
parent fe66fb6eae
commit 1e6a579258
7 changed files with 59 additions and 11 deletions

View File

@ -1,3 +1,15 @@
2008-12-04 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
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 <t-tujikawa@users.sourceforge.net>
Documented the default value of --check-certificate option in man

View File

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

View File

@ -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<std::string, std::string>::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<std::string, std::string>::const_iterator itr = table.find(name);
if(itr == table.end()) {

View File

@ -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;

View File

@ -141,7 +141,7 @@ int main(int argc, char* argv[])
int32_t returnValue = 0;
std::deque<SharedHandle<RequestGroup> > 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<SharedHandle<MetalinkEntry> > 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);

View File

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

View File

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