SNAP Library 2.1, Developer Reference  2013-09-25 10:47:25
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TEnv Class Reference

#include <env.h>

Collaboration diagram for TEnv:

List of all members.

Public Member Functions

 TEnv ()
 TEnv (const TEnv &)
 TEnv (const int &_Args, char **_ArgV, const PNotify &_Notify=NULL)
 TEnv (const TStr &_ArgStr, const PNotify &_Notify=NULL)
TEnvoperator= (const TEnv &Env)
int GetArgs () const
TStr GetArg (const int &ArgN) const
TStr GetExeFNm () const
TStr GetExeFPath () const
TStr GetCmLn (const int &FromArgN=1) const
bool IsSilent () const
int GetPrefixArgN (const TStr &PrefixStr) const
bool IsArgPrefix (const TStr &PrefixStr) const
TStr GetArgPostfix (const TStr &PrefixStr) const
void PrepArgs (const TStr &_HdStr=TStr(), const int &_MnArgs=1, const bool &SilentP=false)
TStr GetHdStr () const
int GetMnArgs () const
bool IsEndOfRun () const
bool IsArgStr (const TStr &ArgStr)
bool IsArgStr (const TStr &ArgStr, const TStr &DNm)
TStr GetIfArgPrefixStr (const TStr &PrefixStr, const TStr &DfVal="", const TStr &DNm="") const
TStrV GetIfArgPrefixStrV (const TStr &PrefixStr, TStrV &DfValV, const TStr &DNm="") const
TStrV GetIfArgPrefixStrV (const TStr &PrefixStr, const TStr &DNm="") const
TStrSet GetIfArgPrefixStrSet (const TStr &PrefixStr, TStrV &DfValV, const TStr &DNm="") const
TStrSet GetIfArgPrefixStrSet (const TStr &PrefixStr, const TStr &DNm="") const
bool GetIfArgPrefixBool (const TStr &PrefixStr, const bool &DfVal=false, const TStr &DNm="") const
int GetIfArgPrefixInt (const TStr &PrefixStr, const int &DfVal=0, const TStr &DNm="") const
TIntV GetIfArgPrefixIntV (const TStr &PrefixStr, TIntV &DfValV, const TStr &DNm="") const
TIntV GetIfArgPrefixIntV (const TStr &PrefixStr, const TStr &DNm="") const
double GetIfArgPrefixFlt (const TStr &PrefixStr, const double &DfVal=0, const TStr &DNm="") const
TStr GetVarVal (const TStr &VarNm) const
void GetVarNmV (TStrV &VarNmV)
void GetVarNmValV (TStrV &NmValV)

Static Public Member Functions

static void PutVarVal (const TStr &VarNm, const TStr &VarVal)

Private Attributes

TStrV ArgV
TStr HdStr
int MnArgs
bool SilentP
PNotify Notify

Detailed Description

Definition at line 3 of file env.h.


Constructor & Destructor Documentation

TEnv::TEnv ( ) [inline]

Definition at line 13 of file env.h.

: /*Args(0), ArgV(NULL)*/ ArgV(), HdStr(), MnArgs(1), SilentP(false), Notify(){}
TEnv::TEnv ( const TEnv )
TEnv::TEnv ( const int &  _Args,
char **  _ArgV,
const PNotify _Notify = NULL 
)

Definition at line 3 of file env.cpp.

References TVec< TVal, TSizeTy >::Add(), and ArgV.

                                                                :
    ArgV(), HdStr(), MnArgs(1), SilentP(false), Notify(_Notify) {
  for (int ArgN = 0; ArgN < _Args; ArgN++)
      ArgV.Add(TStr(_ArgV[ArgN]));
}

Here is the call graph for this function:

TEnv::TEnv ( const TStr _ArgStr,
const PNotify _Notify = NULL 
)

Definition at line 9 of file env.cpp.

References ArgV, and TStr::SplitOnAllCh().

                                                     :
    ArgV(), HdStr(), MnArgs(1), SilentP(false), Notify(_Notify) {
  _ArgStr.SplitOnAllCh(' ', ArgV);
}

Here is the call graph for this function:


Member Function Documentation

TStr TEnv::GetArg ( const int &  ArgN) const [inline]

Definition at line 28 of file env.h.

References ArgV, GetArgs(), and IAssert.

Referenced by GetArgPostfix(), GetCmLn(), GetExeFNm(), GetIfArgPrefixStrV(), GetPrefixArgN(), IsArgStr(), and PrepArgs().

                                     {
    IAssert((0<=ArgN)&&(ArgN<GetArgs())); return ArgV[ArgN];}

Here is the call graph for this function:

Here is the caller graph for this function:

TStr TEnv::GetArgPostfix ( const TStr PrefixStr) const

Definition at line 40 of file env.cpp.

References GetArg(), GetPrefixArgN(), TStr::GetSubStr(), IAssert, and TStr::Len().

Referenced by GetIfArgPrefixBool(), GetIfArgPrefixFlt(), GetIfArgPrefixInt(), and GetIfArgPrefixStr().

                                                    {
  int ArgN=GetPrefixArgN(PrefixStr); IAssert(ArgN!=-1);
  TStr ArgStr=GetArg(ArgN);
  return ArgStr.GetSubStr(PrefixStr.Len(), ArgStr.Len());
}

Here is the call graph for this function:

Here is the caller graph for this function:

int TEnv::GetArgs ( ) const [inline]

Definition at line 25 of file env.h.

References ArgV, and TVec< TVal, TSizeTy >::Len().

Referenced by GetArg(), GetCmLn(), GetIfArgPrefixBool(), GetIfArgPrefixFlt(), GetIfArgPrefixInt(), GetIfArgPrefixStr(), GetIfArgPrefixStrV(), GetPrefixArgN(), IsArgStr(), IsEndOfRun(), and PrepArgs().

{return ArgV.Len();}

Here is the call graph for this function:

Here is the caller graph for this function:

TStr TEnv::GetCmLn ( const int &  FromArgN = 1) const

Definition at line 22 of file env.cpp.

References GetArg(), and GetArgs().

                                            {
  TChA CmLnChA;
  for (int ArgN=FromArgN; ArgN<GetArgs(); ArgN++){
    if (ArgN>FromArgN){CmLnChA+=' ';}
    CmLnChA+=GetArg(ArgN);
  }
  return CmLnChA;
}

Here is the call graph for this function:

TStr TEnv::GetExeFNm ( ) const

Definition at line 14 of file env.cpp.

References GetArg(), TStr::GetSubStr(), TStr::IsPrefix(), and TStr::Len().

Referenced by GetExeFPath().

                           {
  TStr ExeFNm=GetArg(0);
  if (ExeFNm.IsPrefix("//?")){ // observed on Win64 CGI
    ExeFNm=ExeFNm.GetSubStr(3, ExeFNm.Len());
  }
  return ExeFNm;
}

Here is the call graph for this function:

Here is the caller graph for this function:

TStr TEnv::GetExeFPath ( ) const [inline]

Definition at line 31 of file env.h.

References GetExeFNm(), and TStr::GetFPath().

{return GetExeFNm().GetFPath();}

Here is the call graph for this function:

TStr TEnv::GetHdStr ( ) const [inline]

Definition at line 44 of file env.h.

References HdStr.

{return HdStr;}
bool TEnv::GetIfArgPrefixBool ( const TStr PrefixStr,
const bool &  DfVal = false,
const TStr DNm = "" 
) const

Definition at line 178 of file env.cpp.

References TStr::CStr(), TStr::DelSubStr(), Env, GetArgPostfix(), GetArgs(), TBool::GetStr(), TBool::GetValFromStr(), TBool::GetYesNoStr(), IsArgPrefix(), TStr::LastCh(), TStr::Len(), MnArgs, Notify, TNotify::OnStatus(), and SilentP.

Referenced by PrepArgs().

                                                                  {
  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;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

double TEnv::GetIfArgPrefixFlt ( const TStr PrefixStr,
const double &  DfVal = 0,
const TStr DNm = "" 
) const

Definition at line 250 of file env.cpp.

References TStr::CStr(), Env, GetArgPostfix(), GetArgs(), TStr::GetFlt(), TFlt::GetStr(), IsArgPrefix(), MnArgs, Notify, TNotify::OnStatus(), and SilentP.

                                                                    {
  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;
  }
}

Here is the call graph for this function:

int TEnv::GetIfArgPrefixInt ( const TStr PrefixStr,
const int &  DfVal = 0,
const TStr DNm = "" 
) const

Definition at line 209 of file env.cpp.

References TStr::CStr(), Env, GetArgPostfix(), GetArgs(), TStr::GetInt(), TInt::GetStr(), IsArgPrefix(), MnArgs, Notify, TNotify::OnStatus(), and SilentP.

                                                                 {
  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;
  }
}

Here is the call graph for this function:

TIntV TEnv::GetIfArgPrefixIntV ( const TStr PrefixStr,
TIntV DfValV,
const TStr DNm = "" 
) const

Definition at line 231 of file env.cpp.

References TVec< TVal, TSizeTy >::Add(), GetIfArgPrefixStrV(), TInt::GetStr(), and TVec< TVal, TSizeTy >::Len().

Referenced by GetIfArgPrefixIntV().

                                                              {
  // 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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

TIntV TEnv::GetIfArgPrefixIntV ( const TStr PrefixStr,
const TStr DNm = "" 
) const [inline]

Definition at line 62 of file env.h.

References GetIfArgPrefixIntV().

                                                                            {
    TIntV DfValV; return GetIfArgPrefixIntV(PrefixStr, DfValV, DNm);}

Here is the call graph for this function:

TStr TEnv::GetIfArgPrefixStr ( const TStr PrefixStr,
const TStr DfVal = "",
const TStr DNm = "" 
) const

Definition at line 110 of file env.cpp.

References TStr::CStr(), Env, GetArgPostfix(), GetArgs(), TStr::GetSubStr(), IsArgPrefix(), TStr::LastCh(), TStr::Len(), MnArgs, Notify, TNotify::OnStatus(), and SilentP.

                                                                  {
  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;
  }
}

Here is the call graph for this function:

TStrSet TEnv::GetIfArgPrefixStrSet ( const TStr PrefixStr,
TStrV DfValV,
const TStr DNm = "" 
) const [inline]

Definition at line 55 of file env.h.

References GetIfArgPrefixStrV().

                                                                                               {
    return TStrSet(GetIfArgPrefixStrV(PrefixStr, DfValV, DNm));}

Here is the call graph for this function:

TStrSet TEnv::GetIfArgPrefixStrSet ( const TStr PrefixStr,
const TStr DNm = "" 
) const [inline]

Definition at line 57 of file env.h.

References GetIfArgPrefixStrV().

                                                                                {
    return TStrSet(GetIfArgPrefixStrV(PrefixStr, DNm));}

Here is the call graph for this function:

TStrV TEnv::GetIfArgPrefixStrV ( const TStr PrefixStr,
TStrV DfValV,
const TStr DNm = "" 
) const

Definition at line 136 of file env.cpp.

References TVec< TVal, TSizeTy >::Add(), TStr::CStr(), TVec< TVal, TSizeTy >::Empty(), Env, GetArg(), GetArgs(), TStr::GetSubStr(), TVec< TVal, TSizeTy >::Last(), TStr::Len(), TVec< TVal, TSizeTy >::Len(), MnArgs, Notify, TNotify::OnStatus(), and SilentP.

Referenced by GetIfArgPrefixIntV(), GetIfArgPrefixStrSet(), and GetIfArgPrefixStrV().

                                                              {
  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;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

TStrV TEnv::GetIfArgPrefixStrV ( const TStr PrefixStr,
const TStr DNm = "" 
) const [inline]

Definition at line 53 of file env.h.

References GetIfArgPrefixStrV().

                                                                            {
    TStrV DfValV; return GetIfArgPrefixStrV(PrefixStr, DfValV, DNm);}

Here is the call graph for this function:

int TEnv::GetMnArgs ( ) const [inline]

Definition at line 45 of file env.h.

References MnArgs.

{return MnArgs;}
int TEnv::GetPrefixArgN ( const TStr PrefixStr) const

Definition at line 31 of file env.cpp.

References GetArg(), GetArgs(), and TStr::Len().

Referenced by GetArgPostfix(), and IsArgPrefix().

                                                   {
  int ArgN=0;
  while (ArgN<GetArgs()){
    if (GetArg(ArgN).GetSubStr(0, PrefixStr.Len()-1)==PrefixStr){return ArgN;}
    ArgN++;
  }
  return -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void TEnv::GetVarNmV ( TStrV VarNmV)

Definition at line 272 of file env.cpp.

References TVec< TVal, TSizeTy >::Add(), TVec< TVal, TSizeTy >::Clr(), and TStr::SplitOnCh().

                                 {
  VarNmV.Clr(); int VarN=0;
  while (_environ[VarN]!=NULL){
    TStr VarNmVal=_environ[VarN++];
    TStr VarNm; TStr VarVal; VarNmVal.SplitOnCh(VarNm, '=', VarVal);
    VarNmV.Add(VarNm);
  }
}

Here is the call graph for this function:

void TEnv::GetVarNmValV ( TStrV NmValV)

Definition at line 281 of file env.cpp.

References TVec< TVal, TSizeTy >::Add(), and TVec< TVal, TSizeTy >::Clr().

                                       {
  VarNmValV.Clr(); int VarN=0;
  while (_environ[VarN]!=NULL){
    VarNmValV.Add(_environ[VarN++]);
  }
}

Here is the call graph for this function:

TStr TEnv::GetVarVal ( const TStr VarNm) const

Definition at line 293 of file env.cpp.

References TStr::CStr().

                                            {
  return TStr(getenv(VarNm.CStr()));
}

Here is the call graph for this function:

bool TEnv::IsArgPrefix ( const TStr PrefixStr) const [inline]

Definition at line 37 of file env.h.

References GetPrefixArgN().

Referenced by GetIfArgPrefixBool(), GetIfArgPrefixFlt(), GetIfArgPrefixInt(), and GetIfArgPrefixStr().

                                                {
    return GetPrefixArgN(PrefixStr)!=-1;}

Here is the call graph for this function:

Here is the caller graph for this function:

bool TEnv::IsArgStr ( const TStr ArgStr)

Definition at line 86 of file env.cpp.

References GetArg(), and GetArgs().

Referenced by IsArgStr().

                                      {
  int ArgN=0;
  while (ArgN<GetArgs()){
    if (GetArg(ArgN)==ArgStr){return true;}
    ArgN++;
  }
  return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool TEnv::IsArgStr ( const TStr ArgStr,
const TStr DNm 
)

Definition at line 95 of file env.cpp.

References TStr::CStr(), Env, GetArgs(), IsArgStr(), MnArgs, Notify, TNotify::OnStatus(), and SilentP.

                                                       {
  if (Env.GetArgs()<=MnArgs){
    // 'usage' argument message
    if (!SilentP){
      printf("   %s %s \n", ArgStr.CStr(), DNm.CStr());}
    return false;
  } else {
    // argument & value message
    bool Val=Env.IsArgStr(ArgStr);
    TStr MsgStr=DNm+" ("+ArgStr+(Val?")=YES":")=NO");
    if (!SilentP){TNotify::OnStatus(Notify, MsgStr);}
    return Val;
  }
}

Here is the call graph for this function:

bool TEnv::IsEndOfRun ( ) const

Definition at line 72 of file env.cpp.

References TStr::Empty(), Env, GetArgs(), HdStr, TStr::Len(), MnArgs, and SilentP.

                            {
  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;
}

Here is the call graph for this function:

bool TEnv::IsSilent ( ) const [inline]

Definition at line 33 of file env.h.

References SilentP.

{return SilentP;}
TEnv& TEnv::operator= ( const TEnv Env) [inline]

Definition at line 19 of file env.h.

References ArgV, HdStr, MnArgs, Notify, and SilentP.

                                  {
    /*Args=Env.Args;*/ ArgV=Env.ArgV; HdStr=Env.HdStr;
    MnArgs=Env.MnArgs; SilentP=Env.SilentP; Notify=Env.Notify; return *this;}
void TEnv::PrepArgs ( const TStr _HdStr = TStr(),
const int &  _MnArgs = 1,
const bool &  SilentP = false 
)

Definition at line 46 of file env.cpp.

References TStr::CStr(), TStr::Empty(), Env, GetArg(), GetArgs(), TStr::GetFBase(), GetIfArgPrefixBool(), HdStr, TStr::Len(), MnArgs, and 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());
    }
  }
}

Here is the call graph for this function:

void TEnv::PutVarVal ( const TStr VarNm,
const TStr VarVal 
) [static]

Definition at line 288 of file env.cpp.

References TStr::CStr(), TStr::Fmt(), and IAssert.

                                                          {
  const int RetVal = putenv(TStr::Fmt("%s=%s", VarNm.CStr(), VarVal.CStr()).CStr());
  IAssert(RetVal==0);
}

Here is the call graph for this function:


Member Data Documentation

TStrV TEnv::ArgV [private]

Definition at line 7 of file env.h.

Referenced by GetArg(), GetArgs(), operator=(), and TEnv().

TStr TEnv::HdStr [private]

Definition at line 8 of file env.h.

Referenced by GetHdStr(), IsEndOfRun(), operator=(), and PrepArgs().


The documentation for this class was generated from the following files: