cmCommandArgumentsHelper.cxx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCommandArgumentsHelper.h"
  11. cmCommandArgument::cmCommandArgument(cmCommandArgumentsHelper* args,
  12. const char* key,
  13. cmCommandArgumentGroup* group)
  14. : Key(key)
  15. , Group(group)
  16. , WasActive(false)
  17. , ArgumentsBeforeEmpty(true)
  18. , CurrentIndex(0)
  19. {
  20. if (args != CM_NULLPTR) {
  21. args->AddArgument(this);
  22. }
  23. if (this->Group != CM_NULLPTR) {
  24. this->Group->ContainedArguments.push_back(this);
  25. }
  26. }
  27. void cmCommandArgument::Reset()
  28. {
  29. this->WasActive = false;
  30. this->CurrentIndex = 0;
  31. this->DoReset();
  32. }
  33. void cmCommandArgument::Follows(const cmCommandArgument* arg)
  34. {
  35. this->ArgumentsBeforeEmpty = false;
  36. this->ArgumentsBefore.insert(arg);
  37. }
  38. void cmCommandArgument::FollowsGroup(const cmCommandArgumentGroup* group)
  39. {
  40. if (group != CM_NULLPTR) {
  41. this->ArgumentsBeforeEmpty = false;
  42. this->ArgumentsBefore.insert(group->ContainedArguments.begin(),
  43. group->ContainedArguments.end());
  44. }
  45. }
  46. bool cmCommandArgument::MayFollow(const cmCommandArgument* current) const
  47. {
  48. if (this->ArgumentsBeforeEmpty) {
  49. return true;
  50. }
  51. std::set<const cmCommandArgument*>::const_iterator argIt =
  52. this->ArgumentsBefore.find(current);
  53. if (argIt != this->ArgumentsBefore.end()) {
  54. return true;
  55. }
  56. return false;
  57. }
  58. bool cmCommandArgument::KeyMatches(const std::string& key) const
  59. {
  60. if ((this->Key == CM_NULLPTR) || (this->Key[0] == '\0')) {
  61. return true;
  62. }
  63. return (key == this->Key);
  64. }
  65. void cmCommandArgument::ApplyOwnGroup()
  66. {
  67. if (this->Group != CM_NULLPTR) {
  68. for (std::vector<cmCommandArgument*>::const_iterator it =
  69. this->Group->ContainedArguments.begin();
  70. it != this->Group->ContainedArguments.end(); ++it) {
  71. if (*it != this) {
  72. this->ArgumentsBefore.insert(*it);
  73. }
  74. }
  75. }
  76. }
  77. void cmCommandArgument::Activate()
  78. {
  79. this->WasActive = true;
  80. this->CurrentIndex = 0;
  81. }
  82. bool cmCommandArgument::Consume(const std::string& arg)
  83. {
  84. bool res = this->DoConsume(arg, this->CurrentIndex);
  85. this->CurrentIndex++;
  86. return res;
  87. }
  88. cmCAStringVector::cmCAStringVector(cmCommandArgumentsHelper* args,
  89. const char* key,
  90. cmCommandArgumentGroup* group)
  91. : cmCommandArgument(args, key, group)
  92. , Ignore(CM_NULLPTR)
  93. {
  94. if ((key == CM_NULLPTR) || (*key == 0)) {
  95. this->DataStart = 0;
  96. } else {
  97. this->DataStart = 1;
  98. }
  99. }
  100. bool cmCAStringVector::DoConsume(const std::string& arg, unsigned int index)
  101. {
  102. if (index >= this->DataStart) {
  103. if ((this->Ignore == CM_NULLPTR) || (arg != this->Ignore)) {
  104. this->Vector.push_back(arg);
  105. }
  106. }
  107. return false;
  108. }
  109. void cmCAStringVector::DoReset()
  110. {
  111. this->Vector.clear();
  112. }
  113. cmCAString::cmCAString(cmCommandArgumentsHelper* args, const char* key,
  114. cmCommandArgumentGroup* group)
  115. : cmCommandArgument(args, key, group)
  116. {
  117. if ((key == CM_NULLPTR) || (*key == 0)) {
  118. this->DataStart = 0;
  119. } else {
  120. this->DataStart = 1;
  121. }
  122. }
  123. bool cmCAString::DoConsume(const std::string& arg, unsigned int index)
  124. {
  125. if (index == this->DataStart) {
  126. this->String = arg;
  127. }
  128. return index >= this->DataStart;
  129. }
  130. void cmCAString::DoReset()
  131. {
  132. this->String = "";
  133. }
  134. cmCAEnabler::cmCAEnabler(cmCommandArgumentsHelper* args, const char* key,
  135. cmCommandArgumentGroup* group)
  136. : cmCommandArgument(args, key, group)
  137. , Enabled(false)
  138. {
  139. }
  140. bool cmCAEnabler::DoConsume(const std::string&, unsigned int index)
  141. {
  142. if (index == 0) {
  143. this->Enabled = true;
  144. }
  145. return true;
  146. }
  147. void cmCAEnabler::DoReset()
  148. {
  149. this->Enabled = false;
  150. }
  151. cmCADisabler::cmCADisabler(cmCommandArgumentsHelper* args, const char* key,
  152. cmCommandArgumentGroup* group)
  153. : cmCommandArgument(args, key, group)
  154. , Enabled(true)
  155. {
  156. }
  157. bool cmCADisabler::DoConsume(const std::string&, unsigned int index)
  158. {
  159. if (index == 0) {
  160. this->Enabled = false;
  161. }
  162. return true;
  163. }
  164. void cmCADisabler::DoReset()
  165. {
  166. this->Enabled = true;
  167. }
  168. void cmCommandArgumentGroup::Follows(const cmCommandArgument* arg)
  169. {
  170. for (std::vector<cmCommandArgument*>::iterator it =
  171. this->ContainedArguments.begin();
  172. it != this->ContainedArguments.end(); ++it) {
  173. (*it)->Follows(arg);
  174. }
  175. }
  176. void cmCommandArgumentGroup::FollowsGroup(const cmCommandArgumentGroup* group)
  177. {
  178. for (std::vector<cmCommandArgument*>::iterator it =
  179. this->ContainedArguments.begin();
  180. it != this->ContainedArguments.end(); ++it) {
  181. (*it)->FollowsGroup(group);
  182. }
  183. }
  184. void cmCommandArgumentsHelper::Parse(const std::vector<std::string>* args,
  185. std::vector<std::string>* unconsumedArgs)
  186. {
  187. if (args == CM_NULLPTR) {
  188. return;
  189. }
  190. for (std::vector<cmCommandArgument*>::iterator argIt =
  191. this->Arguments.begin();
  192. argIt != this->Arguments.end(); ++argIt) {
  193. (*argIt)->ApplyOwnGroup();
  194. (*argIt)->Reset();
  195. }
  196. cmCommandArgument* activeArgument = CM_NULLPTR;
  197. const cmCommandArgument* previousArgument = CM_NULLPTR;
  198. for (std::vector<std::string>::const_iterator it = args->begin();
  199. it != args->end(); ++it) {
  200. for (std::vector<cmCommandArgument*>::iterator argIt =
  201. this->Arguments.begin();
  202. argIt != this->Arguments.end(); ++argIt) {
  203. if ((*argIt)->KeyMatches(*it) &&
  204. ((*argIt)->MayFollow(previousArgument))) {
  205. activeArgument = *argIt;
  206. activeArgument->Activate();
  207. break;
  208. }
  209. }
  210. if (activeArgument) {
  211. bool argDone = activeArgument->Consume(*it);
  212. previousArgument = activeArgument;
  213. if (argDone) {
  214. activeArgument = CM_NULLPTR;
  215. }
  216. } else {
  217. if (unconsumedArgs != CM_NULLPTR) {
  218. unconsumedArgs->push_back(*it);
  219. }
  220. }
  221. }
  222. }
  223. void cmCommandArgumentsHelper::AddArgument(cmCommandArgument* arg)
  224. {
  225. this->Arguments.push_back(arg);
  226. }