PivotCommand::ColRow PivotCommand:: MakeColRow( const CSVRow & row ) const { if ( mCol >= row.size() || mRow >= row.size() ) { CSVTHROW( "Invalid row/column index" ); } return PivotCommand::ColRow( row[mCol], row[mRow] ); }
void AddVars( ALib::Expression & e, const IOManager & io, const CSVRow & row ) { e.ClearPosParams(); e.AddVar( LINE_VAR, ALib::Str( io.CurrentLine() )); e.AddVar( FILE_VAR, ALib::Str( io.CurrentFileName())); e.AddVar( FIELD_VAR, ALib::Str( row.size())); for ( unsigned int j = 0; j < row.size(); j++ ) { e.AddPosParam( row.at( j ) ); } }
bool Test::compare_rows(CSVRow r1, CSVRow r2) { if (r1.size() != r2.size() ) { return false; } for (int i = 0; i < r1.size(); i++ ) { if (r1[i] != r2[i]) { return false; } } return true; }
bool Test::compare_rows(CSVRow r1, std::vector<std::string> r2) { if (r1.size() != r2.size() ) { return false; } for (int i = 0; i < r1.size(); i++ ) { if (r1[i] != r2[i]) { return false; } } return true; }
int SummaryCommand :: Cmp( const CSVRow & r1, const CSVRow & r2 ) const { for ( unsigned int i = 0; i < mFields.size(); i++ ) { unsigned int fi = mFields[i]; if ( fi >= r1.size() || fi >= r2.size() ) { CSVTHROW( "Bad field index" ); } int cmp = NSCmp( r1[fi], r2[fi] ); if ( cmp != 0 ) { return cmp; } } return 0; }
string DSVWriteCommand :: MakeDSV( const CSVRow & in ) { CSVRow row; BuildCSVRow( in, row ); string line; for ( unsigned int i = 0; i < row.size(); i++ ) { line += MakeField( row[i] ); if ( i != row.size() - 1 ) { line += Delim(); } } return line; }
bool operator()( const CSVRow & r1, const CSVRow & r2 ) const { if ( mCol >= r1.size() || mCol >= r2.size() ) { CSVTHROW( "Invalid field index " << (mCol + 1) ); } double d1 = ALib::ToReal( r1[mCol] ); double d2 = ALib::ToReal( r2[mCol] ); if ( d1 < d2 ) { return true; } else if ( d1 > d2 ) { return false; } return false; }
string SQLUpdateCommand :: MakeSetClause( const CSVRow & row ) const { string sc; for ( unsigned int i = 0; i < DataCols().size(); i++ ) { unsigned int fi = DataCols().at( i ).mField; if ( fi >= row.size() ) { CSVTHROW( "Required field " << fi + 1 << " missing from input" ); } if ( sc != "" ) { sc += ", "; } string field = EmptyToNull( row[fi] ); sc += DataCols().at(i).mColName + " = " + (( DoSQLQuote( i ) && ! NoNullQuote( field ) ) ? ALib::SQuote( ALib::SQLQuote( field ) ) : field ); } return "SET " + sc; }
void EditCommand :: EditRow( CSVRow & row ) { for ( unsigned int i = 0; i < row.size(); i++ ) { if ( mCols.size() == 0 || ALib::Contains( mCols, i ) ) { EditField( row[i] ); } } }
string TemplateCommand :: Eval( const CSVRow & row, const string & expr ) { ALib::Expression ex; for( unsigned int i = 0; i < row.size(); i++ ) { ex.AddPosParam( row[i] ); } return ex.Evaluate( expr.substr( 1 ) ); }
void EvalCommand :: Evaluate( CSVRow & row ) { bool skipelse = false; for ( unsigned int i = 0; i < mFieldExprs.size() ; i++ ) { if ( mIsIf[i] ) { if ( i < mFieldExprs.size() - 1 && mIsIf[i+2] ) { CSVTHROW( "Cannot have consecutive -if options" ); } if ( i >= mFieldExprs.size() - 2 ) { CSVTHROW( "Need two -e options after -if" ); } string r = mFieldExprs[i].mExpr.Evaluate(); if ( ALib::Expression::ToBool( r ) ) { skipelse = true; } else { i++; } continue; } string r = mFieldExprs[i].mExpr.Evaluate(); if ( mFieldExprs[i].mField < 0 || mFieldExprs[i].mField >= (int) row.size() ) { row.push_back( r ); } else { row[ mFieldExprs[i].mField ] = r; } if ( skipelse ) { i++; skipelse = false; } } }
bool DateReadCommand :: ConvertDates( CSVRow & row ) { bool havebad = false; for ( unsigned int i = 0; i < row.size(); i++ ) { if ( ALib::Contains( mFields, i ) ) { string ds = row[i]; ALib::Date dt; bool ok = mReader->Read( ds, dt ); if ( ok ) { row[i] = dt.Str(); } else { havebad = true; // error handling not yet } } } if ( havebad ) { return mWriteAction == WriteAll || mWriteAction == WriteBad; } else { return mWriteAction == WriteAll || mWriteAction == WriteGood; } }
string SQLCommand :: MakeWhereClause( const CSVRow & row ) const { string wc; for ( unsigned int i = 0; i < WhereCols().size(); i++ ) { unsigned int wi = WhereCols().at( i ).mField; if ( wi >= row.size() ) { CSVTHROW( "Required field " << wi + 1 << " missing in input" ); } if ( wc != "" ) { wc += " AND "; } string field = EmptyToNull( row[wi] ); wc += WhereCols().at(i).mColName + (field == "NULL" ? " IS " : " = " ) + ( (DoSQLQuote( i ) && ! NoNullQuote( field )) ? ALib::SQuote( ALib::SQLQuote( field ) ) : field ); } return "WHERE " + wc; }
void TruncCommand :: ProcessRow( CSVRow & row, unsigned int ncols, const ALib::CommaList & ) { if ( ncols < row.size() ) { CSVRow nrow( ncols ); std::copy( row.begin(), row.begin() + ncols, nrow.begin() ); row.swap( nrow ); } }
void AbstractFileSource::display(const CSVRow& row) { if(!row.size()) return; CSVRowCI i=row.begin(); std::cout<<*(i++); for(;i != row.end();++i) std::cout<<','<<*i; };
void DateFormatCommand :: FormatDates( CSVRow & row ) { for ( unsigned int i = 0; i < row.size(); i++ ) { if ( ALib::Contains( mFields, i ) ) { string ds = row[i]; row[i] = FormatDate( ds ); } } }
void TrimCommand :: Chop( CSVRow & row, unsigned int i ) { if ( mFields.size() == 0 ) { if ( i < mWidths.size() && i < row.size() ) { if ( mWidths.at(i) >= 0 ) { row.at(i) = row.at(i).substr( 0, mWidths.at(i) ); } } } else { int idx = ALib::IndexOf( mFields, i ); if ( idx >= 0 ) { if ( idx < (int) mWidths.size() && i < row.size() ) { if ( mWidths.at(idx) >= 0 ) { row.at(i) = row.at(i).substr( 0, mWidths.at(idx) ); } } } } }
string FileSplitCommand :: MakeKey( const CSVRow & row ) { string key; for ( unsigned int i = 0; i < row.size() ; i++ ) { if ( ALib::Contains( mColIndex, i ) ) { key += row[i]; key += '\0'; } } return key; }
void SummaryCommand :: RecordSizes( const CSVRow & row, SizeMap & sm ) { for ( unsigned int i = 0; i < row.size(); i++ ) { int sz = row[i].size(); SizeMap::iterator pos = sm.find( i ); if ( pos == sm.end() ) { sm[ i ] = std::make_pair( INT_MAX, 0 ); } sm[i].first = std::min( sm[i].first, sz ); sm[i].second = std::max( sm[i].second, sz ); } }
static void SumRow( vector <double> & sums, const CSVRow & row, const FieldList & fl ) { for ( unsigned int i = 0; i < fl.size(); i++ ) { unsigned int fi = fl.at(i); if ( fi >= row.size() ) { CSVTHROW( "Invalid field index" ); } sums.at(i) += ALib::ToReal( row.at(fi) ); } }
void AsciiTableCommand :: AddRow( const CSVRow & row ) { unsigned int n = row.size(); while( mWidths.size() < n ) { mWidths.push_back( 0 ); } for ( unsigned int i = 0; i < n ; i++ ) { if ( mWidths[i] < row[i].size() ) { mWidths[i] = row[i].size(); } } mRows.push_back( row ); }
string JoinCommand :: MakeKey( const CSVRow & row, bool first ) { string key; for ( unsigned int i = 0; i < mJoinSpecs.size(); i++ ) { unsigned int col = first ? mJoinSpecs[i].first : mJoinSpecs[i].second; if ( col >= row.size() ) { continue; } key += mIgnoreCase ? ALib::Upper( row[col] ) : row[col]; key += '\0'; // key element separator } return key; }
string SummaryCommand :: MakeKey( const CSVRow & row ) const { string key; for ( unsigned int i = 0; i < mFields.size(); i++ ) { if ( mFields.at(i) >= row.size() ) { key += ""; } else { key += row.at( mFields.at(i) ); } key += '\0'; } return key; }
void AsciiTableCommand :: OutputHeadings( std::ostream & os, const CSVRow & row ) { os << "|"; for ( unsigned int i = 0; i < mWidths.size() ; i++ ) { if ( i < row.size() ) { os << " " << ALib::Centre( row[i], mWidths[i] ) << " |"; } else { os << " " << ALib::RightPad( "", mWidths[i] ) << " |"; } } os << "\n"; os << MakeSep() << "\n"; }
void JoinCommand :: BuildRowMap( ALib::CSVStreamParser * sp ) { string line; std::unique_ptr <ALib::CSVStreamParser> p( sp ); CSVRow row; while( p->ParseNext( row ) ) { string key = MakeKey( row, false ); CSVRow jrow; for ( unsigned int i = 0; i < row.size(); i++ ) { if ( (! IsJoinCol( i )) || mKeep ) { jrow.push_back( row[i] ); } } mRowMap.insert( std::make_pair( key, jrow ) ); } }
string WriteFixedCommand :: MakeFixedOutput( const CSVRow & row ) { string s; for ( unsigned int i = 0; i < mFields.size(); i++ ) { string val = mFields[i].first > row.size() ? "" : row[ mFields[i].first - 1 ]; unsigned int width = mFields[i].second; s += ALib::RightPad( val, width ); } return s; }
void read_csv_transposed(std::istream& input, matrix<T>& M) { CSVRow row; input >> row; // read header row M.rows = row.size()-1; // first column expected to contain IDs M.data = std::vector<std::vector<double> >(M.rows); while (input >> row) { for (int i=0; i<M.rows; ++i) { double cell = std::atof(row[i+1].c_str()); M.data[i].push_back(cell); } M.columns++; } }
void AsciiTableCommand :: OutputRow( std::ostream & os, const CSVRow & row ) { os << "|"; for ( unsigned int i = 0; i < mWidths.size() ; i++ ) { if ( i < row.size() ) { if ( ALib::Contains( mRightAlign, i ) ) { os << " " << ALib::LeftPad( row[i], mWidths[i] ) << " |"; } else { os << " " << ALib::RightPad( row[i], mWidths[i] ) << " |"; } } else { os << " " << ALib::RightPad( "", mWidths[i] ) << " |"; } } os << "\n"; }
void DSVBase :: BuildCSVRow( const CSVRow & in, CSVRow & out ) const { if ( mFields.size() == 0 ) { out = in; } else { out.clear(); for ( unsigned int i = 0; i < mFields.size(); i++ ) { unsigned int f = mFields[ i ]; if ( f < in.size() ) { out.push_back( in[ f ] ); } else { out.push_back( "" ); } } } }
void PadCommand :: ProcessRow( CSVRow & row, unsigned int ncols, const ALib::CommaList & cl ) { unsigned int sz = row.size(); for ( unsigned int i = sz; i < ncols; i++ ) { if ( cl.Size() == 0 ) { row.push_back( "" ); } else { unsigned int ci = i - sz; if ( ci >= cl.Size() ) { row.push_back( cl.At( cl.Size() - 1 ) ); } else { row.push_back( cl.At( ci ) ); } } } }