AFLOW
 
Loading...
Searching...
No Matches
aurostd_argv.cpp
Go to the documentation of this file.
1// ***************************************************************************
2// * *
3// * Aflow STEFANO CURTAROLO - Duke University 2003-2024 *
4// * *
5// ***************************************************************************
6// Stefano Curtarolo
7
8#ifndef _AUROSTD_ARGV_CPP_
9#define _AUROSTD_ARGV_CPP_
10
11#include "aurostd_argv.h"
12
13#include <cstddef>
14#include <cstdlib>
15#include <iostream>
16#include <string>
17#include <vector>
18
19#include "aurostd.h"
21#include "aurostd_defs.h"
22#include "aurostd_xerror.h"
23#include "aurostd_xscalar.h"
24#include "aurostd_xvector.h"
25
26using std::cerr;
27using std::endl;
28using std::string;
29using std::vector;
30
31#define VERBOSE_ARGV false // DX20200907
32
33namespace aurostd { // namespace aurostd
34 string attach(const string& s1) {
35 return s1;
36 }
37 string attach(const string& s1, const string& s2) {
38 return s1 + "|" + s2;
39 }
40 string attach(const string& s1, const string& s2, const string& s3) {
41 return s1 + "|" + s2 + "|" + s3;
42 }
43 string attach(const string& s1, const string& s2, const string& s3, const string& s4) {
44 return s1 + "|" + s2 + "|" + s3 + "|" + s4;
45 }
46 string attach(const string& s1, const string& s2, const string& s3, const string& s4, const string& s5) {
47 return s1 + "|" + s2 + "|" + s3 + "|" + s4 + "|" + s5;
48 }
49} // namespace aurostd
50
51// ***************************************************************************
52// Function get_arguments_from_input
53// ***************************************************************************
54// this function creates the argv vector as vector<string> easier to handle than the char **argv
55namespace aurostd { // namespace aurostd
56 vector<string> get_arguments_from_input(int _argc, char** _argv) {
57 const bool VERBOSE = (false || VERBOSE_ARGV); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG
58 vector<string> out_argv;
59 for (int i = 0; i < _argc; i++) {
60 string argi(_argv[i]);
61 if (argi == "-np" || argi == "-npmax") {
62 argi = string("-") + argi;
63 }
64 if (argi == "-f" || argi == "-F") {
65 argi = string("-") + argi;
66 }
67 if (argi == "-d" || argi == "-D") {
68 argi = string("-") + argi;
69 }
70 if (argi.size() >= 2) {
71 if (argi[0] == '-' && argi[1] == 'n') {
72 argi = string("-") + argi; // for -np
73 }
74 }
75 // if(argi.size()>=2) if(argi[0]=='-' && argi[1]=='m') argi=string("-")+argi; // for -machine
76 if (argi.size() >= 2) {
77 if (argi[0] == '-' && argi[1] == 'f') {
78 argi = string("-") + argi; // for -f
79 }
80 }
81 if (argi.size() >= 2) {
82 if (argi[0] == '-' && argi[1] == 'F') {
83 argi = string("-") + argi; // for -F
84 }
85 }
86 if (argi.size() >= 2) {
87 if (argi[0] == '-' && argi[1] == 'd') {
88 argi = string("-") + argi; // for -d
89 }
90 }
91 if (argi.size() >= 2) {
92 if (argi[0] == '-' && argi[1] == 'D') {
93 argi = string("-") + argi; // for -D
94 }
95 }
96
97 if (argi == "--machine") {
98 argi += string("="); // forcing "=" after machine !
99 }
100 if (argi == "--machine_name") {
101 argi += string("="); // forcing "=" after machine_name ! //HE20220309
102 }
103 if (argi == "--aflowlib") {
104 argi += string("="); // forcing "=" after aflowlib !
105 }
106 if (argi == "--np") {
107 argi += string("="); // forcing "=" after np !
108 }
109 if (argi.at(argi.size() - 1) == '=' && i < _argc - 1) {
110 argi += string(_argv[i + 1]);
111 i++;
112 } // fixing space after "= "
113 out_argv.push_back(argi);
114 }
115 if (VERBOSE) {
116 for (size_t i = 0; i < out_argv.size(); i++) {
117 cerr << "out_argv.at(" << i << ")=" << out_argv[i] << endl;
118 }
119 }
120 return out_argv;
121 }
122} // namespace aurostd
123
124// ***************************************************************************
125// Function args2flag without/with commands
126// ***************************************************************************
127namespace aurostd { // namespace aurostd
128 bool args2flag(const vector<string>& argv, const string& s0) {
129 const bool VERBOSE = (false || VERBOSE_ARGV); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG
130 const string s = aurostd::RemoveWhiteSpaces(s0);
131 vector<string> tokens;
132 aurostd::string2tokens(s, tokens, "|");
133 if (VERBOSE) {
134 for (size_t j = 0; j < tokens.size(); j++) {
135 cerr << "[" << tokens[j] << "]" << endl;
136 }
137 }
138 for (size_t j = 0; j < tokens.size(); j++) {
139 for (size_t i = 0; i < argv.size(); i++) {
140 if (argv[i] == tokens[j]) {
141 return true;
142 }
143 }
144 }
145 return false;
146 }
147
148 bool args2flag(const vector<string>& argv, std::vector<string>& cmds, const string& s0) {
149 const bool VERBOSE = (false || VERBOSE_ARGV); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG
150 const string s = aurostd::RemoveWhiteSpaces(s0);
151 vector<string> tokens;
152 aurostd::string2tokens(s, tokens, "|");
153 if (VERBOSE) {
154 for (size_t j = 0; j < tokens.size(); j++) {
155 cerr << "[" << tokens[j] << "]" << endl;
156 }
157 }
158 for (size_t j = 0; j < tokens.size(); j++) {
159 cmds.push_back(tokens[j]);
160 }
161 for (size_t i = 0; i < argv.size(); i++) {
162 for (size_t j = 0; j < tokens.size(); j++) {
163 if (argv[i] == tokens[j]) {
164 return true;
165 }
166 }
167 }
168 return false;
169 }
170} // namespace aurostd
171
172// ***************************************************************************
173// args2utype of utype type
174// ***************************************************************************
175namespace aurostd {
176 // namespace aurostd
177 template <class utype> utype args2utype(const vector<string>& argv, const string& s0, utype def_out) {
178 const bool VERBOSE = (false || VERBOSE_ARGV); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG
179 const string s = aurostd::RemoveWhiteSpaces(s0);
180 vector<string> tokens;
181 aurostd::string2tokens(s, tokens, "|");
182 if (VERBOSE) {
183 for (size_t j = 0; j < tokens.size(); j++) {
184 cerr << "[" << tokens[j] << "]" << endl;
185 }
186 }
187 utype out = def_out;
188 for (size_t i = 1; i < argv.size() - 1; i++) {
189 for (size_t j = 0; j < tokens.size(); j++) {
190 if (argv[i] == tokens[j]) {
191 if (_isfloat(out)) {
192 out = (utype) atof(argv.at(i + 1).c_str());
193 } else {
194 out = (utype) atoi(argv.at(i + 1).c_str());
195 }
196 } // OLD
197 }
198 }
199 return out;
200 }
201#define AST_TEMPLATE(atype) template atype args2utype(const vector<string>&, const string&, atype);
203#undef AST_TEMPLATE
204} // namespace aurostd
205
206// ***************************************************************************
207// Function get xvector from input
208// ***************************************************************************
209namespace aurostd { // namespace aurostd
210 template <class utype> xvector<utype> args2xvectorutype(const vector<string>& argv, const string& s0, const xvector<utype>& def_out) {
211 const bool VERBOSE = (false || VERBOSE_ARGV); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG
212 const string s = aurostd::RemoveWhiteSpaces(s0);
213 vector<string> tokens;
214 aurostd::string2tokens(s, tokens, "|");
215 if (VERBOSE) {
216 for (size_t j = 0; j < tokens.size(); j++) {
217 cerr << "[" << tokens[j] << "]" << endl;
218 }
219 }
220 xvector<utype> out(def_out.lrows, def_out.urows);
221 out = def_out;
222 for (size_t i = 0; i < argv.size(); i++) {
223 if (i + out.rows < argv.size()) {
224 for (size_t j = 0; j < tokens.size(); j++) {
225 if (argv[i] == tokens[j]) {
226 for (int k = 0; k < out.rows; k++) {
227 if (_isfloat(out(1))) {
228 out(k + out.lrows) = (utype) atof(argv.at(i + k + 1).c_str());
229 } else {
230 out(k + out.lrows) = (utype) atoi(argv.at(i + k + 1).c_str());
231 }
232 }
233 }
234 }
235 }
236 }
237 return out;
238 }
239#define AST_TEMPLATE(atype) template xvector<atype> args2xvectorutype(const vector<string>&, const string&, const xvector<atype>&);
241#undef AST_TEMPLATE
242
243 template <class utype> xvector<utype> args2xvectorutype(const vector<string>& argv, const string& s0, int dim) {
244 const bool VERBOSE = (false || VERBOSE_ARGV); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG
245 const string s = aurostd::RemoveWhiteSpaces(s0);
246 vector<string> tokens;
247 aurostd::string2tokens(s, tokens, "|");
248 if (VERBOSE) {
249 for (size_t j = 0; j < tokens.size(); j++) {
250 cerr << "[" << tokens[j] << "]" << endl;
251 }
252 }
253 const xvector<utype> out(1, dim);
254 for (size_t i = 0; i < argv.size(); i++) {
255 if (i + out.rows < argv.size()) {
256 for (size_t j = 0; j < tokens.size(); j++) {
257 if (argv[i] == tokens[j]) {
258 for (int k = 0; k < out.rows; k++) {
259 if (_isfloat(out(1))) {
260 out(k + out.lrows) = (utype) atof(argv.at(i + k + 1).c_str());
261 } else {
262 out(k + out.lrows) = (utype) atoi(argv.at(i + k + 1).c_str());
263 }
264 }
265 }
266 }
267 }
268 }
269 return out; // something phony to keep t used
270 }
271#define AST_TEMPLATE(atype) template xvector<atype> args2xvectorutype(const vector<string>&, const string&, int);
273#undef AST_TEMPLATE
274} // namespace aurostd
275// ***************************************************************************
276// Function get vector/deque from input
277// ***************************************************************************
278namespace aurostd {
279 // namespace aurostd
280 template <class utype> vector<utype> args2vectorutype(const vector<string>& argv, const string& s0) {
281 const bool VERBOSE = (false || VERBOSE_ARGV); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG
282 const string s = aurostd::RemoveWhiteSpaces(s0);
283 vector<string> tokens;
284 aurostd::string2tokens(s, tokens, "|");
285 if (VERBOSE) {
286 for (size_t j = 0; j < tokens.size(); j++) {
287 cerr << "[" << tokens[j] << "]" << endl;
288 }
289 }
290 vector<utype> out;
291 for (size_t i = 0; i < argv.size(); i++) {
292 for (size_t j = 0; j < tokens.size(); j++) {
293 if (argv[i] == tokens[j]) {
294 for (size_t k = 0; k < argv.size(); k++) {
295 if (_isfloat(out.at(0))) {
296 out.push_back((utype) atof(argv.at(i + k + 1).c_str()));
297 } else {
298 out.push_back((utype) atoi(argv.at(i + k + 1).c_str()));
299 }
300 }
301 }
302 }
303 }
304 return out;
305 }
306
307 template <class utype> deque<utype> args2dequeutype(const deque<string>& argv, const string& s0) {
308 const bool VERBOSE = (false || VERBOSE_ARGV); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG
309 const string s = aurostd::RemoveWhiteSpaces(s0);
310 deque<string> tokens;
311 aurostd::string2tokens(s, tokens, "|");
312 if (VERBOSE) {
313 for (size_t j = 0; j < tokens.size(); j++) {
314 cerr << "[" << tokens[j] << "]" << endl;
315 }
316 }
317 deque<utype> out;
318 for (size_t i = 0; i < argv.size(); i++) {
319 for (size_t j = 0; j < tokens.size(); j++) {
320 if (argv[i] == tokens[j]) {
321 for (size_t k = 0; k < argv.size(); k++) {
322 if (_isfloat(out.at(0))) {
323 out.push_back((utype) atof(argv.at(i + k + 1).c_str()));
324 } else {
325 out.push_back((utype) atoi(argv.at(i + k + 1).c_str()));
326 }
327 }
328 }
329 }
330 }
331 return out;
332 }
333} // namespace aurostd
334
335// ***************************************************************************
336// Functions args2string functions
337// ***************************************************************************
338namespace aurostd { // namespace aurostd
339 string args2string(const vector<string>& argv, const string& s0, const string& s_def) {
340 const bool VERBOSE = (false || VERBOSE_ARGV); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG
341 const string s = aurostd::RemoveWhiteSpaces(s0);
342 vector<string> tokens;
343 aurostd::string2tokens(s, tokens, "|");
344 if (VERBOSE) {
345 for (size_t j = 0; j < tokens.size(); j++) {
346 cerr << "[" << tokens[j] << "]" << endl;
347 }
348 }
349 for (size_t i = 0; i < argv.size() - 1; i++) {
350 for (size_t j = 0; j < tokens.size(); j++) {
351 if (argv[i] == tokens[j]) {
352 return argv.at(i + 1);
353 }
354 }
355 }
356 return s_def;
357 }
358
359 string args2string(const vector<string>& argv, vector<string>& cmds, const string& s0, const string& s_def) {
360 const bool VERBOSE = (false || VERBOSE_ARGV); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG
361 const string s = aurostd::RemoveWhiteSpaces(s0);
362 vector<string> tokens;
363 aurostd::string2tokens(s, tokens, "|");
364 if (VERBOSE) {
365 for (size_t j = 0; j < tokens.size(); j++) {
366 cerr << "[" << tokens[j] << "]" << endl;
367 }
368 }
369 for (size_t j = 0; j < tokens.size(); j++) {
370 cmds.push_back(tokens[j]);
371 }
372 for (size_t i = 0; i < argv.size() - 1; i++) {
373 for (size_t j = 0; j < tokens.size(); j++) {
374 if (argv[i] == tokens[j]) {
375 return argv.at(i + 1);
376 }
377 }
378 }
379 return s_def;
380 }
381} // namespace aurostd
382
383// ***************************************************************************
384// Functions args2vectorstring
385// ***************************************************************************
386namespace aurostd {
387 vector<string> string2vstring(const string& str_in) {
388 vector<string> str_out;
389 str_out.push_back(str_in);
390 return str_out;
391 }
392
393 vector<string> args2vectorstring(const vector<string>& argv, const string& s0, const string& s_def) {
394 const bool VERBOSE = (false || VERBOSE_ARGV); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG
395 const string s = aurostd::RemoveWhiteSpaces(s0);
396 vector<string> tokens;
397 aurostd::string2tokens(s, tokens, "|");
398 if (VERBOSE) {
399 for (size_t j = 0; j < tokens.size(); j++) {
400 cerr << "[" << tokens[j] << "]" << endl;
401 }
402 }
403 for (size_t i = 0; i < argv.size() - 1; i++) {
404 for (size_t j = 0; j < tokens.size(); j++) {
405 if (argv[i] == tokens[j]) {
406 return vector<string>(argv.begin() + (i + 1), argv.end());
407 }
408 }
409 }
410 return string2vstring(s_def);
411 }
412} // namespace aurostd
413
414// ***************************************************************************
415// Functions get_itemized_vector_string stuff
416// ***************************************************************************
417namespace aurostd { // namespace aurostd
418 bool get_itemized_vector_string_from_input(const vector<string>& argv, const string& s0, vector<string>& tokens, const string& delimiter) {// =":")
419 if (aurostd::substring2bool(s0, "|")) {
420 const string message = "not ported to \"|\"";
422 }
423 uint icount = 0;
424 string s0neq = s0;
425 string s0equ;
426 aurostd::StringSubstInPlace(s0neq, "=", "");
427 s0equ = s0neq + "=";
428 if (aurostd::args2attachedflag(argv, s0equ)) {
429 icount += aurostd::string2tokens(aurostd::args2attachedstring(argv, s0equ, EMPTY_WORDING), tokens, delimiter);
430 }
431 if (aurostd::args2flag(argv, s0neq)) {
433 tokens = aurostd::args2vectorstring(argv, s0neq, EMPTY_WORDING);
434 }
435 if (tokens.size() == 1 && aurostd::substring2bool(tokens[0], delimiter)) {
436 s0equ = tokens[0];
437 icount += aurostd::string2tokens(s0equ, tokens, delimiter);
438 }
439 if (tokens.empty()) {
440 return false;
441 }
442 if (icount == 0) {
443 return false;
444 }
445 return true;
446 }
447 bool get_itemized_vector_string_from_input(const vector<string>& argv, const string& s0, const string& s1, vector<string>& tokens, const string& delimiter) {// =":")
448 uint icount = 0;
449 string s0neq = s0;
450 string s0equ;
451 aurostd::StringSubstInPlace(s0neq, "=", "");
452 s0equ = s0neq + "=";
453 string s1neq = s1;
454 string s1equ;
455 aurostd::StringSubstInPlace(s1neq, "=", "");
456 s1equ = s1neq + "=";
457 tokens.clear();
458 if (tokens.empty() && aurostd::args2attachedflag(argv, s0equ)) {
459 icount += aurostd::string2tokens(aurostd::args2attachedstring(argv, s0equ, EMPTY_WORDING), tokens, delimiter);
460 }
461 if (tokens.empty() && aurostd::args2attachedflag(argv, s1equ)) {
462 icount += aurostd::string2tokens(aurostd::args2attachedstring(argv, s1equ, EMPTY_WORDING), tokens, delimiter);
463 }
464 if (tokens.empty() && aurostd::args2flag(argv, s0neq)) {
466 tokens = aurostd::args2vectorstring(argv, s0neq, EMPTY_WORDING);
467 }
468 if (tokens.empty() && aurostd::args2flag(argv, s1neq)) {
470 tokens = aurostd::args2vectorstring(argv, s1neq, EMPTY_WORDING);
471 }
472 if (tokens.size() == 1 && aurostd::substring2bool(tokens[0], delimiter)) {
473 s0equ = tokens[0];
474 icount += aurostd::string2tokens(s0equ, tokens, delimiter);
475 }
476 if (tokens.empty()) {
477 return false;
478 }
479 if (icount == 0) {
480 return false;
481 }
482 return true;
483 }
484 bool get_itemized_vector_string_from_input(const vector<string>& argv, const string& s0, const string& s1, const string& s2, vector<string>& tokens, const string& delimiter) {// =":")
485 uint icount = 0;
486 string s0neq = s0;
487 string s0equ;
488 aurostd::StringSubstInPlace(s0neq, "=", "");
489 s0equ = s0neq + "=";
490 string s1neq = s1;
491 string s1equ;
492 aurostd::StringSubstInPlace(s1neq, "=", "");
493 s1equ = s1neq + "=";
494 string s2neq = s2;
495 string s2equ;
496 aurostd::StringSubstInPlace(s2neq, "=", "");
497 s2equ = s2neq + "=";
498 if (aurostd::args2attachedflag(argv, s0equ)) {
499 icount += aurostd::string2tokens(aurostd::args2attachedstring(argv, s0equ, EMPTY_WORDING), tokens, delimiter);
500 }
501 if (aurostd::args2attachedflag(argv, s1equ)) {
502 icount += aurostd::string2tokens(aurostd::args2attachedstring(argv, s1equ, EMPTY_WORDING), tokens, delimiter);
503 }
504 if (aurostd::args2attachedflag(argv, s2equ)) {
505 icount += aurostd::string2tokens(aurostd::args2attachedstring(argv, s2equ, EMPTY_WORDING), tokens, delimiter);
506 }
507 if (aurostd::args2flag(argv, s0neq)) {
509 tokens = aurostd::args2vectorstring(argv, s0neq, EMPTY_WORDING);
510 }
511 if (aurostd::args2flag(argv, s1neq)) {
513 tokens = aurostd::args2vectorstring(argv, s1neq, EMPTY_WORDING);
514 }
515 if (aurostd::args2flag(argv, s2neq)) {
517 tokens = aurostd::args2vectorstring(argv, s2neq, EMPTY_WORDING);
518 }
519 if (tokens.size() == 1 && aurostd::substring2bool(tokens[0], delimiter)) {
520 s0equ = tokens[0];
521 icount += aurostd::string2tokens(s0equ, tokens, delimiter);
522 }
523 if (tokens.empty()) {
524 return false;
525 }
526 if (icount == 0) {
527 return false;
528 }
529 return true;
530 }
531 bool get_itemized_vector_string_from_input(const vector<string>& argv, const string& s0, const string& s1, const string& s2, const string& s3, vector<string>& tokens, const string& delimiter) {// =":")
532 uint icount = 0;
533 string s0neq = s0;
534 string s0equ;
535 aurostd::StringSubstInPlace(s0neq, "=", "");
536 s0equ = s0neq + "=";
537 string s1neq = s1;
538 string s1equ;
539 aurostd::StringSubstInPlace(s1neq, "=", "");
540 s1equ = s1neq + "=";
541 string s2neq = s2;
542 string s2equ;
543 aurostd::StringSubstInPlace(s2neq, "=", "");
544 s2equ = s2neq + "=";
545 string s3neq = s3;
546 string s3equ;
547 aurostd::StringSubstInPlace(s3neq, "=", "");
548 s3equ = s3neq + "=";
549 if (aurostd::args2attachedflag(argv, s0equ)) {
550 icount += aurostd::string2tokens(aurostd::args2attachedstring(argv, s0equ, EMPTY_WORDING), tokens, delimiter);
551 }
552 if (aurostd::args2attachedflag(argv, s1equ)) {
553 icount += aurostd::string2tokens(aurostd::args2attachedstring(argv, s1equ, EMPTY_WORDING), tokens, delimiter);
554 }
555 if (aurostd::args2attachedflag(argv, s2equ)) {
556 icount += aurostd::string2tokens(aurostd::args2attachedstring(argv, s2equ, EMPTY_WORDING), tokens, delimiter);
557 }
558 if (aurostd::args2attachedflag(argv, s3equ)) {
559 icount += aurostd::string2tokens(aurostd::args2attachedstring(argv, s3equ, EMPTY_WORDING), tokens, delimiter);
560 }
561 if (aurostd::args2flag(argv, s0neq)) {
563 tokens = aurostd::args2vectorstring(argv, s0neq, EMPTY_WORDING);
564 }
565 if (aurostd::args2flag(argv, s1neq)) {
567 tokens = aurostd::args2vectorstring(argv, s1neq, EMPTY_WORDING);
568 }
569 if (aurostd::args2flag(argv, s2neq)) {
571 tokens = aurostd::args2vectorstring(argv, s2neq, EMPTY_WORDING);
572 }
573 if (aurostd::args2flag(argv, s3neq)) {
575 tokens = aurostd::args2vectorstring(argv, s3neq, EMPTY_WORDING);
576 }
577 if (tokens.size() == 1 && aurostd::substring2bool(tokens[0], delimiter)) {
578 s0equ = tokens[0];
579 icount += aurostd::string2tokens(s0equ, tokens, delimiter);
580 }
581 if (tokens.empty()) {
582 return false;
583 }
584 if (icount == 0) {
585 return false;
586 }
587 return true;
588 }
589} // namespace aurostd
590
591// ***************************************************************************
592// args2attachedflag without/with commands
593// ***************************************************************************
594namespace aurostd { // namespace aurostd
595
596 bool args2attachedflag(const vector<string>& argv, const string& s0) {
597 const bool VERBOSE = (false || VERBOSE_ARGV); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG
598 const string s = aurostd::RemoveWhiteSpaces(s0);
599 vector<string> tokens;
600 aurostd::string2tokens(s, tokens, "|");
601 if (VERBOSE) {
602 for (size_t j = 0; j < tokens.size(); j++) {
603 cerr << "[" << tokens[j] << "]" << endl;
604 }
605 }
606 for (size_t i = 0; i < argv.size(); i++) {
607 for (size_t j = 0; j < tokens.size(); j++) {
608 if (aurostd::substring2bool(argv[i], tokens[j])) {
609 return true;
610 }
611 }
612 }
613 return false;
614 }
615
616 bool args2attachedflag(const vector<string>& argv, std::vector<string>& cmds, const string& s0) {
617 const bool VERBOSE = (false || VERBOSE_ARGV); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG
618 const string s = aurostd::RemoveSpaces(s0);
619 vector<string> tokens;
620 aurostd::string2tokens(s, tokens, "|");
621 if (VERBOSE) {
622 for (size_t j = 0; j < tokens.size(); j++) {
623 cerr << "[" << tokens[j] << "]" << endl;
624 }
625 }
626 for (size_t j = 0; j < tokens.size(); j++) {
627 cmds.push_back(tokens[j]);
628 }
629 for (size_t i = 0; i < argv.size(); i++) {
630 for (size_t j = 0; j < tokens.size(); j++) {
631 if (aurostd::substring2bool(argv[i], tokens[j])) {
632 return true;
633 }
634 }
635 }
636 return false;
637 }
638} // namespace aurostd
639
640// ***************************************************************************
641// args2attachedstring(argv,"-xxx=",abc) returns the content of xxx= or abc
642// ***************************************************************************
643namespace aurostd { // namespace aurostd
644 string args2attachedstring(const vector<string>& argv, const string& s0, string s_def) { // string=""
645 const bool VERBOSE = (false || VERBOSE_ARGV); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG
646 const string s = aurostd::RemoveWhiteSpaces(s0);
647 string output;
648 vector<string> tokens;
649 aurostd::string2tokens(s, tokens, "|");
650 if (VERBOSE) {
651 cerr << "argv.size()=" << argv.size() << endl;
652 for (size_t j = 0; j < argv.size(); j++) {
653 cerr << "[" << argv[j] << "]" << endl;
654 }
655 }
656 if (VERBOSE) {
657 cerr << "tokens.size()=" << tokens.size() << endl;
658 for (size_t j = 0; j < tokens.size(); j++) {
659 cerr << "[" << tokens[j] << "]" << endl;
660 }
661 }
662 for (size_t i = 1; i < argv.size(); i++) {
663 for (size_t j = 0; j < tokens.size(); j++) {
664 if (argv[i].find(tokens[j]) != string::npos) {
665 output = argv[i].substr(argv[i].find(tokens[j]) + tokens[j].length());
666 if (!output.empty()) {
667 return output;
668 }
669 }
670 }
671 }
672 return s_def;
673 }
674
675 template <typename string> string args2attachedutype(const vector<string>& argv, const string& s0, const string& s_def) {
676 const bool VERBOSE = (false || VERBOSE_ARGV); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG
677 string s = aurostd::RemoveWhiteSpaces(s0);
678 string output = "";
679 vector<string> tokens;
680 aurostd::string2tokens(s, tokens, "|");
681 if (VERBOSE) {
682 cerr << "argv.size()=" << argv.size() << endl;
683 for (size_t j = 0; j < argv.size(); j++) {
684 cerr << "[" << argv[j] << "]" << endl;
685 }
686 }
687 if (VERBOSE) {
688 cerr << "tokens.size()=" << tokens.size() << endl;
689 for (size_t j = 0; j < tokens.size(); j++) {
690 cerr << "[" << tokens[j] << "]" << endl;
691 }
692 }
693 for (size_t i = 1; i < argv.size(); i++) {
694 for (size_t j = 0; j < tokens.size(); j++) {
695 if (argv[i].find(tokens[j]) != string::npos) {
696 return argv[i].substr(argv[i].find(tokens[j]) + tokens[j].length());
697 }
698 }
699 }
700 return s_def;
701 }
702} // namespace aurostd
703
704// ***************************************************************************
705// args2attachedutype
706// ***************************************************************************
707namespace aurostd { // namespace aurostd
708 template <typename utype> utype args2attachedutype(const vector<string>& argv, const string& str1, const utype& utype_default) {
709 const bool VERBOSE = (false || VERBOSE_ARGV); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG
710 vector<string> tokens1;
712 if (VERBOSE) {
713 cerr << "argv.size()=" << argv.size() << endl;
714 for (size_t j = 0; j < argv.size(); j++) {
715 cerr << "[" << argv[j] << "]" << endl;
716 }
717 }
718 if (VERBOSE) {
719 cerr << "tokens1.size()=" << tokens1.size() << endl;
720 for (size_t j = 0; j < tokens1.size(); j++) {
721 cerr << "[" << tokens1[j] << "]" << endl;
722 }
723 }
724 utype out = utype_default;
725 for (size_t j = 0; j < tokens1.size(); j++) {
726 string s1 = tokens1[j];
727 string s1eq;
728 string s1neq; // s1=aurostd::RemoveSubString(s1,"-");s1=aurostd::RemoveSubString(s1,"-");
729 s1 = aurostd::RemoveSubString(s1, "=");
730 s1eq = s1 + "=";
731 s1neq = s1;// cerr << s1eq << " " << s1neq << endl;
732 if (aurostd::args2flag(argv, s1neq) || aurostd::args2attachedflag(argv, s1eq)) {
733 if (aurostd::args2flag(argv, s1neq)) {
734 out = aurostd::args2utype(argv, s1neq, out);
735 }
736 if (aurostd::args2attachedflag(argv, s1eq)) {
737 vector<string> tokens2;
738 get_itemized_vector_string_from_input(argv, s1neq, tokens2, ",");
739 if (!tokens2.empty()) {
740 out = aurostd::string2utype<utype>(tokens2[0]);
741 }
742 }
743 }
744 }
745 return out;
746 }
747#define AST_TEMPLATE(atype) template atype args2attachedutype(const vector<string>&, const string&, const atype&);
749#undef AST_TEMPLATE
750} // namespace aurostd
751
752// *******************************************************************************************
753// *******************************************************************************************
754
755#endif // _AURO_IMPLEMENTATIONS_
756
757// ***************************************************************************
758// * *
759// * Aflow STEFANO CURTAROLO - Duke University 2003-2024 *
760// * *
761// ***************************************************************************
unsigned uint
Definition aurostd.h:39
#define __AFLOW_FILE__
Definition aurostd.h:44
#define __AFLOW_FUNC__
Definition aurostd.h:43
#define VERBOSE_ARGV
This file contains the preprocessor macros to ensure a proper instantiation of all aurostd functions.
#define AST_GEN_1(type_selection)
autogenerate 1D code based on AST_TEMPLATE
#define AST_UTYPE_NUM
#define EMPTY_WORDING
#define _INPUT_ILLEGAL_
string RemoveSubString(const string &str_orig, const string &str_rm) __xprototype
void StringSubstInPlace(string &work_string, const string &old_string, const string &new_string)
substitute parts of a strings in place
xvector< utype > args2xvectorutype(const vector< string > &argv, const string &s0, const xvector< utype > &def_out)
deque< utype > args2dequeutype(const deque< string > &argv, const string &s0)
uint string2tokens(const string &str, vector< string > &tokens, const string &delimiters=" ", bool consecutive=false) __xprototype
string RemoveSpaces(const string &s) __xprototype
bool get_itemized_vector_string_from_input(const vector< string > &argv, const string &s0, vector< string > &tokens, const string &delimiter)
utype string2utype(const string &from, const uint base=10)
string attach(const string &s1)
bool args2flag(const vector< string > &argv, const string &s0)
vector< string > get_arguments_from_input(int _argc, char **_argv)
vector< string > string2vstring(const string &str_in)
string args2string(const vector< string > &argv, const string &s0, const string &s_def)
string args2attachedutype(const vector< string > &argv, const string &s0, const string &s_def)
utype args2utype(const vector< string > &argv, const string &s0, utype def_out)
vector< utype > args2vectorutype(const vector< string > &argv, const string &s0)
bool _isfloat(bool)
bool args2attachedflag(const vector< string > &argv, const string &s0)
string args2attachedstring(const vector< string > &argv, const string &s0, string s_def)
vector< string > args2vectorstring(const vector< string > &argv, const string &s0, const string &s_def)
string RemoveWhiteSpaces(const string &s) __xprototype
bool substring2bool(const string &strstream, const string &strsub1, bool RemoveWS=false, bool RemoveComments=true)