summaryrefslogtreecommitdiffstats
path: root/utils/zenutils/libraries/getpot-c++-1.1.17/getpot/getpot.hpp
blob: 9986d17893e1f3fa513b5b00954416cf5d72b08a (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
//  -*- c++ -*-
//  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-2005 Frank R. Schaefer <fschaef@users.sf.net>
//==========================================================================

#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 <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
	const std::istream::pos_type  Pos = istr.tellg();
	unsigned    i=0;
	for(; i<_comment_start.length() ; ++i) {
	    if( tmp != _comment_start[i] ) { 
// HACK: The following line throws off msvc8:
//		istr.seekg(Pos);
		// -- one step more backwards, since 'tmp' already at non-whitespace
		istr.unget();
		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()-1 )
		    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)
{
    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.
    char* result = new char[Remain.length()+1];
    strncpy(result, Remain.c_str(), Remain.length()+1);

    // store the created string internally, delete if when object deleted
    __internal_string_container.push_back(result);

    return result;
}

//     -- follow() function group
//        distinct option to be searched for
inline int
GetPot::follow(int Default, const char* Option)
{
    // (*) record requested of argument is entirely handled in 'search()' and 'next()'
    if( search(Option) == false ) return Default;
    return next(Default);
}

inline double
GetPot::follow(const double& Default, const char* Option)
{
    // (*) record requested of argument is entirely handled in 'search()' and 'next()'
    if( search(Option) == false ) return Default;
    return next(Default);
}

inline const std::string
GetPot::follow(const char* 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
inline int
GetPot::follow(int 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;
}

inline double
GetPot::follow(const double& 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);
    for(unsigned i=1; i<No; ++i) {
	char* Opt = va_arg(ap, char *);
	if( search(Opt) == true ) {
	    va_end(ap);
	    return next(Default);
	}
    }
    va_end(ap);
    return Default;
}

inline const std::string
GetPot::follow(const char* 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;
}


///////////////////////////////////////////////////////////////////////////////
// (*) lists of nominus following an option
//.............................................................................
//
inline std::vector<std::string> 
GetPot::nominus_followers(const char* Option)
{
    std::vector<std::string>  result_list;
    if( search(Option) == false ) return result_list;
    while( 1 + 1 == 2 ) {
	++cursor;
	if( cursor >= argv.size() ) { 
	    cursor = argv.size() - 1;
	    return result_list;
	}
	if( argv[cursor].length() >= 1 ) {
	    if( argv[cursor][0] == '-' ) {
		return result_list;
	    }
	    // -- record for later ufo-detection
	    __record_argument_request(argv[cursor]);
	    // -- append to the result list
	    result_list.push_back(argv[cursor]);
	}
    }
}

inline std::vector<std::string> 
GetPot::nominus_followers(unsigned No, ...)
{
    std::vector<std::string>  result_list;
    // (*) record requested of argument is entirely handled in 'search()' 
    //     and 'nominus_followers()'
    if( No == 0 ) return result_list;

    va_list ap;
    va_start(ap, No);   
    for(unsigned i=0; i<No; ++i) {
	char* Option = va_arg(ap, char *);
	std::vector<std::string> tmp = nominus_followers(Option);
	result_list.insert(result_list.end(), tmp.begin(), tmp.end());

	// std::cerr << "option = '" << Option << "'" << std::endl;
	// std::cerr << "length = " << tmp.size() << std::endl;
	// std::cerr << "new result list = <";
	// for(std::vector<std::string>::const_iterator it = result_list.begin();
	//    it != result_list.end(); ++it) 
	//    std::cerr << *it << ", ";
	// std::cerr << ">\n";
    }
    va_end(ap);
    return result_list;
}


///////////////////////////////////////////////////////////////////////////////
// (*) directly connected options
//.............................................................................
//
inline int
GetPot::direct_follow(int 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, Default);
}

inline double
GetPot::direct_follow(const double& Default, const char* Option)
{
    const char* FollowStr = __match_starting_string(Option);
    if( FollowStr == 0 )  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, Default);
}

inline const std::string
GetPot::direct_follow(const char* Default, const char* Option)
{
    if( search_failed_f ) return Default;
    const char* FollowStr = __match_starting_string(Option);
    if( FollowStr == 0 )  return Default;

    // (*) record requested of argument for later ufo-detection
    if( FollowStr ) __record_argument_request(std::string(Option) + FollowStr);

    if( ++cursor >= static_cast<unsigned int>(argv.size()) ) cursor = static_cast<unsigned int>(argv.size());
    return std::string(FollowStr);
}

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( strncmp(StartString, (*it).c_str(), N) != 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;
}

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( strncmp(StartString, (*it).c_str(), N) != 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;
}

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( strncmp(StartString, (*it).c_str(), N) != 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;
}





inline const char*
GetPot::__match_starting_string(const char* StartString)
    // pointer  to the place where the string after
    //          the match inside the found argument starts.
    // 0        no argument matches the starting string.
{
    const unsigned N         = static_cast<unsigned int>(strlen(StartString));
    unsigned       OldCursor = cursor;

    if( OldCursor >= static_cast<unsigned int>(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( strncmp(StartString, argv[c].c_str(), N) == 0)
	{ cursor = c; search_failed_f = false; return &(argv[c].c_str()[N]); }
    }

    if( ! search_loop_f ) return false;

    // (*) second loop from 0 to old cursor position
    for(c = 1; c < OldCursor; c++) {
	if( strncmp(StartString, argv[c].c_str(), N) == 0)
	{ cursor = c; search_failed_f = false; return &(argv[c].c_str()[N]); }
    }
    return 0;
}

///////////////////////////////////////////////////////////////////////////////
// (*) search for flags
//.............................................................................
//
inline bool
GetPot::options_contain(const char* FlagList) const
{
    // go through all arguments that start with a '-' (but not '--')
    std::string str;
    STRING_VECTOR::const_iterator it = argv.begin();
    for(; it != argv.end(); ++it) {
	str = __get_remaining_string(*it, prefix);

	if( str.length() >= 2 && str[0] == '-' && str[1] != '-' )
	    if( __check_flags(str, FlagList) ) return true;
    }
    return false;
}

inline bool
GetPot::argument_contains(unsigned Idx, const char* FlagList) const
{
    if( Idx >= argv.size() ) return false;

    // (*) record requested of argument for later ufo-detection
    //     an argument that is checked for flags is considered to be 'requested'
    ((GetPot*)this)->__record_argument_request(argv[Idx]);

    if( prefix == "" )
	// search argument for any flag in flag list
	return __check_flags(argv[Idx], FlagList);

    // if a prefix is set, then the argument index is the index
    //   inside the 'namespace'
    // => only check list of arguments that start with prefix
    unsigned no_matches = 0;
    unsigned i=0;
    for(; i<argv.size(); ++i) {
	const std::string Remain = __get_remaining_string(argv[i], prefix);
	if( Remain != "") {
	    no_matches += 1;
	    if( no_matches == Idx)
		return __check_flags(Remain, FlagList);
	}
    }
    // no argument in this namespace
    return false;
}

inline bool
GetPot::__check_flags(const std::string& Str, const char* FlagList) const
{
    const char* p=FlagList;
    for(; *p != '\0' ; p++)
	if( Str.find(*p) != std::string::npos ) return true; // found something
    return false;
}

///////////////////////////////////////////////////////////////////////////////
// (*) nominus arguments
inline STRING_VECTOR
GetPot::nominus_vector() const
    // return vector of nominus arguments
{
    STRING_VECTOR nv;
    std::vector<unsigned>::const_iterator it = idx_nominus.begin();
    for(; it != idx_nominus.end(); ++it) {
	nv.push_back(argv[*it]);

	// (*) record for later ufo-detection
	//     when a nominus vector is requested, the entire set of nominus arguments are 
	//     tagged as 'requested'
	((GetPot*)this)->__record_argument_request(argv[*it]);
    }
    return nv;
}

inline std::string
GetPot::next_nominus()
{
    if( nominus_cursor < int(idx_nominus.size()) - 1 ) {
	const std::string Tmp = argv[idx_nominus[++nominus_cursor]];

	// (*) record for later ufo-detection
	__record_argument_request(Tmp);

	// -- cannot use the Tmp variable, since it is temporary and c_str() will return a pointer
	//    to something that does no longer exist.
	return Tmp; 
    }
    return std::string("");
}

///////////////////////////////////////////////////////////////////////////////
// (*) variables
//.............................................................................
//
inline int
GetPot::operator()(const char* VarName, int Default) const
{
    // (*) recording of requested variables happens in '__find_variable()'
    const variable*  sv = __find_variable(VarName);
    if( sv == 0 ) return Default;
    return __convert_to_type(sv->original, Default);
}

inline double
GetPot::operator()(const char* VarName, const double& Default) const
{
    // (*) recording of requested variables happens in '__find_variable()'
    const variable*  sv = __find_variable(VarName);
    if( sv == 0 ) return Default;
    return __convert_to_type(sv->original, Default);
}

inline const std::string
GetPot::operator()(const char* VarName, const char* Default) const
{
    // (*) recording of requested variables happens in '__find_variable()'
    const variable*  sv = __find_variable(VarName);
    if( sv == 0 ) return Default;
    // -- returning a c_str() pointer is OK here, since the variable remains existant,
    //    while 'sv' of course is delete at the end of the function.
    return sv->original;
}

inline int
GetPot::operator()(const char* VarName, int Default, unsigned Idx) const
{
    // (*) recording of requested variables happens in '__find_variable()'
    const variable* sv = __find_variable(VarName);
    if( sv == 0 ) return Default;
    const std::string*  element = sv->get_element(Idx);
    if( element == 0 ) return Default;
    return __convert_to_type(*element, Default);
}

inline double
GetPot::operator()(const char* VarName, const double& Default, unsigned Idx) const
{
    // (*) recording of requested variables happens in '__find_variable()'
    const variable* sv = __find_variable(VarName);
    if( sv == 0 ) return Default;
    const std::string*  element = sv->get_element(Idx);
    if( element == 0 ) return Default;
    return __convert_to_type(*element, Default);
}

inline const std::string
GetPot::operator()(const char* VarName, const char* Default, unsigned Idx) const
{
    // (*) recording of requested variables happens in '__find_variable()'
    const variable*  sv = __find_variable(VarName);
    if( sv == 0 ) return Default;
    const std::string* element = sv->get_element(Idx);
    if( element == 0 )  return Default;
    return *element;
}

inline void 
GetPot::__record_argument_request(const std::string& Name)
{
    if( ! __request_recording_f ) return; 

    // (*) record requested variable for later ufo detection
    _requested_arguments.push_back(Name);

    // (*) record considered section for ufo detection
    STRING_VECTOR      STree = __get_section_tree(Name);
    victorate(std::string, STree, it)
	if( find(_requested_sections.begin(), _requested_sections.end(), *it) == _requested_sections.end() )
	    if( section.length() != 0 ) _requested_sections.push_back(*it);
}

inline void 
GetPot::__record_variable_request(const std::string& Name)
{
    if( ! __request_recording_f ) return; 

    // (*) record requested variable for later ufo detection
    _requested_variables.push_back(Name);

    // (*) record considered section for ufo detection
    STRING_VECTOR      STree = __get_section_tree(Name);
    victorate(std::string, STree, it)
	if( find(_requested_sections.begin(), _requested_sections.end(), *it) == _requested_sections.end() )
	    if( section.length() != 0 ) _requested_sections.push_back(*it);
}

// (*) following functions are to be used from 'outside', after getpot has parsed its
//     arguments => append an argument in the argument vector that reflects the addition
inline void
GetPot::__set_variable(const char* VarName, const char* Value)
{
    const GetPot::variable* Var = __find_variable(VarName);
    if( Var == 0 ) variables.push_back(variable(VarName, Value, _field_separator.c_str()));
    else           ((GetPot::variable*)Var)->take(Value, _field_separator.c_str());    
}

inline void
GetPot::set(const char* VarName, const char* Value, const bool Requested /* = yes */)
{     
    const std::string Arg = prefix + std::string(VarName) + std::string("=") + std::string(Value);
    argv.push_back(Arg);
    __set_variable(VarName, Value);

    // if user does not specify the variable as 'not being requested' it will be 
    // considered amongst the requested variables
    if( Requested ) __record_variable_request(Arg);
}

inline void
GetPot::set(const char* VarName, const double& Value, const bool Requested /* = yes */)
{ __set_variable(VarName, __double2string(Value).c_str()); }

inline void 
GetPot::set(const char* VarName, const int Value, const bool Requested /* = yes */)
{ __set_variable(VarName, __int2string(Value).c_str()); }


inline unsigned
GetPot::vector_variable_size(const char* VarName) const
{
    const variable*  sv = __find_variable(VarName);
    if( sv == 0 ) return 0;
    return static_cast<unsigned int>(sv->value.size());
}

inline STRING_VECTOR
GetPot::get_variable_names() const
{
    STRING_VECTOR  result;
    std::vector<GetPot::variable>::const_iterator it = variables.begin();
    for(; it != variables.end(); ++it) {
	const std::string Tmp = __get_remaining_string((*it).name, prefix);
	if( Tmp != "" ) result.push_back(Tmp);
    }
    return result;
}

inline STRING_VECTOR
GetPot::get_section_names() const
{ return section_list; }

inline const GetPot::variable*
GetPot::__find_variable(const char* VarName) const
{
    const std::string Name = prefix + VarName;

    // (*) record requested variable for later ufo detection
    ((GetPot*)this)->__record_variable_request(Name);

    std::vector<variable>::const_iterator it = variables.begin();
    for(; it != variables.end(); ++it) {
	if( (*it).name == Name ) return &(*it);
    }
    return 0;
}

///////////////////////////////////////////////////////////////////////////////
// (*) ouput (basically for debugging reasons
//.............................................................................
//
inline int
GetPot::print() const
{
    std::cout << "argc = " << static_cast<unsigned int>(argv.size()) << std::endl;
    STRING_VECTOR::const_iterator it = argv.begin();
    for(; it != argv.end(); ++it)
	std::cout << *it << std::endl;
    std::cout << std::endl;
    return 1;
}

// (*) dollar bracket expressions (DBEs) ------------------------------------
//
//     1) Entry Function: __DBE_expand_string()
//        Takes a string such as
//
//          "${+ ${x} ${y}}   Subject-${& ${section} ${subsection}}:   ${title}"
//
//        calls __DBE_expand() for each of the expressions
//
//           ${+ ${x} ${y}}
//           ${& ${section} ${subsection}}
//           ${Title}
//
//        and returns the string
//
//          "4711 Subject-1.01:   Mit den Clowns kamen die Schwaene"
//
//        assuming that
//            x          = "4699"
//            y          = "12"
//            section    = "1."
//            subsection = "01"
//            title      = "Mit den Clowns kamen die Schwaene"
//
//      2) __DBE_expand():
//
//           checks for the command, i.e. the 'sign' that follows '${'
//           divides the argument list into sub-expressions using
//           __DBE_get_expr_list()
//
//           ${+ ${x} ${y}}                 -> "${x}"  "${y}"
//           ${& ${section} ${subsection}}  -> "${section}" "${subsection}"
//           ${Title}                       -> Nothing, variable expansion
//
//      3) __DBE_expression_list():
//
//           builds a vector of unbracketed whitespace separated strings, i.e.
//
//           "  ${Number}.a ${: Das Marmorbild} AB-${& Author= ${Eichendorf}-1870}"
//
//           is split into a vector
//
//              [0] ${Number}.a
//              [1] ${: Das Marmorbild}
//              [2] AB-${& Author= ${Eichendorf}}-1870
//
//           Each sub-expression is expanded using expand().
//---------------------------------------------------------------------------
inline std::string
GetPot::__DBE_expand_string(const std::string str)
{
    // Parses for closing operators '${ }' and expands them letting
    // white spaces and other letters as they are.
    std::string   new_string = "";
    unsigned open_brackets = 0;
    unsigned first = 0;
    unsigned i = 0;
    for(;  i<str.size(); ++i) {
	if( i < str.size() - 2 && str.substr(i, 2) == "${" ) {
	    if( open_brackets == 0 ) first = i+2;
	    open_brackets++;
	}
	else if( str[i] == '}' && open_brackets > 0) {
	    open_brackets -= 1;
	    if( open_brackets == 0 ) {
		const std::string Replacement = __DBE_expand(str.substr(first, i - first));
		new_string += Replacement;
	    }
	}
	else if( open_brackets == 0 )
	    new_string += str[i];
    }
    return new_string;
}

inline STRING_VECTOR
GetPot::__DBE_get_expr_list(const std::string str_, const unsigned ExpectedNumber)
    // ensures that the resulting vector has the expected number
    // of arguments, but they may contain an error message
{
    std::string str = str_;
    // Separates expressions by non-bracketed whitespaces, expands them
    // and puts them into a list.

    unsigned i=0;
    // (1) eat initial whitespaces
    for(; i < str.size(); ++i)
	if( ! isspace(str[i]) ) break;

    STRING_VECTOR   expr_list;
    unsigned         open_brackets = 0;
    std::vector<unsigned> start_idx;
    unsigned         start_new_string = i;
    unsigned         l = static_cast<unsigned int>(str.size());

    // (2) search for ${ } expressions ...
    while( i < l ) {
	const char letter = str[i];
	// whitespace -> end of expression
	if( isspace(letter) && open_brackets == 0) {
	    expr_list.push_back(str.substr(start_new_string, i - start_new_string));
	    bool no_breakout_f = true;
	    for(++i; i < l ; ++i) {
		if( ! isspace(str[i]) )
		{ no_breakout_f = false; start_new_string = i; break; }
	    }
	    if( no_breakout_f ) {
		// end of expression list
		if( expr_list.size() < ExpectedNumber ) {
		    const std::string   pre_tmp("<< ${ }: missing arguments>>");
		    STRING_VECTOR tmp(ExpectedNumber - expr_list.size(), pre_tmp);
		    expr_list.insert(expr_list.end(), tmp.begin(), tmp.end());
		}
		return expr_list;
	    }
	}

	// dollar-bracket expression
	if( str.length() >= i+2 && str.substr(i, 2) == "${" ) {
	    open_brackets++;
	    start_idx.push_back(i+2);
	}
	else if( letter == '}' && open_brackets > 0) {
	    int start = start_idx[start_idx.size()-1];
	    start_idx.pop_back();
	    const std::string Replacement = __DBE_expand(str.substr(start, i-start));
	    if( start - 3 < (int)0)
		str = Replacement + str.substr(i+1);
	    else
		str = str.substr(0, start-2) + Replacement + str.substr(i+1);
	    l = static_cast<unsigned int>(str.size());
	    i = start + static_cast<unsigned int>(Replacement.size()) - 3;
	    open_brackets--;
	}
	++i;
    }

    // end of expression list
    expr_list.push_back(str.substr(start_new_string, i-start_new_string));

    if( expr_list.size() < ExpectedNumber ) {
	const std::string   pre_tmp("<< ${ }: missing arguments>>");
	STRING_VECTOR tmp(ExpectedNumber - expr_list.size(), pre_tmp);
	expr_list.insert(expr_list.end(), tmp.begin(), tmp.end());
    }

    return expr_list;
}

inline const GetPot::variable*
GetPot::__DBE_get_variable(std::string VarName)
{
    static GetPot::variable ev;
    std::string secure_Prefix = prefix;

    prefix = section;
    // (1) first search in currently active section
    const GetPot::variable* var = __find_variable(VarName.c_str());
    if( var != 0 ) { prefix = secure_Prefix; return var; }

    // (2) search in root name space
    prefix = "";
    var = __find_variable(VarName.c_str());
    if( var != 0 ) { prefix = secure_Prefix; return var; }

    prefix = secure_Prefix;

    // error occured => variable name == ""
    char* tmp = new char[VarName.length() + 25];
#ifndef WIN32
    snprintf(tmp, (int)sizeof(char)*(VarName.length() + 25), 
#else
    _snprintf(tmp, sizeof(char)*(VarName.length() + 25), 
#endif
	     "<<${ } variable '%s' undefined>>", VarName.c_str());
    ev.name = "";
    ev.original = std::string(tmp);
    delete [] tmp;
    return &ev;
}

inline std::string
GetPot::__DBE_expand(const std::string expr)
{
    // ${: } pure text
    if( expr[0] == ':' )
	return expr.substr(1);

    // ${& expr expr ... } text concatination
    else if( expr[0] == '&' ) {
	const STRING_VECTOR A = __DBE_get_expr_list(expr.substr(1), 1);

	STRING_VECTOR::const_iterator it = A.begin();
	std::string result = *it++;
	for(; it != A.end(); ++it) result += *it;

	return result;
    }

    // ${<-> expr expr expr} text replacement
    else if( expr.length() >= 3 && expr.substr(0, 3) == "<->" ) {
	STRING_VECTOR A = __DBE_get_expr_list(expr.substr(3), 3);
	std::string::size_type tmp = 0;
	const std::string::size_type L = A[1].length();
	while( (tmp = A[0].find(A[1])) != std::string::npos ) {
	    A[0].replace(tmp, L, A[2]);
	}
	return A[0];
    }
    // ${+ ...}, ${- ...}, ${* ...}, ${/ ...} expressions
    else if( expr[0] == '+' ) {
	STRING_VECTOR A = __DBE_get_expr_list(expr.substr(1), 2);
	STRING_VECTOR::const_iterator it = A.begin();
	double result = __convert_to_type(*it++, 0.0);
	for(; it != A.end(); ++it)
	    result += __convert_to_type(*it, 0.0);

	return __double2string(result);
    }
    else if( expr[0] == '-' ) {
	STRING_VECTOR A = __DBE_get_expr_list(expr.substr(1), 2);
	STRING_VECTOR::const_iterator it = A.begin();
	double result = __convert_to_type(*it++, 0.0);
	for(; it != A.end(); ++it)
	    result -= __convert_to_type(*it, 0.0);

	return __double2string(result);
    }
    else if( expr[0] == '*' ) {
	STRING_VECTOR A = __DBE_get_expr_list(expr.substr(1), 2);
	STRING_VECTOR::const_iterator it = A.begin();
	double result = __convert_to_type(*it++, 0.0);
	for(; it != A.end(); ++it)
	    result *= __convert_to_type(*it, 0.0);

	return __double2string(result);
    }
    else if( expr[0] == '/' ) {

	STRING_VECTOR A = __DBE_get_expr_list(expr.substr(1), 2);
	STRING_VECTOR::const_iterator it = A.begin();
	double result = __convert_to_type(*it++, 0.0);
	if( result == 0 ) return "0.0";
	for(; it != A.end(); ++it) {
	    const double Q = __convert_to_type(*it, 0.0);
	    if( Q == 0.0 ) return "0.0";
	    result /= Q;
	}
	return __double2string(result);
    }

    // ${^ ... } power expressions
    else if( expr[0] == '^' ) {
	STRING_VECTOR A = __DBE_get_expr_list(expr.substr(1), 2);
	STRING_VECTOR::const_iterator it = A.begin();
	double result = __convert_to_type(*it++, 0.0);
	for(; it != A.end(); ++it)
	    result = pow(result, __convert_to_type(*it, 0.0));
	return __double2string(result);
    }

    // ${==  } ${<=  } ${>= } comparisons (return the number of the first 'match'
    else if( expr.length() >= 2 &&
	     ( expr.substr(0,2) == "==" || expr.substr(0,2) == ">=" ||
	       expr.substr(0,2) == "<=" || expr[0] == '>'           || expr[0] == '<')) {
	// differentiate between two and one sign operators
	unsigned op = 0;
	enum { EQ, GEQ, LEQ, GT, LT };
	if      ( expr.substr(0, 2) == "==" ) op = EQ;
	else if ( expr.substr(0, 2) == ">=" ) op = GEQ;
	else if ( expr.substr(0, 2) == "<=" ) op = LEQ;
	else if ( expr[0] == '>' )            op = GT;
	else    /*                     "<" */ op = LT;

	STRING_VECTOR a;
	if ( op == GT || op == LT ) a = __DBE_get_expr_list(expr.substr(1), 2);
	else                        a = __DBE_get_expr_list(expr.substr(2), 2);

	std::string   x_orig = a[0];
	double   x = __convert_to_type(x_orig, 1e37);
	unsigned i = 1;

	STRING_VECTOR::const_iterator y_orig = a.begin();
	for(y_orig++; y_orig != a.end(); y_orig++) {
	    double y = __convert_to_type(*y_orig, 1e37);

	    // set the strings as reference if one wasn't a number
	    if ( x == 1e37 || y == 1e37 ) {
		// it's a string comparison
		if( (op == EQ  && x_orig == *y_orig) || (op == GEQ && x_orig >= *y_orig) ||
		    (op == LEQ && x_orig <= *y_orig) || (op == GT  && x_orig >  *y_orig) ||
		    (op == LT  && x_orig <  *y_orig) )
		    return __int2string(i);
	    }
	    else {
		// it's a number comparison
		if( (op == EQ  && x == y) || (op == GEQ && x >= y) ||
		    (op == LEQ && x <= y) || (op == GT  && x >  y) ||
		    (op == LT  && x <  y) )
		    return __int2string(i);
	    }
	    ++i;
	}

	// nothing fulfills the condition => return 0
	return "0";
    }
    // ${?? expr expr} select
    else if( expr.length() >= 2 && expr.substr(0, 2) == "??" ) {
	STRING_VECTOR a = __DBE_get_expr_list(expr.substr(2), 2);
	double x = __convert_to_type(a[0], 1e37);
	// last element is always the default argument
	if( x == 1e37 || x < 0 || x >= a.size() - 1 ) return a[a.size()-1];

	// round x to closest integer
	return a[int(x+0.5)];
    }
    // ${? expr expr expr} if then else conditions
    else if( expr[0] == '?' ) {
	STRING_VECTOR a = __DBE_get_expr_list(expr.substr(1), 2);
	if( __convert_to_type(a[0], 0.0) == 1.0 ) return a[1];
	else if( a.size() > 2 ) return a[2];
    }
    // ${! expr} maxro expansion
    else if( expr[0] == '!' ) {
	const GetPot::variable* Var = __DBE_get_variable(expr.substr(1));
	// error
	if( Var->name == "" ) return std::string(Var->original);

	const STRING_VECTOR A = __DBE_get_expr_list(Var->original, 2);
	return A[0];
    }
    // ${@: } - string subscription
    else if( expr.length() >= 2 && expr.substr(0,2) == "@:" ) {
	const STRING_VECTOR A = __DBE_get_expr_list(expr.substr(2), 2);
	double x = __convert_to_type(A[1], 1e37);

	// last element is always the default argument
	if( x == 1e37 || x < 0 || x >= A[0].size() - 1)
	    return "<<1st index out of range>>";

	if( A.size() > 2 ) {
	    double y = __convert_to_type(A[2], 1e37);
	    if ( y != 1e37 && y > 0 && y <= A[0].size() - 1 && y > x )
		return A[0].substr(int(x+0.5), int(y+1.5) - int(x+0.5));
	    else if( y == -1 )
		return A[0].substr(int(x+0.5));
	    return "<<2nd index out of range>>";
	}
	else {
	    char* tmp = new char[2];
	    tmp[0] = A[0][int(x+0.5)]; tmp[1] = '\0';
	    std::string result(tmp);
	    delete [] tmp;
	    return result;
	}
    }
    // ${@ } - vector subscription
    else if( expr[0] == '@' ) {
	STRING_VECTOR          A   = __DBE_get_expr_list(expr.substr(1), 2);
	const GetPot::variable* Var = __DBE_get_variable(A[0]);
	// error
	if( Var->name == "" ) {
	    // make a copy of the string if an error occured
	    // (since the error variable is a static variable inside get_variable())
	    return std::string(Var->original);
	}

	double x = __convert_to_type(A[1], 1e37);

	// last element is always the default argument
	if (x == 1e37 || x < 0 || x >= Var->value.size() )
	    return "<<1st index out of range>>";

	if ( A.size() > 2) {
	    double y = __convert_to_type(A[2], 1e37);
	    int    begin = int(x+0.5);
	    int    end = 0;
	    if ( y != 1e37 && y > 0 && y <= Var->value.size() && y > x)
		end = int(y+1.5);
	    else if( y == -1 )
		end = static_cast<unsigned int>(Var->value.size());
	    else
		return "<<2nd index out of range>>";

	    std::string result = *(Var->get_element(begin));
	    int i = begin+1;
	    for(; i < end; ++i)
		result += std::string(" ") + *(Var->get_element(i));
	    return result;
	}
	else
	    return *(Var->get_element(int(x+0.5)));
    }

    const STRING_VECTOR    A = __DBE_get_expr_list(expr, 1);
    const GetPot::variable* B = __DBE_get_variable(A[0]);

    // make a copy of the string if an error occured
    // (since the error variable is a static variable inside get_variable())
    if( B->name == "" ) return std::string(B->original);
    // (psuggs@pobox.com mentioned to me the warning MSVC++6.0 produces
    //  with:  else return B->original (thanks))
    return B->original;
}


///////////////////////////////////////////////////////////////////////////////
// (*) unidentified flying objects
//.............................................................................
//
inline bool
GetPot::__search_string_vector(const STRING_VECTOR& VecStr, const std::string& Str) const
{
    victorate(std::string, VecStr, itk) {
	if( *itk == Str ) return true;
    }
    return false;
}

inline STRING_VECTOR
GetPot::unidentified_arguments(unsigned Number,
			       const char* KnownArgument1, ...) const
{
    STRING_VECTOR known_arguments;

    // (1) create a vector of known arguments
    if( Number == 0 ) return STRING_VECTOR();

    va_list ap;
    va_start(ap, KnownArgument1);
    known_arguments.push_back(std::string(KnownArgument1));
    unsigned i=1;
    for(; i<Number; ++i)
	known_arguments.push_back(std::string(va_arg(ap, char *)));
    va_end(ap);

    return unidentified_arguments(known_arguments);
}

inline STRING_VECTOR
GetPot::unidentified_arguments() const
{ return unidentified_arguments(_requested_arguments); }

inline STRING_VECTOR
GetPot::unidentified_arguments(const STRING_VECTOR& Knowns) const
{
    STRING_VECTOR ufos;
    STRING_VECTOR::const_iterator it = argv.begin();
    ++it; // forget about argv[0] (application or filename)
    for(; it != argv.end(); ++it) {
	// -- argument belongs to prefixed section ?
	const std::string arg = __get_remaining_string(*it, prefix);
	if( arg == "" ) continue;

	// -- check if in list
	if( __search_string_vector(Knowns, arg) == false)
	    ufos.push_back(*it);
    }
    return ufos;
}

inline STRING_VECTOR
GetPot::unidentified_options(unsigned Number,
			     const char* KnownOption1, ...) const
{
    STRING_VECTOR known_options;

    // (1) create a vector of known arguments
    if( Number == 0 ) return STRING_VECTOR();

    va_list ap;
    va_start(ap, KnownOption1);
    known_options.push_back(std::string(KnownOption1));
    unsigned i=1;
    for(; i<Number; ++i)
	known_options.push_back(std::string(va_arg(ap, char *)));
    va_end(ap);

    return unidentified_options(known_options);
}

inline STRING_VECTOR
GetPot::unidentified_options() const
{ 
    // -- every option is an argument. 
    // -- the set of requested arguments contains the set of requested options. 
    // -- IF the set of requested arguments contains unrequested options, 
    //    THEN they were requested as 'follow' and 'next' arguments and not as real options.
    //
    // => it is not necessary to separate requested options from the list
    STRING_VECTOR option_list;
    victorate(std::string, _requested_arguments, it) {
	const std::string arg = *it;
	if( arg.length() == 0 ) continue;
	if( arg[0] == '-' )     option_list.push_back(arg);
    }	
    return unidentified_options(option_list); 
}

inline STRING_VECTOR
GetPot::unidentified_options(const STRING_VECTOR& Knowns) const
{
    STRING_VECTOR ufos;
    STRING_VECTOR::const_iterator it = argv.begin();
    ++it; // forget about argv[0] (application or filename)
    for(; it != argv.end(); ++it) {
	// -- argument belongs to prefixed section ?
	const std::string arg = __get_remaining_string(*it, prefix);
	if( arg == "" ) continue;

	// is argument really an option (starting with '-') ?
	if( arg.length() < 1 || arg[0] != '-' ) continue;

	if( __search_string_vector(Knowns, arg) == false)
	    ufos.push_back(*it);
    }

    return ufos;
}

inline std::string
GetPot::unidentified_flags(const char* KnownFlagList, int ArgumentNumber=-1) const
    // Two modes:
    //  ArgumentNumber >= 0 check specific argument
    //  ArgumentNumber == -1 check all options starting with one '-'
    //                       for flags
{
    std::string         ufos;
    STRING_VECTOR known_arguments;
    std::string         KFL(KnownFlagList);

    // (2) iteration over '-' arguments (options)
    if( ArgumentNumber == -1 ) {
	STRING_VECTOR::const_iterator it = argv.begin();
	++it; // forget about argv[0] (application or filename)
	for(; it != argv.end(); ++it) {
	    // -- argument belongs to prefixed section ?
	    const std::string arg = __get_remaining_string(*it, prefix);
	    if( arg == "" ) continue;

	    // -- does arguments start with '-' (but not '--')
	    if     ( arg.length() < 2 ) continue;
	    else if( arg[0] != '-' )    continue;
	    else if( arg[1] == '-' )    continue;

	    // -- check out if flags inside option are contained in KnownFlagList
	    const char* p=arg.c_str();
	    p++; // skip starting minus
	    for(; *p != '\0' ; p++)
		if( KFL.find(*p) == std::string::npos ) ufos += *p;
	}
    }
    // (1) check specific argument
    else {
	// -- only check arguments that start with prefix
	int no_matches = 0;
	unsigned i=1;
	for(; i<argv.size(); ++i) {
	    const std::string Remain = __get_remaining_string(argv[i], prefix);
	    if( Remain != "") {
		no_matches++;
		if( no_matches == ArgumentNumber) {
		    // -- the right argument number inside the section is found
		    // => check it for flags
		    const char* p = Remain.c_str();
		    p++; // skip starting minus
		    for(; *p != '\0' ; p++)
			if( KFL.find(*p) == std::string::npos ) ufos += *p;
		    return ufos;
		}
	    }
	}
    }
    return ufos;
}

inline STRING_VECTOR
GetPot::unidentified_variables(unsigned Number,
			       const char* KnownVariable1, ...) const
{
    STRING_VECTOR known_variables;

    // create vector of known arguments
    if( Number == 0 ) return STRING_VECTOR();

    va_list ap;
    va_start(ap, KnownVariable1);
    known_variables.push_back(std::string(KnownVariable1));
    unsigned i=1;
    for(; i<Number; ++i)
	known_variables.push_back(std::string(va_arg(ap, char *)));
    va_end(ap);

    return unidentified_variables(known_variables);
}

inline STRING_VECTOR
GetPot::unidentified_variables(const STRING_VECTOR& Knowns) const
{
    STRING_VECTOR ufos;

    victorate(GetPot::variable, variables, it) {
	// -- check if variable has specific prefix
	const std::string var_name = __get_remaining_string((*it).name, prefix);
	if( var_name == "" ) continue;

	// -- check if variable is known
	if( __search_string_vector(Knowns, var_name) == false)
	    ufos.push_back((*it).name);
    }
    return ufos;
}

inline STRING_VECTOR
GetPot::unidentified_variables() const
{  return unidentified_variables(_requested_variables); }


inline STRING_VECTOR
GetPot::unidentified_sections(unsigned Number,
			      const char* KnownSection1, ...) const
{
    STRING_VECTOR known_sections;

    // (1) create a vector of known arguments
    if( Number == 0 ) return STRING_VECTOR();

    va_list ap;
    va_start(ap, KnownSection1);
    known_sections.push_back(std::string(KnownSection1));
    unsigned i=1;
    for(; i<Number; ++i) {
	std::string tmp = std::string(va_arg(ap, char *));
	if( tmp.length() == 0 ) continue;
	if( tmp[tmp.length()-1] != '/' ) tmp += '/';
	known_sections.push_back(tmp);
    }
    va_end(ap);

    return unidentified_sections(known_sections);
}

inline STRING_VECTOR
GetPot::unidentified_sections() const
{ return unidentified_sections(_requested_sections); }

inline STRING_VECTOR
GetPot::unidentified_sections(const STRING_VECTOR& Knowns) const
{
    STRING_VECTOR ufos;

    victorate(std::string, section_list, it) {
	// -- check if section conform to prefix
	const std::string sec_name = __get_remaining_string(*it, prefix);
	if( sec_name == "" ) continue;

	// -- check if section is known
	if( __search_string_vector(Knowns, sec_name) == false )
	    ufos.push_back(*it);
    }

    return ufos;
}


inline STRING_VECTOR
GetPot::unidentified_nominuses(unsigned Number, const char* Known, ...) const
{
    STRING_VECTOR known_nominuses;

    // create vector of known arguments
    if( Number == 0 ) return STRING_VECTOR();

    va_list ap;
    va_start(ap, Known);
    known_nominuses.push_back(std::string(Known));
    unsigned i=1;
    for(; i<Number; ++i) {
	std::string tmp = std::string(va_arg(ap, char *));
	if( tmp.length() == 0 ) continue;
	known_nominuses.push_back(tmp);
    }
    va_end(ap);

    return unidentified_nominuses(known_nominuses);
}

inline STRING_VECTOR
GetPot::unidentified_nominuses() const {
    // -- every nominus is an argument. 
    // -- the set of requested arguments contains the set of requested nominuss. 
    // -- IF the set of requested arguments contains unrequested nominuss, 
    //    THEN they were requested as 'follow' and 'next' arguments and not as real nominuses.
    //
    // => it is not necessary to separate requested nominus from the list

    return unidentified_nominuses(_requested_arguments);
}

inline STRING_VECTOR
GetPot::unidentified_nominuses(const STRING_VECTOR& Knowns) const
{
    STRING_VECTOR ufos;

    // (2) iterate over all arguments
    STRING_VECTOR::const_iterator it = argv.begin();
    ++it; // forget about argv[0] (application or filename)
    for(; it != argv.end(); ++it) {
	// -- check if nominus part of prefix
	const std::string arg = __get_remaining_string(*it, prefix);
	if( arg == "" )                                         continue;

	if( arg.length() < 1 )                                  continue;
	// option ? --> not a nomius
	if( arg[0] == '-' )                                     continue;
	// section ? --> not a real nominus
	if( arg[0] == '[' && arg[arg.length()-1] == ']' )       continue;
	// variable definition ? --> not a real nominus
	bool continue_f = false;
	unsigned i=0;
	for(; i<arg.length() ; ++i)
	    if( arg[i] == '=' ) { continue_f = true; break; }
	if( continue_f )                                        continue;

	// real nominuses are compared with the given list
	if( __search_string_vector(Knowns, arg) == false )
	    ufos.push_back(*it);
    }
    return ufos;
}


///////////////////////////////////////////////////////////////////////////////
// (*) variable class
//.............................................................................
//
inline
GetPot::variable::variable()
{}

inline
GetPot::variable::variable(const variable& That)
{
#ifdef WIN32
    operator=(That);
#else
    GetPot::variable::operator=(That);
#endif
}


inline
GetPot::variable::variable(const char* Name, const char* Value, const char* FieldSeparator)
    : name(Name)
{
    // make a copy of the 'Value'
    take(Value, FieldSeparator);
}

inline const std::string*
GetPot::variable::get_element(unsigned Idx) const
{ if( Idx >= value.size() ) return 0; else return &(value[Idx]); }

inline void
GetPot::variable::take(const char* Value, const char* FieldSeparator)
{
    original = std::string(Value);

    // separate string by white space delimiters using 'strtok'
    // thread safe usage of strtok (no static members)
    char* spt = 0;
    // make a copy of the 'Value'
    char* copy = new char[strlen(Value)+1];
    strcpy(copy, Value);
    char* follow_token = strtok_r(copy, FieldSeparator, &spt);
    if( value.size() != 0 ) value.erase(value.begin(), value.end());
    while(follow_token != 0) {
	value.push_back(std::string(follow_token));
	follow_token = strtok_r(NULL, FieldSeparator, &spt);
    }

    delete [] copy;
}

inline
GetPot::variable::~variable()
{}

inline GetPot::variable&
GetPot::variable::operator=(const GetPot::variable& That)
{
    if( &That != this) {
	name     = That.name;
	value    = That.value;
	original = That.original;
    }
    return *this;
}

#undef victorate


#endif // __include_guard_GETPOT_H__