SNAP Library 2.2, User Reference  2014-03-11 19:15:55
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
TPreproc Class Reference

#include <lx.h>

List of all members.

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.

                                                 :
  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();
    }
  }
}

Member Function Documentation

static void TPreproc::Execute ( const TStr InFNm,
const TStr OutFNm,
const TStr InSubstFNm,
const TStrV SubstKeyIdV 
) [inline, static]

Definition at line 373 of file lx.h.

                                                    {
    TPreproc Preproc(InFNm, OutFNm, InSubstFNm, SubstKeyIdV);}
char TPreproc::GetCh ( ) [private]

Definition at line 834 of file lx.cpp.

                    {
  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.

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

Member Data Documentation

char TPreproc::Ch [private]

Definition at line 364 of file lx.h.

char TPreproc::PrevCh [private]

Definition at line 364 of file lx.h.

PSIn TPreproc::SIn [private]

Definition at line 362 of file lx.h.

Definition at line 365 of file lx.h.

Definition at line 363 of file lx.h.


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