1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCommandArgumentsHelper.cxx,v $
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
,
22 cmCommandArgumentGroup
* group
)
26 ,ArgumentsBeforeEmpty(true)
31 args
->AddArgument(this);
36 this->Group
->ContainedArguments
.push_back(this);
40 void cmCommandArgument::Reset()
42 this->WasActive
=false;
43 this->CurrentIndex
= 0;
47 void cmCommandArgument::Follows(const cmCommandArgument
* arg
)
49 this->ArgumentsBeforeEmpty
= false;
50 this->ArgumentsBefore
.insert(arg
);
53 void cmCommandArgument::FollowsGroup(const cmCommandArgumentGroup
* group
)
57 this->ArgumentsBeforeEmpty
= false;
58 for(std::vector
<cmCommandArgument
*>::const_iterator
59 argIt
= group
->ContainedArguments
.begin();
60 argIt
!= group
->ContainedArguments
.end();
63 this->ArgumentsBefore
.insert(*argIt
);
68 bool cmCommandArgument::MayFollow(const cmCommandArgument
* current
) const
70 if (this->ArgumentsBeforeEmpty
)
75 std::set
<const cmCommandArgument
*>::const_iterator argIt
76 = this->ArgumentsBefore
.find(current
);
77 if (argIt
!= this->ArgumentsBefore
.end())
85 bool cmCommandArgument::KeyMatches(const std::string
& key
) const
87 if ((this->Key
==0) || (this->Key
[0]=='\0'))
91 return (key
==this->Key
);
94 void cmCommandArgument::ApplyOwnGroup()
98 for (std::vector
<cmCommandArgument
*>::const_iterator
99 it
= this->Group
->ContainedArguments
.begin();
100 it
!= this->Group
->ContainedArguments
.end();
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
++;
125 cmCAStringVector::cmCAStringVector(cmCommandArgumentsHelper
* args
,
127 cmCommandArgumentGroup
* group
)
128 :cmCommandArgument(args
, key
, group
)
131 if ((key
==0) || (*key
==0))
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
);
154 void cmCAStringVector::DoReset()
156 this->Vector
.clear();
159 cmCAString::cmCAString(cmCommandArgumentsHelper
* args
,
161 cmCommandArgumentGroup
* group
)
162 :cmCommandArgument(args
, key
, group
)
164 if ((key
==0) || (*key
==0))
174 bool cmCAString::DoConsume(const std::string
& arg
, unsigned int index
)
176 if (index
== this->DataStart
)
181 return index
>= this->DataStart
;
184 void cmCAString::DoReset()
186 this->String
= this->DefaultString
;
189 cmCAEnabler::cmCAEnabler(cmCommandArgumentsHelper
* args
,
191 cmCommandArgumentGroup
* group
)
192 :cmCommandArgument(args
, key
, group
)
196 bool cmCAEnabler::DoConsume(const std::string
&, unsigned int index
)
200 this->Enabled
= true;
205 void cmCAEnabler::DoReset()
207 this->Enabled
= false;
210 cmCADisabler::cmCADisabler(cmCommandArgumentsHelper
* args
,
212 cmCommandArgumentGroup
* group
)
213 :cmCommandArgument(args
, key
, group
)
217 bool cmCADisabler::DoConsume(const std::string
&, unsigned int index
)
221 this->Enabled
= false;
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();
242 void cmCommandArgumentGroup::FollowsGroup(const cmCommandArgumentGroup
* group
)
244 for(std::vector
<cmCommandArgument
*>::iterator
245 it
= this->ContainedArguments
.begin();
246 it
!= this->ContainedArguments
.end();
249 (*it
)->FollowsGroup(group
);
253 void cmCommandArgumentsHelper::Parse(const std::vector
<std::string
>* args
,
254 std::vector
<std::string
>* unconsumedArgs
)
261 for(std::vector
<cmCommandArgument
*>::iterator
262 argIt
= this->Arguments
.begin();
263 argIt
!= this->Arguments
.end();
266 (*argIt
)->ApplyOwnGroup();
270 cmCommandArgument
* activeArgument
= 0;
271 const cmCommandArgument
* previousArgument
= 0;
272 for(std::vector
<std::string
>::const_iterator it
= args
->begin();
276 for(std::vector
<cmCommandArgument
*>::iterator
277 argIt
= this->Arguments
.begin();
278 argIt
!= this->Arguments
.end();
281 if ((*argIt
)->KeyMatches(*it
) && ((*argIt
)->MayFollow(previousArgument
)))
283 activeArgument
= *argIt
;
284 activeArgument
->Activate();
291 bool argDone
= activeArgument
->Consume(*it
);
292 previousArgument
= activeArgument
;
300 if (unconsumedArgs
!=0)
302 unconsumedArgs
->push_back(*it
);
308 void cmCommandArgumentsHelper::AddArgument(cmCommandArgument
* arg
)
310 this->Arguments
.push_back(arg
);