SNAP Library 4.1, Developer Reference  2018-07-26 16:30:42
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
TPreproc Class Reference

#include <lx.h>

Collaboration diagram for TPreproc:

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, TStrPrVSubstIdToKeyIdValPrVH
 

Detailed Description

Definition at line 360 of file lx.h.

Constructor & Destructor Documentation

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(), 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().

858  :
859  SIn(), SubstKeyIdV(_SubstKeyIdV),
860  PrevCh('\0'), Ch('\0'){
861  // load substitution file
862  if (!SubstFNm.Empty()){
863  PXmlDoc XmlDoc=TXmlDoc::LoadTxt(SubstFNm);
864  // get list of substitutions
865  TXmlTokV SubstTokV; XmlDoc->GetTok()->GetTagTokV("Subst", SubstTokV);
866  for (int SubstTokN=0; SubstTokN<SubstTokV.Len(); SubstTokN++){
867  PXmlTok SubstTok=SubstTokV[SubstTokN];
868  // get substitution-id
869  TStr SubstId=SubstTok->GetArgVal("Id", "");
870  if (!SubstId.Empty()){
871  // create substitution
872  TStrPrV& KeyIdValPrV=SubstIdToKeyIdValPrVH.AddDat(SubstId);
873  // get list of substitution-strings
874  TXmlTokV StrTokV; SubstTok->GetTagTokV("Str", StrTokV);
875  for (int StrTokN=0; StrTokN<StrTokV.Len(); StrTokN++){
876  PXmlTok StrTok=StrTokV[StrTokN];
877  // get key-value pair
878  TStr KeyId=StrTok->GetArgVal("Key", "");
879  TStr ValStr=StrTok->GetTokStr(false);
880  // assign key-value-pair
881  if (!KeyId.Empty()){
882  KeyIdValPrV.Add(TStrPr(KeyId, ValStr));
883  }
884  }
885  }
886  }
887  }
888  // substitution
889  // open files
890  SIn=TFIn::New(InFNm);
891  PSOut SOut=TFOut::New(OutFNm);
892  // set copy & ignore mode
893  bool CopyModeP=false; bool IgnoreModeP=false;
894  GetCh();
895  while (Ch!=TCh::EofCh){
896  if (isalpha(Ch)||(((PrevCh=='\0')||(PrevCh=='\r')||(PrevCh=='\n'))&&(Ch=='#'))){
897  // collect identifier
898  TChA IdChA;
899  do {
900  IdChA+=Ch; GetCh();
901  } while ((Ch!=TCh::EofCh)&&(isalnum(Ch)));
902  // check identifier
903  if (IdChA=="#ifdef"){
904  // collect condition-key-id
905  TChA CondKeyIdChA;
906  while ((Ch!=TCh::EofCh)&&(Ch!='\n')&&(Ch!='\r')){
907  CondKeyIdChA+=Ch; GetCh();}
908  // skip eoln
909  if (Ch=='\n'){GetCh(); if (Ch=='\r'){GetCh();}}
910  else if (Ch=='\r'){GetCh(); if (Ch=='\n'){GetCh();}}
911  // check for key
912  CondKeyIdChA.Trunc();
913  IAssert(CopyModeP==false);
914  IAssert(IgnoreModeP==false);
915  if (SubstKeyIdV.IsIn(CondKeyIdChA)){
916  CopyModeP=true; IgnoreModeP=false;
917  } else {
918  CopyModeP=false; IgnoreModeP=true;
919  }
920  } else
921  if (IdChA=="#endif"){
922  // move to eoln
923  while ((Ch!=TCh::EofCh)&&(Ch!='\n')&&(Ch!='\r')){
924  GetCh();}
925  // skip eoln
926  if (Ch=='\n'){GetCh(); if (Ch=='\r'){GetCh();}}
927  else if (Ch=='\r'){GetCh(); if (Ch=='\n'){GetCh();}}
928  // reset copy&ignore modes
929  IAssert(CopyModeP||IgnoreModeP);
930  CopyModeP=false; IgnoreModeP=false;
931  } else {
932  // substitution or add id-as-seen
933  TStr SubstValStr;
934  if ((!CopyModeP)&&(IsSubstId(IdChA, SubstValStr))){
935  if (!IgnoreModeP){SOut->PutStr(SubstValStr);}
936  } else {
937  if (!IgnoreModeP){SOut->PutStr(IdChA);}
938  }
939  }
940  } else {
941  // single character
942  if (!IgnoreModeP){SOut->PutCh(Ch);}
943  GetCh();
944  }
945  }
946 }
#define IAssert(Cond)
Definition: bd.h:262
static PSOut New(const TStr &FNm, const bool &Append=false)
Definition: fl.cpp:442
bool IsSubstId(const TStr &SubstId, TStr &SubstValStr) const
Definition: lx.cpp:842
char PrevCh
Definition: lx.h:364
TPair< TStr, TStr > TStrPr
Definition: ds.h:107
bool IsIn(const TVal &Val) const
Checks whether element Val is a member of the vector.
Definition: ds.h:828
TStrV SubstKeyIdV
Definition: lx.h:363
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
static const char EofCh
Definition: dt.h:1037
static PSIn New(const TStr &FNm)
Definition: fl.cpp:290
char GetCh()
Definition: lx.cpp:834
void Trunc()
Definition: dt.cpp:420
Definition: dt.h:201
Definition: dt.h:412
bool Empty() const
Definition: dt.h:488
PSIn SIn
Definition: lx.h:362
static PXmlDoc LoadTxt(TXmlLx &Lx)
Definition: xml.cpp:1401
Definition: bd.h:196
char Ch
Definition: lx.h:364
THash< TStr, TStrPrV > SubstIdToKeyIdValPrVH
Definition: lx.h:365
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602

Here is the call graph for this function:

Member Function Documentation

static void TPreproc::Execute ( const TStr InFNm,
const TStr OutFNm,
const TStr InSubstFNm,
const TStrV SubstKeyIdV 
)
inlinestatic

Definition at line 373 of file lx.h.

374  {
375  TPreproc Preproc(InFNm, OutFNm, InSubstFNm, SubstKeyIdV);}
Definition: lx.h:360
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().

834  {
835  Assert(Ch!=TCh::EofCh);
836  PrevCh=Ch;
837  Ch=((SIn->Eof()) ? TCh::EofCh : SIn->GetCh());
838  //putchar(Ch);
839  return Ch;
840 }
char PrevCh
Definition: lx.h:364
static const char EofCh
Definition: dt.h:1037
virtual bool Eof()=0
#define Assert(Cond)
Definition: bd.h:251
PSIn SIn
Definition: lx.h:362
char Ch
Definition: lx.h:364
virtual char GetCh()=0

Here is the call graph for this function:

Here is the caller graph for this function:

bool TPreproc::IsSubstId ( const TStr SubstId,
TStr SubstValStr 
) const
private

Definition at line 842 of file lx.cpp.

References TVec< TVal, TSizeTy >::IsIn(), TVec< TVal, TSizeTy >::Len(), SubstIdToKeyIdValPrVH, and SubstKeyIdV.

Referenced by TPreproc().

842  {
843  if (SubstIdToKeyIdValPrVH.IsKey(SubstId)){
844  const TStrPrV& KeyIdValPrV=SubstIdToKeyIdValPrVH.GetDat(SubstId);
845  for (int KeyN=0; KeyN<KeyIdValPrV.Len(); KeyN++){
846  if (SubstKeyIdV.IsIn(KeyIdValPrV[KeyN].Val1)){
847  SubstValStr=KeyIdValPrV[KeyN].Val2;
848  return true;
849  }
850  }
851  return false;
852  } else {
853  return false;
854  }
855 }
bool IsIn(const TVal &Val) const
Checks whether element Val is a member of the vector.
Definition: ds.h:828
TStrV SubstKeyIdV
Definition: lx.h:363
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
THash< TStr, TStrPrV > SubstIdToKeyIdValPrVH
Definition: lx.h:365
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430

Here is the call graph for this function:

Here is the caller graph for this function:

TPreproc::UndefDefaultCopyAssign ( TPreproc  )
private

Member Data Documentation

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().


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