-
Notifications
You must be signed in to change notification settings - Fork 2
/
GetPot.cpp
2532 lines (2163 loc) · 85.2 KB
/
GetPot.cpp
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
// SPDX license identifier: MIT; Project 'GetPot'
// MIT License
//
// Copyright (C) 2001-2016 Frank-Rene Schaefer.
//
// While not directly linked to license requirements, the author would like
// this software not to be used for military purposes.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//=============================================================================
#ifndef __INCLUDE_GUARD_GETPOT_CPP__
#define __INCLUDE_GUARD_GETPOT_CPP__
#if defined(WIN32) || defined(SOLARIS_RAW) || (__GNUC__ == 2) || defined(__HP_aCC)
// WINDOWS or SOLARIS or gcc 2.* or HP aCC
# define GETPOT_STRTOK(a, b, c) strtok_r(a, b, c)
# define GETPOT_STRTOK_3_ARGS
#else
# define GETPOT_STRTOK(a, b, c) strtok(a, b)
#endif
#if defined(WIN32)
# define GETPOT_SNPRINTF(STR, SIZE, FORMAT_STR, ...) \
_snprintf(tmp, (size_t)SIZE, FORMAT_STR, __VA_ARGS__)
#else
# define GETPOT_SNPRINTF(STR, SIZE, FORMAT_STR, ...) \
snprintf(tmp, (int)SIZE, FORMAT_STR, __VA_ARGS__)
#endif
/* In case that a single header is intended, __GETPOT_INLINE is set to 'inline'
* otherwise, it is set to 'empty', so that normal object code is produced. */
#ifndef __GETPOT_INLINE
# define __GETPOT_INLINE /*empty*/
#endif
/* The default values for strings that represent 'true' and 'false' */
#include "GetPot.hpp"
#ifdef GETPOT_SETTING_NAMESPACE
namespace GETPOT_SETTING_NAMESPACE {
#endif
#define victorate(TYPE, VARIABLE, ITERATOR) \
for(std::vector<TYPE >::const_iterator ITERATOR = (VARIABLE).begin(); \
(ITERATOR) != (VARIABLE).end(); ++(ITERATOR))
template <class T> class TypeInfo;
template <> class TypeInfo<bool> {
public: static const char* name() { return "bool"; }
static bool default_value() { return false; }
typedef bool type;
static bool convert(bool& X) { return (bool)X; }
};
template <> class TypeInfo<const char*> {
public: static const char* name() { return "string"; }
static const char * default_value() { return ""; }
typedef const char* type;
static const char* convert(const char* X) { return (const char*)X; }
};
template <> class TypeInfo<int> {
public: static const char* name() { return "integer"; }
static int default_value() { return 0; }
typedef int type;
static int convert(int& X) { return (int)X; }
};
template <> class TypeInfo<std::string> : public TypeInfo<const char*>
{ public:
static const char* convert(std::string& X) { return (const char*)"inadmissible"; }
static std::string default_value() { return std::string(""); }
};
template <> class TypeInfo<unsigned int> : public TypeInfo<int>
{ public: static int convert(unsigned int& X) { return (int)X; } };
template <> class TypeInfo<char> : public TypeInfo<int>
{ public: static int convert(char& X) { return (int)X; } };
template <> class TypeInfo<unsigned char> : public TypeInfo<int>
{ public: static int convert(unsigned char& X) { return (int)X; } };
template <> class TypeInfo<short> : public TypeInfo<int>
{ public: static int convert(short& X) { return (int)X; } };
template <> class TypeInfo<unsigned short> : public TypeInfo<int>
{ public: static int convert(unsigned short& X) { return (int)X; } };
template <> class TypeInfo<long> : public TypeInfo<int>
{ public: static int convert(long& X) { return (int)X; } };
template <> class TypeInfo<unsigned long> : public TypeInfo<int>
{ public: static int convert(unsigned long& X) { return (int)X; } };
template <> class TypeInfo<double> {
public: static const char* name() { return "double"; }
static double default_value() { return 0.; }
typedef double type;
static double convert(double& X) { return (double)X; }
};
template <> class TypeInfo<float> : public TypeInfo<double>
{ public: static double convert(float& X) { return (double)X; } };
///////////////////////////////////////////////////////////////////////////////
// (*) constructors, destructor, assignment operator
//.............................................................................
//
__GETPOT_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";
// true and false strings
__split(GETPOT_SETTING_DEFAULT_TRUE_STRING_LIST, true_string_list);
__split(GETPOT_SETTING_DEFAULT_FALSE_STRING_LIST, false_string_list);
}
__GETPOT_INLINE
GetPot::GetPot():
built(false)
{
__basic_initialization();
STRING_VECTOR _apriori_argv;
_apriori_argv.push_back(std::string("Empty"));
__parse_argument_vector(_apriori_argv);
built = true;
}
__GETPOT_INLINE
GetPot::GetPot(const int argc_, char ** argv_,
const StringOrCharP FieldSeparator /* =0x0 */):
built(false)
// 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.content.length() != 0 ) _field_separator = FieldSeparator.content;
// -- 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);
built = true;
}
__GETPOT_INLINE
GetPot::GetPot(StringOrCharP FileName,
const StringOrCharP CommentStart /* = 0x0 */,
const StringOrCharP CommentEnd /* = 0x0 */,
const StringOrCharP FieldSeparator/* = 0x0 */):
built(false)
{
__build(FileName.content, CommentStart.content, CommentEnd.content, FieldSeparator.content);
built = true;
}
__GETPOT_INLINE
GetPot::GetPot(const GetPot& That)
{ GetPot::operator=(That); }
__GETPOT_INLINE
GetPot::~GetPot()
{
// may be some return strings had to be created, delete now !
victorate(std::vector<char>*, __internal_string_container, it) {
delete *it;
}
}
__GETPOT_INLINE GetPot&
GetPot::operator=(const GetPot& That)
{
if (&That == this) return *this;
built = That.built;
filename = That.filename;
true_string_list = That.true_string_list;
false_string_list= That.false_string_list;
_comment_start = That._comment_start;
_comment_end = That._comment_end;
_field_separator= That. _field_separator;
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;
}
__GETPOT_INLINE void
GetPot::absorb(const GetPot& That)
{
if (&That == this) return;
STRING_VECTOR __tmp(That.argv);
__tmp.erase(__tmp.begin());
__parse_argument_vector(__tmp);
}
__GETPOT_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());
}
__GETPOT_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 ?
std::ptrdiff_t i = 0;
for(std::string::iterator it = arg.begin(); it != arg.end(); ++it, ++i) {
if( *it != '=' ) continue;
// (*) 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);
const bool tmp = __request_recording_f;
__request_recording_f = false;
__set_variable(arg.substr(0, i), arg.substr(i+1));
__request_recording_f = tmp;
break;
}
}
}
__GETPOT_INLINE STRING_VECTOR
GetPot::__read_in_file(const std::string& FileName)
{
std::ifstream i(FileName.c_str());
if( ! i ) {
throw std::runtime_error(GETPOT_STR_FILE_NOT_FOUND(FileName));
return STRING_VECTOR();
}
// argv[0] == the filename of the file that was read in
return __read_in_stream(i);
}
__GETPOT_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;
}
__GETPOT_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();
}
__GETPOT_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;
}
}
__GETPOT_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;
}
}
__GETPOT_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;
}
}
__GETPOT_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'
size_t i = 0;
size_t prev_slash_i = -1; // points to last slash
const size_t L = sname.length();
for(i = 0; i < L; ++i) {
if( sname[i] != '/' ) continue;
if( i - prev_slash_i > 1 ) {
const size_t Delta = i - (prev_slash_i + 1);
section_stack.push_back(sname.substr(prev_slash_i + 1, Delta));
}
prev_slash_i = i;
}
if( i - prev_slash_i > 1 ) {
const size_t Delta = i - (prev_slash_i + 1);
section_stack.push_back(sname.substr(prev_slash_i + 1, Delta));
}
}
std::string section = "";
if( section_stack.size() != 0 ) {
victorate(std::string, section_stack, it) {
section += *it + "/";
}
}
return section;
}
// convert string to BOOL, if not possible return Default
template <> __GETPOT_INLINE bool
GetPot::__convert_to_type<bool>(const std::string& String, bool Default,
bool ThrowExceptionF /* = GETPOT_SETTING_THROW_EXCEPTION_ON_DEFAULT */) const
{
// No 'lower' or 'upper' unification. If someone wants that, he
// han specify the true-string or fals-strings as he likes.
victorate(std::string, true_string_list, it1) {
if( String == *it1 ) return true;
}
victorate(std::string, false_string_list, it2) {
if( String == *it2 ) return false;
}
if( ThrowExceptionF ) {
throw std::runtime_error(std::string(GETPOT_STR_FILE) + " \"" + filename + "\": " +
GETPOT_STR_CANNOT_CONVERT_TO(String, TypeInfo<bool>::name()));
}
else
return Default;
}
// convert string to DOUBLE, if not possible return Default
template <> __GETPOT_INLINE double
GetPot::__convert_to_type<double>(const std::string& String, double Default,
bool ThrowExceptionF /* = GETPOT_SETTING_THROW_EXCEPTION_ON_DEFAULT */) const
{
double tmp;
if( sscanf(String.c_str(),"%lf", &tmp) == 1 ) return tmp;
if( ThrowExceptionF )
throw std::runtime_error(std::string(GETPOT_STR_FILE) + " \"" + filename + "\": " +
GETPOT_STR_CANNOT_CONVERT_TO(String, TypeInfo<double>::name()));
else
return Default;
}
// convert string to INT, if not possible return Default
template <> __GETPOT_INLINE int
GetPot::__convert_to_type<int>(const std::string& String, int Default,
bool ThrowExceptionF /* = GETPOT_SETTING_THROW_EXCEPTION_ON_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, ThrowExceptionF);
}
// convert string to string, so that GetPot::read method can be generic (template).
template <> __GETPOT_INLINE const char*
GetPot::__convert_to_type<const char*>(const std::string& String, const char* Default,
bool ThrowExceptionF /* = GETPOT_SETTING_THROW_EXCEPTION_ON_DEFAULT */) const
{
return __get_const_char(String);
}
__GETPOT_INLINE const char*
GetPot::__get_const_char(const std::string& String) const
{
std::vector<char>* c_str_copy = new std::vector<char>(String.length() + 1, '\0');
std::copy(String.begin(), String.end(), c_str_copy->begin());
((GetPot*)this)->__internal_string_container.push_back(c_str_copy);
return &(*c_str_copy)[0];
}
//////////////////////////////////////////////////////////////////////////////
// (*) cursor oriented functions
//.............................................................................
__GETPOT_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
__GETPOT_INLINE bool
GetPot::search(const char* Option)
{
unsigned OldCursor = cursor;
std::string search_term;
if( ! Option ) return false;
search_term = prefix + Option;
// (*) record requested arguments for later ufo detection
__record_argument_request(search_term);
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] == search_term )
{ 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] == search_term )
{ cursor = c; search_failed_f = false; return true; }
}
// in case nothing is found the cursor stays where it was
return false;
}
__GETPOT_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;
}
__GETPOT_INLINE void
GetPot::reset_cursor()
{ search_failed_f = false; cursor = 0; }
__GETPOT_INLINE void
GetPot::init_multiple_occurrence()
{ disable_loop(); reset_cursor(); }
__GETPOT_INLINE void
GetPot::set_true_string_list(unsigned N, const char* StringForTrue, ...)
{
true_string_list.clear();
if( N == 0 ) return;
va_list ap;
unsigned i = 1;
va_start(ap, StringForTrue);
for(; i < N ; ++i) {
char* Opt = va_arg(ap, char *);
true_string_list.push_back(std::string(Opt));
}
va_end(ap);
}
__GETPOT_INLINE void
GetPot::set_false_string_list(unsigned N, const char* StringForFalse, ...)
{
false_string_list.clear();
if( N == 0 ) return;
va_list ap;
unsigned i = 1;
va_start(ap, StringForFalse);
for(; i < N ; ++i) {
char* Opt = va_arg(ap, char *);
false_string_list.push_back(std::string(Opt));
}
va_end(ap);
}
///////////////////////////////////////////////////////////////////////////////
// (*) direct access to command line arguments
//
__GETPOT_INLINE const std::string
GetPot::operator[](unsigned idx) const
{ return idx < argv.size() ? argv[idx] : ""; }
template <class T> __GETPOT_INLINE T
GetPot::get(unsigned Idx, T Default) const
{
if( Idx >= argv.size() ) return Default;
return __convert_to_type(argv[Idx], (typename TypeInfo<T>::type)Default);
}
template <> __GETPOT_INLINE const char*
GetPot::get<const char*>(unsigned Idx, const char* Default) const
{
if( Idx >= argv.size() ) return Default;
else return __get_const_char(argv[Idx]);
}
template <class T> __GETPOT_INLINE T
GetPot::get(const StringOrCharP VarName)
{
return get<T>(VarName, (const char*)0x0);
}
template <class T> __GETPOT_INLINE T
GetPot::get(const StringOrCharP VarName, const char* Constraint)
{
return __get<T>(VarName, Constraint, /* ThrowExceptionF = */true);
}
template <class T> __GETPOT_INLINE T
GetPot::get(const StringOrCharP VarName, const char* Constraint, T Default)
{
return __get<T>(VarName, Constraint, Default, /* ThrowExceptionF = */GETPOT_SETTING_THROW_EXCEPTION_ON_DEFAULT);
}
__GETPOT_INLINE unsigned
GetPot::size() const
{ return static_cast<unsigned int>(argv.size()); }
// -- next() function group
template <class T> __GETPOT_INLINE T
GetPot::next(T 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, (typename TypeInfo<T>::type)Default) : Default;
}
// -- follow() function group
// distinct option to be searched for
template <class T> __GETPOT_INLINE T
GetPot::follow(T Default, const char* Option)
{
// (*) record requested of argument is entirely handled in 'search()' and 'next()'
if( search(Option) == false ) return Default;
return next(Default);
}
// -- second follow() function group
// multiple option to be searched for
template <class T> __GETPOT_INLINE T
GetPot::follow(T Default, unsigned No, const char* P, ...)
{
// (*) record requested of argument is entirely handled in 'search()' and 'next()'
if( No == 0 ) return Default;
if( search(P) == true ) return next(Default);
va_list ap;
va_start(ap, P);
unsigned i=1;
for(; i<No; ++i) {
char* Opt = va_arg(ap, char *);
if( search(Opt) == true ) {
va_end(ap);
return next(Default);
}
}
va_end(ap);
return Default;
}
///////////////////////////////////////////////////////////////////////////////
// (*) directly connected options
//.............................................................................
//
template <class T> __GETPOT_INLINE T
GetPot::direct_follow(T Default, const char* Option)
{
const char* FollowStr = __match_starting_string(Option);
if( FollowStr == 0x0 ) return Default;
// (*) record requested of argument for later ufo-detection
__record_argument_request(std::string(Option) + FollowStr);
if( ++cursor >= static_cast<unsigned int>(argv.size()) ) cursor = static_cast<unsigned int>(argv.size());
return __convert_to_type(FollowStr, (typename TypeInfo<T>::type)Default);
}
__GETPOT_INLINE std::vector<std::string>
GetPot::string_tails(const char* StartString)
{
std::vector<std::string> result;
const unsigned N = static_cast<unsigned int>(strlen(StartString));
std::vector<std::string>::iterator it = argv.begin();
unsigned idx = 0;
while( it != argv.end() ) {
// (*) does start string match the given option?
// NO -> goto next option
if( (*it).compare(0, N, StartString) != 0 ) { ++it; ++idx; continue; }
// append the found tail to the result vector
result.push_back((*it).substr(N));
// adapt the nominus vector
std::vector<unsigned>::iterator nit = idx_nominus.begin();
for(; nit != idx_nominus.end(); ++nit) {
if( *nit == idx ) {
idx_nominus.erase(nit);
for(; nit != idx_nominus.end(); ++nit) *nit -= 1;
break;
}
}
// erase the found option
argv.erase(it);
// 100% safe solution: set iterator back to the beginning.
// (normally, 'it--' would be enough, but who knows how the
// iterator is implemented and .erase() definitely invalidates
// the current iterator position.
if( argv.empty() ) break;
it = argv.begin();
}
cursor = 0;
nominus_cursor = -1;
return result;
}
__GETPOT_INLINE std::vector<int>
GetPot::int_tails(const char* StartString, const int Default /* = -1 */)
{
std::vector<int> result;
const unsigned N = static_cast<unsigned int>(strlen(StartString));
std::vector<std::string>::iterator it = argv.begin();
unsigned idx = 0;
while( it != argv.end() ) {
// (*) does start string match the given option?
// NO -> goto next option
if( (*it).compare(0, N, StartString) != 0 ) { ++it; ++idx; continue; }
// append the found tail to the result vector
result.push_back(__convert_to_type((*it).substr(N), Default));
// adapt the nominus vector
std::vector<unsigned>::iterator nit = idx_nominus.begin();
for(; nit != idx_nominus.end(); ++nit) {
if( *nit == idx ) {
idx_nominus.erase(nit);
for(; nit != idx_nominus.end(); ++nit) *nit -= 1;
break;
}
}
// erase the found option
argv.erase(it);
// 100% safe solution: set iterator back to the beginning.
// (normally, 'it--' would be enough, but who knows how the
// iterator is implemented and .erase() definitely invalidates
// the current iterator position.
if( argv.empty() ) break;
it = argv.begin();
}
cursor = 0;
nominus_cursor = -1;
return result;
}
__GETPOT_INLINE std::vector<double>
GetPot::double_tails(const char* StartString,
const double Default /* = -1.0 */)
{
std::vector<double> result;
const unsigned N = static_cast<unsigned int>(strlen(StartString));
std::vector<std::string>::iterator it = argv.begin();
unsigned idx = 0;
while( it != argv.end() ) {
// (*) does start string match the given option?
// NO -> goto next option
if( (*it).compare(0, N, StartString) != 0 ) { ++it; ++idx; continue; }
// append the found tail to the result vector
result.push_back(__convert_to_type((*it).substr(N), Default));
// adapt the nominus vector
std::vector<unsigned>::iterator nit = idx_nominus.begin();
for(; nit != idx_nominus.end(); ++nit) {
if( *nit == idx ) {
idx_nominus.erase(nit);
for(; nit != idx_nominus.end(); ++nit) *nit -= 1;
break;
}
}
// erase the found option
argv.erase(it);
// 100% safe solution: set iterator back to the beginning.
// (normally, 'it--' would be enough, but who knows how the
// iterator is implemented and .erase() definitely invalidates
// the current iterator position.
if( argv.empty() ) break;
it = argv.begin();
}
cursor = 0;
nominus_cursor = -1;
return result;
}
///////////////////////////////////////////////////////////////////////////////
// (*) lists of nominus following an option