OptionHandlerImpl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #ifndef _D_OPTION_HANDLER_IMPL_H_
  36. #define _D_OPTION_HANDLER_IMPL_H_
  37. #include "OptionHandler.h"
  38. #include "NameMatchOptionHandler.h"
  39. #include "Util.h"
  40. #include "FatalException.h"
  41. #include "prefs.h"
  42. #include "Option.h"
  43. #include "StringFormat.h"
  44. #include <utility>
  45. #include <algorithm>
  46. namespace aria2 {
  47. class NullOptionHandler : public OptionHandler {
  48. public:
  49. virtual ~NullOptionHandler() {}
  50. virtual bool canHandle(const std::string& optName) { return true; }
  51. virtual void parse(Option* option, const std::string& arg) {}
  52. };
  53. class BooleanOptionHandler : public NameMatchOptionHandler {
  54. public:
  55. BooleanOptionHandler(const std::string& optName):NameMatchOptionHandler(optName) {}
  56. virtual ~BooleanOptionHandler() {}
  57. virtual void parseArg(Option* option, const std::string& optarg)
  58. {
  59. if(optarg == "true") {
  60. option->put(_optName, V_TRUE);
  61. } else if(optarg == "false") {
  62. option->put(_optName, V_FALSE);
  63. } else {
  64. std::string msg = _optName+" "+_("must be either 'true' or 'false'.");
  65. throw FatalException(msg);
  66. }
  67. }
  68. };
  69. class IntegerRangeOptionHandler : public NameMatchOptionHandler {
  70. private:
  71. int32_t _min;
  72. int32_t _max;
  73. public:
  74. IntegerRangeOptionHandler(const std::string& optName, int32_t min, int32_t max):NameMatchOptionHandler(optName), _min(min), _max(max) {}
  75. virtual ~IntegerRangeOptionHandler() {}
  76. virtual void parseArg(Option* option, const std::string& optarg)
  77. {
  78. IntSequence seq = Util::parseIntRange(optarg);
  79. while(seq.hasNext()) {
  80. int32_t v = seq.next();
  81. if(v < _min || _max < v) {
  82. std::string msg = _optName+" "+_("must be between %s and %s.");
  83. throw FatalException
  84. (StringFormat(msg.c_str(), Util::itos(_min).c_str(),
  85. Util::itos(_max).c_str()).str());
  86. }
  87. option->put(_optName, optarg);
  88. }
  89. }
  90. };
  91. class NumberOptionHandler : public NameMatchOptionHandler {
  92. private:
  93. int64_t _min;
  94. int64_t _max;
  95. public:
  96. NumberOptionHandler(const std::string& optName, int64_t min = -1, int64_t max = -1):NameMatchOptionHandler(optName), _min(min), _max(max) {}
  97. virtual ~NumberOptionHandler() {}
  98. virtual void parseArg(Option* option, const std::string& optarg)
  99. {
  100. int64_t num = Util::parseLLInt(optarg);
  101. parseArg(option, num);
  102. }
  103. void parseArg(Option* option, int64_t number)
  104. {
  105. if((_min == -1 || _min <= number) && (_max == -1 || number <= _max)) {
  106. option->put(_optName, Util::itos(number));
  107. } else {
  108. std::string msg = _optName+" ";
  109. if(_min == -1 && _max != -1) {
  110. msg += StringFormat(_("must be smaller than or equal to %s."),
  111. Util::itos(_max).c_str()).str();
  112. } else if(_min != -1 && _max != -1) {
  113. msg += StringFormat(_("must be between %s and %s."),
  114. Util::itos(_min).c_str(), Util::itos(_max).c_str()).str();
  115. } else if(_min != -1 && _max == -1) {
  116. msg += StringFormat(_("must be greater than or equal to %s."),
  117. Util::itos(_min).c_str()).str();
  118. } else {
  119. msg += _("must be a number.");
  120. }
  121. throw FatalException(msg);
  122. }
  123. }
  124. };
  125. class UnitNumberOptionHandler : public NumberOptionHandler {
  126. public:
  127. UnitNumberOptionHandler(const std::string& optName, int64_t min = -1, int64_t max = -1):NumberOptionHandler(optName, min, max) {}
  128. virtual ~UnitNumberOptionHandler() {}
  129. virtual void parseArg(Option* option, const std::string& optarg)
  130. {
  131. int64_t num = Util::getRealSize(optarg);
  132. NumberOptionHandler::parseArg(option, num);
  133. }
  134. };
  135. class FloatNumberOptionHandler : public NameMatchOptionHandler {
  136. private:
  137. double _min;
  138. double _max;
  139. public:
  140. FloatNumberOptionHandler(const std::string& optName, double min = -1, double max = -1):NameMatchOptionHandler(optName), _min(min), _max(max) {}
  141. virtual ~FloatNumberOptionHandler() {}
  142. virtual void parseArg(Option* option, const std::string& optarg)
  143. {
  144. double number = strtod(optarg.c_str(), 0);
  145. if((_min < 0 || _min <= number) && (_max < 0 || number <= _max)) {
  146. option->put(_optName, optarg);
  147. } else {
  148. std::string msg = _optName+" ";
  149. if(_min < 0 && _max >= 0) {
  150. msg += StringFormat(_("must be smaller than or equal to %.1f."),
  151. _max).str();
  152. } else if(_min >= 0 && _max >= 0) {
  153. msg += StringFormat(_("must be between %.1f and %.1f."),
  154. _min, _max).str();
  155. } else if(_min >= 0 && _max < 0) {
  156. msg += StringFormat(_("must be greater than or equal to %.1f."),
  157. _min).str();
  158. } else {
  159. msg += _("must be a number.");
  160. }
  161. throw FatalException(msg);
  162. }
  163. }
  164. };
  165. class DefaultOptionHandler : public NameMatchOptionHandler {
  166. public:
  167. DefaultOptionHandler(const std::string& optName):NameMatchOptionHandler(optName) {}
  168. virtual ~DefaultOptionHandler() {}
  169. virtual void parseArg(Option* option, const std::string& optarg)
  170. {
  171. option->put(_optName, optarg);
  172. }
  173. };
  174. class CumulativeOptionHandler : public NameMatchOptionHandler {
  175. private:
  176. std::string _delim;
  177. public:
  178. CumulativeOptionHandler(const std::string& optName,
  179. const std::string& delim):
  180. NameMatchOptionHandler(optName),
  181. _delim(delim) {}
  182. virtual ~CumulativeOptionHandler() {}
  183. virtual void parseArg(Option* option, const std::string& optarg)
  184. {
  185. std::string value = option->get(_optName);
  186. value += optarg+_delim;
  187. option->put(_optName, value);
  188. }
  189. };
  190. class ParameterOptionHandler : public NameMatchOptionHandler {
  191. private:
  192. std::deque<std::string> _validParamValues;
  193. public:
  194. ParameterOptionHandler(const std::string& optName, const std::deque<std::string>& validParamValues):
  195. NameMatchOptionHandler(optName), _validParamValues(validParamValues) {}
  196. ParameterOptionHandler(const std::string& optName, const std::string& validParamValue):
  197. NameMatchOptionHandler(optName)
  198. {
  199. _validParamValues.push_back(validParamValue);
  200. }
  201. ParameterOptionHandler(const std::string& optName,
  202. const std::string& validParamValue1,
  203. const std::string& validParamValue2):
  204. NameMatchOptionHandler(optName)
  205. {
  206. _validParamValues.push_back(validParamValue1);
  207. _validParamValues.push_back(validParamValue2);
  208. }
  209. ParameterOptionHandler(const std::string& optName,
  210. const std::string& validParamValue1,
  211. const std::string& validParamValue2,
  212. const std::string& validParamValue3):
  213. NameMatchOptionHandler(optName)
  214. {
  215. _validParamValues.push_back(validParamValue1);
  216. _validParamValues.push_back(validParamValue2);
  217. _validParamValues.push_back(validParamValue3);
  218. }
  219. virtual ~ParameterOptionHandler() {}
  220. virtual void parseArg(Option* option, const std::string& optarg)
  221. {
  222. std::deque<std::string>::const_iterator itr =
  223. std::find(_validParamValues.begin(), _validParamValues.end(), optarg);
  224. if(itr == _validParamValues.end()) {
  225. std::string msg = _optName+" "+_("must be one of the following:");
  226. if(_validParamValues.size() == 0) {
  227. msg += "''";
  228. } else {
  229. for(std::deque<std::string>::const_iterator itr = _validParamValues.begin();
  230. itr != _validParamValues.end(); ++itr) {
  231. msg += "'"+*itr+"' ";
  232. }
  233. }
  234. throw FatalException(msg);
  235. } else {
  236. option->put(_optName, optarg);
  237. }
  238. }
  239. };
  240. class HostPortOptionHandler : public NameMatchOptionHandler {
  241. private:
  242. std::string _hostOptionName;
  243. std::string _portOptionName;
  244. public:
  245. HostPortOptionHandler(const std::string& optName,
  246. const std::string& hostOptionName,
  247. const std::string& portOptionName):
  248. NameMatchOptionHandler(optName),
  249. _hostOptionName(hostOptionName),
  250. _portOptionName(portOptionName) {}
  251. virtual ~HostPortOptionHandler() {}
  252. virtual void parseArg(Option* option, const std::string& optarg)
  253. {
  254. std::pair<std::string, std::string> proxy = Util::split(optarg, ":");
  255. int32_t port = Util::parseInt(proxy.second);
  256. if(proxy.first.empty() || proxy.second.empty() ||
  257. port <= 0 || 65535 < port) {
  258. throw FatalException(_("unrecognized proxy format"));
  259. }
  260. option->put(_optName, optarg);
  261. setHostAndPort(option, proxy.first, port);
  262. }
  263. void setHostAndPort(Option* option, const std::string& hostname, uint16_t port)
  264. {
  265. option->put(_hostOptionName, hostname);
  266. option->put(_portOptionName, Util::uitos(port));
  267. }
  268. };
  269. class HttpProxyOptionHandler : public HostPortOptionHandler {
  270. public:
  271. HttpProxyOptionHandler(const std::string& optName,
  272. const std::string& hostOptionName,
  273. const std::string& portOptionName):
  274. HostPortOptionHandler(optName, hostOptionName, portOptionName) {}
  275. virtual ~HttpProxyOptionHandler() {}
  276. virtual void parseArg(Option* option, const std::string& optarg)
  277. {
  278. HostPortOptionHandler::parseArg(option, optarg);
  279. option->put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  280. }
  281. };
  282. class LogOptionHandler : public NameMatchOptionHandler {
  283. public:
  284. LogOptionHandler(const std::string& optName):NameMatchOptionHandler(optName) {}
  285. virtual ~LogOptionHandler() {}
  286. virtual void parseArg(Option* option, const std::string& optarg)
  287. {
  288. if("-" == optarg) {
  289. option->put(PREF_STDOUT_LOG, V_TRUE);
  290. } else {
  291. option->put(PREF_LOG, optarg);
  292. }
  293. }
  294. };
  295. } // namespace aria2
  296. #endif // _D_OPTION_HANDLER_IMPL_H_