SNAP Library 6.0, Developer Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
env.cpp
Go to the documentation of this file.
1 // Environment
3 TEnv::TEnv(const int& _Args, char** _ArgV, const PNotify& _Notify):
4  ArgV(), HdStr(), MnArgs(1), SilentP(false), Notify(_Notify) {
5  for (int ArgN = 0; ArgN < _Args; ArgN++)
6  ArgV.Add(TStr(_ArgV[ArgN]));
7 }
8 
9 TEnv::TEnv(const TStr& _ArgStr, const PNotify& _Notify):
10  ArgV(), HdStr(), MnArgs(1), SilentP(false), Notify(_Notify) {
11  _ArgStr.SplitOnAllCh(' ', ArgV);
12 }
13 
15  TStr ExeFNm=GetArg(0);
16  if (ExeFNm.IsPrefix("//?")){ // observed on Win64 CGI
17  ExeFNm=ExeFNm.GetSubStr(3, ExeFNm.Len());
18  }
19  return ExeFNm;
20 }
21 
22 TStr TEnv::GetCmLn(const int& FromArgN) const {
23  TChA CmLnChA;
24  for (int ArgN=FromArgN; ArgN<GetArgs(); ArgN++){
25  if (ArgN>FromArgN){CmLnChA+=' ';}
26  CmLnChA+=GetArg(ArgN);
27  }
28  return CmLnChA;
29 }
30 
31 int TEnv::GetPrefixArgN(const TStr& PrefixStr) const {
32  int ArgN=0;
33  while (ArgN<GetArgs()){
34  if (GetArg(ArgN).GetSubStr(0, PrefixStr.Len()-1)==PrefixStr){return ArgN;}
35  ArgN++;
36  }
37  return -1;
38 }
39 
40 TStr TEnv::GetArgPostfix(const TStr& PrefixStr) const {
41  int ArgN=GetPrefixArgN(PrefixStr); IAssert(ArgN!=-1);
42  TStr ArgStr=GetArg(ArgN);
43  return ArgStr.GetSubStr(PrefixStr.Len(), ArgStr.Len());
44 }
45 
46 void TEnv::PrepArgs(const TStr& _HdStr, const int& _MnArgs, const bool& _SilentP){
47  // put environment state
48  HdStr=_HdStr;
49  MnArgs=_MnArgs;
50  // silence
51  SilentP=true;
52  SilentP=Env.GetIfArgPrefixBool("-silent:", false, "Silence");
53  // start header
54  if (!SilentP){
55  // print header
56  if (!HdStr.Empty()){
57  // print header-string
58  TStr DateStr=__DATE__;
59  printf("%s [%s]\n", HdStr.CStr(), DateStr.CStr());
60  // print header-line
61  for (int ChN=0; ChN<HdStr.Len()+DateStr.Len()+3; ChN++){printf("=");}
62  printf("\n");
63  }
64  // print start of 'usage' message if not enough arguments
65  if (Env.GetArgs()<=MnArgs){
66  TStr ExeFNm=Env.GetArg(0).GetFBase();
67  printf("usage: %s\n", ExeFNm.CStr());
68  }
69  }
70 }
71 
72 bool TEnv::IsEndOfRun() const {
73  if (!SilentP){
74  // print line in length of header-line
75  if (HdStr.Empty()){
76  printf("========================================\n");
77  } else {
78  for (int ChN=0; ChN<HdStr.Len(); ChN++){printf("=");}
79  printf("\n");
80  }
81  }
82  // return
83  return Env.GetArgs()<=MnArgs;
84 }
85 
86 bool TEnv::IsArgStr(const TStr& ArgStr) {
87  int ArgN=0;
88  while (ArgN<GetArgs()){
89  if (GetArg(ArgN)==ArgStr){return true;}
90  ArgN++;
91  }
92  return false;
93 }
94 
95 bool TEnv::IsArgStr(const TStr& ArgStr, const TStr& DNm) {
96  if (Env.GetArgs()<=MnArgs){
97  // 'usage' argument message
98  if (!SilentP){
99  printf(" %s %s \n", ArgStr.CStr(), DNm.CStr());}
100  return false;
101  } else {
102  // argument & value message
103  bool Val=Env.IsArgStr(ArgStr);
104  TStr MsgStr=DNm+" ("+ArgStr+(Val?")=YES":")=NO");
105  if (!SilentP){TNotify::OnStatus(Notify, MsgStr);}
106  return Val;
107  }
108 }
109 
111  const TStr& PrefixStr, const TStr& DfVal, const TStr& DNm) const {
112  if (Env.GetArgs()<=MnArgs){
113  // 'usage' argument message
114  if (!SilentP){
115  printf(" %s%s (default:'%s')\n", PrefixStr.CStr(), DNm.CStr(), DfVal.CStr());}
116  return DfVal;
117  } else {
118  // argument & value message
119  TStr Val;
120  if (Env.IsArgPrefix(PrefixStr)){
121  Val=Env.GetArgPostfix(PrefixStr);
122  if (Val.Len()>1){
123  if ((Val[0]=='\"')&&(Val.LastCh()=='\"')){
124  Val=Val.GetSubStr(1, Val.Len()-2);
125  }
126  }
127  } else {
128  Val=DfVal;
129  }
130  TStr MsgStr=DNm+" ("+PrefixStr+")="+Val;
131  if (!SilentP){TNotify::OnStatus(Notify, MsgStr);}
132  return Val;
133  }
134 }
135 
137  const TStr& PrefixStr, TStrV& DfValV, const TStr& DNm) const {
138  TStrV ArgValV;
139  if (Env.GetArgs()<=MnArgs){
140  // 'usage' argument message
141  if (!SilentP){
142  printf(" %s%s (default:", PrefixStr.CStr(), DNm.CStr());
143  for (int DfValN=0; DfValN<DfValV.Len(); DfValN++){
144  if (DfValN>0){printf(", ");}
145  printf("'%s'", DfValV[DfValN].CStr());
146  }
147  printf(")\n");
148  }
149  return ArgValV;
150  } else {
151  // argument & value message
152  TStr ArgValVChA;
153  for (int ArgN=0; ArgN<GetArgs(); ArgN++){
154  // get argument string
155  TStr ArgStr=GetArg(ArgN);
156  if (ArgStr.GetSubStr(0, PrefixStr.Len()-1)==PrefixStr){
157  // extract & add argument value
158  TStr ArgVal=ArgStr.GetSubStr(PrefixStr.Len(), ArgStr.Len());
159  ArgValV.Add(ArgVal);
160  // add to message string
161  if (ArgValV.Len()>1){ArgValVChA+=", ";}
162  ArgValVChA+=ArgValV.Last();
163  }
164  }
165  if (ArgValV.Empty()){ArgValV=DfValV;}
166  // output argument values
167  TChA MsgChA;
168  MsgChA+=DNm; MsgChA+=" ("; MsgChA+=PrefixStr; MsgChA+=")=";
169  for (int ArgValN=0; ArgValN<ArgValV.Len(); ArgValN++){
170  if (ArgValN>0){MsgChA+=", ";}
171  MsgChA+="'"; MsgChA+=ArgValV[ArgValN]; MsgChA+="'";
172  }
173  if (!SilentP){TNotify::OnStatus(Notify, MsgChA);}
174  return ArgValV;
175  }
176 }
177 
179  const TStr& PrefixStr, const bool& DfVal, const TStr& DNm) const {
180  if (Env.GetArgs()<=MnArgs){
181  // 'usage' argument message
182  if (!SilentP){
183  printf(" %s%s (default:'%s')\n",
184  PrefixStr.CStr(), DNm.CStr(), TBool::GetStr(DfVal).CStr());}
185  return DfVal;
186  } else {
187  // argument & value message
188  bool Val;
189  if ((PrefixStr.Len()>0)&&(PrefixStr.LastCh()==':')){
190  if (Env.IsArgPrefix(PrefixStr)){
191  // try to find one of boolean string value representations
192  TStr ValStr=Env.GetArgPostfix(PrefixStr);
193  Val=TBool::GetValFromStr(ValStr, DfVal);
194  } else {
195  // remove ':' and try to find option
196  TStr RedPrefixStr=PrefixStr;
197  RedPrefixStr.DelSubStr(PrefixStr.Len()-1, PrefixStr.Len()-1);
198  if (Env.IsArgPrefix(RedPrefixStr)){Val=true;} else {Val=DfVal;}
199  }
200  } else {
201  if (Env.IsArgPrefix(PrefixStr)){Val=true;} else {Val=DfVal;}
202  }
203  TStr MsgStr=DNm+" ("+PrefixStr+")="+TBool::GetYesNoStr(Val);
204  if (!SilentP){TNotify::OnStatus(Notify, MsgStr);}
205  return Val;
206  }
207 }
208 
210  const TStr& PrefixStr, const int& DfVal, const TStr& DNm) const {
211  if (Env.GetArgs()<=MnArgs){
212  // 'usage' argument message
213  if (!SilentP){
214  printf(" %s%s (default:%d)\n", PrefixStr.CStr(), DNm.CStr(), DfVal);}
215  return DfVal;
216  } else {
217  // argument & value message
218  int Val;
219  if (Env.IsArgPrefix(PrefixStr)){
220  TStr ValStr=Env.GetArgPostfix(PrefixStr);
221  Val=ValStr.GetInt(DfVal);
222  } else {
223  Val=DfVal;
224  }
225  TStr MsgStr=DNm+" ("+PrefixStr+")="+TInt::GetStr(Val);
226  if (!SilentP){TNotify::OnStatus(Notify, MsgStr);}
227  return Val;
228  }
229 }
230 
232  const TStr& PrefixStr, TIntV& DfValV, const TStr& DNm) const {
233  // convert default-integer-values to default-string-values
234  TStrV DfValStrV;
235  for (int ValN=0; ValN<DfValV.Len(); ValN++){
236  DfValStrV.Add(TInt::GetStr(DfValV[ValN]));}
237  // get string-values
238  TStrV ValStrV=GetIfArgPrefixStrV(PrefixStr, DfValStrV, DNm);
239  // convert string-values to integer-values
240  TIntV ValV;
241  for (int ValN=0; ValN<ValStrV.Len(); ValN++){
242  int Val;
243  if (ValStrV[ValN].IsInt(Val)){
244  ValV.Add(Val);}
245  }
246  // return value-vector
247  return ValV;
248 }
249 
251  const TStr& PrefixStr, const double& DfVal, const TStr& DNm) const {
252  if (Env.GetArgs()<=MnArgs){
253  // 'usage' argument message
254  if (!SilentP){
255  printf(" %s%s (default:%g)\n", PrefixStr.CStr(), DNm.CStr(), DfVal);}
256  return DfVal;
257  } else {
258  // argument & value message
259  double Val;
260  if (Env.IsArgPrefix(PrefixStr)){
261  TStr ValStr=Env.GetArgPostfix(PrefixStr);
262  Val=ValStr.GetFlt(DfVal);
263  } else {
264  Val=DfVal;
265  }
266  TStr MsgStr=DNm+" ("+PrefixStr+")="+TFlt::GetStr(Val);
267  if (!SilentP){TNotify::OnStatus(Notify, MsgStr);}
268  return Val;
269  }
270 }
271 
272 void TEnv::GetVarNmV(TStrV& VarNmV){
273  VarNmV.Clr(); int VarN=0;
274  while (_environ[VarN]!=NULL){
275  TStr VarNmVal=_environ[VarN++];
276  TStr VarNm; TStr VarVal; VarNmVal.SplitOnCh(VarNm, '=', VarVal);
277  VarNmV.Add(VarNm);
278  }
279 }
280 
281 void TEnv::GetVarNmValV(TStrV& VarNmValV){
282  VarNmValV.Clr(); int VarN=0;
283  while (_environ[VarN]!=NULL){
284  VarNmValV.Add(_environ[VarN++]);
285  }
286 }
287 
288 void TEnv::PutVarVal(const TStr& VarNm, const TStr& VarVal) {
289  const int RetVal = putenv(TStr::Fmt("%s=%s", VarNm.CStr(), VarVal.CStr()).CStr());
290  IAssert(RetVal==0);
291 }
292 
293 TStr TEnv::GetVarVal(const TStr& VarNm) const {
294  return TStr(getenv(VarNm.CStr()));
295 }
296 
298 
int GetIfArgPrefixInt(const TStr &PrefixStr, const int &DfVal=0, const TStr &DNm="") const
Definition: env.cpp:209
#define IAssert(Cond)
Definition: bd.h:262
int GetInt() const
Definition: dt.h:581
void GetVarNmV(TStrV &VarNmV)
Definition: env.cpp:272
TStr GetStr() const
Definition: dt.h:1200
TStr GetIfArgPrefixStr(const TStr &PrefixStr, const TStr &DfVal="", const TStr &DNm="") const
Definition: env.cpp:110
int Len() const
Definition: dt.h:490
bool SilentP
Definition: env.h:10
bool IsPrefix(const char *Str) const
Definition: dt.cpp:1081
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
Definition: env.h:3
static TStr GetYesNoStr(const bool &Val)
Definition: dt.h:1017
bool IsEndOfRun() const
Definition: env.cpp:72
TStr GetSubStr(const int &BChN, const int &EChN) const
Definition: dt.cpp:811
void GetVarNmValV(TStrV &NmValV)
Definition: env.cpp:281
TIntV GetIfArgPrefixIntV(const TStr &PrefixStr, TIntV &DfValV, const TStr &DNm="") const
Definition: env.cpp:231
int GetPrefixArgN(const TStr &PrefixStr) const
Definition: env.cpp:31
TStr HdStr
Definition: env.h:8
static bool GetValFromStr(const TStr &Str)
Definition: dt.cpp:1850
TStr GetCmLn(const int &FromArgN=1) const
Definition: env.cpp:22
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:570
double GetFlt() const
Definition: dt.h:631
void SplitOnCh(TStr &LStr, const char &SplitCh, TStr &RStr) const
Definition: dt.cpp:901
int MnArgs
Definition: env.h:9
TEnv Env
Definition: env.cpp:297
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
TStr GetExeFNm() const
Definition: env.cpp:14
TStrV GetIfArgPrefixStrV(const TStr &PrefixStr, TStrV &DfValV, const TStr &DNm="") const
Definition: env.cpp:136
bool IsArgStr(const TStr &ArgStr)
Definition: env.cpp:86
double GetIfArgPrefixFlt(const TStr &PrefixStr, const double &DfVal=0, const TStr &DNm="") const
Definition: env.cpp:250
bool IsArgPrefix(const TStr &PrefixStr) const
Definition: env.h:37
char LastCh() const
Definition: dt.h:487
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:579
void PrepArgs(const TStr &_HdStr=TStr(), const int &_MnArgs=1, const bool &SilentP=false)
Definition: env.cpp:46
bool GetIfArgPrefixBool(const TStr &PrefixStr, const bool &DfVal=false, const TStr &DNm="") const
Definition: env.cpp:178
void DelSubStr(const int &BChN, const int &EChN)
Definition: dt.cpp:850
TStr GetArgPostfix(const TStr &PrefixStr) const
Definition: env.cpp:40
Definition: dt.h:201
TStr GetVarVal(const TStr &VarNm) const
Definition: env.cpp:293
TStrV ArgV
Definition: env.h:7
TEnv()
Definition: env.h:13
Definition: dt.h:412
bool Empty() const
Definition: dt.h:491
PNotify Notify
Definition: env.h:11
virtual void OnStatus(const TStr &)
Definition: ut.h:39
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
static void PutVarVal(const TStr &VarNm, const TStr &VarVal)
Definition: env.cpp:288
void SplitOnAllCh(const char &SplitCh, TStrV &StrV, const bool &SkipEmpty=true) const
Definition: dt.cpp:926
int GetArgs() const
Definition: env.h:25
static TStr GetStr(const bool &Val)
Definition: dt.h:1011
TStr GetStr() const
Definition: dt.h:1462
char * CStr()
Definition: dt.h:479
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
TStr GetFBase() const
Definition: dt.cpp:1396
TStr GetArg(const int &ArgN) const
Definition: env.h:28