AFLOW
 
Loading...
Searching...
No Matches
aurostd_xoption.cpp
Go to the documentation of this file.
1// ***************************************************************************
2// * *
3// * Aflow STEFANO CURTAROLO - Duke University 2003-2024 *
4// * *
5// ***************************************************************************
6// Written by Stefano Curtarolo 2013-2014
7// added template<class utype> bool xoption::args2addattachedscheme SC 2017
8// streamline schemes SC 2017
9
10#ifndef _AUROSTD_XOPTION_CPP_
11#define _AUROSTD_XOPTION_CPP_
12
13#include "aurostd_xoption.h"
14
15#include <cstddef>
16#include <iostream>
17#include <string>
18
19#include "aurostd.h"
20#include "aurostd_argv.h"
22#include "aurostd_xerror.h"
23
24using std::cerr;
25using std::endl;
26using std::ostream;
27using std::string;
28
29#define VERBOSE_XOPTION false // DX20200907
30
31namespace aurostd {
32 // ***************************************************************************
33
34 // constructure
36 free();
37 } // CO20200624 - moved to free()
38
39 // destructor
41 free();
42 } // CO20200624 - moved to free()
43
44 // free
46 keyword = ""; // DX20180824 - missing from constructor
47 isentry = false;
48 content_string = "";
49 content_double = 0.0;
50 content_int = 0;
51 content_uint = 0;
52 option = false;
53 option_default = false;
54 xscheme = "";
55 vxscheme.clear();
56 vxsghost.clear();
57 preserved = false;
58 }
59
60 // copy fuction
61 void xoption::copy(const xoption& b) {
62 keyword = b.keyword; // DX20180824 - missing from copy constructor
63 isentry = b.isentry;
68 option = b.option;
70 xscheme = b.xscheme;
71 vxscheme.clear();
72 for (size_t i = 0; i < b.vxscheme.size(); i++) {
73 vxscheme.push_back(b.vxscheme[i]);
74 }
75 vxsghost.clear();
76 for (size_t i = 0; i < b.vxsghost.size(); i++) {
77 vxsghost.push_back(b.vxsghost[i]);
78 }
80 }
81
82 // copy conctructor
84 // free();
85 // *this=b;
86 copy(b);
87 }
88
89 // copy operator b=a
90 const xoption& xoption::operator=(const xoption& b) { // operator=
91 if (this != &b) {
92 free();
93 copy(b);
94 }
95 return *this;
96 }
97
98 std::ostream& operator<<(std::ostream& oss, const xoption& a) {
99 for (size_t i = 0; i < a.vxscheme.size(); i++) {
100 oss << a.vxscheme[i] << (i < a.vxscheme.size() - 1 ? "," : "");
101 }
102 return oss;
103 }
104
106 //[CO20200624 - creating objects is SLOW]xoption aflow_option_temp;
107 //[CO20200624 - creating objects is SLOW]copy(aflow_option_temp);
108 free();
109 }
110
111 // **************************************************************************
112 // void xoption::options2entry(const string& options_FILE,const string& input_keyword,int _option_DEFAULT,const string& xscheme_DEFAULT) //CO20210805 - const&
113 void xoption::options2entry(const string& options_FILE_IN, const string& input_keyword_IN, int option_DEFAULT_IN, const string& xscheme_DEFAULT_IN) {
114 const bool VERBOSE = (false || VERBOSE_XOPTION); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG;
115
116 // CO20210909 - BIG BUG HERE
117 // the following clear() will reset all of the internal xoption variables
118 // if we pass one of these variables into options2entry(), it is reset as well
119 // see for example, this construction:
120 // vflags.KBIN_VASP_FORCE_OPTION_NELM_EQUAL.options2entry(AflowIn,_STROPT_+"NELM=",false,vflags.KBIN_VASP_FORCE_OPTION_NELM_EQUAL.xscheme);
121 // xscheme gets cleared before it's set
122 // in order to preserve this construction and prevent headaches, make copies of the inputs BEFORE the clear
123 const string options_FILE = options_FILE_IN;
124 const string input_keyword = input_keyword_IN;
125 const int _option_DEFAULT = option_DEFAULT_IN;
126 const string xscheme_DEFAULT = xscheme_DEFAULT_IN;
127
128 clear(); // CO20210909 - DANGEROUS! see note above
129
130 bool option_DEFAULT = false;
131 if (_option_DEFAULT == 0) {
132 option_DEFAULT = false; // it is a int.. it might be -1
133 }
134 if (_option_DEFAULT == 1) {
135 option_DEFAULT = true; // it is a int.. it might be -1
136 }
137 isentry = option_DEFAULT;
138 option = option_DEFAULT;
139 content_string = xscheme_DEFAULT;
140 xscheme = xscheme_DEFAULT;
141 preserved = false; // DEFAULT
142 if (VERBOSE) {
143 cerr << "DEBUG - " << __AFLOW_FUNC__ << " BEGIN " << endl;
144 cerr << "DEBUG - " << __AFLOW_FUNC__ << " input_keyword=\"" << input_keyword << "\"" << endl;
145 cerr << "DEBUG - " << __AFLOW_FUNC__ << " option_DEFAULT=" << (option_DEFAULT ? "true" : "false") << endl;
146 cerr << "DEBUG - " << __AFLOW_FUNC__ << " xscheme_DEFAULT=\"" << xscheme_DEFAULT << "\"" << endl;
147 }
148 // start the scan
149 // string keyword; //CO20180404 - now a member of the object
150 vector<string> vkeyword;
151 // tokenize the option
152 aurostd::string2tokens(input_keyword, vkeyword, "|");
153 if (VERBOSE) {
154 for (size_t i = 0; i < vkeyword.size(); i++) {
155 cerr << "\"" << vkeyword[i] << "\"" << endl;
156 }
157 }
158 // loop through the scan
159 if (!vkeyword.empty()) {
160 // some default
161 keyword = vkeyword[0];
162 for (size_t i = 0; i < vkeyword.size(); i++) {
163 if (aurostd::substring2bool(options_FILE, vkeyword[i], true)) {
164 keyword = vkeyword[i];
165 }
166 }
167 // found one keyword
168 if (VERBOSE) {
169 cerr << "DEBUG - " << __AFLOW_FUNC__ << " keyword=\"" << keyword << "\"" << endl;
170 }
171 // LOOK FOR EXIST/!EXIST ENTRY
172 if (_option_DEFAULT == aurostd_xoptionONOFF) {
173 isentry = aurostd::substring2bool(options_FILE, keyword, true);
174 if (isentry) {
175 option = true;
176 content_string = "ON";
177 }
178 if (!isentry) {
179 option = false;
180 content_string = "OFF";
181 }
182 } // aurostd_xoptionONOFF exit/~exit
183 // LOOK FOR ON/OFF MODE WITH strings/schemes.
184 if (_option_DEFAULT == 0 || _option_DEFAULT == 1) {
185 if (VERBOSE) {
186 cerr << "DEBUG - " << __AFLOW_FUNC__ << " LOOK FOR ON/OFF MODE WITH strings/schemes" << endl;
187 }
188 // start the scan
189 isentry = aurostd::substring2bool(options_FILE, keyword, true);
190 if (isentry && xscheme_DEFAULT.empty()) {
192 if (content_string.empty()) {
194 } // CO20200731 - "[AFLOW]SYSTEM=" vs. "[AFLOW] SYSTEM = "
195 const string saus = content_string;
196 content_string = "";
197 if (VERBOSE) {
198 cerr << "DEBUG - " << __AFLOW_FUNC__ << " found saus=" << saus << endl;
199 }
200 vector<string> tokens;
201 aurostd::string2tokens(saus, tokens, ",");
202 for (size_t i = 0; i < tokens.size(); i++) { // c<< tokens[i] << endl;
203 if (tokens[i] == "ON" || tokens[i][0] == 'T' || tokens[i][0] == 't' || tokens[i][0] == '1' || tokens[i][0] == 'Y' || tokens[i][0] == 'y') {
204 option = true;
205 content_string = saus;
206 } // modify option and value
207 if (tokens[i] == "OFF" || tokens[i][0] == 'F' || tokens[i][0] == 'f' || tokens[i][0] == '0' || tokens[i][0] == 'N' || tokens[i][0] == 'n') {
208 option = false;
209 content_string = saus;
210 } // modify option and value
211 // if(tokens[i]=="REMOVE_RELAX_1") {option=true;content_string=saus;} // modify option and value // compatibility with SPIN
212 // if(tokens[i]=="REMOVE_RELAX_2") {option=true;content_string=saus;} // modify option and value // compatibility with SPIN
213 if (tokens[i] == "REMOVE_RELAX_1") {
214 content_string = saus;
215 } // modify option and value // compatibility with SPIN but dont touch ON/OFF
216 if (tokens[i] == "REMOVE_RELAX_2") {
217 content_string = saus;
218 } // modify option and value // compatibility with SPIN but dont touch ON/OFF
219 }
220 }
221 // SCHEME MODE
222 if (VERBOSE) {
223 cerr << "DEBUG - " << __AFLOW_FUNC__ << " xscheme_DEFAULT=\"" << xscheme_DEFAULT << "\"" << endl;
224 }
225 if (VERBOSE) {
226 cerr << "DEBUG - " << __AFLOW_FUNC__ << " xscheme_DEFAULT.empty()=" << xscheme_DEFAULT.empty() << endl;
227 }
228 if (isentry && !xscheme_DEFAULT.empty()) {
229 if (VERBOSE) {
230 cerr << "DEBUG - " << __AFLOW_FUNC__ << " SCHEME MODE" << endl;
231 }
233 if (content_string.empty()) {
235 } // CO20200731 - "[AFLOW]SYSTEM=" vs. "[AFLOW] SYSTEM = "
236 // ME20181030 - Special case: if the scheme is a Boolean keyword, unset option
237 // ME20190107 - Cannot use N or F because it's ambiguous (nitrogen, fluorine)
238 const string content = aurostd::toupper(content_string);
239 if ((content == "OFF") || (content == "false") || (content == "NO")) {
240 option = false;
241 } else {
242 option = isentry;
243 }
244 }
245 if (isentry && (xscheme_DEFAULT.empty() && content_string.empty())) {
246 if (VERBOSE) {
247 cerr << "DEBUG - " << __AFLOW_FUNC__ << " SCHEME MODE EMPTY DEFAULT STILL EMPTY CONTENT" << endl;
248 }
250 if (content_string.empty()) {
252 } // CO20200731 - "[AFLOW]SYSTEM=" vs. "[AFLOW] SYSTEM = "
253 option = isentry;
254 }
255 if (!isentry && option_DEFAULT) {
256 option = true;
257 content_string = "ON";
258 }
259 } // 0/1 on/off mode
260 // LOOK FOR EXIST/!EXIST ENTRY
261 if (_option_DEFAULT == aurostd_xoptionMULTI) {
262 vector<string> voptions_FILE;
263 vector<string> vcontent;
264 aurostd::string2vectorstring(options_FILE, voptions_FILE);
265 isentry = false;
266 content_string = "";
267 for (size_t i = 0; i < voptions_FILE.size(); i++) {
268 if (aurostd::substring2bool(voptions_FILE[i], keyword, true)) {
269 vector<string> vstrcheck;
270 string strcheck = aurostd::toupper(aurostd::RemoveWhiteSpaces(aurostd::substring2string(voptions_FILE[i], keyword, 1, false)));
271 aurostd::StringSubstInPlace(strcheck, ";", ",");
272 aurostd::string2tokens(strcheck, vstrcheck, ",");
273 for (size_t j = 0; j < vstrcheck.size(); j++) {
274 if (VERBOSE) {
275 cerr << "DEBUG - " << __AFLOW_FUNC__ << " BEFORE keyword=" << keyword << " " << "vstrcheck[j]=" << vstrcheck[j] << endl;
276 }
277 if (aurostd::substring2bool(keyword, "KPOINTS")) {
278 if (vstrcheck[j] == "A") {
279 vstrcheck[j] = "AUTO";
280 }
281 if (vstrcheck[j] == "G") {
282 vstrcheck[j] = "GAMMA";
283 }
284 if (vstrcheck[j] == "M") {
285 vstrcheck[j] = "MONKHORST_PACK";
286 }
287 }
288 if (aurostd::substring2bool(keyword, "IGNORE_AFIX")) {
289 ;
290 }; // dummy load
291 if (aurostd::substring2bool(keyword, "CONVERT_UNIT_CELL")) {
292 if (vstrcheck[j] == "SPRIM") {
293 vstrcheck[j] = "STANDARD_PRIMITIVE";
294 }
295 if (vstrcheck[j] == "STD_PRIM") {
296 vstrcheck[j] = "STANDARD_PRIMITIVE";
297 }
298 if (vstrcheck[j] == "STANDARD_PRIMITIVE") {
299 vstrcheck[j] = "STANDARD_PRIMITIVE";
300 }
301 if (vstrcheck[j] == "SCONV") {
302 vstrcheck[j] = "STANDARD_CONVENTIONAL";
303 }
304 if (vstrcheck[j] == "STD_CONV") {
305 vstrcheck[j] = "STANDARD_CONVENTIONAL";
306 }
307 if (vstrcheck[j] == "STANDARD_CONVENTIONAL") {
308 vstrcheck[j] = "STANDARD_CONVENTIONAL";
309 }
310 if (vstrcheck[j] == "NIGGLI") {
311 vstrcheck[j] = "NIGGLI";
312 }
313 if (vstrcheck[j] == "MINK") {
314 vstrcheck[j] = "MINKOWSKI";
315 }
316 if (vstrcheck[j] == "MINKOWSKI") {
317 vstrcheck[j] = "MINKOWSKI";
318 }
319 if (vstrcheck[j] == "INCELL") {
320 vstrcheck[j] = "INCELL";
321 }
322 if (vstrcheck[j] == "COMPACT") {
323 vstrcheck[j] = "COMPACT";
324 }
325 if (vstrcheck[j] == "INCOMPACT") {
326 vstrcheck[j] = "COMPACT";
327 }
328 if (vstrcheck[j] == "INWIGNERSEITZ") {
329 vstrcheck[j] = "WIGNERSEITZ";
330 }
331 if (vstrcheck[j] == "WS") {
332 vstrcheck[j] = "WIGNERSEITZ";
333 }
334 if (vstrcheck[j] == "WIGNERSEITZ") {
335 vstrcheck[j] = "WIGNERSEITZ";
336 }
337 if (vstrcheck[j] == "C") {
338 vstrcheck[j] = "CARTESIAN";
339 }
340 if (vstrcheck[j] == "CART") {
341 vstrcheck[j] = "CARTESIAN";
342 }
343 if (vstrcheck[j] == "CARTESIAN") {
344 vstrcheck[j] = "CARTESIAN";
345 }
346 if (vstrcheck[j] == "F") {
347 vstrcheck[j] = "FRACTIONAL";
348 }
349 if (vstrcheck[j] == "FRAC") {
350 vstrcheck[j] = "FRACTIONAL";
351 }
352 if (vstrcheck[j] == "FRACTIONAL") {
353 vstrcheck[j] = "FRACTIONAL";
354 }
355 if (vstrcheck[j] == "D") {
356 vstrcheck[j] = "DIRECT";
357 }
358 if (vstrcheck[j] == "DIR") {
359 vstrcheck[j] = "DIRECT";
360 }
361 if (vstrcheck[j] == "DIRECT") {
362 vstrcheck[j] = "DIRECT";
363 }
364 if (vstrcheck[j] == "PRE") {
365 vstrcheck[j] = "PRESERVE";
366 }
367 if (vstrcheck[j] == "PRES") {
368 vstrcheck[j] = "PRESERVE";
369 }
370 if (vstrcheck[j] == "PRESERVE") {
371 vstrcheck[j] = "PRESERVE";
372 }
373 }
374 if (VERBOSE) {
375 cerr << "DEBUG - " << __AFLOW_FUNC__ << " AFTER keyword=" << keyword << " " << "vstrcheck[j]=" << vstrcheck[j] << endl;
376 }
377 vcontent.push_back(vstrcheck[j]);
378 }
379 }
380 }
381 for (size_t i = 0; i < vcontent.size(); i++) {
382 content_string += vcontent[i] + (i < vcontent.size() - 1 ? "," : "");
383 }
386 if (!vcontent.empty()) {
387 isentry = true;
388 }
389 } // aurostd_xoptionMULTI list
390 }
396 if (VERBOSE) {
397 if (_option_DEFAULT == aurostd_xoptionMULTI) {
398 for (size_t i = 0; i < vxscheme.size(); i++) {
399 cerr << "DEBUG - " << __AFLOW_FUNC__ << " vxscheme.at(" << i << ")=" << vxscheme[i] << endl;
400 }
401 }
402 }
403
404 preserved = false;
405 for (size_t i = 0; i < vxscheme.size() && !preserved; i++) {
406 preserved = (vxscheme[i] == "PRESERVED");
407 }
408 if (VERBOSE) {
409 cerr << "DEBUG - " << __AFLOW_FUNC__ << " isentry=" << (isentry ? "true" : "false") << endl;
410 }
411 if (VERBOSE) {
412 cerr << "DEBUG - " << __AFLOW_FUNC__ << " content_string=\"" << content_string << "\"" << endl;
413 }
414 if (VERBOSE) {
415 cerr << "DEBUG - " << __AFLOW_FUNC__ << " content_double=\"" << content_double << "\"" << endl;
416 }
417 if (VERBOSE) {
418 cerr << "DEBUG - " << __AFLOW_FUNC__ << " content_int=\"" << content_int << "\"" << endl;
419 }
420 if (VERBOSE) {
421 cerr << "DEBUG - " << __AFLOW_FUNC__ << " content_uint=\"" << content_uint << "\"" << endl;
422 }
423 if (VERBOSE) {
424 cerr << "DEBUG - " << __AFLOW_FUNC__ << " option=" << (option ? "true" : "false") << endl;
425 }
426 if (VERBOSE) {
427 cerr << "DEBUG - " << __AFLOW_FUNC__ << " preserved=" << (preserved ? "true" : "false") << endl;
428 }
429 if (VERBOSE) {
430 cerr << "DEBUG - " << __AFLOW_FUNC__ << " xscheme=\"" << xscheme << "\"" << endl;
431 }
432 if (isentry && content_string.empty()) {
433 stringstream message;
434 message << "Content string empty. content_string=" << content_string << ", content_double=" << content_double << ", content_int=" << content_int << ", content_uint=" << content_uint
435 << ", keyword=" << keyword << ", isentry=" << isentry;
437 }
438 if (VERBOSE) {
439 cerr << "DEBUG - " << __AFLOW_FUNC__ << " END" << endl;
440 }
441 // return isentry;
442 }
443
444 void xoption::scheme2scheme(char c, const string& s) { // CO20210805 - const&
445 for (size_t i = 0; i < vxscheme.size(); i++) {
446 if (vxscheme[i].at(0) == c || vxscheme[i].at(0) == aurostd::tolower(c) || vxscheme[i].at(0) == aurostd::toupper(c)) {
447 xscheme = s;
448 }
449 }
450 }
451
452 void xoption::scheme2scheme(const string& s1, const string& s2) { // CO20210805 - const&
453 for (size_t i = 0; i < vxscheme.size(); i++) {
454 if (vxscheme[i] == s1 || vxscheme[i] == aurostd::tolower(s1) || vxscheme[i] == aurostd::toupper(s1)) {
455 xscheme = s2;
456 // for(size_t i=0;i<vxscheme.size();i++) if(vxscheme[i]==s1) scheme=s2;
457 }
458 }
459 }
460
461 bool xoption::isscheme(const string& check) const { // CO20180101 //CO20210805 - const&
462 // ISSCHEME and FLAG checks only vxscheme... does not manage the ghost, so for example //SC20200114
463 // CONVERT_UNIT_CELL (as flag) will not be confused with CONVERT_UNIT_CELL=STANDARD as method. //SC20200114
464 // Thanks to Marco Esters for getting this bug. //SC20200114
465 string a;
466 string b;
467 // check schemes list going through vxscheme 1 by 1
468 for (size_t i = 0; i < vxscheme.size(); i++) {
469 a = aurostd::toupper(vxscheme[i]); // shortcuts
470 b = aurostd::toupper(check); // shortcuts
471 // cerr << "xoption::isscheme for scheme i=" << i << " " << a << " " << b << endl;
472 aurostd::StringSubstInPlace(a, "GAMMA", "G");
473 aurostd::StringSubstInPlace(b, "GAMMA", "G"); // shortcuts
474 aurostd::StringSubstInPlace(a, "MONKHORST_PACK", "M");
475 aurostd::StringSubstInPlace(b, "MONKHORST_PACK", "M"); // shortcuts
476 aurostd::StringSubstInPlace(a, "MP", "M");
477 aurostd::StringSubstInPlace(b, "MP", "M"); // shortcuts
478 aurostd::StringSubstInPlace(a, "AUTO", "A");
479 aurostd::StringSubstInPlace(b, "AUTO", "A"); // shortcuts
480 if (a == b) {
481 // cerr << "xoption::isscheme BINGO FOUND SCHEME " << a << " " << b << endl;
482 return true;
483 }
484 }
485 // SC20200310 // THIS IS INCORRECT
486 // SC20200310 // check attached schemes list going through vxsghost 2 by 2 //SC20191227
487 // SC20200310 for(size_t i=0;i<vxsghost.size();i+=2) {
488 // SC20200310 // cerr << "xoption::isscheme for attached scheme i=" << i << " " << a << " " << b << endl;
489 // SC20200310 a=aurostd::toupper(vxsghost.at(i)); // shortcuts
490 // SC20200310 b=aurostd::toupper(check); // shortcuts
491 // SC20200310 if(a==b) {
492 // SC20200310 // cerr << "xoption::isscheme BINGO FOUND ATTACHED SCHEME" << a << " " << b << endl;
493 // SC20200310 return true;
494 // SC20200310 }
495 // SC20200310 }
496 // SC20200310 // nor in scheme nor in attached scheme... exit
497 return false;
498 }
499
513
514 uint xoption::push(const string& _xscheme) {
515 return opscheme(_xscheme, true);
516 } // CO20210805 - const&
517 uint xoption::pop(const string& _xscheme) {
518 return opscheme(_xscheme, false);
519 } // CO20210805 - const&
520
521 uint xoption::opscheme(const string& _xscheme, bool operation) { // CO20210805 - const&
522 const bool VERBOSE = (false || VERBOSE_XOPTION); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG;
523 if (operation == true) {
524 if (VERBOSE) {
525 cerr << "DEBUG - aurostd::xoption::opscheme: ADD=" << aurostd::toupper(_xscheme) << endl;
526 }
527 if (VERBOSE) {
528 for (size_t i = 0; i < vxscheme.size(); i++) {
529 cerr << "DEBUG - aurostd::xoption::opscheme: ADD_BEFORE vxscheme.at(" << i << ")=" << vxscheme.at(i) << endl;
530 }
531 }
532 // CO20181226 START - check that it doesn't already exist, multiples don't affect isscheme, but affects how we iterate through aplopts
533 for (size_t i = 0; i < vxscheme.size(); i++) {
534 if (aurostd::toupper(vxscheme[i]) == aurostd::toupper(_xscheme)) {
535 opscheme(_xscheme, false);
536 } // recursion is GNU's pleasure
537 }
538 // CO20181226 STOP
539 vxscheme.push_back(aurostd::toupper(_xscheme));
540 if (VERBOSE) {
541 for (size_t i = 0; i < vxscheme.size(); i++) {
542 cerr << "DEBUG - aurostd::xoption::opscheme: ADD_BEFORE vxscheme.at(" << i << ")=" << vxscheme.at(i) << endl;
543 }
544 }
545 } else {
546 if (VERBOSE) {
547 cerr << "DEBUG - aurostd::xoption::opscheme: PURGE=" << aurostd::toupper(_xscheme) << endl;
548 }
549 if (VERBOSE) {
550 for (size_t i = 0; i < vxscheme.size(); i++) {
551 cerr << "DEBUG - aurostd::xoption::opscheme: PURGE_BEFORE vxscheme.at(" << i << ")=" << vxscheme.at(i) << endl;
552 }
553 }
554 vector<string> _vxscheme(vxscheme);
555 vxscheme.clear();
556 for (size_t i = 0; i < _vxscheme.size(); i++) {
557 if (aurostd::toupper(_vxscheme[i]) != aurostd::toupper(_xscheme)) {
558 vxscheme.push_back(_vxscheme[i]);
559 }
560 }
561 if (VERBOSE) {
562 for (size_t i = 0; i < vxscheme.size(); i++) {
563 cerr << "DEBUG - aurostd::xoption::opscheme: PURGE_AFTER vxscheme.at(" << i << ")=" << vxscheme.at(i) << endl;
564 }
565 }
566 }
567 refresh();
568 return vxscheme.size();
569 }
570
571 bool xoption::flag(const string& _xscheme, bool operation) { // CO20210805 - const&
572 if (operation) {
573 opscheme(_xscheme, true); // push
574 }
575 if (!operation) {
576 opscheme(_xscheme, false); // pop
577 }
578 return operation;
579 }
580
581 bool xoption::flag(const string& xscheme) const {
582 return isscheme(xscheme);
583 } // same as ischeme //CO20210805 - const&
584
585 bool xoption::flag() const { // same as ischeme
586 if (!vxscheme.empty()) {
587 return true;
588 }
589 // NO NEED ANYMORE SC20200114 if(vxsghost.size()>0) return true; //SC20191227
590 return false;
591 }
592
593 // now for the attached ones.
594
595 bool xoption::isdefined(const string& check) const { // SC20200114 //CO20210805 - const&
596 // checks only scheme (vxscheme) it does not go through the attached schemes (vxghost). //SC20200114
597 string a;
598 string b; // SC20200114
599 // check schemes list going through vxscheme 1 by 1 //SC20200114
600 // check attached schemes list going through vxsghost 2 by 2 //SC20191227 //SC20200114
601 b = aurostd::toupper(check); // shortcuts //SC20200114 //CO20220630 - no need to redo over and over again
602 for (size_t i = 0; i < vxsghost.size(); i += 2) { // SC20200114
603 // cerr << "xoption::isscheme for attached scheme i=" << i << " " << a << " " << b << endl; //SC20200114
604 a = aurostd::toupper(vxsghost[i]); // shortcuts //SC20200114
605 if (a == b) { // SC20200114
606 // cerr << "xoption::isscheme BINGO FOUND ATTACHED SCHEME" << a << " " << b << endl; //SC20200114
607 return true; // SC20200114
608 } // SC20200114
609 } // SC20200114
610 return false; // SC20200114
611 } // SC20200114
612
613 string xoption::getattachedscheme(const string& xscheme) const {
614 const bool VERBOSE = (false || VERBOSE_XOPTION); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG;
615 if (vxsghost.empty()) {
616 return "";
617 }
618 for (size_t i = 0; i < vxsghost.size() - 1; i += 2) {
619 if (VERBOSE) {
620 cerr << i << " --- [" << aurostd::toupper(xscheme) << "] --- [" << aurostd::toupper(vxsghost.at(i)) << "] --- [" << aurostd::toupper(vxsghost.at(i + 1)) << "]" << endl;
621 }
623 return vxsghost.at(i + 1);
624 }
625 }
626 return "";
627 }
628 template <class utype> utype xoption::getattachedutype(const string& xscheme) const { // CO20200731
630 }
631#define AST_TEMPLATE(utype) template utype xoption::getattachedutype(const string&) const;
634#undef AST_TEMPLATE
635
636 uint xoption::opattachedscheme(const string& _xscheme, const string& attached, bool operation) { // CO20210805 - const&
637 const bool VERBOSE = (false || VERBOSE_XOPTION); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG;
638 if (operation == true) {
639 if (VERBOSE) {
640 cerr << "DEBUG - aurostd::xoption::opattachedscheme: ADD=" << aurostd::toupper(_xscheme) << endl;
641 }
642 if (VERBOSE) {
643 cerr << "DEBUG - aurostd::xoption::opattachedscheme: GHOST=" << attached << endl;
644 }
645 // CO20181226 START - check that it doesn't already exist, multiples affect getattachedscheme
646 for (size_t i = 0; i < vxsghost.size(); i += 2) {
647 if (aurostd::toupper(vxsghost[i]) == aurostd::toupper(_xscheme)) {
648 opattachedscheme(_xscheme, attached, false);
649 } // recursion is GNU's pleasure
650 }
651 // CO20181226 STOP
652 vxsghost.push_back(aurostd::toupper(_xscheme));
653 vxsghost.push_back(attached);
654 } else {
655 if (VERBOSE) {
656 cerr << "DEBUG - aurostd::xoption::opattachedscheme: PURGE=" << aurostd::toupper(_xscheme) << endl;
657 }
658 vector<string> _vxsghost(vxsghost);
659 vxsghost.clear();
660 for (size_t i = 0; i < _vxsghost.size(); i += 2) {
661 if (aurostd::toupper(_vxsghost[i]) != aurostd::toupper(_xscheme)) {
662 vxsghost.push_back(_vxsghost[i]);
663 vxsghost.push_back(_vxsghost.at(i + 1));
664 }
665 }
666 if (VERBOSE) {
667 for (size_t i = 0; i < vxsghost.size(); i++) {
668 cerr << "PURGEATTACHED_AFTER vxsghost.at(" << i << ")=" << vxsghost.at(i) << endl;
669 }
670 }
671 }
672 refresh();
673 return vxsghost.size();
674 }
675
676 uint xoption::addattachedscheme(const string& _xscheme, const string& attached, bool operation) { // CO20210805 - const&
677 if (operation) {
678 return opattachedscheme(_xscheme, attached, true);
679 }
680 return vxsghost.size();
681 }
682
683 uint xoption::push_attached(const string& _xscheme, const string& attached) { // CO20210805 - const&
684 return opattachedscheme(_xscheme, attached, true);
685 }
686
687 uint xoption::pop_attached(const string& _xscheme) { // CO20210805 - const&
688 return opattachedscheme(_xscheme, "", false);
689 }
690
691 bool xoption::args2addattachedscheme(vector<string>& argv, const string _xscheme, const string& _s_search, string string_default) {
692 vector<string> cmds;
693 return args2addattachedscheme(argv, cmds, _xscheme, _s_search, string_default);
694 }
695
696 bool xoption::args2addattachedscheme(vector<string>& argv, vector<string>& cmds, const string xscheme, const string& _s_search, string string_default) {
697 const bool VERBOSE = (false || VERBOSE_XOPTION); // DX20200907 - LDEBUG to VERBOSE; decouple from XHOST.DEBUG;
698 string s_search(_s_search);
699 if (aurostd::args2attachedflag(argv, cmds, s_search)) {
700 flag(xscheme, true);
701 addattachedscheme(xscheme, aurostd::args2attachedstring(argv, s_search, string_default), true);
702 if (VERBOSE) {
703 cerr << "DEBUG - aurostd::xoption::args2addscheme: xscheme=" << xscheme << " s_search=" << s_search << " attached=" << aurostd::args2attachedstring(argv, s_search, string_default) << endl;
704 }
705 return true;
706 }
707 aurostd::StringSubstInPlace(s_search, "=", "");
708 if (aurostd::args2flag(argv, cmds, s_search)) {
709 // cerr << aurostd::args2string(argv,s_search,string_default) << endl;
710 flag(xscheme, true);
711 addattachedscheme(xscheme, aurostd::args2string(argv, s_search, string_default), true);
712 if (VERBOSE) {
713 cerr << "DEBUG - aurostd::xoption::args2addscheme: xscheme=" << xscheme << " s_search=" << s_search << " taking=" << aurostd::args2string(argv, s_search, string_default) << endl;
714 }
715 return true;
716 }
717 return false;
718 }
719
720 bool xoption::args2addattachedscheme(vector<string>& argv, const string xscheme, const string& _s_search, const char* string_default) {
721 return args2addattachedscheme(argv, xscheme, _s_search, string(string_default));
722 }
723
724 bool xoption::args2addattachedscheme(vector<string>& argv, vector<string>& cmds, const string xscheme, const string& _s_search, const char* string_default) {
725 return args2addattachedscheme(argv, cmds, xscheme, _s_search, string(string_default));
726 }
727
728 template <class utype> bool xoption::args2addattachedscheme(vector<string>& argv, const string xscheme, const string& _s_search, utype utype_default) {
729 return args2addattachedscheme(argv, xscheme, _s_search, aurostd::utype2string(utype_default));
730 }
731
732 template <class utype> bool xoption::args2addattachedscheme(vector<string>& argv, vector<string>& cmds, const string xscheme, const string& _s_search, utype utype_default) {
733 return args2addattachedscheme(argv, cmds, xscheme, _s_search, aurostd::utype2string(utype_default));
734 }
735
736} // namespace aurostd
737
738#endif // _AUROSTD_XOPTION_CPP_
739
740// **************************************************************************
741// * *
742// * STEFANO CURTAROLO - Duke University 2003-2024 *
743// * *
744// **************************************************************************
unsigned uint
Definition aurostd.h:39
#define __AFLOW_FILE__
Definition aurostd.h:44
#define __AFLOW_FUNC__
Definition aurostd.h:43
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 AST_UTYPE_BOOL
#define _RUNTIME_ERROR_
#define VERBOSE_XOPTION
#define aurostd_xoptionONOFF
#define aurostd_xoptionMULTI
void options2entry(const std::string &, const std::string &, int=aurostd_xoptionONOFF, const std::string &xscheme_DEFAULT="")
std::string keyword
uint opscheme(const std::string &, bool)
std::vector< std::string > vxscheme
std::string xscheme
uint push_attached(const std::string &scheme, const std::string &attached)
void copy(const xoption &b)
const xoption & operator=(const xoption &b)
void scheme2scheme(char, const std::string &)
uint push(const std::string &)
std::string content_string
bool args2addattachedscheme(std::vector< std::string > &argv, const std::string scheme, const std::string &_s_search, std::string string_default)
bool isdefined(const std::string &) const
bool isscheme(const std::string &) const
uint pop_attached(const std::string &check)
utype getattachedutype(const std::string &scheme) const
uint opattachedscheme(const std::string &, const std::string &, bool)
uint addattachedscheme(const std::string &scheme, const std::string &attached, bool flag)
uint pop(const std::string &)
std::string getattachedscheme(const std::string &scheme) const
std::vector< std::string > vxsghost
void StringSubstInPlace(string &work_string, const string &old_string, const string &new_string)
substitute parts of a strings in place
uint string2tokens(const string &str, vector< string > &tokens, const string &delimiters=" ", bool consecutive=false) __xprototype
utype string2utype(const string &from, const uint base=10)
bool args2flag(const vector< string > &argv, const string &s0)
string utype2string(const utype &from, int precision=AUROSTD_DEFAULT_PRECISION, char FORMAT=DEFAULT_STREAM) __xprototype
size_t string2vectorstring(const string &stringIN, vector< string > &vstringout, bool consecutive=false, bool trim_edges=false)
string args2string(const vector< string > &argv, const string &s0, const string &s_def)
string substring2string(ifstream &input, const string &strsub1, const int instance=1, bool RemoveWS=false, bool RemoveComments=true)
string tolower(const string &in) __xprototype
bool args2attachedflag(const vector< string > &argv, const string &s0)
string toupper(const string &in) __xprototype
string args2attachedstring(const vector< string > &argv, const string &s0, string s_def)
b64_encoder_proxy operator<<(std::ostream &os, b64_encoder_creator)
string RemoveWhiteSpaces(const string &s) __xprototype
bool substring2bool(const string &strstream, const string &strsub1, bool RemoveWS=false, bool RemoveComments=true)