-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.cpp
executable file
·274 lines (255 loc) · 8.04 KB
/
env.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/////////////////////////////////////////////////
// Environment
TEnv::TEnv(const int& _Args, char** _ArgV, const PNotify& _Notify):
ArgV(), HdStr(), MnArgs(1), SilentP(false), Notify(_Notify) {
for (int ArgN = 0; ArgN < _Args; ArgN++)
ArgV.Add(TStr(_ArgV[ArgN]));
}
TEnv::TEnv(const TStr& _ArgStr, const PNotify& _Notify):
ArgV(), HdStr(), MnArgs(1), SilentP(false), Notify(_Notify) {
_ArgStr.SplitOnAllCh(' ', ArgV);
}
TStr TEnv::GetExeFNm() const {
TStr ExeFNm=GetArg(0);
if (ExeFNm.IsPrefix("//?")){ // observed on Win64 CGI
ExeFNm=ExeFNm.GetSubStr(3, ExeFNm.Len());
}
return ExeFNm;
}
TStr TEnv::GetCmLn(const int& FromArgN) const {
TChA CmLnChA;
for (int ArgN=FromArgN; ArgN<GetArgs(); ArgN++){
if (ArgN>FromArgN){CmLnChA+=' ';}
CmLnChA+=GetArg(ArgN);
}
return CmLnChA;
}
int TEnv::GetPrefixArgN(const TStr& PrefixStr) const {
int ArgN=0;
while (ArgN<GetArgs()){
if (GetArg(ArgN).GetSubStr(0, PrefixStr.Len()-1)==PrefixStr){return ArgN;}
ArgN++;
}
return -1;
}
TStr TEnv::GetArgPostfix(const TStr& PrefixStr) const {
int ArgN=GetPrefixArgN(PrefixStr); IAssert(ArgN!=-1);
TStr ArgStr=GetArg(ArgN);
return ArgStr.GetSubStr(PrefixStr.Len(), ArgStr.Len());
}
void TEnv::PrepArgs(const TStr& _HdStr, const int& _MnArgs, const bool& _SilentP){
// put environment state
HdStr=_HdStr;
MnArgs=_MnArgs;
// silence
SilentP=true;
SilentP=Env.GetIfArgPrefixBool("-silent:", false, "Silence");
// start header
if (!SilentP){
// print header
if (!HdStr.Empty()){
// print header-string
TStr DateStr=__DATE__;
printf("%s [%s]\n", HdStr.CStr(), DateStr.CStr());
// print header-line
for (int ChN=0; ChN<HdStr.Len()+DateStr.Len()+3; ChN++){printf("=");}
printf("\n");
}
// print start of 'usage' message if not enough arguments
if (Env.GetArgs()<=MnArgs){
TStr ExeFNm=Env.GetArg(0).GetFBase();
printf("usage: %s\n", ExeFNm.CStr());
}
}
}
bool TEnv::IsEndOfRun() const {
if (!SilentP){
// print line in length of header-line
if (HdStr.Empty()){
printf("========================================\n");
} else {
for (int ChN=0; ChN<HdStr.Len(); ChN++){printf("=");}
printf("\n");
}
}
// return
return Env.GetArgs()<=MnArgs;
}
TStr TEnv::GetIfArgPrefixStr(
const TStr& PrefixStr, const TStr& DfVal, const TStr& DNm) const {
if (Env.GetArgs()<=MnArgs){
// 'usage' argument message
if (!SilentP){
printf(" %s%s (default:'%s')\n", PrefixStr.CStr(), DNm.CStr(), DfVal.CStr());}
return DfVal;
} else {
// argument & value message
TStr Val;
if (Env.IsArgPrefix(PrefixStr)){
Val=Env.GetArgPostfix(PrefixStr);
if (Val.Len()>1){
if ((Val[0]=='\"')&&(Val.LastCh()=='\"')){
Val=Val.GetSubStr(1, Val.Len()-2);
}
}
} else {
Val=DfVal;
}
TStr MsgStr=DNm+" ("+PrefixStr+")="+Val;
if (!SilentP){TNotify::OnStatus(Notify, MsgStr);}
return Val;
}
}
TStrV TEnv::GetIfArgPrefixStrV(
const TStr& PrefixStr, TStrV& DfValV, const TStr& DNm) const {
TStrV ArgValV;
if (Env.GetArgs()<=MnArgs){
// 'usage' argument message
if (!SilentP){
printf(" %s%s (default:", PrefixStr.CStr(), DNm.CStr());
for (int DfValN=0; DfValN<DfValV.Len(); DfValN++){
if (DfValN>0){printf(", ");}
printf("'%s'", DfValV[DfValN].CStr());
}
printf(")\n");
}
return ArgValV;
} else {
// argument & value message
TStr ArgValVChA;
for (int ArgN=0; ArgN<GetArgs(); ArgN++){
// get argument string
TStr ArgStr=GetArg(ArgN);
if (ArgStr.GetSubStr(0, PrefixStr.Len()-1)==PrefixStr){
// extract & add argument value
TStr ArgVal=ArgStr.GetSubStr(PrefixStr.Len(), ArgStr.Len());
ArgValV.Add(ArgVal);
// add to message string
if (ArgValV.Len()>1){ArgValVChA+=", ";}
ArgValVChA+=ArgValV.Last();
}
}
if (ArgValV.Empty()){ArgValV=DfValV;}
// output argument values
TChA MsgChA;
MsgChA+=DNm; MsgChA+=" ("; MsgChA+=PrefixStr; MsgChA+=")=";
for (int ArgValN=0; ArgValN<ArgValV.Len(); ArgValN++){
if (ArgValN>0){MsgChA+=", ";}
MsgChA+="'"; MsgChA+=ArgValV[ArgValN]; MsgChA+="'";
}
if (!SilentP){TNotify::OnStatus(Notify, MsgChA);}
return ArgValV;
}
}
bool TEnv::GetIfArgPrefixBool(
const TStr& PrefixStr, const bool& DfVal, const TStr& DNm) const {
if (Env.GetArgs()<=MnArgs){
// 'usage' argument message
if (!SilentP){
printf(" %s%s (default:'%s')\n",
PrefixStr.CStr(), DNm.CStr(), TBool::GetStr(DfVal).CStr());}
return DfVal;
} else {
// argument & value message
bool Val;
if ((PrefixStr.Len()>0)&&(PrefixStr.LastCh()==':')){
if (Env.IsArgPrefix(PrefixStr)){
// try to find one of boolean string value representations
TStr ValStr=Env.GetArgPostfix(PrefixStr);
Val=TBool::GetValFromStr(ValStr, DfVal);
} else {
// remove ':' and try to find option
TStr RedPrefixStr=PrefixStr;
RedPrefixStr.DelSubStr(PrefixStr.Len()-1, PrefixStr.Len()-1);
if (Env.IsArgPrefix(RedPrefixStr)){Val=true;} else {Val=DfVal;}
}
} else {
if (Env.IsArgPrefix(PrefixStr)){Val=true;} else {Val=DfVal;}
}
TStr MsgStr=DNm+" ("+PrefixStr+")="+TBool::GetYesNoStr(Val);
if (!SilentP){TNotify::OnStatus(Notify, MsgStr);}
return Val;
}
}
int TEnv::GetIfArgPrefixInt(
const TStr& PrefixStr, const int& DfVal, const TStr& DNm) const {
if (Env.GetArgs()<=MnArgs){
// 'usage' argument message
if (!SilentP){
printf(" %s%s (default:%d)\n", PrefixStr.CStr(), DNm.CStr(), DfVal);}
return DfVal;
} else {
// argument & value message
int Val;
if (Env.IsArgPrefix(PrefixStr)){
TStr ValStr=Env.GetArgPostfix(PrefixStr);
Val=ValStr.GetInt(DfVal);
} else {
Val=DfVal;
}
TStr MsgStr=DNm+" ("+PrefixStr+")="+TInt::GetStr(Val);
if (!SilentP){TNotify::OnStatus(Notify, MsgStr);}
return Val;
}
}
TIntV TEnv::GetIfArgPrefixIntV(
const TStr& PrefixStr, TIntV& DfValV, const TStr& DNm) const {
// convert default-integer-values to default-string-values
TStrV DfValStrV;
for (int ValN=0; ValN<DfValV.Len(); ValN++){
DfValStrV.Add(TInt::GetStr(DfValV[ValN]));}
// get string-values
TStrV ValStrV=GetIfArgPrefixStrV(PrefixStr, DfValStrV, DNm);
// convert string-values to integer-values
TIntV ValV;
for (int ValN=0; ValN<ValStrV.Len(); ValN++){
int Val;
if (ValStrV[ValN].IsInt(Val)){
ValV.Add(Val);}
}
// return value-vector
return ValV;
}
double TEnv::GetIfArgPrefixFlt(
const TStr& PrefixStr, const double& DfVal, const TStr& DNm) const {
if (Env.GetArgs()<=MnArgs){
// 'usage' argument message
if (!SilentP){
printf(" %s%s (default:%g)\n", PrefixStr.CStr(), DNm.CStr(), DfVal);}
return DfVal;
} else {
// argument & value message
double Val;
if (Env.IsArgPrefix(PrefixStr)){
TStr ValStr=Env.GetArgPostfix(PrefixStr);
Val=ValStr.GetFlt(DfVal);
} else {
Val=DfVal;
}
TStr MsgStr=DNm+" ("+PrefixStr+")="+TFlt::GetStr(Val);
if (!SilentP){TNotify::OnStatus(Notify, MsgStr);}
return Val;
}
}
void TEnv::GetVarNmV(TStrV& VarNmV){
VarNmV.Clr(); int VarN=0;
while (_environ[VarN]!=NULL){
TStr VarNmVal=_environ[VarN++];
TStr VarNm; TStr VarVal; VarNmVal.SplitOnCh(VarNm, '=', VarVal);
VarNmV.Add(VarNm);
}
}
void TEnv::GetVarNmValV(TStrV& VarNmValV){
VarNmValV.Clr(); int VarN=0;
while (_environ[VarN]!=NULL){
VarNmValV.Add(_environ[VarN++]);
}
}
void TEnv::PutVarVal(const TStr& VarNm, const TStr& VarVal) {
const int RetVal = putenv(TStr::Fmt("%s=%s", VarNm.CStr(), VarVal.CStr()).CStr());
IAssert(RetVal==0);
}
TStr TEnv::GetVarVal(const TStr& VarNm) const {
return TStr(getenv(VarNm.CStr()));
}
TEnv Env;