-
Notifications
You must be signed in to change notification settings - Fork 27
/
ezOptionParser.hpp
2154 lines (1991 loc) · 67.4 KB
/
ezOptionParser.hpp
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
/*
This file is part of ezOptionParser. See MIT-LICENSE.
Copyright (C) 2011,2012 Remik Ziemlinski <first d0t surname att gmail>
CHANGELOG
v0.0.0 20110505 rsz Created.
v0.1.0 20111006 rsz Added validator.
v0.1.1 20111012 rsz Fixed validation of ulonglong.
v0.1.2 20111126 rsz Allow flag names start with alphanumeric (previously, flag had to start with alpha).
v0.1.3 20120108 rsz Created work-around for unique id generation with IDGenerator that avoids retarded c++ translation unit linker errors with single-header static variables. Forced inline on all methods to please retard compiler and avoid multiple def errors.
v0.1.4 20120629 Enforced MIT license on all files.
v0.2.0 20121120 Added parseIndex to OptionGroup.
v0.2.1 20130506 Allow disabling doublespace of OPTIONS usage descriptions.
*/
#ifndef EZ_OPTION_PARSER_H
#define EZ_OPTION_PARSER_H
#include <stdlib.h>
#include <vector>
#include <list>
#include <map>
#include <string>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <limits>
#include <sstream>
#include <cstring>
namespace ez {
#define DEBUGLINE() printf("%s:%d\n", __FILE__, __LINE__);
/* ################################################################### */
template<typename T>
static T fromString(const std::string* s) {
std::istringstream stream (s->c_str());
T t;
stream >> t;
return t;
};
template<typename T>
static T fromString(const char* s) {
std::istringstream stream (s);
T t;
stream >> t;
return t;
};
/* ################################################################### */
static bool isdigit(const std::string & s, int i=0) {
int n = s.length();
for(; i < n; ++i)
switch(s[i]) {
case '0': case '1': case '2':
case '3': case '4': case '5':
case '6': case '7': case '8': case '9': break;
default: return false;
}
return true;
};
/* ################################################################### */
static bool isdigit(const std::string * s, int i=0) {
int n = s->length();
for(; i < n; ++i)
switch(s->at(i)) {
case '0': case '1': case '2':
case '3': case '4': case '5':
case '6': case '7': case '8': case '9': break;
default: return false;
}
return true;
};
/* ################################################################### */
/*
Compare strings for opts, so short opt flags come before long format flags.
For example, -d < --dimension < --dmn, and also lower come before upper. The default STL std::string compare doesn't do that.
*/
static bool CmpOptStringPtr(std::string * s1, std::string * s2) {
int c1,c2;
const char *s=s1->c_str();
for(c1=0; c1 < s1->size(); ++c1)
if (isalnum(s[c1])) // locale sensitive.
break;
s=s2->c_str();
for(c2=0; c2 < s2->size(); ++c2)
if (isalnum(s[c2]))
break;
// Test which has more symbols before its name.
if (c1 > c2)
return false;
else if (c1 < c2)
return true;
// Both have same number of symbols, so compare first letter.
char char1 = s1->at(c1);
char char2 = s2->at(c2);
char lo1 = tolower(char1);
char lo2 = tolower(char2);
if (lo1 != lo2)
return lo1 < lo2;
// Their case doesn't match, so find which is lower.
char up1 = isupper(char1);
char up2 = isupper(char2);
if (up1 && !up2)
return false;
else if (!up1 && up2)
return true;
return (s1->compare(*s2)<0);
};
/* ################################################################### */
/*
Makes a vector of strings from one string,
splitting at (and excluding) delimiter "token".
*/
static void SplitDelim( const std::string& s, const char token, std::vector<std::string*> * result) {
std::string::const_iterator i = s.begin();
std::string::const_iterator j = s.begin();
const std::string::const_iterator e = s.end();
while(i!=e) {
while(i!=e && *i++!=token);
std::string *newstr = new std::string(j, i);
if (newstr->at(newstr->size()-1) == token) newstr->erase(newstr->size()-1);
result->push_back(newstr);
j = i;
}
};
/* ################################################################### */
// Variant that uses deep copies and references instead of pointers (less efficient).
static void SplitDelim( const std::string& s, const char token, std::vector<std::string> & result) {
std::string::const_iterator i = s.begin();
std::string::const_iterator j = s.begin();
const std::string::const_iterator e = s.end();
while(i!=e) {
while(i!=e && *i++!=token);
std::string newstr(j, i);
if (newstr.at(newstr.size()-1) == token) newstr.erase(newstr.size()-1);
result.push_back(newstr);
j = i;
}
};
/* ################################################################### */
// Variant that uses list instead of vector for efficient insertion, etc.
static void SplitDelim( const std::string& s, const char token, std::list<std::string*> & result) {
std::string::const_iterator i = s.begin();
std::string::const_iterator j = s.begin();
const std::string::const_iterator e = s.end();
while(i!=e) {
while(i!=e && *i++!=token);
std::string *newstr = new std::string(j, i);
if (newstr->at(newstr->size()-1) == token) newstr->erase(newstr->size()-1);
result.push_back(newstr);
j = i;
}
};
/* ################################################################### */
static void ToU1(std::string ** strings, unsigned char * out, int n) {
for(int i=0; i < n; ++i) {
out[i] = (unsigned char)atoi(strings[i]->c_str());
}
};
/* ################################################################### */
static void ToS1(std::string ** strings, char * out, int n) {
for(int i=0; i < n; ++i) {
out[i] = (char)atoi(strings[i]->c_str());
}
};
/* ################################################################### */
static void ToU2(std::string ** strings, unsigned short * out, int n) {
for(int i=0; i < n; ++i) {
out[i] = (unsigned short)atoi(strings[i]->c_str());
}
};
/* ################################################################### */
static void ToS2(std::string ** strings, short * out, int n) {
for(int i=0; i < n; ++i) {
out[i] = (short)atoi(strings[i]->c_str());
}
};
/* ################################################################### */
static void ToS4(std::string ** strings, int * out, int n) {
for(int i=0; i < n; ++i) {
out[i] = atoi(strings[i]->c_str());
}
};
/* ################################################################### */
static void ToU4(std::string ** strings, unsigned int * out, int n) {
for(int i=0; i < n; ++i) {
out[i] = (unsigned int)strtoul(strings[i]->c_str(), NULL, 0);
}
};
/* ################################################################### */
static void ToS8(std::string ** strings, long long * out, int n) {
for(int i=0; i < n; ++i) {
std::stringstream ss(strings[i]->c_str());
ss >> out[i];
}
};
/* ################################################################### */
static void ToU8(std::string ** strings, unsigned long long * out, int n) {
for(int i=0; i < n; ++i) {
std::stringstream ss(strings[i]->c_str());
ss >> out[i];
}
};
/* ################################################################### */
static void ToF(std::string ** strings, float * out, int n) {
for(int i=0; i < n; ++i) {
out[i] = (float)atof(strings[i]->c_str());
}
};
/* ################################################################### */
static void ToD(std::string ** strings, double * out, int n) {
for(int i=0; i < n; ++i) {
out[i] = (double)atof(strings[i]->c_str());
}
};
/* ################################################################### */
static void StringsToInts(std::vector<std::string> & strings, std::vector<int> & out) {
for(int i=0; i < strings.size(); ++i) {
out.push_back(atoi(strings[i].c_str()));
}
};
/* ################################################################### */
static void StringsToInts(std::vector<std::string*> * strings, std::vector<int> * out) {
for(int i=0; i < strings->size(); ++i) {
out->push_back(atoi(strings->at(i)->c_str()));
}
};
/* ################################################################### */
static void StringsToLongs(std::vector<std::string> & strings, std::vector<long> & out) {
for(int i=0; i < strings.size(); ++i) {
out.push_back(atol(strings[i].c_str()));
}
};
/* ################################################################### */
static void StringsToLongs(std::vector<std::string*> * strings, std::vector<long> * out) {
for(int i=0; i < strings->size(); ++i) {
out->push_back(atol(strings->at(i)->c_str()));
}
};
/* ################################################################### */
static void StringsToULongs(std::vector<std::string> & strings, std::vector<unsigned long> & out) {
for(int i=0; i < strings.size(); ++i) {
out.push_back(strtoul(strings[i].c_str(),0,0));
}
};
/* ################################################################### */
static void StringsToULongs(std::vector<std::string*> * strings, std::vector<unsigned long> * out) {
for(int i=0; i < strings->size(); ++i) {
out->push_back(strtoul(strings->at(i)->c_str(),0,0));
}
};
/* ################################################################### */
static void StringsToFloats(std::vector<std::string> & strings, std::vector<float> & out) {
for(int i=0; i < strings.size(); ++i) {
out.push_back(atof(strings[i].c_str()));
}
};
/* ################################################################### */
static void StringsToFloats(std::vector<std::string*> * strings, std::vector<float> * out) {
for(int i=0; i < strings->size(); ++i) {
out->push_back(atof(strings->at(i)->c_str()));
}
};
/* ################################################################### */
static void StringsToDoubles(std::vector<std::string> & strings, std::vector<double> & out) {
for(int i=0; i < strings.size(); ++i) {
out.push_back(atof(strings[i].c_str()));
}
};
/* ################################################################### */
static void StringsToDoubles(std::vector<std::string*> * strings, std::vector<double> * out) {
for(int i=0; i < strings->size(); ++i) {
out->push_back(atof(strings->at(i)->c_str()));
}
};
/* ################################################################### */
static void StringsToStrings(std::vector<std::string*> * strings, std::vector<std::string> * out) {
for(int i=0; i < strings->size(); ++i) {
out->push_back( *strings->at(i) );
}
};
/* ################################################################### */
static void ToLowerASCII(std::string & s) {
int n = s.size();
int i=0;
char c;
for(; i < n; ++i) {
c = s[i];
if(c<='Z' && c>='A')
s[i] = c+32;
}
}
/* ################################################################### */
static char** CommandLineToArgvA(char* CmdLine, int* _argc) {
char** argv;
char* _argv;
unsigned long len;
unsigned long argc;
char a;
unsigned long i, j;
bool in_QM;
bool in_TEXT;
bool in_SPACE;
len = strlen(CmdLine);
i = ((len+2)/2)*sizeof(void*) + sizeof(void*);
argv = (char**)malloc(i + (len+2)*sizeof(char));
_argv = (char*)(((unsigned char*)argv)+i);
argc = 0;
argv[argc] = _argv;
in_QM = false;
in_TEXT = false;
in_SPACE = true;
i = 0;
j = 0;
while((a = CmdLine[i] )) {
if(in_QM) {
if( (a == '\"') ||
(a == '\'')) // rsz. Added single quote.
{
in_QM = false;
} else {
_argv[j] = a;
j++;
}
} else {
switch(a) {
case '\"':
case '\'': // rsz. Added single quote.
in_QM = true;
in_TEXT = true;
if(in_SPACE) {
argv[argc] = _argv+j;
argc++;
}
in_SPACE = false;
break;
case ' ':
case '\t':
case '\n':
case '\r':
if(in_TEXT) {
_argv[j] = '\0';
j++;
}
in_TEXT = false;
in_SPACE = true;
break;
default:
in_TEXT = true;
if(in_SPACE) {
argv[argc] = _argv+j;
argc++;
}
_argv[j] = a;
j++;
in_SPACE = false;
break;
}
}
i++;
}
_argv[j] = '\0';
argv[argc] = NULL;
(*_argc) = argc;
return argv;
};
/* ################################################################### */
// Create unique ids with static and still allow single header that avoids multiple definitions linker error.
class ezOptionParserIDGenerator {
public:
static ezOptionParserIDGenerator& instance () { static ezOptionParserIDGenerator Generator; return Generator; }
short next () { return ++_id; }
private:
ezOptionParserIDGenerator() : _id(-1) {}
short _id;
};
/* ################################################################### */
/* Validate a value by checking:
- if as string, see if converted value is within datatype's limits,
- and see if falls within a desired range,
- or see if within set of given list of values.
If comparing with a range, the values list must contain one or two values. One value is required when comparing with <, <=, >, >=. Use two values when requiring a test such as <x<, <=x<, <x<=, <=x<=.
A regcomp/regexec based class could be created in the future if a need arises.
*/
class ezOptionValidator {
public:
inline ezOptionValidator(const char* _type, const char* _op=0, const char* list=0, bool _insensitive=false);
inline ezOptionValidator(char _type);
inline ezOptionValidator(char _type, char _op, const char* list, int _size);
inline ezOptionValidator(char _type, char _op, const unsigned char* list, int _size);
inline ezOptionValidator(char _type, char _op, const short* list, int _size);
inline ezOptionValidator(char _type, char _op, const unsigned short* list, int _size);
inline ezOptionValidator(char _type, char _op, const int* list, int _size);
inline ezOptionValidator(char _type, char _op, const unsigned int* list, int _size);
inline ezOptionValidator(char _type, char _op, const long long* list, int _size);
inline ezOptionValidator(char _type, char _op, const unsigned long long* list, int _size=0);
inline ezOptionValidator(char _type, char _op, const float* list, int _size);
inline ezOptionValidator(char _type, char _op, const double* list, int _size);
inline ezOptionValidator(char _type, char _op, const char** list, int _size, bool _insensitive);
inline ~ezOptionValidator();
inline bool isValid(const std::string * value);
inline void print();
inline void reset();
/* If value must be in custom range, use these comparison modes. */
enum OP { NOOP=0,
LT, /* value < list[0] */
LE, /* value <= list[0] */
GT, /* value > list[0] */
GE, /* value >= list[0] */
GTLT, /* list[0] < value < list[1] */
GELT, /* list[0] <= value < list[1] */
GELE, /* list[0] <= value <= list[1] */
GTLE, /* list[0] < value <= list[1] */
FINAL_VALUE /* if value is in list */
};
enum TYPE { NOTYPE=0, S1, U1, S2, U2, S4, U4, S8, U8, F, D, T };
enum TYPE2 { NOTYPE2=0, INT8, UINT8, INT16, UINT16, INT32, UINT32, INT64, UINT64, FLOAT, DOUBLE, TEXT };
union {
unsigned char *u1;
char *s1;
unsigned short *u2;
short *s2;
unsigned int *u4;
int *s4;
unsigned long long *u8;
long long *s8;
float *f;
double *d;
std::string** t;
};
char op;
bool quiet;
short id;
char type;
int size;
bool insensitive;
};
/* ------------------------------------------------------------------- */
ezOptionValidator::~ezOptionValidator() {
reset();
};
/* ------------------------------------------------------------------- */
void ezOptionValidator::reset() {
#define CLEAR(TYPE,P) case TYPE: if (P) delete [] P; P = 0; break;
switch(type) {
CLEAR(S1,s1);
CLEAR(U1,u1);
CLEAR(S2,s2);
CLEAR(U2,u2);
CLEAR(S4,s4);
CLEAR(U4,u4);
CLEAR(S8,s8);
CLEAR(U8,u8);
CLEAR(F,f);
CLEAR(D,d);
case T:
for(int i=0; i < size; ++i)
delete t[i];
delete [] t;
t = 0;
break;
default: break;
}
size = 0;
op = NOOP;
type = NOTYPE;
};
/* ------------------------------------------------------------------- */
ezOptionValidator::ezOptionValidator(char _type) : insensitive(0), op(0), size(0), s1(0), type(_type), quiet(0) {
id = ezOptionParserIDGenerator::instance().next();
};
/* ------------------------------------------------------------------- */
ezOptionValidator::ezOptionValidator(char _type, char _op, const char* list, int _size) : insensitive(0), op(_op), size(_size), s1(0), type(_type), quiet(0) {
id = ezOptionParserIDGenerator::instance().next();
s1 = new char[size];
memcpy(s1, list, size);
};
/* ------------------------------------------------------------------- */
ezOptionValidator::ezOptionValidator(char _type, char _op, const unsigned char* list, int _size) : insensitive(0), op(_op), size(_size), u1(0), type(_type), quiet(0) {
id = ezOptionParserIDGenerator::instance().next();
u1 = new unsigned char[size];
memcpy(u1, list, size);
};
/* ------------------------------------------------------------------- */
ezOptionValidator::ezOptionValidator(char _type, char _op, const short* list, int _size) : insensitive(0), op(_op), size(_size), s2(0), type(_type), quiet(0) {
id = ezOptionParserIDGenerator::instance().next();
s2 = new short[size];
memcpy(s2, list, size*sizeof(short));
};
/* ------------------------------------------------------------------- */
ezOptionValidator::ezOptionValidator(char _type, char _op, const unsigned short* list, int _size) : insensitive(0), op(_op), size(_size), u2(0), type(_type), quiet(0) {
id = ezOptionParserIDGenerator::instance().next();
u2 = new unsigned short[size];
memcpy(u2, list, size*sizeof(unsigned short));
};
/* ------------------------------------------------------------------- */
ezOptionValidator::ezOptionValidator(char _type, char _op, const int* list, int _size) : insensitive(0), op(_op), size(_size), s4(0), type(_type), quiet(0) {
id = ezOptionParserIDGenerator::instance().next();
s4 = new int[size];
memcpy(s4, list, size*sizeof(int));
};
/* ------------------------------------------------------------------- */
ezOptionValidator::ezOptionValidator(char _type, char _op, const unsigned int* list, int _size) : insensitive(0), op(_op), size(_size), u4(0), type(_type), quiet(0) {
id = ezOptionParserIDGenerator::instance().next();
u4 = new unsigned int[size];
memcpy(u4, list, size*sizeof(unsigned int));
};
/* ------------------------------------------------------------------- */
ezOptionValidator::ezOptionValidator(char _type, char _op, const long long* list, int _size) : insensitive(0), op(_op), size(_size), s8(0), type(_type), quiet(0) {
id = ezOptionParserIDGenerator::instance().next();
s8 = new long long[size];
memcpy(s8, list, size*sizeof(long long));
};
/* ------------------------------------------------------------------- */
ezOptionValidator::ezOptionValidator(char _type, char _op, const unsigned long long* list, int _size) : insensitive(0), op(_op), size(_size), u8(0), type(_type), quiet(0) {
id = ezOptionParserIDGenerator::instance().next();
u8 = new unsigned long long[size];
memcpy(u8, list, size*sizeof(unsigned long long));
};
/* ------------------------------------------------------------------- */
ezOptionValidator::ezOptionValidator(char _type, char _op, const float* list, int _size) : insensitive(0), op(_op), size(_size), f(0), type(_type), quiet(0) {
id = ezOptionParserIDGenerator::instance().next();
f = new float[size];
memcpy(f, list, size*sizeof(float));
};
/* ------------------------------------------------------------------- */
ezOptionValidator::ezOptionValidator(char _type, char _op, const double* list, int _size) : insensitive(0), op(_op), size(_size), d(0), type(_type), quiet(0) {
id = ezOptionParserIDGenerator::instance().next();
d = new double[size];
memcpy(d, list, size*sizeof(double));
};
/* ------------------------------------------------------------------- */
ezOptionValidator::ezOptionValidator(char _type, char _op, const char** list, int _size, bool _insensitive) : insensitive(_insensitive), op(_op), size(_size), t(0), type(_type), quiet(0) {
id = ezOptionParserIDGenerator::instance().next();
t = new std::string*[size];
int i=0;
for(; i < size; ++i) {
t[i] = new std::string(list[i]);
}
};
/* ------------------------------------------------------------------- */
/* Less efficient but convenient ctor that parses strings to setup validator.
_type: s1, u1, s2, u2, ..., f, d, t
_op: lt, gt, ..., in
_list: comma-delimited string
*/
ezOptionValidator::ezOptionValidator(const char* _type, const char* _op, const char* _list, bool _insensitive) : insensitive(_insensitive), size(0), t(0), type(0), quiet(0) {
id = ezOptionParserIDGenerator::instance().next();
switch(_type[0]) {
case 'u':
switch(_type[1]) {
case '1': type = U1; break;
case '2': type = U2; break;
case '4': type = U4; break;
case '8': type = U8; break;
default: break;
}
break;
case 's':
switch(_type[1]) {
case '1': type = S1;
break;
case '2': type = S2; break;
case '4': type = S4; break;
case '8': type = S8; break;
default: break;
}
break;
case 'f': type = F; break;
case 'd': type = D; break;
case 't': type = T; break;
default:
if (!quiet)
std::cerr << "ERROR: Unknown validator datatype \"" << _type << "\".\n";
break;
}
int nop = 0;
if (_op != 0)
nop = strlen(_op);
switch(nop) {
case 0: op = NOOP; break;
case 2:
switch(_op[0]) {
case 'g':
switch(_op[1]) {
case 'e': op = GE; break;
default: op = GT; break;
}
break;
case 'i': op = FINAL_VALUE;
break;
default:
switch(_op[1]) {
case 'e': op = LE; break;
default: op = LT; break;
}
break;
}
break;
case 4:
switch(_op[1]) {
case 'e':
switch(_op[3]) {
case 'e': op = GELE; break;
default: op = GELT; break;
}
break;
default:
switch(_op[3]) {
case 'e': op = GTLE; break;
default: op = GTLT; break;
}
break;
}
break;
default:
if (!quiet)
std::cerr << "ERROR: Unknown validator operation \"" << _op << "\".\n";
break;
}
if (_list == 0) return;
// Create list of strings and then cast to native datatypes.
std::string unsplit(_list);
std::list<std::string*> split;
std::list<std::string*>::iterator it;
SplitDelim(unsplit, ',', split);
size = split.size();
std::string **strings = new std::string*[size];
int i = 0;
for(it = split.begin(); it != split.end(); ++it)
strings[i++] = *it;
if (insensitive)
for(i=0; i < size; ++i)
ToLowerASCII(*strings[i]);
#define FreeStrings() { \
for(i=0; i < size; ++i)\
delete strings[i];\
delete [] strings;\
}
#define ToArray(T,P,Y) case T: P = new Y[size]; To##T(strings, P, size); FreeStrings(); break;
switch(type) {
ToArray(S1,s1,char);
ToArray(U1,u1,unsigned char);
ToArray(S2,s2,short);
ToArray(U2,u2,unsigned short);
ToArray(S4,s4,int);
ToArray(U4,u4,unsigned int);
ToArray(S8,s8,long long);
ToArray(U8,u8,unsigned long long);
ToArray(F,f,float);
ToArray(D,d,double);
case T: t = strings; break; /* Don't erase strings array. */
default: break;
}
};
/* ------------------------------------------------------------------- */
void ezOptionValidator::print() {
printf("id=%d, op=%d, type=%d, size=%d, insensitive=%d\n", id, op, type, size, insensitive);
};
/* ------------------------------------------------------------------- */
bool ezOptionValidator::isValid(const std::string * valueAsString) {
if (valueAsString == 0) return false;
#define CHECKRANGE(E,T) {\
std::stringstream ss(valueAsString->c_str()); \
long long E##value; \
ss >> E##value; \
long long E##min = static_cast<long long>(std::numeric_limits<T>::min()); \
if (E##value < E##min) { \
if (!quiet) \
std::cerr << "ERROR: Invalid value " << E##value << " is less than datatype min " << E##min << ".\n"; \
return false; \
} \
\
long long E##max = static_cast<long long>(std::numeric_limits<T>::max()); \
if (E##value > E##max) { \
if (!quiet) \
std::cerr << "ERROR: Invalid value " << E##value << " is greater than datatype max " << E##max << ".\n"; \
return false; \
} \
}
// Check if within datatype limits.
if (type != T) {
switch(type) {
case S1: CHECKRANGE(S1,char); break;
case U1: CHECKRANGE(U1,unsigned char); break;
case S2: CHECKRANGE(S2,short); break;
case U2: CHECKRANGE(U2,unsigned short); break;
case S4: CHECKRANGE(S4,int); break;
case U4: CHECKRANGE(U4,unsigned int); break;
case S8: {
if ( (valueAsString->at(0) == '-') &&
isdigit(valueAsString,1) &&
(valueAsString->size() > 19) &&
(valueAsString->compare(1, 19, "9223372036854775808") > 0) ) {
if (!quiet)
std::cerr << "ERROR: Invalid value " << *valueAsString << " is less than datatype min -9223372036854775808.\n";
return false;
}
if (isdigit(valueAsString) &&
(valueAsString->size() > 18) &&
valueAsString->compare("9223372036854775807") > 0) {
if (!quiet)
std::cerr << "ERROR: Invalid value " << *valueAsString << " is greater than datatype max 9223372036854775807.\n";
return false;
}
} break;
case U8: {
if (valueAsString->compare("0") < 0) {
if (!quiet)
std::cerr << "ERROR: Invalid value " << *valueAsString << " is less than datatype min 0.\n";
return false;
}
if (isdigit(valueAsString) &&
(valueAsString->size() > 19) &&
valueAsString->compare("18446744073709551615") > 0) {
if (!quiet)
std::cerr << "ERROR: Invalid value " << *valueAsString << " is greater than datatype max 18446744073709551615.\n";
return false;
}
} break;
case F: {
double dmax = static_cast<double>(std::numeric_limits<float>::max());
double dvalue = atof(valueAsString->c_str());
double dmin = -dmax;
if (dvalue < dmin) {
if (!quiet) {
fprintf(stderr, "ERROR: Invalid value %g is less than datatype min %g.\n", dvalue, dmin);
}
return false;
}
if (dvalue > dmax) {
if (!quiet)
std::cerr << "ERROR: Invalid value " << dvalue << " is greater than datatype max " << dmax << ".\n";
return false;
}
} break;
case D: {
long double ldmax = static_cast<long double>(std::numeric_limits<double>::max());
std::stringstream ss(valueAsString->c_str());
long double ldvalue;
ss >> ldvalue;
long double ldmin = -ldmax;
if (ldvalue < ldmin) {
if (!quiet)
std::cerr << "ERROR: Invalid value " << ldvalue << " is less than datatype min " << ldmin << ".\n";
return false;
}
if (ldvalue > ldmax) {
if (!quiet)
std::cerr << "ERROR: Invalid value " << ldvalue << " is greater than datatype max " << ldmax << ".\n";
return false;
}
} break;
case NOTYPE: default: break;
}
} else {
if (op == FINAL_VALUE) {
int i=0;
if (insensitive) {
std::string valueAsStringLower(*valueAsString);
ToLowerASCII(valueAsStringLower);
for(; i < size; ++i) {
if (valueAsStringLower.compare(t[i]->c_str()) == 0)
return true;
}
} else {
for(; i < size; ++i) {
if (valueAsString->compare(t[i]->c_str()) == 0)
return true;
}
}
return false;
}
}
// Only check datatype limits, and return;
if (op == NOOP) return true;
#define VALIDATE(T, U, LIST) { \
/* Value string converted to true native type. */ \
std::stringstream ss(valueAsString->c_str());\
U v;\
ss >> v;\
/* Check if within list. */ \
if (op == FINAL_VALUE) { \
T * last = LIST + size;\
return (last != std::find(LIST, last, v)); \
} \
\
/* Check if within user's custom range. */ \
T v0, v1; \
if (size > 0) { \
v0 = LIST[0]; \
} \
\
if (size > 1) { \
v1 = LIST[1]; \
} \
\
switch (op) {\
case LT:\
if (size > 0) {\
return v < v0;\
} else {\
std::cerr << "ERROR: No value given to validate if " << v << " < X.\n";\
return false;\
}\
break;\
case LE:\
if (size > 0) {\
return v <= v0;\
} else {\
std::cerr << "ERROR: No value given to validate if " << v << " <= X.\n";\
return false;\
}\
break;\
case GT:\
if (size > 0) {\
return v > v0;\
} else {\
std::cerr << "ERROR: No value given to validate if " << v << " > X.\n";\
return false;\
}\
break;\
case GE:\
if (size > 0) {\
return v >= v0;\
} else {\
std::cerr << "ERROR: No value given to validate if " << v << " >= X.\n";\
return false;\
}\
break;\
case GTLT:\
if (size > 1) {\
return (v0 < v) && (v < v1);\
} else {\
std::cerr << "ERROR: Missing values to validate if X1 < " << v << " < X2.\n";\
return false;\
}\
break;\
case GELT:\
if (size > 1) {\
return (v0 <= v) && (v < v1);\
} else {\
std::cerr << "ERROR: Missing values to validate if X1 <= " << v << " < X2.\n";\
return false;\
}\
break;\
case GELE:\
if (size > 1) {\
return (v0 <= v) && (v <= v1);\
} else {\
std::cerr << "ERROR: Missing values to validate if X1 <= " << v << " <= X2.\n";\
return false;\
}\
break;\
case GTLE:\
if (size > 1) {\
return (v0 < v) && (v <= v1);\
} else {\
std::cerr << "ERROR: Missing values to validate if X1 < " << v << " <= X2.\n";\
return false;\
}\
break;\
case NOOP: case FINAL_VALUE: default: break;\
} \
}
switch(type) {
case U1: VALIDATE(unsigned char, int, u1); break;
case S1: VALIDATE(char, int, s1); break;
case U2: VALIDATE(unsigned short, int, u2); break;
case S2: VALIDATE(short, int, s2); break;
case U4: VALIDATE(unsigned int, unsigned int, u4); break;
case S4: VALIDATE(int, int, s4); break;
case U8: VALIDATE(unsigned long long, unsigned long long, u8); break;
case S8: VALIDATE(long long, long long, s8); break;
case F: VALIDATE(float, float, f); break;
case D: VALIDATE(double, double, d); break;
default: break;
}
return true;
};
/* ################################################################### */
class OptionGroup {
public:
OptionGroup() : delim(0), expectArgs(0), isSet(false), isRequired(false) { }
~OptionGroup() {
for(int i=0; i < flags.size(); ++i)
delete flags[i];
flags.clear();
parseIndex.clear();
clearArgs();
};
inline void clearArgs();
inline void getInt(int&);
inline void getLong(long&);
inline void getLongLong(long long&);
inline void getULong(unsigned long&);
inline void getULongLong(unsigned long long&);
inline void getFloat(float&);
inline void getDouble(double&);
inline void getString(std::string&);
inline void getInts(std::vector<int>&);
inline void getLongs(std::vector<long>&);
inline void getULongs(std::vector<unsigned long>&);
inline void getFloats(std::vector<float>&);
inline void getDoubles(std::vector<double>&);
inline void getStrings(std::vector<std::string>&);
inline void getMultiInts(std::vector< std::vector<int> >&);
inline void getMultiLongs(std::vector< std::vector<long> >&);
inline void getMultiULongs(std::vector< std::vector<unsigned long> >&);
inline void getMultiFloats(std::vector< std::vector<float> >&);
inline void getMultiDoubles(std::vector< std::vector<double> >&);
inline void getMultiStrings(std::vector< std::vector<std::string> >&);
// defaults value regardless of being set by user.
std::string defaults;
// If expects arguments, this will delimit arg list.
char delim;
// If not 0, then number of delimited args. -1 for arbitrary number.
int expectArgs;
// Descriptive help message shown in usage instructions for option.
std::string help;
// 0 or 1.
bool isRequired;
// A list of flags that denote this option, i.e. -d, --dimension.
std::vector< std::string* > flags;
// If was set (or found).
bool isSet;
// Lists of arguments, per flag instance, after splitting by delimiter.
std::vector< std::vector< std::string* > * > args;
// Index where each group was parsed from input stream to track order.
std::vector<int> parseIndex;
};
/* ################################################################### */
void OptionGroup::clearArgs() {
int i,j;
for(i=0; i < args.size(); ++i) {
for(j=0; j < args[i]->size(); ++j)
delete args[i]->at(j);
delete args[i];
}
args.clear();
isSet = false;
};
/* ################################################################### */
void OptionGroup::getInt(int & out) {
if (!isSet) {
if (defaults.empty())
out = 0;