summaryrefslogtreecommitdiffstats
path: root/Source/cmCommandArgumentsHelper.cxx
blob: 2c311e15d0dcf7a4756d263c73527f267352fb3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*=========================================================================

  Program:   CMake - Cross-Platform Makefile Generator
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#include "cmCommandArgumentsHelper.h"

cmCommandArgument::cmCommandArgument(cmCommandArgumentsHelper* args, 
                                     const char* key, 
                                     cmCommandArgumentGroup* group)
:Key(key)
,Group(group)
,WasActive(false)
,ArgumentsBeforeEmpty(true)
,CurrentIndex(0) 
{
  if (args!=0)
    {
    args->AddArgument(this);
    }
    
  if (this->Group!=0)
    {
    this->Group->ContainedArguments.push_back(this);
    }
}

void cmCommandArgument::Reset()
{
  this->WasActive =false;
  this->CurrentIndex = 0;
  this->DoReset();
}

void cmCommandArgument::Follows(const cmCommandArgument* arg)
{
  this->ArgumentsBeforeEmpty = false;
  this->ArgumentsBefore.insert(arg);
}

void cmCommandArgument::FollowsGroup(const cmCommandArgumentGroup* group)
{
  if (group!=0)
    {
    this->ArgumentsBeforeEmpty = false;
    for(std::vector<cmCommandArgument*>::const_iterator 
        argIt= group->ContainedArguments.begin();
        argIt != group->ContainedArguments.end();
        ++argIt)
      {
      this->ArgumentsBefore.insert(*argIt);
      }
    }
}

bool cmCommandArgument::MayFollow(const cmCommandArgument* current) const
{
  if (this->ArgumentsBeforeEmpty)
    {
    return true;
    }

  std::set<const cmCommandArgument*>::const_iterator argIt 
                                         = this->ArgumentsBefore.find(current);
  if (argIt != this->ArgumentsBefore.end())
    {
    return true;
    }

  return false;
}

bool cmCommandArgument::KeyMatches(const std::string& key) const
{
  if ((this->Key==0) || (this->Key[0]=='\0'))
    {
    return true;
    }
  return (key==this->Key);
}

void cmCommandArgument::ApplyOwnGroup()
{
  if (this->Group!=0)
    {
    for (std::vector<cmCommandArgument*>::const_iterator 
         it = this->Group->ContainedArguments.begin();
         it != this->Group->ContainedArguments.end();
         ++it)
      {
      if(*it != this)
        {
        this->ArgumentsBefore.insert(*it);
        }
      }
    }
}

void cmCommandArgument::Activate()
{
  this->WasActive = true; 
  this->CurrentIndex = 0;
}

bool cmCommandArgument::Consume(const std::string& arg)
{
  bool res=this->DoConsume(arg, this->CurrentIndex);
  this->CurrentIndex++;
  return res;
}


cmCAStringVector::cmCAStringVector(cmCommandArgumentsHelper* args, 
                                   const char* key, 
                                   cmCommandArgumentGroup* group)
:cmCommandArgument(args, key, group)
,Ignore(0)
{
  if ((key==0) || (*key==0))
    {
    this->DataStart = 0;
    }
  else
    {
    this->DataStart = 1;
    }
}

bool cmCAStringVector::DoConsume(const std::string& arg,unsigned int index)
{
  if (index >= this->DataStart)
    {
    if ((this->Ignore==0) || (arg != this->Ignore))
      {
      this->Vector.push_back(arg);
      }
    }

  return false;
}

void cmCAStringVector::DoReset()
{
  this->Vector.clear();
}

cmCAString::cmCAString(cmCommandArgumentsHelper* args, 
                       const char* key, 
                       cmCommandArgumentGroup* group)
:cmCommandArgument(args, key, group)
{
  if ((key==0) || (*key==0))
    {
    this->DataStart = 0;
    }
  else
    {
    this->DataStart = 1;
    }
}

bool cmCAString::DoConsume(const std::string& arg, unsigned int index)
{
  if (index == this->DataStart)
    {
    this->String = arg;
    }

  return index >= this->DataStart;
}

void cmCAString::DoReset()
{
  this->String = this->DefaultString;
}

cmCAEnabler::cmCAEnabler(cmCommandArgumentsHelper* args, 
                         const char* key, 
                         cmCommandArgumentGroup* group)
:cmCommandArgument(args, key, group)
,Enabled(false) 
{}

bool cmCAEnabler::DoConsume(const std::string&, unsigned int index)
{
  if (index==0)
    {
    this->Enabled = true;
    }
  return true;
}

void cmCAEnabler::DoReset()
{
  this->Enabled = false;
}

cmCADisabler::cmCADisabler(cmCommandArgumentsHelper* args, 
                           const char* key, 
                           cmCommandArgumentGroup* group)
:cmCommandArgument(args, key, group)
,Enabled(true) 
{}

bool cmCADisabler::DoConsume(const std::string&, unsigned int index)
{
  if (index==0)
    {
    this->Enabled = false;
    }
  return true;
}

void cmCADisabler::DoReset()
{
  this->Enabled = true;
}

void cmCommandArgumentGroup::Follows(const cmCommandArgument* arg)
{
  for(std::vector<cmCommandArgument*>::iterator 
      it = this->ContainedArguments.begin();
      it != this->ContainedArguments.end();
      ++it)
    {
    (*it)->Follows(arg);
    }
}

void cmCommandArgumentGroup::FollowsGroup(const cmCommandArgumentGroup* group)
{
  for(std::vector<cmCommandArgument*>::iterator 
      it = this->ContainedArguments.begin();
      it != this->ContainedArguments.end();
      ++it)
    {
    (*it)->FollowsGroup(group);
    }
}

void cmCommandArgumentsHelper::Parse(const std::vector<std::string>* args, 
                                     std::vector<std::string>* unconsumedArgs)
{
  if(args==0)
    {
    return;
    }

  for(std::vector<cmCommandArgument*>::iterator 
      argIt = this->Arguments.begin();
      argIt != this->Arguments.end();
      ++argIt)
    {
    (*argIt)->ApplyOwnGroup();
    (*argIt)->Reset();
    }

  cmCommandArgument* activeArgument = 0;
  const cmCommandArgument* previousArgument = 0;
  for(std::vector<std::string>::const_iterator it = args->begin();
      it != args->end();
      ++it)
    {
    for(std::vector<cmCommandArgument*>::iterator 
        argIt = this->Arguments.begin();
        argIt != this->Arguments.end();
        ++argIt)
      {
      if ((*argIt)->KeyMatches(*it) && ((*argIt)->MayFollow(previousArgument)))
        {
        activeArgument = *argIt;
        activeArgument->Activate();
        break;
        }
      }

    if (activeArgument)
      {
      bool argDone = activeArgument->Consume(*it);
      previousArgument = activeArgument;
      if (argDone)
        {
        activeArgument = 0;
        }
      }
    else
      {
      if (unconsumedArgs!=0)
        {
        unconsumedArgs->push_back(*it);
        }
      }
    }
}

void cmCommandArgumentsHelper::AddArgument(cmCommandArgument* arg)
{
  this->Arguments.push_back(arg);
}