BUG: fix some bad changes in progress calc
[cmake.git] / Source / kwsys / CommandLineArguments.cxx
blobfecec7e2c6bf72028eff1c56e564c023b804b5bf
1 /*=========================================================================
3 Program: KWSys - Kitware System Library
4 Module: $RCSfile: CommandLineArguments.cxx,v $
6 Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
7 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notices for more information.
13 =========================================================================*/
14 #include "kwsysPrivate.h"
15 #include KWSYS_HEADER(CommandLineArguments.hxx)
17 #include KWSYS_HEADER(Configure.hxx)
18 #include KWSYS_HEADER(String.hxx)
20 #include KWSYS_HEADER(stl/vector)
21 #include KWSYS_HEADER(stl/map)
22 #include KWSYS_HEADER(stl/set)
23 #include KWSYS_HEADER(ios/sstream)
24 #include KWSYS_HEADER(ios/iostream)
26 // Work-around CMake dependency scanning limitation. This must
27 // duplicate the above list of headers.
28 #if 0
29 # include "CommandLineArguments.hxx.in"
30 # include "Configure.hxx.in"
31 # include "kwsys_stl.hxx.in"
32 # include "kwsys_ios_sstream.h.in"
33 # include "kwsys_ios_iostream.h.in"
34 #endif
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
40 #ifdef _MSC_VER
41 # pragma warning (disable: 4786)
42 #endif
44 #if defined(__sgi) && !defined(__GNUC__)
45 # pragma set woff 1375 /* base class destructor not virtual */
46 #endif
48 #if 0
49 # define CommandLineArguments_DEBUG(x) \
50 kwsys_ios::cout << __LINE__ << " CLA: " << x << kwsys_ios::endl
51 #else
52 # define CommandLineArguments_DEBUG(x)
53 #endif
55 namespace KWSYS_NAMESPACE
58 //----------------------------------------------------------------------------
59 //============================================================================
60 struct CommandLineArgumentsCallbackStructure
62 const char* Argument;
63 int ArgumentType;
64 CommandLineArguments::CallbackType Callback;
65 void* CallData;
66 void* Variable;
67 int VariableType;
68 const char* Help;
71 class CommandLineArgumentsVectorOfStrings :
72 public kwsys_stl::vector<kwsys::String> {};
73 class CommandLineArgumentsSetOfStrings :
74 public kwsys_stl::set<kwsys::String> {};
75 class CommandLineArgumentsMapOfStrucs :
76 public kwsys_stl::map<kwsys::String,
77 CommandLineArgumentsCallbackStructure> {};
79 class CommandLineArgumentsInternal
81 public:
82 CommandLineArgumentsInternal()
84 this->UnknownArgumentCallback = 0;
85 this->ClientData = 0;
86 this->LastArgument = 0;
89 typedef CommandLineArgumentsVectorOfStrings VectorOfStrings;
90 typedef CommandLineArgumentsMapOfStrucs CallbacksMap;
91 typedef kwsys::String String;
92 typedef CommandLineArgumentsSetOfStrings SetOfStrings;
94 VectorOfStrings Argv;
95 String Argv0;
96 CallbacksMap Callbacks;
98 CommandLineArguments::ErrorCallbackType UnknownArgumentCallback;
99 void* ClientData;
101 VectorOfStrings::size_type LastArgument;
103 VectorOfStrings UnusedArguments;
105 //============================================================================
106 //----------------------------------------------------------------------------
108 //----------------------------------------------------------------------------
109 CommandLineArguments::CommandLineArguments()
111 this->Internals = new CommandLineArguments::Internal;
112 this->Help = "";
113 this->LineLength = 80;
114 this->StoreUnusedArgumentsFlag = false;
117 //----------------------------------------------------------------------------
118 CommandLineArguments::~CommandLineArguments()
120 delete this->Internals;
123 //----------------------------------------------------------------------------
124 void CommandLineArguments::Initialize(int argc, const char* const argv[])
126 int cc;
128 this->Initialize();
129 this->Internals->Argv0 = argv[0];
130 for ( cc = 1; cc < argc; cc ++ )
132 this->ProcessArgument(argv[cc]);
136 //----------------------------------------------------------------------------
137 void CommandLineArguments::Initialize(int argc, char* argv[])
139 this->Initialize(argc, static_cast<const char* const*>(argv));
142 //----------------------------------------------------------------------------
143 void CommandLineArguments::Initialize()
145 this->Internals->Argv.clear();
146 this->Internals->LastArgument = 0;
149 //----------------------------------------------------------------------------
150 void CommandLineArguments::ProcessArgument(const char* arg)
152 this->Internals->Argv.push_back(arg);
155 //----------------------------------------------------------------------------
156 bool CommandLineArguments::GetMatchedArguments(
157 kwsys_stl::vector<kwsys_stl::string>* matches,
158 const kwsys_stl::string& arg)
160 matches->clear();
161 CommandLineArguments::Internal::CallbacksMap::iterator it;
163 // Does the argument match to any we know about?
164 for ( it = this->Internals->Callbacks.begin();
165 it != this->Internals->Callbacks.end();
166 it ++ )
168 const CommandLineArguments::Internal::String& parg = it->first;
169 CommandLineArgumentsCallbackStructure *cs = &it->second;
170 if (cs->ArgumentType == CommandLineArguments::NO_ARGUMENT ||
171 cs->ArgumentType == CommandLineArguments::SPACE_ARGUMENT)
173 if ( arg == parg )
175 matches->push_back(parg);
178 else if ( arg.find( parg ) == 0 )
180 matches->push_back(parg);
183 return matches->size() > 0;
186 //----------------------------------------------------------------------------
187 int CommandLineArguments::Parse()
189 kwsys_stl::vector<kwsys_stl::string>::size_type cc;
190 kwsys_stl::vector<kwsys_stl::string> matches;
191 if ( this->StoreUnusedArgumentsFlag )
193 this->Internals->UnusedArguments.clear();
195 for ( cc = 0; cc < this->Internals->Argv.size(); cc ++ )
197 const kwsys_stl::string& arg = this->Internals->Argv[cc];
198 CommandLineArguments_DEBUG("Process argument: " << arg);
199 this->Internals->LastArgument = cc;
200 if ( this->GetMatchedArguments(&matches, arg) )
202 // Ok, we found one or more arguments that match what user specified.
203 // Let's find the longest one.
204 CommandLineArguments::Internal::VectorOfStrings::size_type kk;
205 CommandLineArguments::Internal::VectorOfStrings::size_type maxidx = 0;
206 CommandLineArguments::Internal::String::size_type maxlen = 0;
207 for ( kk = 0; kk < matches.size(); kk ++ )
209 if ( matches[kk].size() > maxlen )
211 maxlen = matches[kk].size();
212 maxidx = kk;
215 // So, the longest one is probably the right one. Now see if it has any
216 // additional value
217 CommandLineArgumentsCallbackStructure *cs
218 = &this->Internals->Callbacks[matches[maxidx]];
219 const kwsys_stl::string& sarg = matches[maxidx];
220 if ( cs->Argument != sarg )
222 abort();
224 switch ( cs->ArgumentType )
226 case NO_ARGUMENT:
227 // No value
228 if ( !this->PopulateVariable(cs, 0) )
230 return 0;
232 break;
233 case SPACE_ARGUMENT:
234 if ( cc == this->Internals->Argv.size()-1 )
236 this->Internals->LastArgument --;
237 return 0;
239 CommandLineArguments_DEBUG("This is a space argument: " << arg
240 << " value: " << this->Internals->Argv[cc+1].c_str());
241 // Value is the next argument
242 if ( !this->PopulateVariable(cs, this->Internals->Argv[cc+1].c_str()) )
244 return 0;
246 cc ++;
247 break;
248 case EQUAL_ARGUMENT:
249 if ( arg.size() == sarg.size() || *(arg.c_str() + sarg.size()) != '=' )
251 this->Internals->LastArgument --;
252 return 0;
254 // Value is everythng followed the '=' sign
255 if ( !this->PopulateVariable(cs, arg.c_str() + sarg.size() + 1) )
257 return 0;
259 break;
260 case CONCAT_ARGUMENT:
261 // Value is whatever follows the argument
262 if ( !this->PopulateVariable(cs, arg.c_str() + sarg.size()) )
264 return 0;
266 break;
267 case MULTI_ARGUMENT:
268 // Suck in all the rest of the arguments
269 CommandLineArguments_DEBUG("This is a multi argument: " << arg);
270 for (cc++; cc < this->Internals->Argv.size(); ++ cc )
272 const kwsys_stl::string& marg = this->Internals->Argv[cc];
273 CommandLineArguments_DEBUG(" check multi argument value: " << marg);
274 if ( this->GetMatchedArguments(&matches, marg) )
276 CommandLineArguments_DEBUG("End of multi argument " << arg << " with value: " << marg);
277 break;
279 CommandLineArguments_DEBUG(" populate multi argument value: " << marg);
280 if ( !this->PopulateVariable(cs, marg.c_str()) )
282 return 0;
285 if ( cc != this->Internals->Argv.size() )
287 CommandLineArguments_DEBUG("Again End of multi argument " << arg);
288 cc--;
289 continue;
291 break;
292 default:
293 kwsys_ios::cerr << "Got unknown argument type: \"" << cs->ArgumentType << "\"" << kwsys_ios::endl;
294 this->Internals->LastArgument --;
295 return 0;
298 else
300 // Handle unknown arguments
301 if ( this->Internals->UnknownArgumentCallback )
303 if ( !this->Internals->UnknownArgumentCallback(arg.c_str(),
304 this->Internals->ClientData) )
306 this->Internals->LastArgument --;
307 return 0;
309 return 1;
311 else if ( this->StoreUnusedArgumentsFlag )
313 CommandLineArguments_DEBUG("Store unused argument " << arg);
314 this->Internals->UnusedArguments.push_back(arg.c_str());
316 else
318 kwsys_ios::cerr << "Got unknown argument: \"" << arg.c_str() << "\"" << kwsys_ios::endl;
319 this->Internals->LastArgument --;
320 return 0;
324 return 1;
327 //----------------------------------------------------------------------------
328 void CommandLineArguments::GetRemainingArguments(int* argc, char*** argv)
330 CommandLineArguments::Internal::VectorOfStrings::size_type size
331 = this->Internals->Argv.size() - this->Internals->LastArgument + 1;
332 CommandLineArguments::Internal::VectorOfStrings::size_type cc;
334 // Copy Argv0 as the first argument
335 char** args = new char*[ size ];
336 args[0] = new char[ this->Internals->Argv0.size() + 1 ];
337 strcpy(args[0], this->Internals->Argv0.c_str());
338 int cnt = 1;
340 // Copy everything after the LastArgument, since that was not parsed.
341 for ( cc = this->Internals->LastArgument+1;
342 cc < this->Internals->Argv.size(); cc ++ )
344 args[cnt] = new char[ this->Internals->Argv[cc].size() + 1];
345 strcpy(args[cnt], this->Internals->Argv[cc].c_str());
346 cnt ++;
348 *argc = cnt;
349 *argv = args;
352 //----------------------------------------------------------------------------
353 void CommandLineArguments::GetUnusedArguments(int* argc, char*** argv)
355 CommandLineArguments::Internal::VectorOfStrings::size_type size
356 = this->Internals->UnusedArguments.size() + 1;
357 CommandLineArguments::Internal::VectorOfStrings::size_type cc;
359 // Copy Argv0 as the first argument
360 char** args = new char*[ size ];
361 args[0] = new char[ this->Internals->Argv0.size() + 1 ];
362 strcpy(args[0], this->Internals->Argv0.c_str());
363 int cnt = 1;
365 // Copy everything after the LastArgument, since that was not parsed.
366 for ( cc = 0;
367 cc < this->Internals->UnusedArguments.size(); cc ++ )
369 kwsys::String &str = this->Internals->UnusedArguments[cc];
370 args[cnt] = new char[ str.size() + 1];
371 strcpy(args[cnt], str.c_str());
372 cnt ++;
374 *argc = cnt;
375 *argv = args;
378 //----------------------------------------------------------------------------
379 void CommandLineArguments::DeleteRemainingArguments(int argc, char*** argv)
381 int cc;
382 for ( cc = 0; cc < argc; ++ cc )
384 delete [] (*argv)[cc];
386 delete [] *argv;
389 //----------------------------------------------------------------------------
390 void CommandLineArguments::AddCallback(const char* argument, ArgumentTypeEnum type,
391 CallbackType callback, void* call_data, const char* help)
393 CommandLineArgumentsCallbackStructure s;
394 s.Argument = argument;
395 s.ArgumentType = type;
396 s.Callback = callback;
397 s.CallData = call_data;
398 s.VariableType = CommandLineArguments::NO_VARIABLE_TYPE;
399 s.Variable = 0;
400 s.Help = help;
402 this->Internals->Callbacks[argument] = s;
403 this->GenerateHelp();
406 //----------------------------------------------------------------------------
407 void CommandLineArguments::AddArgument(const char* argument, ArgumentTypeEnum type,
408 VariableTypeEnum vtype, void* variable, const char* help)
410 CommandLineArgumentsCallbackStructure s;
411 s.Argument = argument;
412 s.ArgumentType = type;
413 s.Callback = 0;
414 s.CallData = 0;
415 s.VariableType = vtype;
416 s.Variable = variable;
417 s.Help = help;
419 this->Internals->Callbacks[argument] = s;
420 this->GenerateHelp();
423 //----------------------------------------------------------------------------
424 #define CommandLineArgumentsAddArgumentMacro(type, ctype) \
425 void CommandLineArguments::AddArgument(const char* argument, ArgumentTypeEnum type, \
426 ctype* variable, const char* help) \
428 this->AddArgument(argument, type, CommandLineArguments::type##_TYPE, variable, help); \
431 CommandLineArgumentsAddArgumentMacro(BOOL, bool)
432 CommandLineArgumentsAddArgumentMacro(INT, int)
433 CommandLineArgumentsAddArgumentMacro(DOUBLE, double)
434 CommandLineArgumentsAddArgumentMacro(STRING, char*)
435 CommandLineArgumentsAddArgumentMacro(STL_STRING, kwsys_stl::string)
437 CommandLineArgumentsAddArgumentMacro(VECTOR_BOOL, kwsys_stl::vector<bool>)
438 CommandLineArgumentsAddArgumentMacro(VECTOR_INT, kwsys_stl::vector<int>)
439 CommandLineArgumentsAddArgumentMacro(VECTOR_DOUBLE, kwsys_stl::vector<double>)
440 CommandLineArgumentsAddArgumentMacro(VECTOR_STRING, kwsys_stl::vector<char*>)
441 CommandLineArgumentsAddArgumentMacro(VECTOR_STL_STRING, kwsys_stl::vector<kwsys_stl::string>)
443 //----------------------------------------------------------------------------
444 #define CommandLineArgumentsAddBooleanArgumentMacro(type, ctype) \
445 void CommandLineArguments::AddBooleanArgument(const char* argument, \
446 ctype* variable, const char* help) \
448 this->AddArgument(argument, CommandLineArguments::NO_ARGUMENT, \
449 CommandLineArguments::type##_TYPE, variable, help); \
452 CommandLineArgumentsAddBooleanArgumentMacro(BOOL, bool)
453 CommandLineArgumentsAddBooleanArgumentMacro(INT, int)
454 CommandLineArgumentsAddBooleanArgumentMacro(DOUBLE, double)
455 CommandLineArgumentsAddBooleanArgumentMacro(STRING, char*)
456 CommandLineArgumentsAddBooleanArgumentMacro(STL_STRING, kwsys_stl::string)
458 //----------------------------------------------------------------------------
459 void CommandLineArguments::SetClientData(void* client_data)
461 this->Internals->ClientData = client_data;
464 //----------------------------------------------------------------------------
465 void CommandLineArguments::SetUnknownArgumentCallback(
466 CommandLineArguments::ErrorCallbackType callback)
468 this->Internals->UnknownArgumentCallback = callback;
471 //----------------------------------------------------------------------------
472 const char* CommandLineArguments::GetHelp(const char* arg)
474 CommandLineArguments::Internal::CallbacksMap::iterator it
475 = this->Internals->Callbacks.find(arg);
476 if ( it == this->Internals->Callbacks.end() )
478 return 0;
481 // Since several arguments may point to the same argument, find the one this
482 // one point to if this one is pointing to another argument.
483 CommandLineArgumentsCallbackStructure *cs = &(it->second);
484 for(;;)
486 CommandLineArguments::Internal::CallbacksMap::iterator hit
487 = this->Internals->Callbacks.find(cs->Help);
488 if ( hit == this->Internals->Callbacks.end() )
490 break;
492 cs = &(hit->second);
494 return cs->Help;
497 //----------------------------------------------------------------------------
498 void CommandLineArguments::SetLineLength(unsigned int ll)
500 if ( ll < 9 || ll > 1000 )
502 return;
504 this->LineLength = ll;
505 this->GenerateHelp();
508 //----------------------------------------------------------------------------
509 const char* CommandLineArguments::GetArgv0()
511 return this->Internals->Argv0.c_str();
514 //----------------------------------------------------------------------------
515 unsigned int CommandLineArguments::GetLastArgument()
517 return static_cast<unsigned int>(this->Internals->LastArgument + 1);
520 //----------------------------------------------------------------------------
521 void CommandLineArguments::GenerateHelp()
523 kwsys_ios::ostringstream str;
525 // Collapse all arguments into the map of vectors of all arguments that do
526 // the same thing.
527 CommandLineArguments::Internal::CallbacksMap::iterator it;
528 typedef kwsys_stl::map<CommandLineArguments::Internal::String,
529 CommandLineArguments::Internal::SetOfStrings > MapArgs;
530 MapArgs mp;
531 MapArgs::iterator mpit, smpit;
532 for ( it = this->Internals->Callbacks.begin();
533 it != this->Internals->Callbacks.end();
534 it ++ )
536 CommandLineArgumentsCallbackStructure *cs = &(it->second);
537 mpit = mp.find(cs->Help);
538 if ( mpit != mp.end() )
540 mpit->second.insert(it->first);
541 mp[it->first].insert(it->first);
543 else
545 mp[it->first].insert(it->first);
548 for ( it = this->Internals->Callbacks.begin();
549 it != this->Internals->Callbacks.end();
550 it ++ )
552 CommandLineArgumentsCallbackStructure *cs = &(it->second);
553 mpit = mp.find(cs->Help);
554 if ( mpit != mp.end() )
556 mpit->second.insert(it->first);
557 smpit = mp.find(it->first);
558 CommandLineArguments::Internal::SetOfStrings::iterator sit;
559 for ( sit = smpit->second.begin(); sit != smpit->second.end(); sit++ )
561 mpit->second.insert(*sit);
563 mp.erase(smpit);
565 else
567 mp[it->first].insert(it->first);
571 // Find the length of the longest string
572 CommandLineArguments::Internal::String::size_type maxlen = 0;
573 for ( mpit = mp.begin();
574 mpit != mp.end();
575 mpit ++ )
577 CommandLineArguments::Internal::SetOfStrings::iterator sit;
578 for ( sit = mpit->second.begin(); sit != mpit->second.end(); sit++ )
580 CommandLineArguments::Internal::String::size_type clen = sit->size();
581 switch ( this->Internals->Callbacks[*sit].ArgumentType )
583 case CommandLineArguments::NO_ARGUMENT: clen += 0; break;
584 case CommandLineArguments::CONCAT_ARGUMENT: clen += 3; break;
585 case CommandLineArguments::SPACE_ARGUMENT: clen += 4; break;
586 case CommandLineArguments::EQUAL_ARGUMENT: clen += 4; break;
588 if ( clen > maxlen )
590 maxlen = clen;
595 // Create format for that string
596 char format[80];
597 sprintf(format, " %%-%ds ", static_cast<unsigned int>(maxlen));
599 maxlen += 4; // For the space before and after the option
601 // Print help for each option
602 for ( mpit = mp.begin();
603 mpit != mp.end();
604 mpit ++ )
606 CommandLineArguments::Internal::SetOfStrings::iterator sit;
607 for ( sit = mpit->second.begin(); sit != mpit->second.end(); sit++ )
609 str << kwsys_ios::endl;
610 char argument[100];
611 sprintf(argument, "%s", sit->c_str());
612 switch ( this->Internals->Callbacks[*sit].ArgumentType )
614 case CommandLineArguments::NO_ARGUMENT: break;
615 case CommandLineArguments::CONCAT_ARGUMENT: strcat(argument, "opt"); break;
616 case CommandLineArguments::SPACE_ARGUMENT: strcat(argument, " opt"); break;
617 case CommandLineArguments::EQUAL_ARGUMENT: strcat(argument, "=opt"); break;
618 case CommandLineArguments::MULTI_ARGUMENT: strcat(argument, " opt opt ..."); break;
620 char buffer[80];
621 sprintf(buffer, format, argument);
622 str << buffer;
624 const char* ptr = this->Internals->Callbacks[mpit->first].Help;
625 size_t len = strlen(ptr);
626 int cnt = 0;
627 while ( len > 0)
629 // If argument with help is longer than line length, split it on previous
630 // space (or tab) and continue on the next line
631 CommandLineArguments::Internal::String::size_type cc;
632 for ( cc = 0; ptr[cc]; cc ++ )
634 if ( *ptr == ' ' || *ptr == '\t' )
636 ptr ++;
637 len --;
640 if ( cnt > 0 )
642 for ( cc = 0; cc < maxlen; cc ++ )
644 str << " ";
647 CommandLineArguments::Internal::String::size_type skip = len;
648 if ( skip > this->LineLength - maxlen )
650 skip = this->LineLength - maxlen;
651 for ( cc = skip-1; cc > 0; cc -- )
653 if ( ptr[cc] == ' ' || ptr[cc] == '\t' )
655 break;
658 if ( cc != 0 )
660 skip = cc;
663 str.write(ptr, skip);
664 str << kwsys_ios::endl;
665 ptr += skip;
666 len -= skip;
667 cnt ++;
671 // This can help debugging help string
672 str << endl;
673 unsigned int cc;
674 for ( cc = 0; cc < this->LineLength; cc ++ )
676 str << cc % 10;
678 str << endl;
680 this->Help = str.str();
683 //----------------------------------------------------------------------------
684 void CommandLineArguments::PopulateVariable(
685 bool* variable, const kwsys_stl::string& value)
687 if ( value == "1" || value == "ON" || value == "on" || value == "On" ||
688 value == "TRUE" || value == "true" || value == "True" ||
689 value == "yes" || value == "Yes" || value == "YES" )
691 *variable = true;
693 else
695 *variable = false;
699 //----------------------------------------------------------------------------
700 void CommandLineArguments::PopulateVariable(
701 int* variable, const kwsys_stl::string& value)
703 char* res = 0;
704 *variable = strtol(value.c_str(), &res, 10);
705 //if ( res && *res )
706 // {
707 // Can handle non-int
708 // }
711 //----------------------------------------------------------------------------
712 void CommandLineArguments::PopulateVariable(
713 double* variable, const kwsys_stl::string& value)
715 char* res = 0;
716 *variable = strtod(value.c_str(), &res);
717 //if ( res && *res )
718 // {
719 // Can handle non-double
720 // }
723 //----------------------------------------------------------------------------
724 void CommandLineArguments::PopulateVariable(
725 char** variable, const kwsys_stl::string& value)
727 if ( *variable )
729 delete [] *variable;
730 *variable = 0;
732 *variable = new char[ value.size() + 1 ];
733 strcpy(*variable, value.c_str());
736 //----------------------------------------------------------------------------
737 void CommandLineArguments::PopulateVariable(
738 kwsys_stl::string* variable, const kwsys_stl::string& value)
740 *variable = value;
743 //----------------------------------------------------------------------------
744 void CommandLineArguments::PopulateVariable(
745 kwsys_stl::vector<bool>* variable, const kwsys_stl::string& value)
747 bool val = false;
748 if ( value == "1" || value == "ON" || value == "on" || value == "On" ||
749 value == "TRUE" || value == "true" || value == "True" ||
750 value == "yes" || value == "Yes" || value == "YES" )
752 val = true;
754 variable->push_back(val);
757 //----------------------------------------------------------------------------
758 void CommandLineArguments::PopulateVariable(
759 kwsys_stl::vector<int>* variable, const kwsys_stl::string& value)
761 char* res = 0;
762 variable->push_back(strtol(value.c_str(), &res, 10));
763 //if ( res && *res )
764 // {
765 // Can handle non-int
766 // }
769 //----------------------------------------------------------------------------
770 void CommandLineArguments::PopulateVariable(
771 kwsys_stl::vector<double>* variable, const kwsys_stl::string& value)
773 char* res = 0;
774 variable->push_back(strtod(value.c_str(), &res));
775 //if ( res && *res )
776 // {
777 // Can handle non-int
778 // }
781 //----------------------------------------------------------------------------
782 void CommandLineArguments::PopulateVariable(
783 kwsys_stl::vector<char*>* variable, const kwsys_stl::string& value)
785 char* var = new char[ value.size() + 1 ];
786 strcpy(var, value.c_str());
787 variable->push_back(var);
790 //----------------------------------------------------------------------------
791 void CommandLineArguments::PopulateVariable(
792 kwsys_stl::vector<kwsys_stl::string>* variable,
793 const kwsys_stl::string& value)
795 variable->push_back(value);
798 //----------------------------------------------------------------------------
799 bool CommandLineArguments::PopulateVariable(CommandLineArgumentsCallbackStructure* cs,
800 const char* value)
802 // Call the callback
803 if ( cs->Callback )
805 if ( !cs->Callback(cs->Argument, value, cs->CallData) )
807 this->Internals->LastArgument --;
808 return 0;
811 CommandLineArguments_DEBUG("Set argument: " << cs->Argument << " to " << value);
812 if ( cs->Variable )
814 kwsys_stl::string var = "1";
815 if ( value )
817 var = value;
819 switch ( cs->VariableType )
821 case CommandLineArguments::INT_TYPE:
822 this->PopulateVariable(static_cast<int*>(cs->Variable), var);
823 break;
824 case CommandLineArguments::DOUBLE_TYPE:
825 this->PopulateVariable(static_cast<double*>(cs->Variable), var);
826 break;
827 case CommandLineArguments::STRING_TYPE:
828 this->PopulateVariable(static_cast<char**>(cs->Variable), var);
829 break;
830 case CommandLineArguments::STL_STRING_TYPE:
831 this->PopulateVariable(static_cast<kwsys_stl::string*>(cs->Variable), var);
832 break;
833 case CommandLineArguments::BOOL_TYPE:
834 this->PopulateVariable(static_cast<bool*>(cs->Variable), var);
835 break;
836 case CommandLineArguments::VECTOR_BOOL_TYPE:
837 this->PopulateVariable(static_cast<kwsys_stl::vector<bool>*>(cs->Variable), var);
838 break;
839 case CommandLineArguments::VECTOR_INT_TYPE:
840 this->PopulateVariable(static_cast<kwsys_stl::vector<int>*>(cs->Variable), var);
841 break;
842 case CommandLineArguments::VECTOR_DOUBLE_TYPE:
843 this->PopulateVariable(static_cast<kwsys_stl::vector<double>*>(cs->Variable), var);
844 break;
845 case CommandLineArguments::VECTOR_STRING_TYPE:
846 this->PopulateVariable(static_cast<kwsys_stl::vector<char*>*>(cs->Variable), var);
847 break;
848 case CommandLineArguments::VECTOR_STL_STRING_TYPE:
849 this->PopulateVariable(static_cast<kwsys_stl::vector<kwsys_stl::string>*>(cs->Variable), var);
850 break;
851 default:
852 kwsys_ios::cerr << "Got unknown variable type: \"" << cs->VariableType << "\"" << kwsys_ios::endl;
853 this->Internals->LastArgument --;
854 return 0;
857 return 1;
861 } // namespace KWSYS_NAMESPACE