1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
13 #include "cmCommandArgumentsHelper.h"
15 cmCommandArgument::cmCommandArgument(cmCommandArgumentsHelper* args,
17 cmCommandArgumentGroup* group)
21 ,ArgumentsBeforeEmpty(true)
26 args->AddArgument(this);
31 this->Group->ContainedArguments.push_back(this);
35 void cmCommandArgument::Reset()
37 this->WasActive =false;
38 this->CurrentIndex = 0;
42 void cmCommandArgument::Follows(const cmCommandArgument* arg)
44 this->ArgumentsBeforeEmpty = false;
45 this->ArgumentsBefore.insert(arg);
48 void cmCommandArgument::FollowsGroup(const cmCommandArgumentGroup* group)
52 this->ArgumentsBeforeEmpty = false;
53 for(std::vector<cmCommandArgument*>::const_iterator
54 argIt= group->ContainedArguments.begin();
55 argIt != group->ContainedArguments.end();
58 this->ArgumentsBefore.insert(*argIt);
63 bool cmCommandArgument::MayFollow(const cmCommandArgument* current) const
65 if (this->ArgumentsBeforeEmpty)
70 std::set<const cmCommandArgument*>::const_iterator argIt
71 = this->ArgumentsBefore.find(current);
72 if (argIt != this->ArgumentsBefore.end())
80 bool cmCommandArgument::KeyMatches(const std::string& key) const
82 if ((this->Key==0) || (this->Key[0]=='\0'))
86 return (key==this->Key);
89 void cmCommandArgument::ApplyOwnGroup()
93 for (std::vector<cmCommandArgument*>::const_iterator
94 it = this->Group->ContainedArguments.begin();
95 it != this->Group->ContainedArguments.end();
100 this->ArgumentsBefore.insert(*it);
106 void cmCommandArgument::Activate()
108 this->WasActive = true;
109 this->CurrentIndex = 0;
112 bool cmCommandArgument::Consume(const std::string& arg)
114 bool res=this->DoConsume(arg, this->CurrentIndex);
115 this->CurrentIndex++;
120 cmCAStringVector::cmCAStringVector(cmCommandArgumentsHelper* args,
122 cmCommandArgumentGroup* group)
123 :cmCommandArgument(args, key, group)
126 if ((key==0) || (*key==0))
136 bool cmCAStringVector::DoConsume(const std::string& arg,unsigned int index)
138 if (index >= this->DataStart)
140 if ((this->Ignore==0) || (arg != this->Ignore))
142 this->Vector.push_back(arg);
149 void cmCAStringVector::DoReset()
151 this->Vector.clear();
154 cmCAString::cmCAString(cmCommandArgumentsHelper* args,
156 cmCommandArgumentGroup* group)
157 :cmCommandArgument(args, key, group)
159 if ((key==0) || (*key==0))
169 bool cmCAString::DoConsume(const std::string& arg, unsigned int index)
171 if (index == this->DataStart)
176 return index >= this->DataStart;
179 void cmCAString::DoReset()
184 cmCAEnabler::cmCAEnabler(cmCommandArgumentsHelper* args,
186 cmCommandArgumentGroup* group)
187 :cmCommandArgument(args, key, group)
191 bool cmCAEnabler::DoConsume(const std::string&, unsigned int index)
195 this->Enabled = true;
200 void cmCAEnabler::DoReset()
202 this->Enabled = false;
205 cmCADisabler::cmCADisabler(cmCommandArgumentsHelper* args,
207 cmCommandArgumentGroup* group)
208 :cmCommandArgument(args, key, group)
212 bool cmCADisabler::DoConsume(const std::string&, unsigned int index)
216 this->Enabled = false;
221 void cmCADisabler::DoReset()
223 this->Enabled = true;
226 void cmCommandArgumentGroup::Follows(const cmCommandArgument* arg)
228 for(std::vector<cmCommandArgument*>::iterator
229 it = this->ContainedArguments.begin();
230 it != this->ContainedArguments.end();
237 void cmCommandArgumentGroup::FollowsGroup(const cmCommandArgumentGroup* group)
239 for(std::vector<cmCommandArgument*>::iterator
240 it = this->ContainedArguments.begin();
241 it != this->ContainedArguments.end();
244 (*it)->FollowsGroup(group);
248 void cmCommandArgumentsHelper::Parse(const std::vector<std::string>* args,
249 std::vector<std::string>* unconsumedArgs)
256 for(std::vector<cmCommandArgument*>::iterator
257 argIt = this->Arguments.begin();
258 argIt != this->Arguments.end();
261 (*argIt)->ApplyOwnGroup();
265 cmCommandArgument* activeArgument = 0;
266 const cmCommandArgument* previousArgument = 0;
267 for(std::vector<std::string>::const_iterator it = args->begin();
271 for(std::vector<cmCommandArgument*>::iterator
272 argIt = this->Arguments.begin();
273 argIt != this->Arguments.end();
276 if ((*argIt)->KeyMatches(*it) && ((*argIt)->MayFollow(previousArgument)))
278 activeArgument = *argIt;
279 activeArgument->Activate();
286 bool argDone = activeArgument->Consume(*it);
287 previousArgument = activeArgument;
295 if (unconsumedArgs!=0)
297 unconsumedArgs->push_back(*it);
303 void cmCommandArgumentsHelper::AddArgument(cmCommandArgument* arg)
305 this->Arguments.push_back(arg);