cmCommandArgumentsHelper.cxx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. {
  108. if ((key==0) || (*key==0))
  109. {
  110. this->DataStart = 0;
  111. }
  112. else
  113. {
  114. this->DataStart = 1;
  115. }
  116. }
  117. bool cmCAStringVector::DoConsume(const std::string& arg,unsigned int index)
  118. {
  119. if (index >= this->DataStart)
  120. {
  121. if ((this->Ignore==0) || (arg != this->Ignore))
  122. {
  123. this->Vector.push_back(arg);
  124. }
  125. }
  126. return false;
  127. }
  128. void cmCAStringVector::DoReset()
  129. {
  130. this->Vector.clear();
  131. }
  132. cmCAString::cmCAString(cmCommandArgumentsHelper* args,
  133. const char* key,
  134. cmCommandArgumentGroup* group)
  135. :cmCommandArgument(args, key, group)
  136. {
  137. if ((key==0) || (*key==0))
  138. {
  139. this->DataStart = 0;
  140. }
  141. else
  142. {
  143. this->DataStart = 1;
  144. }
  145. }
  146. bool cmCAString::DoConsume(const std::string& arg, unsigned int index)
  147. {
  148. if (index == this->DataStart)
  149. {
  150. this->String = arg;
  151. }
  152. return index >= this->DataStart;
  153. }
  154. void cmCAString::DoReset()
  155. {
  156. this->String = "";
  157. }
  158. cmCAEnabler::cmCAEnabler(cmCommandArgumentsHelper* args,
  159. const char* key,
  160. cmCommandArgumentGroup* group)
  161. :cmCommandArgument(args, key, group)
  162. ,Enabled(false)
  163. {}
  164. bool cmCAEnabler::DoConsume(const std::string&, unsigned int index)
  165. {
  166. if (index==0)
  167. {
  168. this->Enabled = true;
  169. }
  170. return true;
  171. }
  172. void cmCAEnabler::DoReset()
  173. {
  174. this->Enabled = false;
  175. }
  176. cmCADisabler::cmCADisabler(cmCommandArgumentsHelper* args,
  177. const char* key,
  178. cmCommandArgumentGroup* group)
  179. :cmCommandArgument(args, key, group)
  180. ,Enabled(true)
  181. {}
  182. bool cmCADisabler::DoConsume(const std::string&, unsigned int index)
  183. {
  184. if (index==0)
  185. {
  186. this->Enabled = false;
  187. }
  188. return true;
  189. }
  190. void cmCADisabler::DoReset()
  191. {
  192. this->Enabled = true;
  193. }
  194. void cmCommandArgumentGroup::Follows(const cmCommandArgument* arg)
  195. {
  196. for(std::vector<cmCommandArgument*>::iterator
  197. it = this->ContainedArguments.begin();
  198. it != this->ContainedArguments.end();
  199. ++it)
  200. {
  201. (*it)->Follows(arg);
  202. }
  203. }
  204. void cmCommandArgumentGroup::FollowsGroup(const cmCommandArgumentGroup* group)
  205. {
  206. for(std::vector<cmCommandArgument*>::iterator
  207. it = this->ContainedArguments.begin();
  208. it != this->ContainedArguments.end();
  209. ++it)
  210. {
  211. (*it)->FollowsGroup(group);
  212. }
  213. }
  214. void cmCommandArgumentsHelper::Parse(const std::vector<std::string>* args,
  215. std::vector<std::string>* unconsumedArgs)
  216. {
  217. if(args==0)
  218. {
  219. return;
  220. }
  221. for(std::vector<cmCommandArgument*>::iterator
  222. argIt = this->Arguments.begin();
  223. argIt != this->Arguments.end();
  224. ++argIt)
  225. {
  226. (*argIt)->ApplyOwnGroup();
  227. (*argIt)->Reset();
  228. }
  229. cmCommandArgument* activeArgument = 0;
  230. const cmCommandArgument* previousArgument = 0;
  231. for(std::vector<std::string>::const_iterator it = args->begin();
  232. it != args->end();
  233. ++it)
  234. {
  235. for(std::vector<cmCommandArgument*>::iterator
  236. argIt = this->Arguments.begin();
  237. argIt != this->Arguments.end();
  238. ++argIt)
  239. {
  240. if ((*argIt)->KeyMatches(*it) && ((*argIt)->MayFollow(previousArgument)))
  241. {
  242. activeArgument = *argIt;
  243. activeArgument->Activate();
  244. break;
  245. }
  246. }
  247. if (activeArgument)
  248. {
  249. bool argDone = activeArgument->Consume(*it);
  250. previousArgument = activeArgument;
  251. if (argDone)
  252. {
  253. activeArgument = 0;
  254. }
  255. }
  256. else
  257. {
  258. if (unconsumedArgs!=0)
  259. {
  260. unconsumedArgs->push_back(*it);
  261. }
  262. }
  263. }
  264. }
  265. void cmCommandArgumentsHelper::AddArgument(cmCommandArgument* arg)
  266. {
  267. this->Arguments.push_back(arg);
  268. }