SNAP Library 2.0, User Reference  2013-05-13 16:33:57
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
macro.cpp
Go to the documentation of this file.
00001 
00002 // Macro-Processor
00003 TMacro::TMacro(const TStr& _TxtStr, const char& _MacroCh, const char& _VarCh):
00004   Ok(true), MsgStr("Ok"), MacroCh(_MacroCh), VarCh(_VarCh),
00005   TxtStr(_TxtStr), SubstToValStrH(10), VarNmToValStrH(10){
00006   int TxtStrLen=TxtStr.Len(); int TxtStrChN=0;
00007   while ((Ok)&&(TxtStrChN<TxtStrLen)){
00008     if (TxtStr[TxtStrChN]==MacroCh){
00009       // extract substitution from text
00010       TChA SubstChA; TxtStrChN++;
00011       while ((TxtStrChN<TxtStrLen)&&(TxtStr[TxtStrChN]!=MacroCh)){
00012         SubstChA+=TxtStr[TxtStrChN]; TxtStrChN++;}
00013       Ok=(TxtStrChN<TxtStrLen);
00014       if (!Ok){MsgStr=SubstChA; break;}
00015       TxtStrChN++;
00016       SubstToValStrH.AddDat(SubstChA);
00017       // extract variable name from substitution
00018       int SubstChN=0;
00019       while ((Ok)&&(SubstChN<SubstChA.Len())){
00020         if (SubstChA[SubstChN]==VarCh){
00021           TChA VarNmChA; SubstChN++;
00022           while ((SubstChN<SubstChA.Len())&&(SubstChA[SubstChN]!=VarCh)){
00023             VarNmChA+=SubstChA[SubstChN]; SubstChN++;}
00024           Ok=(SubstChN<SubstChA.Len());
00025           if (!Ok){MsgStr=VarNmChA; break;}
00026           SubstChN++;
00027           VarNmToValStrH.AddDat(VarNmChA);
00028         } else {
00029           SubstChN++;
00030         }
00031       }
00032     } else {
00033       TxtStrChN++;
00034     }
00035   }
00036 }
00037 
00038 TStr TMacro::GetDstTxtStr() const {
00039   int TxtStrLen=TxtStr.Len(); int TxtStrChN=0;
00040   TChA DstTxtChA;
00041   while (TxtStrChN<TxtStrLen){
00042     if (TxtStr[TxtStrChN]==MacroCh){
00043       TChA SubstChA; TxtStrChN++;
00044       while ((TxtStrChN<TxtStrLen)&&(TxtStr[TxtStrChN]!=MacroCh)){
00045         SubstChA+=TxtStr[TxtStrChN]; TxtStrChN++;}
00046       TxtStrChN++;
00047       int SubstKeyId;
00048       if (SubstToValStrH.IsKey(SubstChA, SubstKeyId)){
00049         DstTxtChA+=SubstToValStrH[SubstKeyId];
00050       } else {
00051         DstTxtChA+=MacroCh; DstTxtChA+=SubstChA; DstTxtChA+=MacroCh;
00052       }
00053     } else {
00054       DstTxtChA+=TxtStr[TxtStrChN]; TxtStrChN++;
00055     }
00056   }
00057   return DstTxtChA;
00058 }
00059 
00060 void TMacro::GetSrcSubstStrV(TStrV& SubstStrV) const {
00061   SubstStrV.Gen(GetSubstStrs(), 0);
00062   for (int SubstStrN=0; SubstStrN<GetSubstStrs(); SubstStrN++){
00063     SubstStrV.Add(GetSrcSubstStr(SubstStrN));
00064   }
00065 }
00066 
00067 TStr TMacro::GetDstSubstStr(const int& SubstStrN) const {
00068   TStr SrcSubstStr=SubstToValStrH.GetKey(SubstStrN);
00069   int SrcSubstStrLen=SrcSubstStr.Len();
00070   int SrcSubstChN=0;
00071   TChA DstSubstChA;
00072   while (SrcSubstChN<SrcSubstStrLen){
00073     if (SrcSubstStr[SrcSubstChN]==VarCh){
00074       TChA VarNmChA; SrcSubstChN++;
00075       while ((SrcSubstChN<SrcSubstStrLen)&&(SrcSubstStr[SrcSubstChN]!=VarCh)){
00076         VarNmChA+=SrcSubstStr[SrcSubstChN]; SrcSubstChN++;}
00077       IAssert(SrcSubstChN<SrcSubstStrLen); SrcSubstChN++;
00078       TStr VarVal=GetVarVal(VarNmChA);
00079       DstSubstChA+=VarVal;
00080     } else {
00081       DstSubstChA+=SrcSubstStr[SrcSubstChN]; SrcSubstChN++;
00082     }
00083   }
00084   return DstSubstChA;
00085 }
00086 
00087 TStr TMacro::GetAllSubstValStr() const {
00088   TChA AllSubstValChA;
00089   for (int SubstStrN=0; SubstStrN<GetSubstStrs(); SubstStrN++){
00090     if (SubstStrN>0){AllSubstValChA+=", ";}
00091     AllSubstValChA+=GetSubstValStr(SubstStrN);
00092   }
00093   return AllSubstValChA;
00094 }
00095 
00096 void TMacro::GetVarNmV(TStrV& VarNmV) const {
00097   VarNmV.Gen(GetVars(), 0);
00098   for (int VarN=0; VarN<GetVars(); VarN++){
00099     VarNmV.Add(GetVarNm(VarN));
00100   }
00101 }
00102 
00103 void TMacro::SplitVarNm(
00104  const TStr& VarNm, TStr& CapStr,
00105  bool& IsComboBox, TStr& TbNm, TStr& ListFldNm, TStr& DataFldNm){
00106   if (VarNm.SearchCh(':')==-1){
00107     CapStr=VarNm; IsComboBox=false; TbNm=""; ListFldNm=""; DataFldNm="";
00108   } else {
00109     int FirstColonChN=VarNm.SearchCh(':');
00110     int SecondColonChN=VarNm.SearchCh(':', FirstColonChN+1);
00111     int ThirdColonChN=VarNm.SearchCh(':', SecondColonChN+1);
00112     IAssert((FirstColonChN!=-1)&&(SecondColonChN!=-1));
00113     if (ThirdColonChN==-1){ThirdColonChN=VarNm.Len();}
00114     CapStr=VarNm.GetSubStr(0, FirstColonChN-1);
00115     IsComboBox=true;
00116     TbNm=VarNm.GetSubStr(FirstColonChN+1, SecondColonChN-1);
00117     ListFldNm=VarNm.GetSubStr(SecondColonChN+1, ThirdColonChN-1);
00118     if (ThirdColonChN!=VarNm.Len()){
00119       DataFldNm=VarNm.GetSubStr(ThirdColonChN+1, VarNm.Len()-1);
00120     } else {
00121       DataFldNm=ListFldNm;
00122     }
00123   }
00124 }
00125