AbstractOptionHandler.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 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. #include "AbstractOptionHandler.h"
  36. #include <algorithm>
  37. #include "OptionHandlerException.h"
  38. #include "a2functional.h"
  39. #include "Option.h"
  40. #include "prefs.h"
  41. #include "help_tags.h"
  42. namespace aria2 {
  43. AbstractOptionHandler::AbstractOptionHandler(PrefPtr pref,
  44. const char* description,
  45. const std::string& defaultValue,
  46. ARG_TYPE argType, char shortName)
  47. : pref_(pref),
  48. description_(description),
  49. defaultValue_(defaultValue),
  50. argType_(argType),
  51. shortName_(shortName),
  52. tags_(0),
  53. flags_(0)
  54. {
  55. }
  56. AbstractOptionHandler::~AbstractOptionHandler() = default;
  57. void AbstractOptionHandler::parse(Option& option, const std::string& arg) const
  58. {
  59. try {
  60. parseArg(option, arg);
  61. }
  62. catch (Exception& e) {
  63. throw OPTION_HANDLER_EXCEPTION2(pref_, e);
  64. }
  65. }
  66. bool AbstractOptionHandler::hasTag(uint32_t tag) const
  67. {
  68. return (tags_ & (1 << tag));
  69. }
  70. void AbstractOptionHandler::addTag(uint32_t tag) { tags_ |= (1 << tag); }
  71. std::string AbstractOptionHandler::toTagString() const
  72. {
  73. std::string s;
  74. for (int i = 0; i < MAX_HELP_TAG; ++i) {
  75. if (tags_ & (1 << i)) {
  76. s += strHelpTag(i);
  77. s += ", ";
  78. }
  79. }
  80. if (!s.empty()) {
  81. s.resize(s.size() - 2);
  82. }
  83. return s;
  84. }
  85. const char* AbstractOptionHandler::getName() const { return pref_->k; }
  86. void AbstractOptionHandler::updateFlags(int flag, bool val)
  87. {
  88. if (val) {
  89. flags_ |= flag;
  90. }
  91. else {
  92. flags_ &= ~flag;
  93. }
  94. }
  95. bool AbstractOptionHandler::isHidden() const { return flags_ & FLAG_HIDDEN; }
  96. void AbstractOptionHandler::hide() { updateFlags(FLAG_HIDDEN, true); }
  97. bool AbstractOptionHandler::getEraseAfterParse() const
  98. {
  99. return flags_ & FLAG_ERASE_AFTER_PARSE;
  100. }
  101. void AbstractOptionHandler::setEraseAfterParse(bool f)
  102. {
  103. updateFlags(FLAG_ERASE_AFTER_PARSE, f);
  104. }
  105. bool AbstractOptionHandler::getInitialOption() const
  106. {
  107. return flags_ & FLAG_INITIAL_OPTION;
  108. }
  109. void AbstractOptionHandler::setInitialOption(bool f)
  110. {
  111. updateFlags(FLAG_INITIAL_OPTION, f);
  112. }
  113. bool AbstractOptionHandler::getChangeOption() const
  114. {
  115. return flags_ & FLAG_CHANGE_OPTION;
  116. }
  117. void AbstractOptionHandler::setChangeOption(bool f)
  118. {
  119. updateFlags(FLAG_CHANGE_OPTION, f);
  120. }
  121. bool AbstractOptionHandler::getChangeOptionForReserved() const
  122. {
  123. return flags_ & FLAG_CHANGE_OPTION_FOR_RESERVED;
  124. }
  125. void AbstractOptionHandler::setChangeOptionForReserved(bool f)
  126. {
  127. updateFlags(FLAG_CHANGE_OPTION_FOR_RESERVED, f);
  128. }
  129. bool AbstractOptionHandler::getChangeGlobalOption() const
  130. {
  131. return flags_ & FLAG_CHANGE_GLOBAL_OPTION;
  132. }
  133. void AbstractOptionHandler::setChangeGlobalOption(bool f)
  134. {
  135. updateFlags(FLAG_CHANGE_GLOBAL_OPTION, f);
  136. }
  137. bool AbstractOptionHandler::getCumulative() const
  138. {
  139. return flags_ & FLAG_CUMULATIVE;
  140. }
  141. void AbstractOptionHandler::setCumulative(bool f)
  142. {
  143. updateFlags(FLAG_CUMULATIVE, f);
  144. }
  145. } // namespace aria2