cmCommandArgumentsHelper.cxx 6.6 KB

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