OptionParser.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "OptionParser.h"
  36. #include "Util.h"
  37. #include "OptionHandlerImpl.h"
  38. #include "Option.h"
  39. #include "A2STR.h"
  40. #include "a2functional.h"
  41. #include <istream>
  42. #include <utility>
  43. namespace aria2 {
  44. void OptionParser::parse(Option* option, std::istream& is)
  45. {
  46. std::string line;
  47. int32_t linenum = 0;
  48. while(getline(is, line)) {
  49. ++linenum;
  50. if(Util::startsWith(line, A2STR::SHARP_C)) {
  51. continue;
  52. }
  53. std::pair<std::string, std::string> nv = Util::split(line, A2STR::EQUAL_C);
  54. OptionHandlerHandle handler = getOptionHandlerByName(nv.first);
  55. handler->parse(option, nv.second);
  56. }
  57. }
  58. OptionHandlerHandle OptionParser::getOptionHandlerByName(const std::string& optName)
  59. {
  60. for(OptionHandlers::iterator itr = _optionHandlers.begin();
  61. itr != _optionHandlers.end(); ++itr) {
  62. if((*itr)->canHandle(optName)) {
  63. return *itr;
  64. }
  65. }
  66. return SharedHandle<OptionHandler>(new NullOptionHandler());
  67. }
  68. void OptionParser::setOptionHandlers(const std::deque<SharedHandle<OptionHandler> >& optionHandlers)
  69. {
  70. _optionHandlers = optionHandlers;
  71. }
  72. void OptionParser::addOptionHandler
  73. (const SharedHandle<OptionHandler>& optionHandler)
  74. {
  75. _optionHandlers.push_back(optionHandler);
  76. }
  77. void OptionParser::parseDefaultValues(Option* option) const
  78. {
  79. for(std::deque<SharedHandle<OptionHandler> >::const_iterator i =
  80. _optionHandlers.begin(); i != _optionHandlers.end(); ++i) {
  81. if(!(*i)->getDefaultValue().empty()) {
  82. (*i)->parse(option, (*i)->getDefaultValue());
  83. }
  84. }
  85. }
  86. class FindByTag :
  87. public std::unary_function<SharedHandle<OptionHandler>, bool> {
  88. private:
  89. std::string _tag;
  90. public:
  91. FindByTag(const std::string& tag):_tag(tag) {}
  92. bool operator()(const SharedHandle<OptionHandler>& optionHandler) const
  93. {
  94. return !optionHandler->isHidden() && optionHandler->hasTag(_tag);
  95. }
  96. };
  97. std::deque<SharedHandle<OptionHandler> >
  98. OptionParser::findByTag(const std::string& tag) const
  99. {
  100. std::deque<SharedHandle<OptionHandler> > result;
  101. std::remove_copy_if(_optionHandlers.begin(), _optionHandlers.end(),
  102. std::back_inserter(result),
  103. std::not1(FindByTag(tag)));
  104. return result;
  105. }
  106. class FindByNameSubstring :
  107. public std::unary_function<SharedHandle<OptionHandler> , bool> {
  108. private:
  109. std::string _substring;
  110. public:
  111. FindByNameSubstring(const std::string& substring):_substring(substring) {}
  112. bool operator()(const SharedHandle<OptionHandler>& optionHandler) const
  113. {
  114. return !optionHandler->isHidden() &&
  115. optionHandler->getName().find(_substring) != std::string::npos;
  116. }
  117. };
  118. std::deque<SharedHandle<OptionHandler> >
  119. OptionParser::findByNameSubstring(const std::string& substring) const
  120. {
  121. std::deque<SharedHandle<OptionHandler> > result;
  122. std::remove_copy_if(_optionHandlers.begin(), _optionHandlers.end(),
  123. std::back_inserter(result),
  124. std::not1(FindByNameSubstring(substring)));
  125. return result;
  126. }
  127. class FindByName :
  128. public std::unary_function<SharedHandle<OptionHandler> , bool> {
  129. private:
  130. std::string _name;
  131. public:
  132. FindByName(const std::string& name):_name(name) {}
  133. bool operator()(const SharedHandle<OptionHandler>& optionHandler) const
  134. {
  135. return !optionHandler->isHidden() && optionHandler->getName() == _name;
  136. }
  137. };
  138. std::deque<SharedHandle<OptionHandler> > OptionParser::findAll() const
  139. {
  140. std::deque<SharedHandle<OptionHandler> > result;
  141. std::remove_copy_if(_optionHandlers.begin(), _optionHandlers.end(),
  142. std::back_inserter(result),
  143. mem_fun_sh(&OptionHandler::isHidden));
  144. return result;
  145. }
  146. SharedHandle<OptionHandler>
  147. OptionParser::findByName(const std::string& name) const
  148. {
  149. std::deque<SharedHandle<OptionHandler> >::const_iterator i =
  150. std::find_if(_optionHandlers.begin(), _optionHandlers.end(),
  151. FindByName(name));
  152. if(i == _optionHandlers.end()) {
  153. return SharedHandle<OptionHandler>();
  154. } else {
  155. return *i;
  156. }
  157. }
  158. const std::deque<SharedHandle<OptionHandler> >&
  159. OptionParser::getOptionHandlers() const
  160. {
  161. return _optionHandlers;
  162. }
  163. } // namespace aria2