Debian Codegraph @

metaCommand.cxx

raw
1 /*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18 #include "metaCommand.h"
19
20 #include <stdio.h>
21 #include <stdlib.h> // atoi
22 #include <string.h>
23 #include <string>
24
25 #if (METAIO_USE_NAMESPACE)
26 namespace METAIO_NAMESPACE {
27 #endif
28
29 MetaCommand::MetaCommand()
30 {
31 m_HelpCallBack = nullptr;
32 m_OptionVector.clear();
33 m_Version = "Not defined";
34 m_Date = "Not defined";
35 m_Name = "";
36 m_Author = "Not defined";
37 m_Description = "";
38 m_ParsedOptionVector.clear();
39 m_Verbose = true;
40 }
41
42
43 /** Extract the date from the $Date: 2009-03-12 11:40:28 $ cvs command */
44 METAIO_STL::string MetaCommand::ExtractDateFromCVS(METAIO_STL::string date)
45 {
46 METAIO_STL::string newdate;
47 for(unsigned int i=7; i < date.size()-1; ++i)
48 {
49 newdate += date[i];
50 }
51 return newdate;
52 }
53
54
55 /** */
56 bool MetaCommand::SetOption(Option option)
57 {
58 // need to add some tests here to check if the option is not defined yet
59 m_OptionVector.push_back(option);
60 return true;
61 }
62
63 bool MetaCommand::SetOption(METAIO_STL::string name,
64 METAIO_STL::string tag,
65 bool required,
66 METAIO_STL::string description,
67 METAIO_STL::vector<Field> fields)
68 {
69 // need to add some tests here to check if the option is not defined yet
70 if (tag.empty()) {
71 METAIO_STREAM::cout << "Tag cannot be empty : use AddField() instead." << METAIO_STREAM::endl;
72 return false;
73 }
74
75 Option option;
76 option.name = name;
77 option.tag = tag;
78 option.fields = fields;
79 option.required = required;
80 option.description = description;
81 option.userDefined = false;
82 option.complete = false;
83
84 m_OptionVector.push_back(option);
85 return true;
86 }
87
88
89 bool MetaCommand::SetOption(METAIO_STL::string name,
90 METAIO_STL::string tag,
91 bool required,
92 METAIO_STL::string description,
93 TypeEnumType type,
94 METAIO_STL::string defVal)
95 {
96 // need to add some tests here to check if the option is not defined yet
97 if (tag.empty()) {
98 METAIO_STREAM::cout << "Tag cannot be empty : use AddField() instead." << METAIO_STREAM::endl;
99 return false;
100 }
101
102 Option option;
103 option.tag = tag;
104 option.name = name;
105 option.required = required;
106 option.description = description;
107 option.userDefined = false;
108 option.complete = false;
109
110 // Create a field without description as a flag
111 Field field;
112 if(type == LIST)
113 {
114 field.name = "NumberOfValues";
115 }
116 else
117 {
118 field.name = name;
119 }
120 field.externaldata = DATA_NONE;
121 field.type = type;
122 field.value = defVal;
123 field.userDefined = false;
124 field.required = true;
125 field.rangeMin = "";
126 field.rangeMax = "";
127 option.fields.push_back(field);
128
129 m_OptionVector.push_back(option);
130 return true;
131 }
132
133
134 /** Add a field */
135 bool MetaCommand::AddField(METAIO_STL::string name,
136 METAIO_STL::string description,
137 TypeEnumType type,
138 DataEnumType externalData,
139 METAIO_STL::string rangeMin,
140 METAIO_STL::string rangeMax)
141 {
142 // need to add some tests here to check if the option is not defined yet
143 Option option;
144 option.tag = "";
145
146 // Create a field without description with the specified type
147 Field field;
148 field.name = name;
149 field.type = type;
150 field.required = true;
151 field.userDefined = false;
152 field.externaldata = externalData;
153 field.rangeMin = rangeMin;
154 field.rangeMax = rangeMax;
155 option.fields.push_back(field);
156
157 option.required = true;
158 option.name = name;
159 option.description = description;
160 option.userDefined = false;
161 option.complete = false;
162
163 m_OptionVector.push_back(option);
164 return true;
165 }
166
167
168 /** Collect all the information until the next tag
169 * \warning this function works only if the field is of type String */
170 void MetaCommand::SetOptionComplete(METAIO_STL::string optionName,
171 bool complete)
172 {
173 auto it = m_OptionVector.begin();
174 while(it != m_OptionVector.end())
175 {
176 if((*it).name == optionName)
177 {
178 (*it).complete = complete;
179 return;
180 }
181 ++it;
182 }
183 }
184
185 /** Add a field to a given an option */
186 bool MetaCommand::AddOptionField(METAIO_STL::string optionName,
187 METAIO_STL::string name,
188 TypeEnumType type,
189 bool required,
190 METAIO_STL::string defVal,
191 METAIO_STL::string description,
192 DataEnumType externalData
193 )
194 {
195 auto it = m_OptionVector.begin();
196 while(it != m_OptionVector.end())
197 {
198 if((*it).name == optionName)
199 {
200 // Create a field without description with the specified type
201 Field field;
202 field.name = name;
203 field.type = type;
204 field.required = required;
205 field.value = defVal;
206 field.description = description;
207 field.userDefined = false;
208 field.externaldata = externalData;
209 field.rangeMin = "";
210 field.rangeMax = "";
211
212 // If this is the first field in the list we replace the current field
213 if((*it).fields[0].type == FLAG)
214 {
215 (*it).fields[0] = field;
216 }
217 else
218 {
219 (*it).fields.push_back(field);
220 }
221 return true;
222 }
223 ++it;
224 }
225 return false;
226 }
227
228 /** Set the range of an option */
229 bool MetaCommand::SetOptionRange(METAIO_STL::string optionName,
230 METAIO_STL::string name,
231 METAIO_STL::string rangeMin,
232 METAIO_STL::string rangeMax)
233 {
234 auto it = m_OptionVector.begin();
235 while(it != m_OptionVector.end())
236 {
237 if((*it).name == optionName)
238 {
239 METAIO_STL::vector<Field> & fields = (*it).fields;
240 auto itField = fields.begin();
241 while(itField != fields.end())
242 {
243 if((*itField).name == name)
244 {
245 (*itField).rangeMin = rangeMin;
246 (*itField).rangeMax = rangeMax;
247 return true;
248 }
249 ++itField;
250 }
251 }
252 ++it;
253 }
254 return false;
255 }
256
257
258 /** Return the value of the option as a boolean */
259 bool MetaCommand::GetValueAsBool(METAIO_STL::string optionName,METAIO_STL::string fieldName)
260 {
261 METAIO_STL::string fieldname = fieldName;
262 if (fieldName.empty()) {
263 fieldname = optionName;
264 }
265
266 OptionVector::const_iterator it = m_OptionVector.begin();
267 while(it != m_OptionVector.end())
268 {
269 if((*it).name == optionName)
270 {
271 auto itField = (*it).fields.begin();
272 while(itField != (*it).fields.end())
273 {
274 if((*itField).name == fieldname)
275 {
276 if((*itField).value == "true"
277 || (*itField).value == "1"
278 || (*itField).value == "True"
279 || (*itField).value == "TRUE"
280 )
281 {
282 return true;
283 }
284 return false;
285 }
286 ++itField;
287 }
288 }
289 ++it;
290 }
291 return false;
292 }
293
294
295 /** Return the value of the option as a bool */
296 bool MetaCommand::GetValueAsBool(Option option,METAIO_STL::string fieldName)
297 {
298 METAIO_STL::string fieldname = fieldName;
299 if (fieldName.empty()) {
300 fieldname = option.name;
301 }
302
303 METAIO_STL::vector<Field>::const_iterator itField = option.fields.begin();
304 while(itField != option.fields.end())
305 {
306 if((*itField).name == fieldname)
307 {
308 if((*itField).value == "true"
309 || (*itField).value == "1"
310 || (*itField).value == "True"
311 || (*itField).value == "TRUE"
312 )
313 {
314 return true;
315 }
316 return false;
317 }
318 ++itField;
319 }
320 return false;
321 }
322
323 /** Return the value of the option as a float */
324 float MetaCommand::GetValueAsFloat(METAIO_STL::string optionName,METAIO_STL::string fieldName)
325 {
326 METAIO_STL::string fieldname = fieldName;
327 if (fieldName.empty()) {
328 fieldname = optionName;
329 }
330
331 OptionVector::const_iterator it = m_OptionVector.begin();
332 while(it != m_OptionVector.end())
333 {
334 if((*it).name == optionName)
335 {
336 auto itField = (*it).fields.begin();
337 while(itField != (*it).fields.end())
338 {
339 if((*itField).name == fieldname)
340 {
341 return (float)atof((*itField).value.c_str());
342 }
343 ++itField;
344 }
345 }
346 ++it;
347 }
348 return 0;
349 }
350
351 /** Return the value of the option as a float */
352 float MetaCommand::GetValueAsFloat(Option option,METAIO_STL::string fieldName)
353 {
354 METAIO_STL::string fieldname = fieldName;
355 if (fieldName.empty()) {
356 fieldname = option.name;
357 }
358
359 METAIO_STL::vector<Field>::const_iterator itField = option.fields.begin();
360 while(itField != option.fields.end())
361 {
362 if((*itField).name == fieldname)
363 {
364 return (float)atof((*itField).value.c_str());
365 }
366 ++itField;
367 }
368 return 0;
369 }
370
371 /** Return the value of the option as a int */
372 int MetaCommand::GetValueAsInt(METAIO_STL::string optionName,METAIO_STL::string fieldName)
373 {
374 METAIO_STL::string fieldname = fieldName;
375 if (fieldName.empty()) {
376 fieldname = optionName;
377 }
378
379 OptionVector::const_iterator it = m_OptionVector.begin();
380 while(it != m_OptionVector.end())
381 {
382 if((*it).name == optionName)
383 {
384 auto itField = (*it).fields.begin();
385 while(itField != (*it).fields.end())
386 {
387 if((*itField).name == fieldname)
388 {
389 return atoi((*itField).value.c_str());
390 }
391 ++itField;
392 }
393 }
394 ++it;
395 }
396 return 0;
397 }
398
399 /** Return the value of the option as a int */
400 int MetaCommand::GetValueAsInt(Option option,METAIO_STL::string fieldName)
401 {
402 METAIO_STL::string fieldname = fieldName;
403 if (fieldName.empty()) {
404 fieldname = option.name;
405 }
406
407 METAIO_STL::vector<Field>::const_iterator itField = option.fields.begin();
408 while(itField != option.fields.end())
409 {
410 if((*itField).name == fieldname)
411 {
412 return atoi((*itField).value.c_str());
413 }
414 ++itField;
415 }
416 return 0;
417 }
418
419 /** Return the value of the option as a string */
420 METAIO_STL::string MetaCommand::GetValueAsString(METAIO_STL::string optionName,
421 METAIO_STL::string fieldName)
422 {
423 METAIO_STL::string fieldname = fieldName;
424 if (fieldName.empty()) {
425 fieldname = optionName;
426 }
427
428 OptionVector::const_iterator it = m_OptionVector.begin();
429 while(it != m_OptionVector.end())
430 {
431 if((*it).name == optionName)
432 {
433 auto itField = (*it).fields.begin();
434 while(itField != (*it).fields.end())
435 {
436 if((*itField).name == fieldname)
437 {
438 return (*itField).value;
439 }
440 ++itField;
441 }
442 }
443 ++it;
444 }
445 return "";
446 }
447
448 /** Return the value of the option as a string */
449 METAIO_STL::string MetaCommand::GetValueAsString(Option option,METAIO_STL::string fieldName)
450 {
451 METAIO_STL::string fieldname = fieldName;
452 if (fieldName.empty()) {
453 fieldname = option.name;
454 }
455
456 METAIO_STL::vector<Field>::const_iterator itField = option.fields.begin();
457 while(itField != option.fields.end())
458 {
459 if((*itField).name == fieldname)
460 {
461 return (*itField).value;
462 }
463 ++itField;
464 }
465 return "";
466 }
467
468 /** Return the value of the option as a list of strings */
469 METAIO_STL::list<METAIO_STL::string> MetaCommand::
470 GetValueAsList( Option option )
471 {
472 METAIO_STL::list<METAIO_STL::string> results;
473 results.clear();
474 METAIO_STL::vector<Field>::const_iterator itField = option.fields.begin();
475 ++itField;
476 while(itField != option.fields.end())
477 {
478 results.push_back((*itField).value);
479 ++itField;
480 }
481 return results;
482 }
483
484 METAIO_STL::list< METAIO_STL::string > MetaCommand::
485 GetValueAsList( METAIO_STL::string optionName )
486 {
487 OptionVector::const_iterator it = m_OptionVector.begin();
488 while(it != m_OptionVector.end())
489 {
490 if((*it).name == optionName)
491 {
492 return this->GetValueAsList( *it );
493 }
494 ++it;
495 }
496 METAIO_STL::list< METAIO_STL::string > empty;
497 empty.clear();
498 return empty;
499 }
500
501 bool MetaCommand::
502 GetOptionWasSet(Option option)
503 {
504 return option.userDefined;
505 }
506
507 bool MetaCommand::
508 GetOptionWasSet( METAIO_STL::string optionName)
509 {
510 OptionVector::const_iterator it = m_ParsedOptionVector.begin();
511 while(it != m_ParsedOptionVector.end())
512 {
513 if((*it).name == optionName)
514 {
515 return true;
516 }
517 ++it;
518 }
519 return false;
520 }
521
522 /** List the current options */
523 void MetaCommand::ListOptions()
524 {
525 OptionVector::const_iterator it = m_OptionVector.begin();
526 int i=0;
527 while(it != m_OptionVector.end())
528 {
529 METAIO_STREAM::cout << "Option #" << i << METAIO_STREAM::endl;
530 METAIO_STREAM::cout << " Name: " << (*it).name.c_str() << METAIO_STREAM::endl;
531 if (!(*it).tag.empty()) {
532 METAIO_STREAM::cout << " Tag: " << (*it).tag.c_str() << METAIO_STREAM::endl;
533 }
534 METAIO_STREAM::cout << " Description: " << (*it).description.c_str() << METAIO_STREAM::endl;
535 if((*it).required)
536 {
537 METAIO_STREAM::cout << " Required: true" << METAIO_STREAM::endl;
538 }
539 else
540 {
541 METAIO_STREAM::cout << " Required: false" << METAIO_STREAM::endl;
542 }
543 METAIO_STREAM::cout << " Number of expected values: " << (*it).fields.size()
544 << METAIO_STREAM::endl;
545
546 auto itField = (*it).fields.begin();
547 while(itField != (*it).fields.end())
548 {
549 METAIO_STREAM::cout << " Field Name: " << (*itField).name.c_str()
550 << METAIO_STREAM::endl;
551 METAIO_STREAM::cout << " Description: " << (*itField).description.c_str()
552 << METAIO_STREAM::endl;
553 METAIO_STREAM::cout << " Type: " << this->TypeToString((*itField).type).c_str()
554 << METAIO_STREAM::endl;
555 METAIO_STREAM::cout << " Value: " << (*itField).value.c_str() << METAIO_STREAM::endl;
556
557 if((*itField).externaldata)
558 {
559 METAIO_STREAM::cout << " External Data: true" << METAIO_STREAM::endl;
560 }
561 else
562 {
563 METAIO_STREAM::cout << " External Data: false" << METAIO_STREAM::endl;
564 }
565
566 if((*itField).required)
567 {
568 METAIO_STREAM::cout << " Required: true" << METAIO_STREAM::endl;
569 }
570 else
571 {
572 METAIO_STREAM::cout << " Required: false" << METAIO_STREAM::endl;
573 }
574 ++itField;
575 }
576 METAIO_STREAM::cout << METAIO_STREAM::endl;
577 ++i;
578 ++it;
579 }
580 if (m_HelpCallBack != nullptr) {
581 m_HelpCallBack();
582 }
583 }
584
585 /** List the current options in xml format */
586 void MetaCommand::ListOptionsXML()
587 {
588 OptionVector::const_iterator it = m_OptionVector.begin();
589 int i=0;
590 while(it != m_OptionVector.end())
591 {
592 METAIO_STREAM::cout << "<option>" << METAIO_STREAM::endl;
593 METAIO_STREAM::cout << "<number>" << i << "</number>" << METAIO_STREAM::endl;
594 METAIO_STREAM::cout << "<name>" << (*it).name.c_str() << "</name>" << METAIO_STREAM::endl;
595 METAIO_STREAM::cout << "<tag>" << (*it).tag.c_str() << "</tag>" << METAIO_STREAM::endl;
596 METAIO_STREAM::cout << "<description>" << (*it).description.c_str()
597 << "</description>" << METAIO_STREAM::endl;
598 METAIO_STREAM::cout << "<required>";
599 if((*it).required)
600 {
601 METAIO_STREAM::cout << "1</required>" << METAIO_STREAM::endl;
602 }
603 else
604 {
605 METAIO_STREAM::cout << "0</required>" << METAIO_STREAM::endl;
606 }
607
608 METAIO_STREAM::cout << "<nvalues>" << (*it).fields.size() << "</nvalues>" << METAIO_STREAM::endl;
609
610 auto itField = (*it).fields.begin();
611 while(itField != (*it).fields.end())
612 {
613 METAIO_STREAM::cout << "<field>" << METAIO_STREAM::endl;
614 METAIO_STREAM::cout << "<name>" << (*itField).name.c_str() << "</name>" << METAIO_STREAM::endl;
615 METAIO_STREAM::cout << "<description>" << (*itField).description.c_str()
616 << "</description>" << METAIO_STREAM::endl;
617 METAIO_STREAM::cout << "<type>" << this->TypeToString((*itField).type).c_str()
618 << "</type>" << METAIO_STREAM::endl;
619 METAIO_STREAM::cout << "<value>" << (*itField).value.c_str() << "</value>"
620 << METAIO_STREAM::endl;
621 METAIO_STREAM::cout << "<external>";
622 if((*itField).externaldata)
623 {
624 METAIO_STREAM::cout << "1</external>" << METAIO_STREAM::endl;
625 }
626 else
627 {
628 METAIO_STREAM::cout << "0</external>" << METAIO_STREAM::endl;
629 }
630 METAIO_STREAM::cout << "<required>";
631 if((*itField).required)
632 {
633 METAIO_STREAM::cout << "1</required>" << METAIO_STREAM::endl;
634 }
635 else
636 {
637 METAIO_STREAM::cout << "0</required>" << METAIO_STREAM::endl;
638 }
639
640
641 METAIO_STREAM::cout << "</field>" << METAIO_STREAM::endl;
642 ++itField;
643 }
644 METAIO_STREAM::cout << "</option>" << METAIO_STREAM::endl;
645 ++i;
646 ++it;
647 }
648 }
649
650 /** Internal small XML parser */
651 METAIO_STL::string MetaCommand::GetXML(const char* buffer,
652 const char* desc,
653 unsigned long pos)
654 {
655 METAIO_STL::string begin = "<";
656 begin += desc;
657 begin += ">";
658 METAIO_STL::string end = "</";
659 end += desc;
660 end += ">";
661
662 METAIO_STL::string buf = buffer;
663
664 size_t posb = buf.find(begin,pos);
665 if(posb == std::string::npos)
666 {
667 return "";
668 }
669 size_t pose = buf.find(end,posb);
670 if(pose == std::string::npos)
671 {
672 return "";
673 }
674
675 return buf.substr(posb+begin.size(),pose-posb-begin.size());
676 }
677
678 /** Given an XML buffer fill in the command line arguments */
679 bool MetaCommand::ParseXML(const char* buffer)
680 {
681 m_OptionVector.clear();
682 METAIO_STL::string buf = this->GetXML(buffer,"option",0);
683 long pos = 0;
684 while (!buf.empty()) {
685 Option option;
686 option.name = this->GetXML(buf.c_str(),"name",0);
687 option.tag = this->GetXML(buf.c_str(),"tag",0);
688 option.description = this->GetXML(buf.c_str(),"description",0);
689 if(atoi(this->GetXML(buf.c_str(),"required",0).c_str()) == 0)
690 {
691 option.required = false;
692 }
693 else
694 {
695 option.required = true;
696 }
697 unsigned int n = atoi(this->GetXML(buf.c_str(),"nvalues",0).c_str());
698
699 // Now check the fields
700 long posF = static_cast<long>(buf.find("<field>"));
701 for(unsigned int i=0;i<n;i++)
702 {
703 METAIO_STL::string f = this->GetXML(buf.c_str(),"field",posF);
704 Field field;
705 field.userDefined = false;
706 field.name = this->GetXML(f.c_str(),"name",0);
707 field.description = this->GetXML(f.c_str(),"description",0);
708 field.value = this->GetXML(f.c_str(),"value",0);
709 field.type = this->StringToType(this->GetXML(f.c_str(),"type",0).c_str());
710 if(atoi(this->GetXML(f.c_str(),"external",0).c_str()) == 0)
711 {
712 field.externaldata = DATA_NONE;
713 }
714 else
715 {
716 if(atoi(this->GetXML(f.c_str(),"external",0).c_str()) == 1)
717 {
718 field.externaldata = DATA_IN;
719 }
720 else
721 {
722 field.externaldata = DATA_OUT;
723 }
724 }
725 if(atoi(this->GetXML(f.c_str(),"required",0).c_str()) == 0)
726 {
727 field.required = false;
728 }
729 else
730 {
731 field.required = true;
732 }
733
734 option.fields.push_back(field);
735 posF += static_cast<long>(f.size())+8;
736 }
737
738 m_OptionVector.push_back(option);
739
740 pos += static_cast<long>(buf.size())+17;
741 buf = this->GetXML(buffer,"option",pos);
742 }
743
744 return true;
745 }
746
747
748 /** List the current options */
749 void MetaCommand::ListOptionsSimplified()
750 {
751 OptionVector::const_iterator it = m_OptionVector.begin();
752 while(it != m_OptionVector.end())
753 {
754 if(!(*it).required)
755 {
756 METAIO_STREAM::cout << " [ ";
757 }
758 else
759 {
760 METAIO_STREAM::cout << " ";
761 }
762 if (!(*it).tag.empty()) {
763 METAIO_STREAM::cout << "-" << (*it).tag.c_str() << " ";
764 }
765 auto itField = (*it).fields.begin();
766 while (itField != (*it).fields.end()) {
767 if ((*itField).type != FLAG) // only display the type if it's not a FLAG
768 {
769 if ((*itField).required) {
770 METAIO_STREAM::cout << "<";
771 }
772 else
773 {
774 METAIO_STREAM::cout << "[";
775 }
776
777 METAIO_STREAM::cout << (*itField).name.c_str();
778
779 if((*itField).required)
780 {
781 METAIO_STREAM::cout << "> ";
782 }
783 else
784 {
785 METAIO_STREAM::cout << "] ";
786 }
787
788 }
789 ++itField;
790 }
791
792 if(!(*it).required)
793 {
794 METAIO_STREAM::cout << "]";
795 }
796 METAIO_STREAM::cout << METAIO_STREAM::endl;
797
798 if (!(*it).description.empty()) {
799 METAIO_STREAM::cout << " = " << (*it).description.c_str();
800 METAIO_STREAM::cout << METAIO_STREAM::endl;
801 itField = (*it).fields.begin();
802 while(itField != (*it).fields.end())
803 {
804 if (!(*itField).description.empty() || !(*itField).value.empty()) {
805 METAIO_STREAM::cout << " With: " << (*itField).name.c_str();
806 if (!(*itField).description.empty()) {
807 METAIO_STREAM::cout << " = " << (*itField).description.c_str();
808 }
809 if (!(*itField).value.empty()) {
810 METAIO_STREAM::cout << " (Default = " << (*itField).value << ")";
811 }
812 METAIO_STREAM::cout << METAIO_STREAM::endl;
813 }
814 ++itField;
815 }
816 }
817
818 METAIO_STREAM::cout << METAIO_STREAM::endl;
819 ++it;
820 }
821
822 if (m_HelpCallBack != nullptr) {
823 m_HelpCallBack();
824 }
825 }
826
827 /** Get the option by "-"+tag */
828 bool
829 MetaCommand::OptionExistsByMinusTag(METAIO_STL::string minusTag)
830 {
831 OptionVector::const_iterator it = m_OptionVector.begin();
832 while(it != m_OptionVector.end())
833 {
834 METAIO_STL::string tagToSearch = "-";
835 tagToSearch += (*it).tag;
836 if(tagToSearch == minusTag)
837 {
838 return true;
839 }
840 ++it;
841 }
842
843 return false;
844
845 }
846
847
848 /** Get the option by "-"+tag */
849 MetaCommand::Option *
850 MetaCommand::GetOptionByMinusTag(METAIO_STL::string minusTag)
851 {
852 auto it = m_OptionVector.begin();
853 while(it != m_OptionVector.end())
854 {
855 METAIO_STL::string tagToSearch = "-";
856 tagToSearch += (*it).tag;
857 if(tagToSearch == minusTag)
858 {
859 return &(*it);
860 }
861 ++it;
862 }
863 return nullptr;
864 }
865
866 /** Get the option by tag */
867 MetaCommand::Option *
868 MetaCommand::GetOptionByTag(METAIO_STL::string minusTag)
869 {
870 auto it = m_OptionVector.begin();
871 while(it != m_OptionVector.end())
872 {
873 if((*it).tag == minusTag)
874 {
875 return &(*it);
876 }
877 ++it;
878 }
879 return nullptr;
880 }
881
882 /** Return the option id. i.e the position in the vector */
883 long
884 MetaCommand::GetOptionId(Option* option)
885 {
886 auto it = m_OptionVector.begin();
887 unsigned long i = 0;
888 while(it != m_OptionVector.end())
889 {
890 if(&(*it) == option)
891 {
892 return i;
893 }
894 ++i;
895 ++it;
896 }
897 return -1;
898 }
899
900 /** Export the current command line arguments to a Grid Application
901 * Description file */
902 bool MetaCommand::ExportGAD(bool dynamic)
903 {
904 METAIO_STREAM::cout << "Exporting GAD file...";
905
906 OptionVector options = m_OptionVector;
907 if(dynamic)
908 {
909 options = m_ParsedOptionVector;
910 }
911
912 if (m_Name.empty()) {
913 METAIO_STREAM::cout << "Set the name of the application using SetName()"
914 << METAIO_STREAM::endl;
915 return false;
916 }
917
918 METAIO_STL::string filename = m_Name;
919 filename += ".gad.xml";
920
921 METAIO_STREAM::ofstream file;
922 file.open(filename.c_str(), METAIO_STREAM::ios::binary | METAIO_STREAM::ios::out);
923 if(!file.is_open())
924 {
925 METAIO_STREAM::cout << "Cannot open file for writing: " << filename.c_str() << METAIO_STREAM::endl;
926 return false;
927 }
928
929 file << R"(<?xml version="1.0" encoding="UTF-8" ?>)" << METAIO_STREAM::endl;
930 file << "<gridApplication" << METAIO_STREAM::endl;
931 file << "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
932 << METAIO_STREAM::endl;
933 file << "xsi:noNamespaceSchemaLocation=\"grid-application-description.xsd\""
934 << METAIO_STREAM::endl;
935 file << "name=\"" << m_Name.c_str() << "\"" << METAIO_STREAM::endl;
936 file << "description=\"" << m_Description.c_str() << "\">"
937 << METAIO_STREAM::endl;
938 file << R"(<applicationComponent name="Client" remoteExecution="true">)"
939 << METAIO_STREAM::endl;
940 file << "<componentActionList>" << METAIO_STREAM::endl;
941 file << METAIO_STREAM::endl;
942
943 unsigned int order = 1;
944 // Write out the input data to be transfered
945 OptionVector::const_iterator it = options.begin();
946 while (it != options.end()) {
947 auto itFields = (*it).fields.begin();
948 while (itFields != (*it).fields.end()) {
949 if ((*itFields).externaldata == DATA_IN) {
950 file << R"( <componentAction type="DataRelocation" order=")" << order
951 << "\">" << METAIO_STREAM::endl;
952 file << R"( <parameter name="Name" value=")" << (*itFields).name
953 << "\"/>" << METAIO_STREAM::endl;
954 file << R"( <parameter name="Host" value="hostname"/>)"
955 << METAIO_STREAM::endl;
956 file << R"( <parameter name="Description" value=")"
957 << (*itFields).description << "\"/>" << METAIO_STREAM::endl;
958 file << R"( <parameter name="Direction" value="In"/>)"
959 << METAIO_STREAM::endl;
960 file << R"( <parameter name="Protocol" value="gsiftp"/>)"
961 << METAIO_STREAM::endl;
962 file << R"( <parameter name="SourceDataPath" value=")"
963 << (*itFields).value << "\"/>" << METAIO_STREAM::endl;
964
965 METAIO_STL::string datapath = (*itFields).value;
966 size_t slash = datapath.find_last_of("/");
967 if (slash > 0) {
968 datapath = datapath.substr(slash + 1, datapath.size() - slash - 1);
969 }
970 slash = datapath.find_last_of("\\");
971 if(slash>0)
972 {
973 datapath = datapath.substr(slash+1,datapath.size()-slash-1);
974 }
975 file << R"( <parameter name="DestDataPath" value=")"
976 << datapath.c_str() << "\"/>" << METAIO_STREAM::endl;
977 file << " </componentAction>" << METAIO_STREAM::endl;
978 file << METAIO_STREAM::endl;
979 ++order;
980 }
981 ++itFields;
982 }
983 ++it;
984 }
985
986 file << R"( <componentAction type="JobSubmission" order=")" << order
987 << "\">" << METAIO_STREAM::endl;
988 file << R"( <parameter name="Executable" value=")"
989 << m_ExecutableName.c_str() << "\"/>" << METAIO_STREAM::endl;
990 file << R"( <parameter name="Arguments" value=")";
991 // Write out the command line arguments
992 it = options.begin();
993 while (it != options.end()) {
994 if (it != options.begin()) {
995 file << " ";
996 }
997 file << "{" << (*it).name.c_str() << "}";
998 ++it;
999 }
1000 file << "\"/>" << METAIO_STREAM::endl;
1001 // Write out the arguments that are not data
1002 it = options.begin();
1003 while(it != options.end())
1004 {
1005 // Find if this is a non data field
1006 bool isData = false;
1007 auto itFields = (*it).fields.begin();
1008 while(itFields != (*it).fields.end())
1009 {
1010 if((*itFields).externaldata != DATA_NONE)
1011 {
1012 isData = true;
1013 break;
1014 }
1015 ++itFields;
1016 }
1017
1018 if(isData)
1019 {
1020 ++it;
1021 continue;
1022 }
1023
1024 file << " <group name=\"" << (*it).name.c_str();
1025 file << "\" syntax=\"";
1026
1027 if (!(*it).tag.empty()) {
1028 file << "-" << (*it).tag.c_str() << " ";
1029 }
1030
1031 itFields = (*it).fields.begin();
1032 while(itFields != (*it).fields.end())
1033 {
1034 if(itFields != (*it).fields.begin())
1035 {
1036 file << " ";
1037 }
1038 file << "{" << (*it).name << (*itFields).name << "}";
1039 ++itFields;
1040 }
1041 file << "\"";
1042
1043 if(!(*it).required)
1044 {
1045 file << " optional=\"true\"";
1046
1047 // Add if the option was selected
1048 if((*it).userDefined)
1049 {
1050 file << " selected=\"true\"";
1051 }
1052 else
1053 {
1054 file << " selected=\"false\"";
1055 }
1056 }
1057
1058 file << ">" << METAIO_STREAM::endl;
1059
1060 // Now writes the value of the arguments
1061 itFields = (*it).fields.begin();
1062 while(itFields != (*it).fields.end())
1063 {
1064 file << " <argument name=\"" << (*it).name << (*itFields).name;
1065 file << "\" value=\"" << (*itFields).value;
1066 file << "\" type=\"" << this->TypeToString((*itFields).type).c_str();
1067 file << "\"";
1068
1069 if (!(*itFields).rangeMin.empty()) {
1070 file << " rangeMin=\"" << (*itFields).rangeMin << "\"";
1071 }
1072
1073 if (!(*itFields).rangeMax.empty()) {
1074 file << " rangeMax=\"" << (*itFields).rangeMax << "\"";
1075 }
1076 file << "/>" << METAIO_STREAM::endl;
1077 ++itFields;
1078 }
1079 file << " </group>" << METAIO_STREAM::endl;
1080 ++it;
1081 }
1082 file << " </componentAction>" << METAIO_STREAM::endl;
1083 ++order;
1084 file << METAIO_STREAM::endl;
1085 // Write out the input data to be transfered
1086 it = options.begin();
1087 while(it != options.end())
1088 {
1089 auto itFields = (*it).fields.begin();
1090 while(itFields != (*it).fields.end())
1091 {
1092 if((*itFields).externaldata == DATA_OUT)
1093 {
1094 file << R"( <componentAction type="DataRelocation" order=")" << order
1095 << "\">" << METAIO_STREAM::endl;
1096 file << R"( <parameter name="Name" Value=")" << (*itFields).name
1097 << "\"/>" << METAIO_STREAM::endl;
1098 file << R"( <parameter name="Host" Value="hostname"/>)"
1099 << METAIO_STREAM::endl;
1100 file << R"( <parameter name="Description" value=")"
1101 << (*itFields).description << "\"/>" << METAIO_STREAM::endl;
1102 file << R"( <parameter name="Direction" value="Out"/>)"
1103 << METAIO_STREAM::endl;
1104 file << R"( <parameter name="Protocol" value="gsiftp"/>)"
1105 << METAIO_STREAM::endl;
1106 METAIO_STL::string datapath = (*itFields).value;
1107 size_t slash = datapath.find_last_of("/");
1108 if(slash>0)
1109 {
1110 datapath = datapath.substr(slash+1,datapath.size()-slash-1);
1111 }
1112 slash = datapath.find_last_of("\\");
1113 if(slash>0)
1114 {
1115 datapath = datapath.substr(slash+1,datapath.size()-slash-1);
1116 }
1117 file << R"( <parameter name="SourceDataPath" value=")"
1118 << datapath.c_str() << "\"/>" << METAIO_STREAM::endl;
1119 file << R"( <parameter name="DestDataPath" value=")"
1120 << (*itFields).value << "\"/>" << METAIO_STREAM::endl;
1121 file << " </componentAction>" << METAIO_STREAM::endl;
1122 file << METAIO_STREAM::endl;
1123 ++order;
1124 }
1125 ++itFields;
1126 }
1127 ++it;
1128 }
1129 file << " </componentActionList>" << METAIO_STREAM::endl;
1130 file << " </applicationComponent>" << METAIO_STREAM::endl;
1131 file << "</gridApplication>" << METAIO_STREAM::endl;
1132
1133 file.close();
1134
1135 METAIO_STREAM::cout << "done" << METAIO_STREAM::endl;
1136 return true;
1137 }
1138
1139
1140 /** Parse the command line */
1141 bool MetaCommand::Parse(int argc, const char* argv[])
1142 {
1143 m_ExecutableName = argv[0];
1144
1145 size_t slash = m_ExecutableName.find_last_of("/");
1146 if(slash>0)
1147 {
1148 m_ExecutableName = m_ExecutableName.substr(slash+1,m_ExecutableName.size()-slash-1);
1149 }
1150 slash = m_ExecutableName.find_last_of("\\");
1151 if(slash>0)
1152 {
1153 m_ExecutableName = m_ExecutableName.substr(slash+1,m_ExecutableName.size()-slash-1);
1154 }
1155
1156 // List the options if using -V
1157 if((argc == 2 && !strcmp(argv[1],"-V"))
1158 || (argc == 2 && !strcmp(argv[1],"-H")))
1159 {
1160 METAIO_STREAM::cout << "Usage : " << argv[0] << METAIO_STREAM::endl;
1161 this->ListOptions();
1162 return false;
1163 }
1164 // List the options if using -v
1165 else if((argc == 2 && !strcmp(argv[1],"-v"))
1166 || (argc == 2 && !strcmp(argv[1],"-h")))
1167 {
1168 METAIO_STREAM::cout << "Usage : " << argv[0] << METAIO_STREAM::endl;
1169 this->ListOptionsSimplified();
1170 return false;
1171 }
1172 else if(argc == 2 && !strcmp(argv[1],"-vxml"))
1173 {
1174 this->ListOptionsXML();
1175 return false;
1176 }
1177 else if(argc == 2 && !strcmp(argv[1],"-version"))
1178 {
1179 METAIO_STREAM::cout << "Version: " << m_Version.c_str() << METAIO_STREAM::endl;
1180 return false;
1181 }
1182 else if(argc == 2 && !strcmp(argv[1],"-date"))
1183 {
1184 METAIO_STREAM::cout << "Date: " << m_Date.c_str() << METAIO_STREAM::endl;
1185 return false;
1186 }
1187 else if(argc == 2 && !strcmp(argv[1],"-exportGAD"))
1188 {
1189 this->ExportGAD();
1190 return false;
1191 }
1192
1193 // Fill in the results
1194 m_ParsedOptionVector.clear();
1195 bool inArgument = false;
1196 METAIO_STL::string tag = "";
1197
1198 long currentField = 0; // current field position
1199 long currentOption = 0; // id of the option to fill
1200 size_t valuesRemaining=0;
1201 bool isComplete = false; // check if the option should be parse until the next tag is found
1202 METAIO_STL::string completeString = "";
1203
1204 bool exportGAD = false;
1205
1206 for(unsigned int i=1; i<(unsigned int)argc; ++i)
1207 {
1208 // If we have the tag -export-gad
1209 if(!strcmp(argv[i],"-exportGAD"))
1210 {
1211 exportGAD = true;
1212 continue;
1213 }
1214
1215 // If this is a tag
1216 if(argv[i][0] == '-' && (atof(argv[i])==0) && (strlen(argv[i])>1))
1217 {
1218 // if we have a tag before the expected values we throw an exception
1219 if(valuesRemaining != 0)
1220 {
1221 if(!isComplete)
1222 {
1223 METAIO_STREAM::cout << "Found tag before end of value list!" << METAIO_STREAM::endl;
1224 return false;
1225 }
1226 else
1227 {
1228 m_OptionVector[currentOption].fields[0].value = completeString;
1229 m_OptionVector[currentOption].fields[0].userDefined = true;
1230 m_OptionVector[currentOption].userDefined = true;
1231 m_ParsedOptionVector.push_back(m_OptionVector[currentOption]);
1232 }
1233 }
1234 inArgument = false;
1235 // New tag so we add the previous values to the tag
1236 tag = argv[i];
1237
1238 // Check if the tag is in the list
1239 if(this->OptionExistsByMinusTag(tag))
1240 {
1241 inArgument = true;
1242 valuesRemaining = this->GetOptionByMinusTag(tag)->fields.size();
1243 currentOption = this->GetOptionId(this->GetOptionByMinusTag(tag));
1244
1245 if(currentOption < 0)
1246 {
1247 METAIO_STREAM::cout << "Error processing tag " << tag.c_str()
1248 << ". Tag exists but cannot find its Id."
1249 << METAIO_STREAM::endl;
1250 }
1251 else
1252 {
1253 isComplete = m_OptionVector[currentOption].complete;
1254
1255 if(m_OptionVector[currentOption].fields[0].type == FLAG)
1256 {
1257 // the tag exists by default
1258 m_OptionVector[currentOption].fields[0].value = "true";
1259 valuesRemaining = 0;
1260 inArgument = false;
1261 }
1262 else if(m_OptionVector[currentOption].fields[0].type == LIST)
1263 {
1264 inArgument = true;
1265 unsigned int valuesInList = (int)atoi(argv[++i]);
1266 m_OptionVector[currentOption].fields[0].value = argv[i];
1267 valuesRemaining += valuesInList-1;
1268 char optName[255];
1269 for(unsigned int j=0; j<valuesInList; j++)
1270 {
1271 snprintf(optName, sizeof(optName), "%03u", j);
1272 this->AddOptionField( m_OptionVector[currentOption].name,
1273 optName, STRING );
1274 }
1275 }
1276 }
1277 }
1278 else if(m_Verbose)
1279 {
1280 METAIO_STREAM::cout << "The tag " << tag.c_str()
1281 << " is not a valid argument : skipping this tag"
1282 << METAIO_STREAM::endl;
1283 }
1284 if(inArgument)
1285 {
1286 ++i;
1287 }
1288 }
1289 else if(!inArgument) // If this is a field
1290 {
1291 // Look for the field to add
1292 auto it = m_OptionVector.begin();
1293 long pos = 0;
1294 bool found = false;
1295 while(it != m_OptionVector.end())
1296 {
1297 if ((pos >= currentField) && ((*it).tag.empty())) {
1298 currentOption = pos;
1299 valuesRemaining = static_cast<unsigned int>((*it).fields.size());
1300 found = true;
1301 break;
1302 }
1303 ++pos;
1304 ++it;
1305 }
1306
1307 if(!found && m_Verbose)
1308 {
1309 METAIO_STREAM::cout << "Too many arguments specified in your command line! "
1310 << "Skipping extra argument: " << argv[i] << METAIO_STREAM::endl;
1311 }
1312
1313 inArgument=true;
1314 currentField=currentOption+1;
1315 }
1316
1317 // We collect the values
1318 if(isComplete)
1319 {
1320 if (completeString.empty()) {
1321 completeString = argv[i];
1322 } else {
1323 completeString += " ";
1324 completeString += argv[i];
1325 }
1326 }
1327 else if(inArgument && i<(unsigned int)argc && valuesRemaining>0)
1328 {
1329 if(currentOption >=0 && currentOption < (int)(m_OptionVector.size()))
1330 {
1331 auto s = static_cast<unsigned long>(
1332 m_OptionVector[currentOption].fields.size());
1333 m_OptionVector[currentOption].fields[s-valuesRemaining].value = argv[i];
1334 m_OptionVector[currentOption].fields[s-valuesRemaining].userDefined =
1335 true;
1336 }
1337 --valuesRemaining;
1338 }
1339
1340 if(valuesRemaining == 0)
1341 {
1342 inArgument = false;
1343 m_OptionVector[currentOption].userDefined = true;
1344 m_ParsedOptionVector.push_back(m_OptionVector[currentOption]);
1345 }
1346 }
1347
1348 if(valuesRemaining > 0)
1349 {
1350 METAIO_STREAM::cout << "Not enough parameters for "
1351 << m_OptionVector[currentOption].name << METAIO_STREAM::endl;
1352 METAIO_STREAM::cout << "Command: " << argv[0] << METAIO_STREAM::endl;
1353 METAIO_STREAM::cout << "Options: " << METAIO_STREAM::endl
1354 << " -v or -h for help listed in short format" << METAIO_STREAM::endl
1355 << " -V or -H for help listed in long format" << METAIO_STREAM::endl
1356 << " -vxml for help listed in xml format" << METAIO_STREAM::endl
1357 << " -export-gad to export Grid Application"
1358 << "Description file format" << METAIO_STREAM::endl;
1359
1360 return false;
1361 }
1362
1363 // Check if the options with required arguments are defined
1364 auto it = m_OptionVector.begin();
1365 bool requiredAndNotDefined = false;
1366 while (it != m_OptionVector.end()) {
1367 if ((*it).required) {
1368 // First check if the option is actually defined
1369 if (!(*it).userDefined) {
1370 METAIO_STREAM::cout << "Option " << (*it).name
1371 << " is required but not defined"
1372 << METAIO_STREAM::endl;
1373 requiredAndNotDefined = true;
1374 ++it;
1375 continue;
1376 }
1377
1378 // Check if the values are defined
1379 METAIO_STL::vector<Field>::const_iterator itFields = (*it).fields.begin();
1380 bool defined = true;
1381 while(itFields != (*it).fields.end())
1382 {
1383 if ((*itFields).value.empty()) {
1384 defined = false;
1385 }
1386 ++itFields;
1387 }
1388
1389 if(!defined)
1390 {
1391 if (!(*it).tag.empty()) {
1392 METAIO_STREAM::cout << "Field " << (*it).tag.c_str()
1393 << " is required but not defined" << METAIO_STREAM::endl;
1394 } else {
1395 METAIO_STREAM::cout << "Field " << (*it).name.c_str()
1396 << " is required but not defined" << METAIO_STREAM::endl;
1397 }
1398 requiredAndNotDefined = true;
1399 }
1400 }
1401 ++it;
1402 }
1403
1404 if(requiredAndNotDefined)
1405 {
1406 METAIO_STREAM::cout << "Command: " << argv[0] << METAIO_STREAM::endl
1407 << "Options: " << METAIO_STREAM::endl
1408 << " -v or -h for help listed in short format" << METAIO_STREAM::endl
1409 << " -V or -H for help listed in long format" << METAIO_STREAM::endl
1410 << " -vxml for help listed in xml format" << METAIO_STREAM::endl
1411 << " -export-gad to export Grid Application"
1412 << "Description file format" << METAIO_STREAM::endl;
1413 return false;
1414 }
1415
1416 // Check if the values are in range (if the range is defined)
1417 auto itParsed = m_ParsedOptionVector.begin();
1418 bool valueInRange = true;
1419 while (itParsed != m_ParsedOptionVector.end()) {
1420 METAIO_STL::vector<Field>::const_iterator itFields =
1421 (*itParsed).fields.begin();
1422 while (itFields != (*itParsed).fields.end()) {
1423 // Check only if this is a number
1424 if (((*itFields).type == INT || (*itFields).type == FLOAT ||
1425 (*itFields).type == CHAR) &&
1426 (!(*itFields).value.empty())) {
1427 // Check the range min
1428 if (((!(*itFields).rangeMin.empty()) &&
1429 (atof((*itFields).rangeMin.c_str()) >
1430 atof((*itFields).value.c_str()))) ||
1431 ((!(*itFields).rangeMax.empty()) &&
1432 (atof((*itFields).rangeMax.c_str()) <
1433 atof((*itFields).value.c_str())))) {
1434 METAIO_STREAM::cout
1435 << (*itParsed).name << "." << (*itFields).name << " : Value ("
1436 << (*itFields).value << ") "
1437 << "is not in the range [" << (*itFields).rangeMin << ","
1438 << (*itFields).rangeMax << "]" << METAIO_STREAM::endl;
1439 valueInRange = false;
1440 }
1441 }
1442 ++itFields;
1443 }
1444 ++itParsed;
1445 }
1446
1447 if(!valueInRange)
1448 {
1449 return false;
1450 }
1451
1452 // If everything is ok
1453 if(exportGAD)
1454 {
1455 this->ExportGAD(true);
1456 return false; // prevent from running the application
1457 }
1458
1459 return true;
1460 }
1461
1462 /** Return the string representation of a type */
1463 METAIO_STL::string MetaCommand::TypeToString(TypeEnumType type)
1464 {
1465 switch(type)
1466 {
1467 case INT:
1468 return "int";
1469 case FLOAT:
1470 return "float";
1471 case STRING:
1472 return "string";
1473 case LIST:
1474 return "list";
1475 case FLAG:
1476 return "flag";
1477 case BOOL:
1478 return "boolean";
1479 default:
1480 return "not defined";
1481 }
1482 }
1483
1484
1485
1486 /** Return a type given a string */
1487 MetaCommand::TypeEnumType MetaCommand::StringToType(const char* type)
1488 {
1489 if(!strcmp(type,"int"))
1490 {
1491 return INT;
1492 }
1493 if(!strcmp(type,"float"))
1494 {
1495 return FLOAT;
1496 }
1497 if(!strcmp(type,"string"))
1498 {
1499 return STRING;
1500 }
1501 if(!strcmp(type,"list"))
1502 {
1503 return LIST;
1504 }
1505 if(!strcmp(type,"flag"))
1506 {
1507 return FLAG;
1508 }
1509
1510 return INT; // by default
1511
1512 }
1513
1514 #if (METAIO_USE_NAMESPACE)
1515 };
1516 #endif
1517