SNAP Library , User Reference  2013-01-07 14:03:36
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
TSsParser Class Reference

#include <ss.h>

List of all members.

Public Member Functions

 TSsParser (const TStr &FNm, const TSsFmt _SsFmt=ssfTabSep, const bool &_SkipLeadBlanks=false, const bool &_SkipCmt=true, const bool &_SkipEmptyFld=false)
 TSsParser (const TStr &FNm, const char &Separator, const bool &_SkipLeadBlanks=false, const bool &_SkipCmt=true, const bool &_SkipEmptyFld=false)
 ~TSsParser ()
bool Next ()
int Len () const
int GetFlds () const
uint64 GetLineNo () const
bool IsCmt () const
bool Eof () const
const TChA & GetLnStr () const
void ToLc ()
const char * GetFld (const int &FldN) const
char * GetFld (const int &FldN)
const char * operator[] (const int &FldN) const
char * operator[] (const int &FldN)
bool GetInt (const int &FldN, int &Val) const
int GetInt (const int &FldN) const
bool IsInt (const int &FldN) const
bool GetFlt (const int &FldN, double &Val) const
bool IsFlt (const int &FldN) const
double GetFlt (const int &FldN) const
const char * DumpStr () const

Static Public Member Functions

static PSsParser New (const TStr &FNm, const TSsFmt SsFmt)

Private Member Functions

 UndefDefaultCopyAssign (TSsParser)

Private Attributes

TCRef CRef
TSsFmt SsFmt
bool SkipLeadBlanks
bool SkipCmt
bool SkipEmptyFld
uint64 LineCnt
char SplitCh
TChA LineStr
TVec< char * > FldV
PSIn FInPt

Friends

class TPt< TSsParser >

Detailed Description

Definition at line 60 of file ss.h.


Constructor & Destructor Documentation

TSsParser::TSsParser ( const TStr &  FNm,
const TSsFmt  _SsFmt = ssfTabSep,
const bool &  _SkipLeadBlanks = false,
const bool &  _SkipCmt = true,
const bool &  _SkipEmptyFld = false 
)

Definition at line 351 of file ss.cpp.

                                                                                                                                       : SsFmt(_SsFmt), 
 SkipLeadBlanks(_SkipLeadBlanks), SkipCmt(_SkipCmt), SkipEmptyFld(_SkipEmptyFld), LineCnt(0), /*Bf(NULL),*/ SplitCh('\t'), FldV(), FInPt(NULL) {
  if (TZipIn::IsZipExt(FNm.GetFExt())) { FInPt = TZipIn::New(FNm); }
  else { FInPt = TFIn::New(FNm); }
  //Bf = new char [BfLen];
  switch(SsFmt) {
    case ssfTabSep : SplitCh = '\t'; break;
    case ssfCommaSep : SplitCh = ','; break;
    case ssfSemicolonSep : SplitCh = ';'; break;
    case ssfVBar : SplitCh = '|'; break;
    case ssfSpaceSep : SplitCh = ' '; break;
    case ssfWhiteSep: SplitCh = ' '; break;
    default: FailR("Unknown separator character.");
  }
}
TSsParser::TSsParser ( const TStr &  FNm,
const char &  Separator,
const bool &  _SkipLeadBlanks = false,
const bool &  _SkipCmt = true,
const bool &  _SkipEmptyFld = false 
)

Definition at line 367 of file ss.cpp.

                                                                                                                                         : SsFmt(ssfSpaceSep), 
 SkipLeadBlanks(_SkipLeadBlanks), SkipCmt(_SkipCmt), SkipEmptyFld(_SkipEmptyFld), LineCnt(0), /*Bf(NULL),*/ SplitCh('\t'), FldV(), FInPt(NULL) {
  if (TZipIn::IsZipExt(FNm.GetFExt())) { FInPt = TZipIn::New(FNm); }
  else { FInPt = TFIn::New(FNm); }
  SplitCh = Separator;
}

Definition at line 374 of file ss.cpp.

                      {
  //if (Bf != NULL) { delete [] Bf; }
}

Member Function Documentation

const char * TSsParser::DumpStr ( ) const

Definition at line 452 of file ss.cpp.

                                     {
  static TChA ChA(10*1024);
  ChA.Clr();
  for (int i = 0; i < FldV.Len(); i++) {
    ChA += TStr::Fmt("  %d: '%s'\n", i, FldV[i]);
  }
  return ChA.CStr();
}
bool TSsParser::Eof ( ) const [inline]

Definition at line 81 of file ss.h.

{ return FInPt->Eof(); }
const char* TSsParser::GetFld ( const int &  FldN) const [inline]

Definition at line 85 of file ss.h.

{ return FldV[FldN]; }
char* TSsParser::GetFld ( const int &  FldN) [inline]

Definition at line 86 of file ss.h.

{ return FldV[FldN]; }
int TSsParser::GetFlds ( ) const [inline]

Definition at line 78 of file ss.h.

{ return Len(); }
bool TSsParser::GetFlt ( const int &  FldN,
double &  Val 
) const

Definition at line 430 of file ss.cpp.

                                                         {
  // parsing format {ws} [+/-] +{d} ([.]{d}) ([E|e] [+/-] +{d})
  const char *c = GetFld(FldN);
  while (TCh::IsWs(*c)) { c++; }
  if (*c=='+' || *c=='-') { c++; }
  if (! TCh::IsNum(*c) && *c!='.') { return false; }
  while (TCh::IsNum(*c)) { c++; }
  if (*c == '.') {
    c++;
    while (TCh::IsNum(*c)) { c++; }
  }
  if (*c=='e' || *c == 'E') {
    c++;
    if (*c == '+' || *c == '-' ) { c++; }
    if (! TCh::IsNum(*c)) { return false; }
    while (TCh::IsNum(*c)) { c++; }
  }
  if (*c != 0) { return false; }
  Val = atof(GetFld(FldN));
  return true;
}
double TSsParser::GetFlt ( const int &  FldN) const [inline]

Definition at line 95 of file ss.h.

                                       {
    double Val=0.0; IAssert(GetFlt(FldN, Val)); return Val; }
bool TSsParser::GetInt ( const int &  FldN,
int &  Val 
) const

Definition at line 411 of file ss.cpp.

                                                      {
  // parsing format {ws} [+/-] +{ddd}
  int _Val = -1;
  bool Minus=false;
  const char *c = GetFld(FldN);
  while (TCh::IsWs(*c)) { c++; }
  if (*c=='-') { Minus=true; c++; }
  if (! TCh::IsNum(*c)) { return false; }
  _Val = TCh::GetNum(*c);  c++;
  while (TCh::IsNum(*c)){ 
    _Val = 10 * _Val + TCh::GetNum(*c); 
    c++; 
  }
  if (Minus) { _Val = -_Val; }
  if (*c != 0) { return false; }
  Val = _Val;
  return true;
}
int TSsParser::GetInt ( const int &  FldN) const [inline]

Definition at line 90 of file ss.h.

                                    {
    int Val=0; IAssertR(GetInt(FldN, Val), TStr::Fmt("Field %d not INT.\n%s", FldN, DumpStr()).CStr()); return Val; }
uint64 TSsParser::GetLineNo ( ) const [inline]

Definition at line 79 of file ss.h.

{ return LineCnt; }
const TChA& TSsParser::GetLnStr ( ) const [inline]

Definition at line 82 of file ss.h.

{ return LineStr; }
bool TSsParser::IsCmt ( ) const [inline]

Definition at line 80 of file ss.h.

{ return Len()>0 && GetFld(0)[0] == '#'; }
bool TSsParser::IsFlt ( const int &  FldN) const [inline]

Definition at line 94 of file ss.h.

{ double v; return GetFlt(FldN, v); }
bool TSsParser::IsInt ( const int &  FldN) const [inline]

Definition at line 92 of file ss.h.

{ int v; return GetInt(FldN, v); }
int TSsParser::Len ( ) const [inline]

Definition at line 77 of file ss.h.

{ return FldV.Len(); }
static PSsParser TSsParser::New ( const TStr &  FNm,
const TSsFmt  SsFmt 
) [inline, static]

Definition at line 74 of file ss.h.

{ return new TSsParser(FNm, SsFmt); }
bool TSsParser::Next ( )

Definition at line 378 of file ss.cpp.

                     { // split on SplitCh
  FldV.Clr(false);
  LineStr.Clr();
  FldV.Clr();
  LineCnt++;
  if (! FInPt->GetNextLn(LineStr)) { return false; }
  if (SkipCmt && LineStr.Len()>0 && LineStr[0]=='#') { return Next(); }

  char* cur = LineStr.CStr();
  if (SkipLeadBlanks) { // skip leadning blanks
    while (*cur && TCh::IsWs(*cur)) { cur++; }
  }
  char *last = cur;
  while (*cur) {
    if (SsFmt == ssfWhiteSep) { while (*cur && ! TCh::IsWs(*cur)) { cur++; } } 
    else { while (*cur && *cur!=SplitCh) { cur++; } }
    if (*cur == 0) { break; }
    *cur = 0;  cur++;
    FldV.Add(last);  last = cur;
    if (SkipEmptyFld && strlen(FldV.Last())==0) { FldV.DelLast(); } // skip empty fields
  }
  FldV.Add(last);  // add last field
  if (SkipEmptyFld && FldV.Empty()) { return Next(); } // skip empty lines
  return true; 
}
const char* TSsParser::operator[] ( const int &  FldN) const [inline]

Definition at line 87 of file ss.h.

{ return FldV[FldN]; }
char* TSsParser::operator[] ( const int &  FldN) [inline]

Definition at line 88 of file ss.h.

{ return FldV[FldN]; }
void TSsParser::ToLc ( )

Definition at line 404 of file ss.cpp.

                     {
  for (int f = 0; f < FldV.Len(); f++) {
    for (char *c = FldV[f]; *c; c++) {
      *c = tolower(*c); }
  }
}

Friends And Related Function Documentation

friend class TPt< TSsParser > [friend]

Definition at line 60 of file ss.h.


Member Data Documentation

Definition at line 60 of file ss.h.

Definition at line 68 of file ss.h.

TVec<char*> TSsParser::FldV [private]

Definition at line 67 of file ss.h.

Definition at line 64 of file ss.h.

Definition at line 66 of file ss.h.

bool TSsParser::SkipCmt [private]

Definition at line 63 of file ss.h.

bool TSsParser::SkipEmptyFld [private]

Definition at line 63 of file ss.h.

bool TSsParser::SkipLeadBlanks [private]

Definition at line 63 of file ss.h.

char TSsParser::SplitCh [private]

Definition at line 65 of file ss.h.

Definition at line 62 of file ss.h.


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