cmCommandArgumentsHelper.cxx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. return this->ArgumentsBefore.find(current) != this->ArgumentsBefore.end();
  52. }
  53. bool cmCommandArgument::KeyMatches(const std::string& key) const
  54. {
  55. if ((this->Key == CM_NULLPTR) || (this->Key[0] == '\0')) {
  56. return true;
  57. }
  58. return (key == this->Key);
  59. }
  60. void cmCommandArgument::ApplyOwnGroup()
  61. {
  62. if (this->Group != CM_NULLPTR) {
  63. for (std::vector<cmCommandArgument*>::const_iterator it =
  64. this->Group->ContainedArguments.begin();
  65. it != this->Group->ContainedArguments.end(); ++it) {
  66. if (*it != this) {
  67. this->ArgumentsBefore.insert(*it);
  68. }
  69. }
  70. }
  71. }
  72. void cmCommandArgument::Activate()
  73. {
  74. this->WasActive = true;
  75. this->CurrentIndex = 0;
  76. }
  77. bool cmCommandArgument::Consume(const std::string& arg)
  78. {
  79. bool res = this->DoConsume(arg, this->CurrentIndex);
  80. this->CurrentIndex++;
  81. return res;
  82. }
  83. cmCAStringVector::cmCAStringVector(cmCommandArgumentsHelper* args,
  84. const char* key,
  85. cmCommandArgumentGroup* group)
  86. : cmCommandArgument(args, key, group)
  87. , Ignore(CM_NULLPTR)
  88. {
  89. if ((key == CM_NULLPTR) || (*key == 0)) {
  90. this->DataStart = 0;
  91. } else {
  92. this->DataStart = 1;
  93. }
  94. }
  95. bool cmCAStringVector::DoConsume(const std::string& arg, unsigned int index)
  96. {
  97. if (index >= this->DataStart) {
  98. if ((this->Ignore == CM_NULLPTR) || (arg != this->Ignore)) {
  99. this->Vector.push_back(arg);
  100. }
  101. }
  102. return false;
  103. }
  104. void cmCAStringVector::DoReset()
  105. {
  106. this->Vector.clear();
  107. }
  108. cmCAString::cmCAString(cmCommandArgumentsHelper* args, const char* key,
  109. cmCommandArgumentGroup* group)
  110. : cmCommandArgument(args, key, group)
  111. {
  112. if ((key == CM_NULLPTR) || (*key == 0)) {
  113. this->DataStart = 0;
  114. } else {
  115. this->DataStart = 1;
  116. }
  117. }
  118. bool cmCAString::DoConsume(const std::string& arg, unsigned int index)
  119. {
  120. if (index == this->DataStart) {
  121. this->String = arg;
  122. }
  123. return index >= this->DataStart;
  124. }
  125. void cmCAString::DoReset()
  126. {
  127. this->String = "";
  128. }
  129. cmCAEnabler::cmCAEnabler(cmCommandArgumentsHelper* args, const char* key,
  130. cmCommandArgumentGroup* group)
  131. : cmCommandArgument(args, key, group)
  132. , Enabled(false)
  133. {
  134. }
  135. bool cmCAEnabler::DoConsume(const std::string&, unsigned int index)
  136. {
  137. if (index == 0) {
  138. this->Enabled = true;
  139. }
  140. return true;
  141. }
  142. void cmCAEnabler::DoReset()
  143. {
  144. this->Enabled = false;
  145. }
  146. cmCADisabler::cmCADisabler(cmCommandArgumentsHelper* args, const char* key,
  147. cmCommandArgumentGroup* group)
  148. : cmCommandArgument(args, key, group)
  149. , Enabled(true)
  150. {
  151. }
  152. bool cmCADisabler::DoConsume(const std::string&, unsigned int index)
  153. {
  154. if (index == 0) {
  155. this->Enabled = false;
  156. }
  157. return true;
  158. }
  159. void cmCADisabler::DoReset()
  160. {
  161. this->Enabled = true;
  162. }
  163. void cmCommandArgumentGroup::Follows(const cmCommandArgument* arg)
  164. {
  165. for (std::vector<cmCommandArgument*>::iterator it =
  166. this->ContainedArguments.begin();
  167. it != this->ContainedArguments.end(); ++it) {
  168. (*it)->Follows(arg);
  169. }
  170. }
  171. void cmCommandArgumentGroup::FollowsGroup(const cmCommandArgumentGroup* group)
  172. {
  173. for (std::vector<cmCommandArgument*>::iterator it =
  174. this->ContainedArguments.begin();
  175. it != this->ContainedArguments.end(); ++it) {
  176. (*it)->FollowsGroup(group);
  177. }
  178. }
  179. void cmCommandArgumentsHelper::Parse(const std::vector<std::string>* args,
  180. std::vector<std::string>* unconsumedArgs)
  181. {
  182. if (args == CM_NULLPTR) {
  183. return;
  184. }
  185. for (std::vector<cmCommandArgument*>::iterator argIt =
  186. this->Arguments.begin();
  187. argIt != this->Arguments.end(); ++argIt) {
  188. (*argIt)->ApplyOwnGroup();
  189. (*argIt)->Reset();
  190. }
  191. cmCommandArgument* activeArgument = CM_NULLPTR;
  192. const cmCommandArgument* previousArgument = CM_NULLPTR;
  193. for (std::vector<std::string>::const_iterator it = args->begin();
  194. it != args->end(); ++it) {
  195. for (std::vector<cmCommandArgument*>::iterator argIt =
  196. this->Arguments.begin();
  197. argIt != this->Arguments.end(); ++argIt) {
  198. if ((*argIt)->KeyMatches(*it) &&
  199. ((*argIt)->MayFollow(previousArgument))) {
  200. activeArgument = *argIt;
  201. activeArgument->Activate();
  202. break;
  203. }
  204. }
  205. if (activeArgument) {
  206. bool argDone = activeArgument->Consume(*it);
  207. previousArgument = activeArgument;
  208. if (argDone) {
  209. activeArgument = CM_NULLPTR;
  210. }
  211. } else {
  212. if (unconsumedArgs != CM_NULLPTR) {
  213. unconsumedArgs->push_back(*it);
  214. }
  215. }
  216. }
  217. }
  218. void cmCommandArgumentsHelper::AddArgument(cmCommandArgument* arg)
  219. {
  220. this->Arguments.push_back(arg);
  221. }