コード例 #1
0
ファイル: Table.cpp プロジェクト: nikhilkhadke/snapr
PTable TTable::LoadSS(const TStr& TableName, const Schema& S, const TStr& InFNm, const char& Separator, TBool HasTitleLine){
  TSsParser Ss(InFNm, Separator);
  PTable T = New(TableName, S);
  // if title line (i.e. names of the columns) is included as first row in the
  // input file - use it to validate schema
  if(HasTitleLine){
    Ss.Next();  
    if(S.Len() != Ss.GetFlds()){TExcept::Throw("Table Schema Mismatch!");}
    for(TInt i = 0; i < Ss.GetFlds(); i++){
      // remove carriage return char
      TInt L = strlen(Ss[i]);
      if(Ss[i][L-1] < ' '){ Ss[i][L-1] = 0;}
      if(T->GetSchemaColName(i) != Ss[i]){ TExcept::Throw("Table Schema Mismatch!");}
    }
  }
  TInt RowLen = S.Len();
  TVec<TYPE> ColTypes = TVec<TYPE>(RowLen);
  for(TInt i = 0; i < RowLen; i++){
    ColTypes[i] = T->GetSchemaColType(i);
  }
  // populate table columns
  while(Ss.Next()){
    TInt IntColIdx = 0;
    TInt FltColIdx = 0;
    TInt StrColIdx = 0;
    Assert(Ss.GetFlds() == RowLen); // compiled only in debug
    for(TInt i = 0; i < RowLen; i++){
      switch(ColTypes[i]){
        case INT:
          T->IntCols[IntColIdx].Add(Ss.GetInt(i));
          IntColIdx++;
          break;
        case FLT:
          T->FltCols[FltColIdx].Add(Ss.GetFlt(i));
          FltColIdx++;
          break;
        case STR:
          T->AddStrVal(StrColIdx, Ss[i]);
          StrColIdx++;
          break;
      }
    }
  }

  // set number of rows and "Next" vector
  T->NumRows = Ss.GetLineNo()-1;
  if(HasTitleLine){T->NumRows--;}
  T->NumValidRows = T->NumRows;
  T->Next = TIntV(T->NumRows,0);
  for(TInt i = 0; i < T->NumRows-1; i++){
    T->Next.Add(i+1);
  }
  T->Next.Add(Last);
  return T;
}