SNAP Library 2.3, Developer Reference  2014-06-16 11:58:46
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
macro.cpp
Go to the documentation of this file.
1 // Macro-Processor
3 TMacro::TMacro(const TStr& _TxtStr, const char& _MacroCh, const char& _VarCh):
4  Ok(true), MsgStr("Ok"), MacroCh(_MacroCh), VarCh(_VarCh),
5  TxtStr(_TxtStr), SubstToValStrH(10), VarNmToValStrH(10){
6  int TxtStrLen=TxtStr.Len(); int TxtStrChN=0;
7  while ((Ok)&&(TxtStrChN<TxtStrLen)){
8  if (TxtStr[TxtStrChN]==MacroCh){
9  // extract substitution from text
10  TChA SubstChA; TxtStrChN++;
11  while ((TxtStrChN<TxtStrLen)&&(TxtStr[TxtStrChN]!=MacroCh)){
12  SubstChA+=TxtStr[TxtStrChN]; TxtStrChN++;}
13  Ok=(TxtStrChN<TxtStrLen);
14  if (!Ok){MsgStr=SubstChA; break;}
15  TxtStrChN++;
16  SubstToValStrH.AddDat(SubstChA);
17  // extract variable name from substitution
18  int SubstChN=0;
19  while ((Ok)&&(SubstChN<SubstChA.Len())){
20  if (SubstChA[SubstChN]==VarCh){
21  TChA VarNmChA; SubstChN++;
22  while ((SubstChN<SubstChA.Len())&&(SubstChA[SubstChN]!=VarCh)){
23  VarNmChA+=SubstChA[SubstChN]; SubstChN++;}
24  Ok=(SubstChN<SubstChA.Len());
25  if (!Ok){MsgStr=VarNmChA; break;}
26  SubstChN++;
27  VarNmToValStrH.AddDat(VarNmChA);
28  } else {
29  SubstChN++;
30  }
31  }
32  } else {
33  TxtStrChN++;
34  }
35  }
36 }
37 
39  int TxtStrLen=TxtStr.Len(); int TxtStrChN=0;
40  TChA DstTxtChA;
41  while (TxtStrChN<TxtStrLen){
42  if (TxtStr[TxtStrChN]==MacroCh){
43  TChA SubstChA; TxtStrChN++;
44  while ((TxtStrChN<TxtStrLen)&&(TxtStr[TxtStrChN]!=MacroCh)){
45  SubstChA+=TxtStr[TxtStrChN]; TxtStrChN++;}
46  TxtStrChN++;
47  int SubstKeyId;
48  if (SubstToValStrH.IsKey(SubstChA, SubstKeyId)){
49  DstTxtChA+=SubstToValStrH[SubstKeyId];
50  } else {
51  DstTxtChA+=MacroCh; DstTxtChA+=SubstChA; DstTxtChA+=MacroCh;
52  }
53  } else {
54  DstTxtChA+=TxtStr[TxtStrChN]; TxtStrChN++;
55  }
56  }
57  return DstTxtChA;
58 }
59 
60 void TMacro::GetSrcSubstStrV(TStrV& SubstStrV) const {
61  SubstStrV.Gen(GetSubstStrs(), 0);
62  for (int SubstStrN=0; SubstStrN<GetSubstStrs(); SubstStrN++){
63  SubstStrV.Add(GetSrcSubstStr(SubstStrN));
64  }
65 }
66 
67 TStr TMacro::GetDstSubstStr(const int& SubstStrN) const {
68  TStr SrcSubstStr=SubstToValStrH.GetKey(SubstStrN);
69  int SrcSubstStrLen=SrcSubstStr.Len();
70  int SrcSubstChN=0;
71  TChA DstSubstChA;
72  while (SrcSubstChN<SrcSubstStrLen){
73  if (SrcSubstStr[SrcSubstChN]==VarCh){
74  TChA VarNmChA; SrcSubstChN++;
75  while ((SrcSubstChN<SrcSubstStrLen)&&(SrcSubstStr[SrcSubstChN]!=VarCh)){
76  VarNmChA+=SrcSubstStr[SrcSubstChN]; SrcSubstChN++;}
77  IAssert(SrcSubstChN<SrcSubstStrLen); SrcSubstChN++;
78  TStr VarVal=GetVarVal(VarNmChA);
79  DstSubstChA+=VarVal;
80  } else {
81  DstSubstChA+=SrcSubstStr[SrcSubstChN]; SrcSubstChN++;
82  }
83  }
84  return DstSubstChA;
85 }
86 
88  TChA AllSubstValChA;
89  for (int SubstStrN=0; SubstStrN<GetSubstStrs(); SubstStrN++){
90  if (SubstStrN>0){AllSubstValChA+=", ";}
91  AllSubstValChA+=GetSubstValStr(SubstStrN);
92  }
93  return AllSubstValChA;
94 }
95 
96 void TMacro::GetVarNmV(TStrV& VarNmV) const {
97  VarNmV.Gen(GetVars(), 0);
98  for (int VarN=0; VarN<GetVars(); VarN++){
99  VarNmV.Add(GetVarNm(VarN));
100  }
101 }
102 
104  const TStr& VarNm, TStr& CapStr,
105  bool& IsComboBox, TStr& TbNm, TStr& ListFldNm, TStr& DataFldNm){
106  if (VarNm.SearchCh(':')==-1){
107  CapStr=VarNm; IsComboBox=false; TbNm=""; ListFldNm=""; DataFldNm="";
108  } else {
109  int FirstColonChN=VarNm.SearchCh(':');
110  int SecondColonChN=VarNm.SearchCh(':', FirstColonChN+1);
111  int ThirdColonChN=VarNm.SearchCh(':', SecondColonChN+1);
112  IAssert((FirstColonChN!=-1)&&(SecondColonChN!=-1));
113  if (ThirdColonChN==-1){ThirdColonChN=VarNm.Len();}
114  CapStr=VarNm.GetSubStr(0, FirstColonChN-1);
115  IsComboBox=true;
116  TbNm=VarNm.GetSubStr(FirstColonChN+1, SecondColonChN-1);
117  ListFldNm=VarNm.GetSubStr(SecondColonChN+1, ThirdColonChN-1);
118  if (ThirdColonChN!=VarNm.Len()){
119  DataFldNm=VarNm.GetSubStr(ThirdColonChN+1, VarNm.Len()-1);
120  } else {
121  DataFldNm=ListFldNm;
122  }
123  }
124 }
125 
#define IAssert(Cond)
Definition: bd.h:262
int SearchCh(const char &Ch, const int &BChN=0) const
Definition: dt.cpp:1043
int Len() const
Definition: dt.h:487
char VarCh
Definition: macro.h:10
TStr GetAllSubstValStr() const
Definition: macro.cpp:87
int Len() const
Definition: dt.h:259
TStr GetSubStr(const int &BChN, const int &EChN) const
Definition: dt.cpp:811
TStrStrH SubstToValStrH
Definition: macro.h:12
TStr GetVarVal(const TStr &VarNm) const
Definition: macro.h:52
TStr GetSrcSubstStr(const int &SubstStrN) const
Definition: macro.h:33
TStrStrH VarNmToValStrH
Definition: macro.h:13
static void SplitVarNm(const TStr &VarNm, TStr &CapStr, bool &IsComboBox, TStr &TbNm, TStr &ListFldNm, TStr &DataFldNm)
Definition: macro.cpp:103
bool Ok
Definition: macro.h:7
char MacroCh
Definition: macro.h:9
TStr GetVarNm(const int &VarN) const
Definition: macro.h:48
void GetVarNmV(TStrV &VarNmV) const
Definition: macro.cpp:96
TStr MsgStr
Definition: macro.h:8
int GetVars() const
Definition: macro.h:47
Definition: dt.h:201
TStr GetDstTxtStr() const
Definition: macro.cpp:38
TStr GetDstSubstStr(const int &SubstStrN=0) const
Definition: macro.cpp:67
Definition: dt.h:412
TMacro(const TStr &TxtStr, const char &_MacroCh='$', const char &_VarCh='#')
Definition: macro.cpp:3
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:486
TStr TxtStr
Definition: macro.h:11
int GetSubstStrs() const
Definition: macro.h:32
bool IsKey(const TKey &Key) const
Definition: hash.h:216
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:559
TStr GetSubstValStr(const int &SubstStrN) const
Definition: macro.h:43
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:210
void GetSrcSubstStrV(TStrV &SubstStrV) const
Definition: macro.cpp:60