Initial commit.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmCommandArgumentsHelper.cxx
blob887e3e4e46c1de1baf358d57e626ff072245f816
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCommandArgumentsHelper.cxx,v $
5 Language: C++
6 Date: $Date: 2007/08/23 20:13:15 $
7 Version: $Revision: 1.4 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
18 #include "cmCommandArgumentsHelper.h"
20 cmCommandArgument::cmCommandArgument(cmCommandArgumentsHelper* args,
21 const char* key,
22 cmCommandArgumentGroup* group)
23 :Key(key)
24 ,Group(group)
25 ,WasActive(false)
26 ,ArgumentsBeforeEmpty(true)
27 ,CurrentIndex(0)
29 if (args!=0)
31 args->AddArgument(this);
34 if (this->Group!=0)
36 this->Group->ContainedArguments.push_back(this);
40 void cmCommandArgument::Reset()
42 this->WasActive =false;
43 this->CurrentIndex = 0;
44 this->DoReset();
47 void cmCommandArgument::Follows(const cmCommandArgument* arg)
49 this->ArgumentsBeforeEmpty = false;
50 this->ArgumentsBefore.insert(arg);
53 void cmCommandArgument::FollowsGroup(const cmCommandArgumentGroup* group)
55 if (group!=0)
57 this->ArgumentsBeforeEmpty = false;
58 for(std::vector<cmCommandArgument*>::const_iterator
59 argIt= group->ContainedArguments.begin();
60 argIt != group->ContainedArguments.end();
61 ++argIt)
63 this->ArgumentsBefore.insert(*argIt);
68 bool cmCommandArgument::MayFollow(const cmCommandArgument* current) const
70 if (this->ArgumentsBeforeEmpty)
72 return true;
75 std::set<const cmCommandArgument*>::const_iterator argIt
76 = this->ArgumentsBefore.find(current);
77 if (argIt != this->ArgumentsBefore.end())
79 return true;
82 return false;
85 bool cmCommandArgument::KeyMatches(const std::string& key) const
87 if ((this->Key==0) || (this->Key[0]=='\0'))
89 return true;
91 return (key==this->Key);
94 void cmCommandArgument::ApplyOwnGroup()
96 if (this->Group!=0)
98 for (std::vector<cmCommandArgument*>::const_iterator
99 it = this->Group->ContainedArguments.begin();
100 it != this->Group->ContainedArguments.end();
101 ++it)
103 if(*it != this)
105 this->ArgumentsBefore.insert(*it);
111 void cmCommandArgument::Activate()
113 this->WasActive = true;
114 this->CurrentIndex = 0;
117 bool cmCommandArgument::Consume(const std::string& arg)
119 bool res=this->DoConsume(arg, this->CurrentIndex);
120 this->CurrentIndex++;
121 return res;
125 cmCAStringVector::cmCAStringVector(cmCommandArgumentsHelper* args,
126 const char* key,
127 cmCommandArgumentGroup* group)
128 :cmCommandArgument(args, key, group)
129 ,Ignore(0)
131 if ((key==0) || (*key==0))
133 this->DataStart = 0;
135 else
137 this->DataStart = 1;
141 bool cmCAStringVector::DoConsume(const std::string& arg,unsigned int index)
143 if (index >= this->DataStart)
145 if ((this->Ignore==0) || (arg != this->Ignore))
147 this->Vector.push_back(arg);
151 return false;
154 void cmCAStringVector::DoReset()
156 this->Vector.clear();
159 cmCAString::cmCAString(cmCommandArgumentsHelper* args,
160 const char* key,
161 cmCommandArgumentGroup* group)
162 :cmCommandArgument(args, key, group)
164 if ((key==0) || (*key==0))
166 this->DataStart = 0;
168 else
170 this->DataStart = 1;
174 bool cmCAString::DoConsume(const std::string& arg, unsigned int index)
176 if (index == this->DataStart)
178 this->String = arg;
181 return index >= this->DataStart;
184 void cmCAString::DoReset()
186 this->String = this->DefaultString;
189 cmCAEnabler::cmCAEnabler(cmCommandArgumentsHelper* args,
190 const char* key,
191 cmCommandArgumentGroup* group)
192 :cmCommandArgument(args, key, group)
193 ,Enabled(false)
196 bool cmCAEnabler::DoConsume(const std::string&, unsigned int index)
198 if (index==0)
200 this->Enabled = true;
202 return true;
205 void cmCAEnabler::DoReset()
207 this->Enabled = false;
210 cmCADisabler::cmCADisabler(cmCommandArgumentsHelper* args,
211 const char* key,
212 cmCommandArgumentGroup* group)
213 :cmCommandArgument(args, key, group)
214 ,Enabled(true)
217 bool cmCADisabler::DoConsume(const std::string&, unsigned int index)
219 if (index==0)
221 this->Enabled = false;
223 return true;
226 void cmCADisabler::DoReset()
228 this->Enabled = true;
231 void cmCommandArgumentGroup::Follows(const cmCommandArgument* arg)
233 for(std::vector<cmCommandArgument*>::iterator
234 it = this->ContainedArguments.begin();
235 it != this->ContainedArguments.end();
236 ++it)
238 (*it)->Follows(arg);
242 void cmCommandArgumentGroup::FollowsGroup(const cmCommandArgumentGroup* group)
244 for(std::vector<cmCommandArgument*>::iterator
245 it = this->ContainedArguments.begin();
246 it != this->ContainedArguments.end();
247 ++it)
249 (*it)->FollowsGroup(group);
253 void cmCommandArgumentsHelper::Parse(const std::vector<std::string>* args,
254 std::vector<std::string>* unconsumedArgs)
256 if(args==0)
258 return;
261 for(std::vector<cmCommandArgument*>::iterator
262 argIt = this->Arguments.begin();
263 argIt != this->Arguments.end();
264 ++argIt)
266 (*argIt)->ApplyOwnGroup();
267 (*argIt)->Reset();
270 cmCommandArgument* activeArgument = 0;
271 const cmCommandArgument* previousArgument = 0;
272 for(std::vector<std::string>::const_iterator it = args->begin();
273 it != args->end();
274 ++it)
276 for(std::vector<cmCommandArgument*>::iterator
277 argIt = this->Arguments.begin();
278 argIt != this->Arguments.end();
279 ++argIt)
281 if ((*argIt)->KeyMatches(*it) && ((*argIt)->MayFollow(previousArgument)))
283 activeArgument = *argIt;
284 activeArgument->Activate();
285 break;
289 if (activeArgument)
291 bool argDone = activeArgument->Consume(*it);
292 previousArgument = activeArgument;
293 if (argDone)
295 activeArgument = 0;
298 else
300 if (unconsumedArgs!=0)
302 unconsumedArgs->push_back(*it);
308 void cmCommandArgumentsHelper::AddArgument(cmCommandArgument* arg)
310 this->Arguments.push_back(arg);