OptionHandlerImpl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 <vector>
  39. #include "AbstractOptionHandler.h"
  40. #include "A2STR.h"
  41. namespace aria2 {
  42. class Option;
  43. struct Pref;
  44. class BooleanOptionHandler : public AbstractOptionHandler {
  45. public:
  46. BooleanOptionHandler(PrefPtr pref, const char* description = NO_DESCRIPTION,
  47. const std::string& defaultValue = NO_DEFAULT_VALUE,
  48. OptionHandler::ARG_TYPE argType = OptionHandler::REQ_ARG,
  49. char shortName = 0);
  50. virtual ~BooleanOptionHandler();
  51. virtual void parseArg(Option& option,
  52. const std::string& optarg) const CXX11_OVERRIDE;
  53. virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
  54. };
  55. class IntegerRangeOptionHandler : public AbstractOptionHandler {
  56. private:
  57. int32_t min_;
  58. int32_t max_;
  59. public:
  60. IntegerRangeOptionHandler(PrefPtr pref, const char* description,
  61. const std::string& defaultValue, int32_t min,
  62. int32_t max, char shortName = 0);
  63. virtual ~IntegerRangeOptionHandler();
  64. virtual void parseArg(Option& option,
  65. const std::string& optarg) const CXX11_OVERRIDE;
  66. virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
  67. };
  68. class NumberOptionHandler : public AbstractOptionHandler {
  69. private:
  70. int64_t min_;
  71. int64_t max_;
  72. public:
  73. NumberOptionHandler(PrefPtr pref, const char* description = NO_DESCRIPTION,
  74. const std::string& defaultValue = NO_DEFAULT_VALUE,
  75. int64_t min = -1, int64_t max = -1, char shortName = 0);
  76. virtual ~NumberOptionHandler();
  77. virtual void parseArg(Option& option,
  78. const std::string& optarg) const CXX11_OVERRIDE;
  79. void parseArg(Option& option, int64_t number) const;
  80. virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
  81. };
  82. class UnitNumberOptionHandler : public NumberOptionHandler {
  83. public:
  84. UnitNumberOptionHandler(PrefPtr pref,
  85. const char* description = NO_DESCRIPTION,
  86. const std::string& defaultValue = NO_DEFAULT_VALUE,
  87. int64_t min = -1, int64_t max = -1,
  88. char shortName = 0);
  89. virtual ~UnitNumberOptionHandler();
  90. virtual void parseArg(Option& option,
  91. const std::string& optarg) const CXX11_OVERRIDE;
  92. };
  93. class FloatNumberOptionHandler : public AbstractOptionHandler {
  94. private:
  95. double min_;
  96. double max_;
  97. public:
  98. FloatNumberOptionHandler(PrefPtr pref,
  99. const char* description = NO_DESCRIPTION,
  100. const std::string& defaultValue = NO_DEFAULT_VALUE,
  101. double min = -1, double max = -1,
  102. char shortName = 0);
  103. virtual ~FloatNumberOptionHandler();
  104. virtual void parseArg(Option& option,
  105. const std::string& optarg) const CXX11_OVERRIDE;
  106. virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
  107. };
  108. class DefaultOptionHandler : public AbstractOptionHandler {
  109. private:
  110. std::string possibleValuesString_;
  111. bool allowEmpty_;
  112. public:
  113. DefaultOptionHandler(PrefPtr pref, const char* description = NO_DESCRIPTION,
  114. const std::string& defaultValue = NO_DEFAULT_VALUE,
  115. const std::string& possibleValuesString = A2STR::NIL,
  116. OptionHandler::ARG_TYPE argType = OptionHandler::REQ_ARG,
  117. char shortName = 0);
  118. virtual ~DefaultOptionHandler();
  119. virtual void parseArg(Option& option,
  120. const std::string& optarg) const CXX11_OVERRIDE;
  121. virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
  122. void setAllowEmpty(bool allow);
  123. };
  124. class CumulativeOptionHandler : public AbstractOptionHandler {
  125. private:
  126. std::string delim_;
  127. std::string possibleValuesString_;
  128. public:
  129. CumulativeOptionHandler(
  130. PrefPtr pref, const char* description, const std::string& defaultValue,
  131. const std::string& delim,
  132. const std::string& possibleValuesString = A2STR::NIL,
  133. OptionHandler::ARG_TYPE argType = OptionHandler::REQ_ARG,
  134. char shortName = 0);
  135. virtual ~CumulativeOptionHandler();
  136. virtual void parseArg(Option& option,
  137. const std::string& optarg) const CXX11_OVERRIDE;
  138. virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
  139. };
  140. class IndexOutOptionHandler : public AbstractOptionHandler {
  141. public:
  142. IndexOutOptionHandler(PrefPtr pref, const char* description,
  143. char shortName = 0);
  144. virtual ~IndexOutOptionHandler();
  145. virtual void parseArg(Option& option,
  146. const std::string& optarg) const CXX11_OVERRIDE;
  147. virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
  148. };
  149. class ChecksumOptionHandler : public AbstractOptionHandler {
  150. public:
  151. ChecksumOptionHandler(PrefPtr pref, const char* description,
  152. char shortName = 0);
  153. ChecksumOptionHandler(PrefPtr pref, const char* description,
  154. std::vector<std::string> acceptableTypes,
  155. char shortName = 0);
  156. virtual ~ChecksumOptionHandler();
  157. virtual void parseArg(Option& option,
  158. const std::string& optarg) const CXX11_OVERRIDE;
  159. virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
  160. private:
  161. // message digest type acceptable for this option. Empty means that
  162. // it accepts all supported types.
  163. std::vector<std::string> acceptableTypes_;
  164. };
  165. class ParameterOptionHandler : public AbstractOptionHandler {
  166. private:
  167. std::vector<std::string> validParamValues_;
  168. public:
  169. ParameterOptionHandler(PrefPtr pref, const char* description,
  170. const std::string& defaultValue,
  171. std::vector<std::string> validParamValues,
  172. char shortName = 0);
  173. virtual ~ParameterOptionHandler();
  174. virtual void parseArg(Option& option,
  175. const std::string& optarg) const CXX11_OVERRIDE;
  176. virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
  177. };
  178. class HostPortOptionHandler : public AbstractOptionHandler {
  179. private:
  180. PrefPtr hostOptionName_;
  181. PrefPtr portOptionName_;
  182. public:
  183. HostPortOptionHandler(PrefPtr pref, const char* description,
  184. const std::string& defaultValue, PrefPtr hostOptionName,
  185. PrefPtr portOptionName, char shortName = 0);
  186. virtual ~HostPortOptionHandler();
  187. virtual void parseArg(Option& option,
  188. const std::string& optarg) const CXX11_OVERRIDE;
  189. void setHostAndPort(Option& option, const std::string& hostname,
  190. uint16_t port) const;
  191. virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
  192. };
  193. class HttpProxyOptionHandler : public AbstractOptionHandler {
  194. private:
  195. PrefPtr proxyUserPref_;
  196. PrefPtr proxyPasswdPref_;
  197. public:
  198. HttpProxyOptionHandler(PrefPtr pref, const char* description,
  199. const std::string& defaultValue, char shortName = 0);
  200. virtual ~HttpProxyOptionHandler();
  201. virtual void parseArg(Option& option,
  202. const std::string& optarg) const CXX11_OVERRIDE;
  203. virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
  204. };
  205. class LocalFilePathOptionHandler : public AbstractOptionHandler {
  206. private:
  207. bool acceptStdin_;
  208. public:
  209. LocalFilePathOptionHandler(PrefPtr pref,
  210. const char* description = NO_DESCRIPTION,
  211. const std::string& defaultValue = NO_DEFAULT_VALUE,
  212. bool acceptStdin = false, char shortName = 0);
  213. virtual void parseArg(Option& option,
  214. const std::string& optarg) const CXX11_OVERRIDE;
  215. virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
  216. };
  217. class PrioritizePieceOptionHandler : public AbstractOptionHandler {
  218. public:
  219. PrioritizePieceOptionHandler(
  220. PrefPtr pref, const char* description = NO_DESCRIPTION,
  221. const std::string& defaultValue = NO_DEFAULT_VALUE, char shortName = 0);
  222. virtual void parseArg(Option& option,
  223. const std::string& optarg) const CXX11_OVERRIDE;
  224. virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
  225. };
  226. class OptimizeConcurrentDownloadsOptionHandler : public AbstractOptionHandler {
  227. public:
  228. OptimizeConcurrentDownloadsOptionHandler(
  229. PrefPtr pref, const char* description = NO_DESCRIPTION,
  230. const std::string& defaultValue = NO_DEFAULT_VALUE, char shortName = 0);
  231. virtual void parseArg(Option& option,
  232. const std::string& optarg) const CXX11_OVERRIDE;
  233. virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
  234. };
  235. // This class is used to deprecate option and optionally handle its
  236. // option value using replacing option.
  237. class DeprecatedOptionHandler : public OptionHandler {
  238. private:
  239. OptionHandler* depOptHandler_;
  240. const OptionHandler* repOptHandler_;
  241. bool stillWork_;
  242. std::string additionalMessage_;
  243. public:
  244. // depOptHandler is deprecated option and repOptHandler is replacing
  245. // new option. If there is no replacing option, specify nullptr. If
  246. // there is no replacing option, but the option still lives, give
  247. // true to stillWork. Set additional message to additionalMessage.
  248. DeprecatedOptionHandler(OptionHandler* depOptHandler,
  249. const OptionHandler* repOptHandler = nullptr,
  250. bool stillWork = false,
  251. std::string additionalMessage = "");
  252. virtual ~DeprecatedOptionHandler();
  253. virtual void parse(Option& option,
  254. const std::string& arg) const CXX11_OVERRIDE;
  255. virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
  256. virtual bool hasTag(uint32_t tag) const CXX11_OVERRIDE;
  257. virtual void addTag(uint32_t tag) CXX11_OVERRIDE;
  258. virtual std::string toTagString() const CXX11_OVERRIDE;
  259. virtual const char* getName() const CXX11_OVERRIDE;
  260. virtual const char* getDescription() const CXX11_OVERRIDE;
  261. virtual const std::string& getDefaultValue() const CXX11_OVERRIDE;
  262. virtual bool isHidden() const CXX11_OVERRIDE;
  263. virtual void hide() CXX11_OVERRIDE;
  264. virtual PrefPtr getPref() const CXX11_OVERRIDE;
  265. virtual ARG_TYPE getArgType() const CXX11_OVERRIDE;
  266. virtual char getShortName() const CXX11_OVERRIDE;
  267. virtual bool getEraseAfterParse() const CXX11_OVERRIDE;
  268. virtual void setEraseAfterParse(bool eraseAfterParse) CXX11_OVERRIDE;
  269. virtual bool getInitialOption() const CXX11_OVERRIDE;
  270. virtual void setInitialOption(bool f) CXX11_OVERRIDE;
  271. virtual bool getChangeOption() const CXX11_OVERRIDE;
  272. virtual void setChangeOption(bool f) CXX11_OVERRIDE;
  273. virtual bool getChangeOptionForReserved() const CXX11_OVERRIDE;
  274. virtual void setChangeOptionForReserved(bool f) CXX11_OVERRIDE;
  275. virtual bool getChangeGlobalOption() const CXX11_OVERRIDE;
  276. virtual void setChangeGlobalOption(bool f) CXX11_OVERRIDE;
  277. virtual bool getCumulative() const CXX11_OVERRIDE;
  278. virtual void setCumulative(bool f) CXX11_OVERRIDE;
  279. };
  280. } // namespace aria2
  281. #endif // D_OPTION_HANDLER_IMPL_H