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
|
#include <lx.h>
Public Member Functions | |
TPreproc (const TStr &InFNm, const TStr &OutFNm, const TStr &SubstFNm, const TStrV &_SubstKeyIdV) | |
Static Public Member Functions | |
static void | Execute (const TStr &InFNm, const TStr &OutFNm, const TStr &InSubstFNm, const TStrV &SubstKeyIdV) |
Private Member Functions | |
char | GetCh () |
bool | IsSubstId (const TStr &SubstId, TStr &SubstValStr) const |
UndefDefaultCopyAssign (TPreproc) | |
Private Attributes | |
PSIn | SIn |
TStrV | SubstKeyIdV |
char | PrevCh |
char | Ch |
THash< TStr, TStrPrV > | SubstIdToKeyIdValPrVH |
TPreproc::TPreproc | ( | const TStr & | InFNm, |
const TStr & | OutFNm, | ||
const TStr & | SubstFNm, | ||
const TStrV & | _SubstKeyIdV | ||
) |
Definition at line 857 of file lx.cpp.
References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), Ch, TStr::Empty(), TCh::EofCh, TXmlTok::GetArgVal(), GetCh(), TXmlTok::GetTagTokV(), TXmlTok::GetTokStr(), IAssert, TVec< TVal, TSizeTy >::IsIn(), IsSubstId(), TVec< TVal, TSizeTy >::Len(), TXmlDoc::LoadTxt(), TFIn::New(), TFOut::New(), PrevCh, TSOut::PutCh(), TSOut::PutStr(), SIn, SubstIdToKeyIdValPrVH, SubstKeyIdV, and TChA::Trunc().
: SIn(), SubstKeyIdV(_SubstKeyIdV), PrevCh('\0'), Ch('\0'){ // load substitution file if (!SubstFNm.Empty()){ PXmlDoc XmlDoc=TXmlDoc::LoadTxt(SubstFNm); // get list of substitutions TXmlTokV SubstTokV; XmlDoc->GetTok()->GetTagTokV("Subst", SubstTokV); for (int SubstTokN=0; SubstTokN<SubstTokV.Len(); SubstTokN++){ PXmlTok SubstTok=SubstTokV[SubstTokN]; // get substitution-id TStr SubstId=SubstTok->GetArgVal("Id", ""); if (!SubstId.Empty()){ // create substitution TStrPrV& KeyIdValPrV=SubstIdToKeyIdValPrVH.AddDat(SubstId); // get list of substitution-strings TXmlTokV StrTokV; SubstTok->GetTagTokV("Str", StrTokV); for (int StrTokN=0; StrTokN<StrTokV.Len(); StrTokN++){ PXmlTok StrTok=StrTokV[StrTokN]; // get key-value pair TStr KeyId=StrTok->GetArgVal("Key", ""); TStr ValStr=StrTok->GetTokStr(false); // assign key-value-pair if (!KeyId.Empty()){ KeyIdValPrV.Add(TStrPr(KeyId, ValStr)); } } } } } // substitution // open files SIn=TFIn::New(InFNm); PSOut SOut=TFOut::New(OutFNm); // set copy & ignore mode bool CopyModeP=false; bool IgnoreModeP=false; GetCh(); while (Ch!=TCh::EofCh){ if (isalpha(Ch)||(((PrevCh=='\0')||(PrevCh=='\r')||(PrevCh=='\n'))&&(Ch=='#'))){ // collect identifier TChA IdChA; do { IdChA+=Ch; GetCh(); } while ((Ch!=TCh::EofCh)&&(isalnum(Ch))); // check identifier if (IdChA=="#ifdef"){ // collect condition-key-id TChA CondKeyIdChA; while ((Ch!=TCh::EofCh)&&(Ch!='\n')&&(Ch!='\r')){ CondKeyIdChA+=Ch; GetCh();} // skip eoln if (Ch=='\n'){GetCh(); if (Ch=='\r'){GetCh();}} else if (Ch=='\r'){GetCh(); if (Ch=='\n'){GetCh();}} // check for key CondKeyIdChA.Trunc(); IAssert(CopyModeP==false); IAssert(IgnoreModeP==false); if (SubstKeyIdV.IsIn(CondKeyIdChA)){ CopyModeP=true; IgnoreModeP=false; } else { CopyModeP=false; IgnoreModeP=true; } } else if (IdChA=="#endif"){ // move to eoln while ((Ch!=TCh::EofCh)&&(Ch!='\n')&&(Ch!='\r')){ GetCh();} // skip eoln if (Ch=='\n'){GetCh(); if (Ch=='\r'){GetCh();}} else if (Ch=='\r'){GetCh(); if (Ch=='\n'){GetCh();}} // reset copy&ignore modes IAssert(CopyModeP||IgnoreModeP); CopyModeP=false; IgnoreModeP=false; } else { // substitution or add id-as-seen TStr SubstValStr; if ((!CopyModeP)&&(IsSubstId(IdChA, SubstValStr))){ if (!IgnoreModeP){SOut->PutStr(SubstValStr);} } else { if (!IgnoreModeP){SOut->PutStr(IdChA);} } } } else { // single character if (!IgnoreModeP){SOut->PutCh(Ch);} GetCh(); } } }
static void TPreproc::Execute | ( | const TStr & | InFNm, |
const TStr & | OutFNm, | ||
const TStr & | InSubstFNm, | ||
const TStrV & | SubstKeyIdV | ||
) | [inline, static] |
char TPreproc::GetCh | ( | ) | [private] |
Definition at line 834 of file lx.cpp.
References Assert, Ch, TSIn::Eof(), TCh::EofCh, TSIn::GetCh(), PrevCh, and SIn.
Referenced by TPreproc().
{ Assert(Ch!=TCh::EofCh); PrevCh=Ch; Ch=((SIn->Eof()) ? TCh::EofCh : SIn->GetCh()); //putchar(Ch); return Ch; }
bool TPreproc::IsSubstId | ( | const TStr & | SubstId, |
TStr & | SubstValStr | ||
) | const [private] |
Definition at line 842 of file lx.cpp.
References THash< TKey, TDat, THashFunc >::GetDat(), TVec< TVal, TSizeTy >::IsIn(), THash< TKey, TDat, THashFunc >::IsKey(), TVec< TVal, TSizeTy >::Len(), SubstIdToKeyIdValPrVH, and SubstKeyIdV.
Referenced by TPreproc().
{ if (SubstIdToKeyIdValPrVH.IsKey(SubstId)){ const TStrPrV& KeyIdValPrV=SubstIdToKeyIdValPrVH.GetDat(SubstId); for (int KeyN=0; KeyN<KeyIdValPrV.Len(); KeyN++){ if (SubstKeyIdV.IsIn(KeyIdValPrV[KeyN].Val1)){ SubstValStr=KeyIdValPrV[KeyN].Val2; return true; } } return false; } else { return false; } }
TPreproc::UndefDefaultCopyAssign | ( | TPreproc | ) | [private] |
char TPreproc::Ch [private] |
Definition at line 364 of file lx.h.
Referenced by GetCh(), and TPreproc().
char TPreproc::PrevCh [private] |
Definition at line 364 of file lx.h.
Referenced by GetCh(), and TPreproc().
PSIn TPreproc::SIn [private] |
Definition at line 362 of file lx.h.
Referenced by GetCh(), and TPreproc().
THash<TStr, TStrPrV> TPreproc::SubstIdToKeyIdValPrVH [private] |
Definition at line 365 of file lx.h.
Referenced by IsSubstId(), and TPreproc().
TStrV TPreproc::SubstKeyIdV [private] |
Definition at line 363 of file lx.h.
Referenced by IsSubstId(), and TPreproc().