-
Notifications
You must be signed in to change notification settings - Fork 17
/
GetPot
2441 lines (2087 loc) · 84.4 KB
/
GetPot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- c++ -*- vim: set syntax=cpp:
// GetPot Version $$Version$$ $$Date$$
//
// WEBSITE: http://getpot.sourceforge.net
//
// NOTE: The LPGL License for this library is only valid in case that
// it is not used for the production or development of applications
// dedicated to military industry. This is what the author calls
// the 'unofficial peace version of the LPGL'.
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
//
// (C) 2001-2007 Frank R. Schaefer <[email protected]>
//==========================================================================
#ifndef __include_guard_GETPOT_H__
#define __include_guard_GETPOT_H__
#if defined(WIN32) || defined(SOLARIS_RAW) || (__GNUC__ == 2) || defined(__HP_aCC)
#define strtok_r(a, b, c) strtok(a, b)
#endif // WINDOWS or SOLARIS or gcc 2.* or HP aCC
extern "C" {
// leave the 'extern C' to make it 100% sure to work -
// expecially with older distributions of header files.
#ifndef WIN32
// this is necessary (depending on OS)
#include <ctype.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <string.h>
}
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <iostream> // not every compiler distribution includes <iostream>
// // with <fstream>
typedef std::vector<std::string> STRING_VECTOR;
#define victorate(TYPE, VARIABLE, ITERATOR) \
std::vector<TYPE>::const_iterator ITERATOR = (VARIABLE).begin(); \
for(; (ITERATOR) != (VARIABLE).end(); (ITERATOR)++)
class GetPot {
//--------
inline void __basic_initialization();
public:
// (*) constructors, destructor, assignment operator -----------------------
inline GetPot();
inline GetPot(const GetPot&);
inline GetPot(const int argc_, char** argv_,
const char* FieldSeparator=0x0);
inline GetPot(const char* FileName,
const char* CommentStart=0x0, const char* CommentEnd=0x0,
const char* FieldSeparator=0x0);
inline ~GetPot();
inline GetPot& operator=(const GetPot&);
// (*) absorbing contents of another GetPot object
inline void absorb(const GetPot& That);
// -- for ufo detection: recording requested arguments, options etc.
inline void clear_requests();
inline void disable_request_recording() { __request_recording_f = false; }
inline void enable_request_recording() { __request_recording_f = true; }
// (*) direct access to command line arguments -----------------------------
inline const std::string operator[](unsigned Idx) const;
inline int get(unsigned Idx, int Default) const;
inline double get(unsigned Idx, const double& Default) const;
inline const std::string get(unsigned Idx, const char* Default) const;
inline unsigned size() const;
// (*) flags ---------------------------------------------------------------
inline bool options_contain(const char* FlagList) const;
inline bool argument_contains(unsigned Idx, const char* FlagList) const;
// (*) variables -----------------------------------------------------------
// -- scalar values
inline int operator()(const char* VarName, int Default) const;
inline double operator()(const char* VarName, const double& Default) const;
inline const std::string operator()(const char* VarName, const char* Default) const;
// -- vectors
inline int operator()(const char* VarName, int Default, unsigned Idx) const;
inline double operator()(const char* VarName, const double& Default, unsigned Idx) const;
inline const std::string operator()(const char* VarName, const char* Default, unsigned Idx) const;
// -- setting variables
// i) from outside of GetPot (considering prefix etc.)
// ii) from inside, use '__set_variable()' below
inline void set(const char* VarName, const char* Value, const bool Requested = true);
inline void set(const char* VarName, const double& Value, const bool Requested = true);
inline void set(const char* VarName, const int Value, const bool Requested = true);
inline unsigned vector_variable_size(const char* VarName) const;
inline STRING_VECTOR get_variable_names() const;
inline STRING_VECTOR get_section_names() const;
// (*) cursor oriented functions -------------------------------------------
inline void set_prefix(const char* Prefix) { prefix = std::string(Prefix); }
inline bool search_failed() const { return search_failed_f; }
// -- enable/disable search for an option in loop
inline void disable_loop() { search_loop_f = false; }
inline void enable_loop() { search_loop_f = true; }
// -- reset cursor to position '1'
inline void reset_cursor();
inline void init_multiple_occurrence();
// -- search for a certain option and set cursor to position
inline bool search(const char* option);
inline bool search(unsigned No, const char* P, ...);
// -- get argument at cursor++
inline int next(int Default);
inline double next(const double& Default);
inline const std::string next(const char* Default);
// -- search for option and get argument at cursor++
inline int follow(int Default, const char* Option);
inline double follow(const double& Default, const char* Option);
inline const std::string follow(const char* Default, const char* Option);
// -- search for one of the given options and get argument that follows it
inline int follow(int Default, unsigned No, const char* Option, ...);
inline double follow(const double& Default, unsigned No, const char* Option, ...);
inline const std::string follow(const char* Default, unsigned No, const char* Option, ...);
// -- lists of nominuses following an option
inline std::vector<std::string> nominus_followers(const char* Option);
inline std::vector<std::string> nominus_followers(unsigned No, ...);
// -- directly followed arguments
inline int direct_follow(int Default, const char* Option);
inline double direct_follow(const double& Default, const char* Option);
inline const std::string direct_follow(const char* Default, const char* Option);
inline std::vector<std::string> string_tails(const char* StartString);
inline std::vector<int> int_tails(const char* StartString, const int Default = 1);
inline std::vector<double> double_tails(const char* StartString, const double Default = 1.0);
// (*) nominus arguments ---------------------------------------------------
inline STRING_VECTOR nominus_vector() const;
inline unsigned nominus_size() const { return static_cast<unsigned int>(idx_nominus.size()); }
inline std::string next_nominus();
// (*) unidentified flying objects -----------------------------------------
inline STRING_VECTOR unidentified_arguments(unsigned Number, const char* Known, ...) const;
inline STRING_VECTOR unidentified_arguments(const STRING_VECTOR& Knowns) const;
inline STRING_VECTOR unidentified_arguments() const;
inline STRING_VECTOR unidentified_options(unsigned Number, const char* Known, ...) const;
inline STRING_VECTOR unidentified_options(const STRING_VECTOR& Knowns) const;
inline STRING_VECTOR unidentified_options() const;
inline std::string unidentified_flags(const char* Known,
int ArgumentNumber /* =-1 */) const;
inline STRING_VECTOR unidentified_variables(unsigned Number, const char* Known, ...) const;
inline STRING_VECTOR unidentified_variables(const STRING_VECTOR& Knowns) const;
inline STRING_VECTOR unidentified_variables() const;
inline STRING_VECTOR unidentified_sections(unsigned Number, const char* Known, ...) const;
inline STRING_VECTOR unidentified_sections(const STRING_VECTOR& Knowns) const;
inline STRING_VECTOR unidentified_sections() const;
inline STRING_VECTOR unidentified_nominuses(unsigned Number, const char* Known, ...) const;
inline STRING_VECTOR unidentified_nominuses(const STRING_VECTOR& Knowns) const;
inline STRING_VECTOR unidentified_nominuses() const;
// (*) output --------------------------------------------------------------
inline int print() const;
private:
// (*) Type Declaration ----------------------------------------------------
struct variable {
//-----------
// Variable to be specified on the command line or in input files.
// (i.e. of the form var='12 312 341')
// -- constructors, destructors, assignment operator
variable();
variable(const variable&);
variable(const char* Name, const char* Value, const char* FieldSeparator);
~variable();
variable& operator=(const variable& That);
void take(const char* Value, const char* FieldSeparator);
// -- get a specific element in the string vector
// (return 0 if not present)
const std::string* get_element(unsigned Idx) const;
// -- data memebers
std::string name; // identifier of variable
STRING_VECTOR value; // value of variable stored in vector
std::string original; // value of variable as given on command line
};
// (*) member variables --------------------------------------------------------------
std::string prefix; // prefix automatically added in queries
std::string section; // (for dollar bracket parsing)
STRING_VECTOR section_list; // list of all parsed sections
// -- argument vector
STRING_VECTOR argv; // vector of command line arguments stored as strings
unsigned cursor; // cursor for argv
bool search_loop_f; // shall search start at beginning after
// // reaching end of arg array ?
bool search_failed_f; // flag indicating a failed search() operation
// // (e.g. next() functions react with 'missed')
// -- nominus vector
int nominus_cursor; // cursor for nominus_pointers
std::vector<unsigned> idx_nominus; // indecies of 'no minus' arguments
// -- variables
// (arguments of the form "variable=value")
std::vector<variable> variables;
// -- comment delimiters
std::string _comment_start;
std::string _comment_end;
// -- field separator (separating elements of a vector)
std::string _field_separator;
// -- some functions return a char pointer to a temporarily existing string
// this container makes them 'available' until the getpot object is destroyed.
std::vector<char*> __internal_string_container;
// -- keeping track about arguments that are requested, so that the UFO detection
// can be simplified
STRING_VECTOR _requested_arguments;
STRING_VECTOR _requested_variables;
STRING_VECTOR _requested_sections;
bool __request_recording_f; // speed: request recording can be turned off
// -- if an argument is requested record it and the 'tag' the section branch to which
// it belongs. Caution: both functions mark the sections as 'tagged'.
void __record_argument_request(const std::string& Arg);
void __record_variable_request(const std::string& Arg);
// (*) helper functions ----------------------------------------------------
// set variable from inside GetPot (no prefix considered)
inline void __set_variable(const char* VarName, const char* Value);
// -- produce three basic data vectors:
// - argument vector
// - nominus vector
// - variable dictionary
inline void __parse_argument_vector(const STRING_VECTOR& ARGV);
// -- helpers for argument list processing
// * search for a variable in 'variables' array
inline const variable* __find_variable(const char*) const;
// * support finding directly followed arguments
inline const char* __match_starting_string(const char* StartString);
// * support search for flags in a specific argument
inline bool __check_flags(const std::string& Str, const char* FlagList) const;
// * type conversion if possible
inline int __convert_to_type(const std::string& String, int Default) const;
inline double __convert_to_type(const std::string& String, double Default) const;
// * prefix extraction
const std::string __get_remaining_string(const std::string& String,
const std::string& Start) const;
// * search for a specific string
inline bool __search_string_vector(const STRING_VECTOR& Vec,
const std::string& Str) const;
// -- helpers to parse input file
// create an argument vector based on data found in an input file, i.e.:
// 1) delete comments (in between '_comment_start' '_comment_end')
// 2) contract assignment expressions, such as
// my-variable = '007 J. B.'
// into
// my-variable='007 J. B.'
// 3) interprete sections like '[../my-section]' etc.
inline void __skip_whitespace(std::istream& istr);
inline const std::string __get_next_token(std::istream& istr);
inline const std::string __get_string(std::istream& istr);
inline const std::string __get_until_closing_bracket(std::istream& istr);
inline STRING_VECTOR __read_in_stream(std::istream& istr);
inline STRING_VECTOR __read_in_file(const char* FileName);
inline std::string __process_section_label(const std::string& Section,
STRING_VECTOR& section_stack);
// -- dollar bracket expressions
std::string __DBE_expand_string(const std::string str);
std::string __DBE_expand(const std::string str);
const GetPot::variable* __DBE_get_variable(const std::string str);
STRING_VECTOR __DBE_get_expr_list(const std::string str, const unsigned ExpectedNumber);
std::string __double2string(const double& Value) const {
// -- converts a double integer into a string
char* tmp = new char[128];
#ifndef WIN32
snprintf(tmp, (int)sizeof(char)*128, "%e", Value);
#else
_snprintf(tmp, sizeof(char)*128, "%e", Value);
#endif
std::string result(tmp);
delete [] tmp;
return result;
}
std::string __int2string(const int& Value) const {
// -- converts an integer into a string
char* tmp = new char[128];
#ifndef WIN32
snprintf(tmp, (int)sizeof(char)*128, "%i", Value);
#else
_snprintf(tmp, sizeof(char)*128, "%i", Value);
#endif
std::string result(tmp);
delete [] tmp;
return result;
}
STRING_VECTOR __get_section_tree(const std::string& FullPath) {
// -- cuts a variable name into a tree of sub-sections. this is requested for recording
// requested sections when dealing with 'ufo' detection.
STRING_VECTOR result;
const char* Start = FullPath.c_str();
for(char *p = (char*)Start; *p ; p++) {
if( *p == '/' ) {
*p = '\0'; // set terminating zero for convinience
const std::string Section = Start;
*p = '/'; // reset slash at place
result.push_back(Section);
}
}
return result;
}
};
///////////////////////////////////////////////////////////////////////////////
// (*) constructors, destructor, assignment operator
//.............................................................................
//
inline void
GetPot::__basic_initialization()
{
cursor = 0; nominus_cursor = -1;
search_failed_f = true; search_loop_f = true;
prefix = ""; section = "";
// automatic request recording for later ufo detection
__request_recording_f = true;
// comment start and end strings
_comment_start = std::string("#");
_comment_end = std::string("\n");
// default: separate vector elements by whitespaces
_field_separator = " \t\n";
}
inline
GetPot::GetPot()
{
__basic_initialization();
STRING_VECTOR _apriori_argv;
_apriori_argv.push_back(std::string("Empty"));
__parse_argument_vector(_apriori_argv);
}
inline
GetPot::GetPot(const int argc_, char ** argv_,
const char* FieldSeparator /* =0x0 */)
// leave 'char**' non-const to honor less capable compilers ...
{
// TODO: Ponder over the problem when the argument list is of size = 0.
// This is 'sabotage', but it can still occur if the user specifies
// it himself.
assert(argc_ >= 1);
__basic_initialization();
// if specified -> overwrite default string
if( FieldSeparator ) _field_separator = std::string(FieldSeparator);
// -- make an internal copy of the argument list:
STRING_VECTOR _apriori_argv;
// -- for the sake of clarity: we do want to include the first argument in the argument vector !
// it will not be a nominus argument, though. This gives us a minimun vector size of one
// which facilitates error checking in many functions. Also the user will be able to
// retrieve the name of his application by "get[0]"
_apriori_argv.push_back(std::string(argv_[0]));
int i=1;
for(; i<argc_; ++i) {
std::string tmp(argv_[i]); // recall the problem with temporaries,
_apriori_argv.push_back(tmp); // reference counting in arguement lists ...
}
__parse_argument_vector(_apriori_argv);
}
inline
GetPot::GetPot(const char* FileName,
const char* CommentStart /* = 0x0 */, const char* CommentEnd /* = 0x0 */,
const char* FieldSeparator/* = 0x0 */)
{
__basic_initialization();
// if specified -> overwrite default strings
if( CommentStart ) _comment_start = std::string(CommentStart);
if( CommentEnd ) _comment_end = std::string(CommentEnd);
if( FieldSeparator ) _field_separator = FieldSeparator;
STRING_VECTOR _apriori_argv;
// -- file name is element of argument vector, however, it is not parsed for
// variable assignments or nominuses.
_apriori_argv.push_back(std::string(FileName));
STRING_VECTOR args = __read_in_file(FileName);
_apriori_argv.insert(_apriori_argv.begin()+1, args.begin(), args.end());
__parse_argument_vector(_apriori_argv);
}
inline
GetPot::GetPot(const GetPot& That)
{ GetPot::operator=(That); }
inline
GetPot::~GetPot()
{
// may be some return strings had to be created, delete now !
victorate(char*, __internal_string_container, it)
delete [] *it;
}
inline GetPot&
GetPot::operator=(const GetPot& That)
{
if (&That == this) return *this;
_comment_start = That._comment_start;
_comment_end = That._comment_end;
argv = That.argv;
variables = That.variables;
prefix = That.prefix;
cursor = That.cursor;
nominus_cursor = That.nominus_cursor;
search_failed_f = That.search_failed_f;
idx_nominus = That.idx_nominus;
search_loop_f = That.search_loop_f;
return *this;
}
inline void
GetPot::absorb(const GetPot& That)
{
if (&That == this) return;
STRING_VECTOR __tmp(That.argv);
__tmp.erase(__tmp.begin());
__parse_argument_vector(__tmp);
}
inline void
GetPot::clear_requests()
{
_requested_arguments.erase(_requested_arguments.begin(), _requested_arguments.end());
_requested_variables.erase(_requested_variables.begin(), _requested_variables.end());
_requested_sections.erase(_requested_sections.begin(), _requested_sections.end());
}
inline void
GetPot::__parse_argument_vector(const STRING_VECTOR& ARGV)
{
if( ARGV.size() == 0 ) return;
// build internal databases:
// 1) array with no-minus arguments (usually used as filenames)
// 2) variable assignments:
// 'variable name' '=' number | string
STRING_VECTOR section_stack;
STRING_VECTOR::const_iterator it = ARGV.begin();
section = "";
// -- do not parse the first argument, so that it is not interpreted a s a nominus or so.
argv.push_back(*it);
++it;
// -- loop over remaining arguments
unsigned i=1;
for(; it != ARGV.end(); ++it, ++i) {
std::string arg = *it;
if( arg.length() == 0 ) continue;
// -- [section] labels
if( arg.length() > 1 && arg[0] == '[' && arg[arg.length()-1] == ']' ) {
// (*) sections are considered 'requested arguments'
if( __request_recording_f ) _requested_arguments.push_back(arg);
const std::string Name = __DBE_expand_string(arg.substr(1, arg.length()-2));
section = __process_section_label(Name, section_stack);
// new section --> append to list of sections
if( find(section_list.begin(), section_list.end(), section) == section_list.end() )
if( section.length() != 0 ) section_list.push_back(section);
argv.push_back(arg);
}
else {
arg = section + __DBE_expand_string(arg);
argv.push_back(arg);
}
// -- separate array for nominus arguments
if( arg[0] != '-' ) idx_nominus.push_back(unsigned(i));
// -- variables: does arg contain a '=' operator ?
const char* p = arg.c_str();
for(; *p ; p++) {
if( *p == '=' ) {
// (*) record for later ufo detection
// arguments carriying variables are always treated as 'requested' arguments.
// as a whole! That is 'x=4712' is considered a requested argument.
//
// unrequested variables have to be detected with the ufo-variable
// detection routine.
if( __request_recording_f ) _requested_arguments.push_back(arg);
// set terminating 'zero' to treat first part as single string
// => arg (from start to 'p') = Name of variable
// p+1 (until terminating zero) = value of variable
char* o = (char*)p++;
*o = '\0'; // set temporary terminating zero
// __set_variable(...)
// calls __find_variable(...) which registers the search
// temporarily disable this
const bool tmp = __request_recording_f;
__request_recording_f = false;
__set_variable(arg.c_str(), p); // v-name = c_str() bis 'p', value = rest
__request_recording_f = tmp;
*o = '='; // reset the original '='
break;
}
}
}
}
inline STRING_VECTOR
GetPot::__read_in_file(const char* FileName)
{
std::ifstream i(FileName);
if( ! i ) return STRING_VECTOR();
// argv[0] == the filename of the file that was read in
return __read_in_stream(i);
}
inline STRING_VECTOR
GetPot::__read_in_stream(std::istream& istr)
{
STRING_VECTOR brute_tokens;
while(istr) {
__skip_whitespace(istr);
const std::string Token = __get_next_token(istr);
if( Token.length() == 0 || Token[0] == EOF) break;
brute_tokens.push_back(Token);
}
// -- reduce expressions of token1'='token2 to a single
// string 'token1=token2'
// -- copy everything into 'argv'
// -- arguments preceded by something like '[' name ']' (section)
// produce a second copy of each argument with a prefix '[name]argument'
unsigned i1 = 0;
unsigned i2 = 1;
unsigned i3 = 2;
STRING_VECTOR arglist;
while( i1 < brute_tokens.size() ) {
const std::string& SRef = brute_tokens[i1];
// 1) concatinate 'abcdef' '=' 'efgasdef' to 'abcdef=efgasdef'
// note: java.lang.String: substring(a,b) = from a to b-1
// C++ string: substr(a,b) = from a to a + b
if( i2 < brute_tokens.size() && brute_tokens[i2] == "=" ) {
if( i3 >= brute_tokens.size() )
arglist.push_back(brute_tokens[i1] + brute_tokens[i2]);
else
arglist.push_back(brute_tokens[i1] + brute_tokens[i2] + brute_tokens[i3]);
i1 = i3+1; i2 = i3+2; i3 = i3+3;
continue;
}
else {
arglist.push_back(SRef);
i1=i2; i2=i3; i3++;
}
}
return arglist;
}
inline void
GetPot::__skip_whitespace(std::istream& istr)
// find next non-whitespace while deleting comments
{
int tmp = istr.get();
do {
// -- search a non whitespace
while( isspace(tmp) ) {
tmp = istr.get();
if( ! istr ) return;
}
// -- look if characters match the comment starter string
unsigned i=0;
for(; i<_comment_start.length() ; ++i) {
if( tmp != _comment_start[i] ) {
// NOTE: Due to a 'strange behavior' in Microsoft's streaming lib we do
// a series of unget()s instead a quick seek. See
// http://sourceforge.net/tracker/index.php?func=detail&aid=1545239&group_id=31994&atid=403915
// for a detailed discussion.
// -- one step more backwards, since 'tmp' already at non-whitespace
do istr.unget(); while( i-- != 0 );
return;
}
tmp = istr.get();
if( ! istr ) { istr.unget(); return; }
}
// 'tmp' contains last character of _comment_starter
// -- comment starter found -> search for comment ender
unsigned match_no=0;
while(1+1 == 2) {
tmp = istr.get();
if( ! istr ) { istr.unget(); return; }
if( tmp == _comment_end[match_no] ) {
match_no++;
if( match_no == _comment_end.length() ) {
istr.unget();
break; // shuffle more whitespace, end of comment found
}
}
else
match_no = 0;
}
tmp = istr.get();
} while( istr );
istr.unget();
}
inline const std::string
GetPot::__get_next_token(std::istream& istr)
// get next concatinates string token. consider quotes that embrace
// whitespaces
{
std::string token;
int tmp = 0;
int last_letter = 0;
while(1+1 == 2) {
last_letter = tmp; tmp = istr.get();
if( tmp == EOF
|| ((tmp == ' ' || tmp == '\t' || tmp == '\n') && last_letter != '\\') ) {
return token;
}
else if( tmp == '\'' && last_letter != '\\' ) {
// QUOTES: un-backslashed quotes => it's a string
token += __get_string(istr);
continue;
}
else if( tmp == '{' && last_letter == '$') {
token += '{' + __get_until_closing_bracket(istr);
continue;
}
else if( tmp == '$' && last_letter == '\\') {
token += tmp; tmp = 0; // so that last_letter will become = 0, not '$';
continue;
}
else if( tmp == '\\' && last_letter != '\\')
continue; // don't append un-backslashed backslashes
token += tmp;
}
}
inline const std::string
GetPot::__get_string(std::istream& istr)
// parse input until next matching '
{
std::string str;
int tmp = 0;
int last_letter = 0;
while(1 + 1 == 2) {
last_letter = tmp; tmp = istr.get();
if( tmp == EOF) return str;
// un-backslashed quotes => it's the end of the string
else if( tmp == '\'' && last_letter != '\\') return str;
else if( tmp == '\\' && last_letter != '\\') continue; // don't append
str += tmp;
}
}
inline const std::string
GetPot::__get_until_closing_bracket(std::istream& istr)
// parse input until next matching }
{
std::string str = "";
int tmp = 0;
int last_letter = 0;
int brackets = 1;
while(1 + 1 == 2) {
last_letter = tmp; tmp = istr.get();
if( tmp == EOF) return str;
else if( tmp == '{' && last_letter == '$') brackets += 1;
else if( tmp == '}') {
brackets -= 1;
// un-backslashed brackets => it's the end of the string
if( brackets == 0) return str + '}';
else if( tmp == '\\' && last_letter != '\\')
continue; // do not append an unbackslashed backslash
}
str += tmp;
}
}
inline std::string
GetPot::__process_section_label(const std::string& Section,
STRING_VECTOR& section_stack)
{
std::string sname = Section;
// 1) subsection of actual section ('./' prefix)
if( sname.length() >= 2 && sname.substr(0, 2) == "./" ) {
sname = sname.substr(2);
}
// 2) subsection of parent section ('../' prefix)
else if( sname.length() >= 3 && sname.substr(0, 3) == "../" ) {
do {
if( section_stack.end() != section_stack.begin() )
section_stack.pop_back();
sname = sname.substr(3);
} while( sname.substr(0, 3) == "../" );
}
// 3) subsection of the root-section
else {
section_stack.erase(section_stack.begin(), section_stack.end());
// [] => back to root section
}
if( sname != "" ) {
// parse section name for 'slashes'
unsigned i=0;
while( i < sname.length() ) {
if( sname[i] == '/' ) {
section_stack.push_back(sname.substr(0,i));
if( i+1 < sname.length() )
sname = sname.substr(i+1);
i = 0;
}
else
++i;
}
section_stack.push_back(sname);
}
std::string section = "";
if( section_stack.size() != 0 ) {
victorate(std::string, section_stack, it)
section += *it + "/";
}
return section;
}
// convert string to DOUBLE, if not possible return Default
inline double
GetPot::__convert_to_type(const std::string& String, double Default) const
{
double tmp;
if( sscanf(String.c_str(),"%lf", &tmp) != 1 ) return Default;
return tmp;
}
// convert string to INT, if not possible return Default
inline int
GetPot::__convert_to_type(const std::string& String, int Default) const
{
// NOTE: intermediate results may be floating points, so that the string
// may look like 2.0e1 (i.e. float format) => use float conversion
// in any case.
return (int)__convert_to_type(String, (double)Default);
}
//////////////////////////////////////////////////////////////////////////////
// (*) cursor oriented functions
//.............................................................................
inline const std::string
GetPot::__get_remaining_string(const std::string& String, const std::string& Start) const
// Checks if 'String' begins with 'Start' and returns the remaining String.
// Returns None if String does not begin with Start.
{
if( Start == "" ) return String;
// note: java.lang.String: substring(a,b) = from a to b-1
// C++ string: substr(a,b) = from a to a + b
if( String.find(Start) == 0 ) return String.substr(Start.length());
else return "";
}
// -- search for a certain argument and set cursor to position
inline bool
GetPot::search(const char* Option)
{
unsigned OldCursor = cursor;
const std::string SearchTerm = prefix + Option;
// (*) record requested arguments for later ufo detection
__record_argument_request(SearchTerm);
if( OldCursor >= argv.size() ) OldCursor = static_cast<unsigned int>(argv.size()) - 1;
search_failed_f = true;
// (*) first loop from cursor position until end
unsigned c = cursor;
for(; c < argv.size(); c++) {
if( argv[c] == SearchTerm )
{ cursor = c; search_failed_f = false; return true; }
}
if( ! search_loop_f ) return false;
// (*) second loop from 0 to old cursor position
for(c = 1; c < OldCursor; c++) {
if( argv[c] == SearchTerm )
{ cursor = c; search_failed_f = false; return true; }
}
// in case nothing is found the cursor stays where it was
return false;
}
inline bool
GetPot::search(unsigned No, const char* P, ...)
{
// (*) recording the requested arguments happens in subroutine 'search'
if( No == 0 ) return false;
// search for the first argument
if( search(P) == true ) return true;
// start interpreting variable argument list
va_list ap;
va_start(ap, P);
unsigned i = 1;
for(; i < No; ++i) {
char* Opt = va_arg(ap, char *);
if( search(Opt) == true ) break;
}
if( i < No ) {
++i;
// loop was left before end of array --> hit but
// make sure that the rest of the search terms is marked
// as requested.
for(; i < No; ++i) {
char* Opt = va_arg(ap, char *);
// (*) record requested arguments for later ufo detection
__record_argument_request(Opt);
}
va_end(ap);
return true;
}
va_end(ap);
// loop was left normally --> no hit
return false;
}
inline void
GetPot::reset_cursor()
{ search_failed_f = false; cursor = 0; }
inline void
GetPot::init_multiple_occurrence()
{ disable_loop(); reset_cursor(); }
///////////////////////////////////////////////////////////////////////////////
// (*) direct access to command line arguments
//.............................................................................
//
inline const std::string
GetPot::operator[](unsigned idx) const
{ return idx < argv.size() ? argv[idx] : ""; }
inline int
GetPot::get(unsigned Idx, int Default) const
{
if( Idx >= argv.size() ) return Default;
return __convert_to_type(argv[Idx], Default);
}
inline double
GetPot::get(unsigned Idx, const double& Default) const
{
if( Idx >= argv.size() ) return Default;
return __convert_to_type(argv[Idx], Default);
}
inline const std::string
GetPot::get(unsigned Idx, const char* Default) const
{
if( Idx >= argv.size() ) return Default;
else return argv[Idx];
}
inline unsigned
GetPot::size() const
{ return static_cast<unsigned int>(argv.size()); }
// -- next() function group
inline int
GetPot::next(int Default)
{
if( search_failed_f ) return Default;
cursor++;
if( cursor >= argv.size() )
{ cursor = static_cast<unsigned int>(argv.size()); return Default; }
// (*) record requested argument for later ufo detection
__record_argument_request(argv[cursor]);
const std::string Remain = __get_remaining_string(argv[cursor], prefix);
return Remain != "" ? __convert_to_type(Remain, Default) : Default;
}
inline double
GetPot::next(const double& Default)
{
if( search_failed_f ) return Default;
cursor++;
if( cursor >= argv.size() )
{ cursor = static_cast<unsigned int>(argv.size()); return Default; }
// (*) record requested argument for later ufo detection
__record_argument_request(argv[cursor]);
std::string Remain = __get_remaining_string(argv[cursor], prefix);
return Remain != "" ? __convert_to_type(Remain, Default) : Default;
}
inline const std::string
GetPot::next(const char* Default)
{
using namespace std;
if( search_failed_f ) return Default;
cursor++;
if( cursor >= argv.size() )
{ cursor = static_cast<unsigned int>(argv.size()); return Default; }
// (*) record requested argument for later ufo detection
__record_argument_request(argv[cursor]);
const std::string Remain = __get_remaining_string(argv[cursor], prefix);
if( Remain == "" ) return Default;
// (*) function returns a pointer to a char array (inside Remain)
// this array will be deleted, though after this function call.
// To ensure propper functioning, create a copy inside *this
// object and only delete it, when *this is deleted.