OptionHandlerImpl.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <netinet/in.h>
  43. class NullOptionHandler : public OptionHandler {
  44. public:
  45. virtual ~NullOptionHandler() {}
  46. virtual bool canHandle(const string& optName) { return true; }
  47. virtual void parseArg(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 NumberOptionHandler : public NameMatchOptionHandler {
  66. private:
  67. int64_t _min;
  68. int64_t _max;
  69. public:
  70. NumberOptionHandler(const string& optName, int64_t min = -1, int64_t max = -1):NameMatchOptionHandler(optName), _min(min), _max(max) {}
  71. virtual ~NumberOptionHandler() {}
  72. virtual void parseArg(Option* option, const string& optarg)
  73. {
  74. int64_t num = strtoll(optarg.c_str(), 0, 10);
  75. parseArg(option, num);
  76. }
  77. void parseArg(Option* option, int64_t number)
  78. {
  79. if((_min == -1 || _min <= number) && (_max == -1 || number <= _max)) {
  80. option->put(_optName, Util::llitos(number));
  81. } else {
  82. string msg = _optName+" ";
  83. if(_min == -1 && _max != -1) {
  84. msg += _("must be smaller than or equal to %lld.");
  85. throw new FatalException(msg.c_str(), _max);
  86. } else if(_min != -1 && _max != -1) {
  87. msg += _("must be between %lld and %lld.");
  88. throw new FatalException(msg.c_str(), _min, _max);
  89. } else if(_min != -1 && _max == -1) {
  90. msg += _("must be greater than or equal to %lld.");
  91. throw new FatalException(msg.c_str(), _min);
  92. } else {
  93. msg += _("must be a number.");
  94. throw new FatalException(msg.c_str());
  95. }
  96. }
  97. }
  98. };
  99. class UnitNumberOptionHandler : public NumberOptionHandler {
  100. public:
  101. UnitNumberOptionHandler(const string& optName, int64_t min = -1, int64_t max = -1):NumberOptionHandler(optName, min, max) {}
  102. virtual ~UnitNumberOptionHandler() {}
  103. virtual void parseArg(Option* option, const string& optarg)
  104. {
  105. int64_t num = Util::getRealSize(optarg);
  106. NumberOptionHandler::parseArg(option, num);
  107. }
  108. };
  109. class FloatNumberOptionHandler : public NameMatchOptionHandler {
  110. private:
  111. double _min;
  112. double _max;
  113. public:
  114. FloatNumberOptionHandler(const string& optName, double min = -1, double max = -1):NameMatchOptionHandler(optName), _min(min), _max(max) {}
  115. virtual ~FloatNumberOptionHandler() {}
  116. virtual void parseArg(Option* option, const string& optarg)
  117. {
  118. double number = strtod(optarg.c_str(), 0);
  119. if((_min < 0 || _min <= number) && (_max < 0 || number <= _max)) {
  120. option->put(_optName, optarg);
  121. } else {
  122. string msg = _optName+" ";
  123. if(_min < 0 && _max >= 0) {
  124. msg += _("must be smaller than or equal to %.1f.");
  125. throw new FatalException(msg.c_str(), _max);
  126. } else if(_min >= 0 && _max >= 0) {
  127. msg += _("must be between %.1f and %.1f.");
  128. throw new FatalException(msg.c_str(), _min, _max);
  129. } else if(_min >= 0 && _max < 0) {
  130. msg += _("must be greater than or equal to %.1f.");
  131. throw new FatalException(msg.c_str(), _min);
  132. } else {
  133. msg += _("must be a number.");
  134. throw new FatalException(msg.c_str());
  135. }
  136. }
  137. }
  138. };
  139. class DefaultOptionHandler : public NameMatchOptionHandler {
  140. public:
  141. DefaultOptionHandler(const string& optName):NameMatchOptionHandler(optName) {}
  142. virtual ~DefaultOptionHandler() {}
  143. virtual void parseArg(Option* option, const string& optarg)
  144. {
  145. option->put(_optName, optarg);
  146. }
  147. };
  148. class ParameterOptionHandler : public NameMatchOptionHandler {
  149. private:
  150. Strings _validParamValues;
  151. public:
  152. ParameterOptionHandler(const string& optName, const Strings& validParamValues):
  153. NameMatchOptionHandler(optName), _validParamValues(validParamValues) {}
  154. ParameterOptionHandler(const string& optName, const string& validParamValue):
  155. NameMatchOptionHandler(optName)
  156. {
  157. _validParamValues.push_back(validParamValue);
  158. }
  159. ParameterOptionHandler(const string& optName,
  160. const string& validParamValue1,
  161. const string& validParamValue2):
  162. NameMatchOptionHandler(optName)
  163. {
  164. _validParamValues.push_back(validParamValue1);
  165. _validParamValues.push_back(validParamValue2);
  166. }
  167. virtual ~ParameterOptionHandler() {}
  168. virtual void parseArg(Option* option, const string& optarg)
  169. {
  170. Strings::const_iterator itr = find(_validParamValues.begin(), _validParamValues.end(), optarg);
  171. if(itr == _validParamValues.end()) {
  172. string msg = _optName+" "+_("must be one of the following:");
  173. if(_validParamValues.size() == 0) {
  174. msg += "''";
  175. } else {
  176. for(Strings::const_iterator itr = _validParamValues.begin();
  177. itr != _validParamValues.end(); ++itr) {
  178. msg += "'"+*itr+"' ";
  179. }
  180. }
  181. throw new FatalException(msg.c_str());
  182. } else {
  183. option->put(_optName, optarg);
  184. }
  185. }
  186. };
  187. class HttpProxyOptionHandler : public NameMatchOptionHandler {
  188. public:
  189. HttpProxyOptionHandler(const string& optName):NameMatchOptionHandler(optName) {}
  190. virtual ~HttpProxyOptionHandler() {}
  191. virtual void parseArg(Option* option, const string& optarg)
  192. {
  193. pair<string, string> proxy = Util::split(optarg, ":");
  194. in_port_t port = strtol(proxy.second.c_str(), 0, 10);
  195. if(proxy.first.empty() || proxy.second.empty() ||
  196. port <= 0) {
  197. throw new FatalException(_("unrecognized proxy format"));
  198. }
  199. option->put(PREF_HTTP_PROXY, optarg);
  200. option->put(PREF_HTTP_PROXY_HOST, proxy.first);
  201. option->put(PREF_HTTP_PROXY_PORT, Util::itos(port));
  202. option->put(PREF_HTTP_PROXY_ENABLED, V_TRUE);
  203. }
  204. };
  205. class LogOptionHandler : public NameMatchOptionHandler {
  206. public:
  207. LogOptionHandler(const string& optName):NameMatchOptionHandler(optName) {}
  208. virtual ~LogOptionHandler() {}
  209. virtual void parseArg(Option* option, const string& optarg)
  210. {
  211. if("-" == optarg) {
  212. option->put(PREF_STDOUT_LOG, V_TRUE);
  213. } else {
  214. option->put(PREF_LOG, optarg);
  215. }
  216. }
  217. };
  218. #endif // _D_OPTION_HANDLER_IMPL_H_