OptionHandlerImpl.h 9.0 KB

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