OptionHandlerImpl.h 7.9 KB

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