cmCommandArgumentsHelper.cxx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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!=0)
  21. {
  22. args->AddArgument(this);
  23. }
  24. if (this->Group!=0)
  25. {
  26. this->Group->ContainedArguments.push_back(this);
  27. }
  28. }
  29. void cmCommandArgument::Reset()
  30. {
  31. this->WasActive =false;
  32. this->CurrentIndex = 0;
  33. this->DoReset();
  34. }
  35. void cmCommandArgument::Follows(const cmCommandArgument* arg)
  36. {
  37. this->ArgumentsBeforeEmpty = false;
  38. this->ArgumentsBefore.insert(arg);
  39. }
  40. void cmCommandArgument::FollowsGroup(const cmCommandArgumentGroup* group)
  41. {
  42. if (group!=0)
  43. {
  44. this->ArgumentsBeforeEmpty = false;
  45. this->ArgumentsBefore.insert(group->ContainedArguments.begin(),
  46. group->ContainedArguments.end());
  47. }
  48. }
  49. bool cmCommandArgument::MayFollow(const cmCommandArgument* current) const
  50. {
  51. if (this->ArgumentsBeforeEmpty)
  52. {
  53. return true;
  54. }
  55. std::set<const cmCommandArgument*>::const_iterator argIt
  56. = this->ArgumentsBefore.find(current);
  57. if (argIt != this->ArgumentsBefore.end())
  58. {
  59. return true;
  60. }
  61. return false;
  62. }
  63. bool cmCommandArgument::KeyMatches(const std::string& key) const
  64. {
  65. if ((this->Key==0) || (this->Key[0]=='\0'))
  66. {
  67. return true;
  68. }
  69. return (key==this->Key);
  70. }
  71. void cmCommandArgument::ApplyOwnGroup()
  72. {
  73. if (this->Group!=0)
  74. {
  75. for (std::vector<cmCommandArgument*>::const_iterator
  76. it = this->Group->ContainedArguments.begin();
  77. it != this->Group->ContainedArguments.end();
  78. ++it)
  79. {
  80. if(*it != this)
  81. {
  82. this->ArgumentsBefore.insert(*it);
  83. }
  84. }
  85. }
  86. }
  87. void cmCommandArgument::Activate()
  88. {
  89. this->WasActive = true;
  90. this->CurrentIndex = 0;
  91. }
  92. bool cmCommandArgument::Consume(const std::string& arg)
  93. {
  94. bool res=this->DoConsume(arg, this->CurrentIndex);
  95. this->CurrentIndex++;
  96. return res;
  97. }
  98. cmCAStringVector::cmCAStringVector(cmCommandArgumentsHelper* args,
  99. const char* key,
  100. cmCommandArgumentGroup* group)
  101. :cmCommandArgument(args, key, group)
  102. ,Ignore(0)
  103. {
  104. if ((key==0) || (*key==0))
  105. {
  106. this->DataStart = 0;
  107. }
  108. else
  109. {
  110. this->DataStart = 1;
  111. }
  112. }
  113. bool cmCAStringVector::DoConsume(const std::string& arg,unsigned int index)
  114. {
  115. if (index >= this->DataStart)
  116. {
  117. if ((this->Ignore==0) || (arg != this->Ignore))
  118. {
  119. this->Vector.push_back(arg);
  120. }
  121. }
  122. return false;
  123. }
  124. void cmCAStringVector::DoReset()
  125. {
  126. this->Vector.clear();
  127. }
  128. cmCAString::cmCAString(cmCommandArgumentsHelper* args,
  129. const char* key,
  130. cmCommandArgumentGroup* group)
  131. :cmCommandArgument(args, key, group)
  132. {
  133. if ((key==0) || (*key==0))
  134. {
  135. this->DataStart = 0;
  136. }
  137. else
  138. {
  139. this->DataStart = 1;
  140. }
  141. }
  142. bool cmCAString::DoConsume(const std::string& arg, unsigned int index)
  143. {
  144. if (index == this->DataStart)
  145. {
  146. this->String = arg;
  147. }
  148. return index >= this->DataStart;
  149. }
  150. void cmCAString::DoReset()
  151. {
  152. this->String = "";
  153. }
  154. cmCAEnabler::cmCAEnabler(cmCommandArgumentsHelper* args,
  155. const char* key,
  156. cmCommandArgumentGroup* group)
  157. :cmCommandArgument(args, key, group)
  158. ,Enabled(false)
  159. {}
  160. bool cmCAEnabler::DoConsume(const std::string&, unsigned int index)
  161. {
  162. if (index==0)
  163. {
  164. this->Enabled = true;
  165. }
  166. return true;
  167. }
  168. void cmCAEnabler::DoReset()
  169. {
  170. this->Enabled = false;
  171. }
  172. cmCADisabler::cmCADisabler(cmCommandArgumentsHelper* args,
  173. const char* key,
  174. cmCommandArgumentGroup* group)
  175. :cmCommandArgument(args, key, group)
  176. ,Enabled(true)
  177. {}
  178. bool cmCADisabler::DoConsume(const std::string&, unsigned int index)
  179. {
  180. if (index==0)
  181. {
  182. this->Enabled = false;
  183. }
  184. return true;
  185. }
  186. void cmCADisabler::DoReset()
  187. {
  188. this->Enabled = true;
  189. }
  190. void cmCommandArgumentGroup::Follows(const cmCommandArgument* arg)
  191. {
  192. for(std::vector<cmCommandArgument*>::iterator
  193. it = this->ContainedArguments.begin();
  194. it != this->ContainedArguments.end();
  195. ++it)
  196. {
  197. (*it)->Follows(arg);
  198. }
  199. }
  200. void cmCommandArgumentGroup::FollowsGroup(const cmCommandArgumentGroup* group)
  201. {
  202. for(std::vector<cmCommandArgument*>::iterator
  203. it = this->ContainedArguments.begin();
  204. it != this->ContainedArguments.end();
  205. ++it)
  206. {
  207. (*it)->FollowsGroup(group);
  208. }
  209. }
  210. void cmCommandArgumentsHelper::Parse(const std::vector<std::string>* args,
  211. std::vector<std::string>* unconsumedArgs)
  212. {
  213. if(args==0)
  214. {
  215. return;
  216. }
  217. for(std::vector<cmCommandArgument*>::iterator
  218. argIt = this->Arguments.begin();
  219. argIt != this->Arguments.end();
  220. ++argIt)
  221. {
  222. (*argIt)->ApplyOwnGroup();
  223. (*argIt)->Reset();
  224. }
  225. cmCommandArgument* activeArgument = 0;
  226. const cmCommandArgument* previousArgument = 0;
  227. for(std::vector<std::string>::const_iterator it = args->begin();
  228. it != args->end();
  229. ++it)
  230. {
  231. for(std::vector<cmCommandArgument*>::iterator
  232. argIt = this->Arguments.begin();
  233. argIt != this->Arguments.end();
  234. ++argIt)
  235. {
  236. if ((*argIt)->KeyMatches(*it) && ((*argIt)->MayFollow(previousArgument)))
  237. {
  238. activeArgument = *argIt;
  239. activeArgument->Activate();
  240. break;
  241. }
  242. }
  243. if (activeArgument)
  244. {
  245. bool argDone = activeArgument->Consume(*it);
  246. previousArgument = activeArgument;
  247. if (argDone)
  248. {
  249. activeArgument = 0;
  250. }
  251. }
  252. else
  253. {
  254. if (unconsumedArgs!=0)
  255. {
  256. unconsumedArgs->push_back(*it);
  257. }
  258. }
  259. }
  260. }
  261. void cmCommandArgumentsHelper::AddArgument(cmCommandArgument* arg)
  262. {
  263. this->Arguments.push_back(arg);
  264. }