-
Notifications
You must be signed in to change notification settings - Fork 2
/
paramproc.cpp
126 lines (112 loc) · 3.58 KB
/
paramproc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifdef _MSC_VER
#pragma warning(disable : 4786)
#endif
#include "paramproc.h"
#include <limits>
#include <string>
static char _uninitialized[] = "Uninitialized";
// using namespace std;
Param::Param(Type pt, int argc, const char* const argv[])
: _b(false),
_on(false),
_i(INT_MAX),
_u(unsigned(-1)),
_d(-1.29384756657),
_s(_uninitialized),
_pt(pt),
_key("") {
// errproc(pt==NOPARAM," This constructor can only work with
// Param:NOPARAM\n");
(void)argv; // please compiler
_b = (argc < 2 ? true : false);
return;
}
Param::Param(const char* key, Type pt, int argc, const char* const argv[])
: _b(false),
_on(false),
_i(-1),
_u(unsigned(-1)),
_d(-1.23456),
_s(_uninitialized),
_pt(pt),
_key(key) {
// errproc(strlen(_key)>0," Zero length key for command line parameter");
int n = 0;
if (_pt == NOPARAM) {
if (argc < 2)
_b = true;
else
_b = false;
return;
}
while (++n < argc && !found()) {
if (argv[n][0] == '-' || argv[n][0] == '+') {
const char* start = argv[n] + 1;
if (argv[n][0] == '-') {
if (argv[n][1] == '-') start++;
} else
_on = true;
if (strcmp(start, _key) == 0) {
_b = true;
if (n + 1 < argc) {
char tmp[80];
strncpy_s(tmp, argv[n + 1], 80);
switch (_pt) {
case BOOL:
break;
case INT:
_i = atoi(argv[n + 1]);
break;
case UNSIGNED:
_u = strtoul(argv[n + 1], (char**)NULL, 10);
break;
case DOUBLE:
_d = atof(argv[n + 1]);
break;
case STRING:
_s = argv[n + 1];
break;
default:
printf(
" Unknown command line parameter"); // abkfatal(0,"
// Unknown
// command
// line
// parameter");
}
}
}
}
}
}
bool Param::found() const { return _b; }
bool Param::on() const // true for +option, false otherwise
{
// errproc(found()," Parameter not found: you need to check for this
// first\n");
return _on;
}
int Param::getInt() const {
// errproc(_pt==INT," Parameter is not INT ");
// errproc(found()," Parameter not found: you need to check for this
// first\n");
return _i;
}
unsigned Param::getUnsigned() const {
// errproc(_pt==UNSIGNED," Parameter is not UNSIGNED ");
// errproc(found(),
// " UNSIGNED Parameter not found: you need to check for this first\n");
return _u;
}
double Param::getDouble() const {
// errproc(_pt==DOUBLE," Parameter is not DOUBLE ");
// errproc(found(),
// " DOUBLE parameter not found: you need to check for this first\n");
return _d;
}
const char* Param::getString() const {
// errproc(_pt==STRING," Parameter is not STRING");
// errproc(found(),
// " STRING parameter not found: you need to check for this first\n");
return _s;
}