OptionHandlerImpl.h 10 KB

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