Initial commit.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmListCommand.cxx
blob1da59e1fe8300c6b7ba53c88b0ca634d71cbe61c
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmListCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2008/01/23 15:27:59 $
7 Version: $Revision: 1.17 $
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 =========================================================================*/
17 #include "cmListCommand.h"
18 #include <cmsys/RegularExpression.hxx>
19 #include <cmsys/SystemTools.hxx>
21 #include <stdlib.h> // required for atoi
22 #include <ctype.h>
23 //----------------------------------------------------------------------------
24 bool cmListCommand
25 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
27 if(args.size() < 1)
29 this->SetError("must be called with at least one argument.");
30 return false;
33 const std::string &subCommand = args[0];
34 if(subCommand == "LENGTH")
36 return this->HandleLengthCommand(args);
38 if(subCommand == "GET")
40 return this->HandleGetCommand(args);
42 if(subCommand == "APPEND")
44 return this->HandleAppendCommand(args);
46 if(subCommand == "FIND")
48 return this->HandleFindCommand(args);
50 if(subCommand == "INSERT")
52 return this->HandleInsertCommand(args);
54 if(subCommand == "REMOVE_AT")
56 return this->HandleRemoveAtCommand(args);
58 if(subCommand == "REMOVE_ITEM")
60 return this->HandleRemoveItemCommand(args);
62 if(subCommand == "SORT")
64 return this->HandleSortCommand(args);
66 if(subCommand == "REVERSE")
68 return this->HandleReverseCommand(args);
71 std::string e = "does not recognize sub-command "+subCommand;
72 this->SetError(e.c_str());
73 return false;
76 //----------------------------------------------------------------------------
77 bool cmListCommand::GetListString(std::string& listString, const char* var)
79 if ( !var )
81 return false;
83 // get the old value
84 const char* cacheValue
85 = this->Makefile->GetDefinition(var);
86 if(!cacheValue)
88 return false;
90 listString = cacheValue;
91 return true;
94 //----------------------------------------------------------------------------
95 bool cmListCommand::GetList(std::vector<std::string>& list, const char* var)
97 std::string listString;
98 if ( !this->GetListString(listString, var) )
100 return false;
102 // expand the variable
103 cmSystemTools::ExpandListArgument(listString, list);
104 return true;
107 //----------------------------------------------------------------------------
108 bool cmListCommand::HandleLengthCommand(std::vector<std::string> const& args)
110 if(args.size() != 3)
112 this->SetError("sub-command LENGTH requires two arguments.");
113 return false;
116 const std::string& listName = args[1];
117 const std::string& variableName = args[args.size() - 1];
118 std::vector<std::string> varArgsExpanded;
119 // do not check the return value here
120 // if the list var is not found varArgsExpanded will have size 0
121 // and we will return 0
122 this->GetList(varArgsExpanded, listName.c_str());
123 size_t length = varArgsExpanded.size();
124 char buffer[1024];
125 sprintf(buffer, "%d", static_cast<int>(length));
127 this->Makefile->AddDefinition(variableName.c_str(), buffer);
128 return true;
131 //----------------------------------------------------------------------------
132 bool cmListCommand::HandleGetCommand(std::vector<std::string> const& args)
134 if(args.size() < 4)
136 this->SetError("sub-command GET requires at least three arguments.");
137 return false;
140 const std::string& listName = args[1];
141 const std::string& variableName = args[args.size() - 1];
142 // expand the variable
143 std::vector<std::string> varArgsExpanded;
144 if ( !this->GetList(varArgsExpanded, listName.c_str()) )
146 this->Makefile->AddDefinition(variableName.c_str(), "NOTFOUND");
147 return true;
150 std::string value;
151 size_t cc;
152 for ( cc = 2; cc < args.size()-1; cc ++ )
154 int item = atoi(args[cc].c_str());
155 if (value.size())
157 value += ";";
159 size_t nitem = varArgsExpanded.size();
160 if ( item < 0 )
162 item = (int)nitem + item;
164 if ( item < 0 || nitem <= (size_t)item )
166 cmOStringStream str;
167 str << "index: " << item << " out of range (-"
168 << varArgsExpanded.size() << ", "
169 << varArgsExpanded.size()-1 << ")";
170 this->SetError(str.str().c_str());
171 return false;
173 value += varArgsExpanded[item];
176 this->Makefile->AddDefinition(variableName.c_str(), value.c_str());
177 return true;
180 //----------------------------------------------------------------------------
181 bool cmListCommand::HandleAppendCommand(std::vector<std::string> const& args)
183 if(args.size() < 2)
185 this->SetError("sub-command APPEND requires at least one argument.");
186 return false;
189 // Skip if nothing to append.
190 if(args.size() < 3)
192 return true;
195 const std::string& listName = args[1];
196 // expand the variable
197 std::string listString;
198 this->GetListString(listString, listName.c_str());
199 size_t cc;
200 for ( cc = 2; cc < args.size(); ++ cc )
202 if ( listString.size() )
204 listString += ";";
206 listString += args[cc];
209 this->Makefile->AddDefinition(listName.c_str(), listString.c_str());
210 return true;
213 //----------------------------------------------------------------------------
214 bool cmListCommand::HandleFindCommand(std::vector<std::string> const& args)
216 if(args.size() != 4)
218 this->SetError("sub-command FIND requires three arguments.");
219 return false;
222 const std::string& listName = args[1];
223 const std::string& variableName = args[args.size() - 1];
224 // expand the variable
225 std::vector<std::string> varArgsExpanded;
226 if ( !this->GetList(varArgsExpanded, listName.c_str()) )
228 this->Makefile->AddDefinition(variableName.c_str(), "-1");
229 return true;
232 std::vector<std::string>::iterator it;
233 unsigned int index = 0;
234 for ( it = varArgsExpanded.begin(); it != varArgsExpanded.end(); ++ it )
236 if ( *it == args[2] )
238 char indexString[32];
239 sprintf(indexString, "%d", index);
240 this->Makefile->AddDefinition(variableName.c_str(), indexString);
241 return true;
243 index++;
246 this->Makefile->AddDefinition(variableName.c_str(), "-1");
247 return true;
250 //----------------------------------------------------------------------------
251 bool cmListCommand::HandleInsertCommand(std::vector<std::string> const& args)
253 if(args.size() < 4)
255 this->SetError("sub-command INSERT requires at least three arguments.");
256 return false;
259 const std::string& listName = args[1];
261 // expand the variable
262 int item = atoi(args[2].c_str());
263 std::vector<std::string> varArgsExpanded;
264 if ( !this->GetList(varArgsExpanded, listName.c_str()) && item != 0)
266 cmOStringStream str;
267 str << "index: " << item << " out of range (0, 0)";
268 this->SetError(str.str().c_str());
269 return false;
272 if ( varArgsExpanded.size() != 0 )
274 size_t nitem = varArgsExpanded.size();
275 if ( item < 0 )
277 item = (int)nitem + item;
279 if ( item < 0 || nitem <= (size_t)item )
281 cmOStringStream str;
282 str << "index: " << item << " out of range (-"
283 << varArgsExpanded.size() << ", "
284 << (varArgsExpanded.size() == 0?0:(varArgsExpanded.size()-1)) << ")";
285 this->SetError(str.str().c_str());
286 return false;
289 size_t cc;
290 size_t cnt = 0;
291 for ( cc = 3; cc < args.size(); ++ cc )
293 varArgsExpanded.insert(varArgsExpanded.begin()+item+cnt, args[cc]);
294 cnt ++;
297 std::string value;
298 for ( cc = 0; cc < varArgsExpanded.size(); cc ++ )
300 if (value.size())
302 value += ";";
304 value += varArgsExpanded[cc];
307 this->Makefile->AddDefinition(listName.c_str(), value.c_str());
308 return true;
311 //----------------------------------------------------------------------------
312 bool cmListCommand
313 ::HandleRemoveItemCommand(std::vector<std::string> const& args)
315 if(args.size() < 3)
317 this->SetError("sub-command REMOVE_ITEM requires two or more arguments.");
318 return false;
321 const std::string& listName = args[1];
322 // expand the variable
323 std::vector<std::string> varArgsExpanded;
324 if ( !this->GetList(varArgsExpanded, listName.c_str()) )
326 this->SetError("sub-command REMOVE_ITEM requires list to be present.");
327 return false;
330 size_t cc;
331 for ( cc = 2; cc < args.size(); ++ cc )
333 size_t kk = 0;
334 while ( kk < varArgsExpanded.size() )
336 if ( varArgsExpanded[kk] == args[cc] )
338 varArgsExpanded.erase(varArgsExpanded.begin()+kk);
340 else
342 kk ++;
347 std::string value;
348 for ( cc = 0; cc < varArgsExpanded.size(); cc ++ )
350 if (value.size())
352 value += ";";
354 value += varArgsExpanded[cc];
357 this->Makefile->AddDefinition(listName.c_str(), value.c_str());
358 return true;
361 //----------------------------------------------------------------------------
362 bool cmListCommand
363 ::HandleReverseCommand(std::vector<std::string> const& args)
365 if(args.size() < 2)
367 this->SetError("sub-command REVERSE requires a list as an argument.");
368 return false;
371 const std::string& listName = args[1];
372 // expand the variable
373 std::vector<std::string> varArgsExpanded;
374 if ( !this->GetList(varArgsExpanded, listName.c_str()) )
376 this->SetError("sub-command REVERSE requires list to be present.");
377 return false;
380 std::string value;
381 std::vector<std::string>::reverse_iterator it;
382 for ( it = varArgsExpanded.rbegin(); it != varArgsExpanded.rend(); ++ it )
384 if (value.size())
386 value += ";";
388 value += it->c_str();
391 this->Makefile->AddDefinition(listName.c_str(), value.c_str());
392 return true;
395 //----------------------------------------------------------------------------
396 bool cmListCommand
397 ::HandleSortCommand(std::vector<std::string> const& args)
399 if(args.size() < 2)
401 this->SetError("sub-command SORT requires a list as an argument.");
402 return false;
405 const std::string& listName = args[1];
406 // expand the variable
407 std::vector<std::string> varArgsExpanded;
408 if ( !this->GetList(varArgsExpanded, listName.c_str()) )
410 this->SetError("sub-command SORT requires list to be present.");
411 return false;
414 std::sort(varArgsExpanded.begin(), varArgsExpanded.end());
416 std::string value;
417 std::vector<std::string>::iterator it;
418 for ( it = varArgsExpanded.begin(); it != varArgsExpanded.end(); ++ it )
420 if (value.size())
422 value += ";";
424 value += it->c_str();
427 this->Makefile->AddDefinition(listName.c_str(), value.c_str());
428 return true;
431 //----------------------------------------------------------------------------
432 bool cmListCommand::HandleRemoveAtCommand(
433 std::vector<std::string> const& args)
435 if(args.size() < 3)
437 this->SetError("sub-command REMOVE_AT requires at least "
438 "two arguments.");
439 return false;
442 const std::string& listName = args[1];
443 // expand the variable
444 std::vector<std::string> varArgsExpanded;
445 if ( !this->GetList(varArgsExpanded, listName.c_str()) )
447 this->SetError("sub-command REMOVE_AT requires list to be present.");
448 return false;
451 size_t cc;
452 std::vector<size_t> removed;
453 for ( cc = 2; cc < args.size(); ++ cc )
455 int item = atoi(args[cc].c_str());
456 size_t nitem = varArgsExpanded.size();
457 if ( item < 0 )
459 item = (int)nitem + item;
461 if ( item < 0 || nitem <= (size_t)item )
463 cmOStringStream str;
464 str << "index: " << item << " out of range (-"
465 << varArgsExpanded.size() << ", "
466 << varArgsExpanded.size()-1 << ")";
467 this->SetError(str.str().c_str());
468 return false;
470 removed.push_back(static_cast<size_t>(item));
473 std::string value;
474 for ( cc = 0; cc < varArgsExpanded.size(); ++ cc )
476 size_t kk;
477 bool found = false;
478 for ( kk = 0; kk < removed.size(); ++ kk )
480 if ( cc == removed[kk] )
482 found = true;
486 if ( !found )
488 if (value.size())
490 value += ";";
492 value += varArgsExpanded[cc];
496 this->Makefile->AddDefinition(listName.c_str(), value.c_str());
497 return true;