Example #1
0
void PivotCommand :: OutputPivot( IOManager & io ) {

    CSVRow r;
    r.push_back( "" );    // corresponds to row header
    for ( auto col : mCols ) {
        r.push_back( col );
    }
    io.WriteRow( r );

    for( auto row : mRows ) {
        r.clear();
        r.push_back( row );
        for ( auto col : mCols ) {
            ColRow cr( col, row );
            SumCount sc = mColRowValMap[ cr ];
            if ( mAction == Action::Average ) {
                r.push_back( ALib::Str( sc.mSum / sc.mCount ) );
            }
            else {
                r.push_back( ALib::Str( sc.mSum ) );
            }
        }
        io.WriteRow( r );
    }


}
Example #2
0
void DSVReadCommand :: ParseDSV( const string & line, CSVRow & rv ) {

    CSVRow row;

    unsigned int pos = 0, len = line.size();
    string val;

    while( pos < len ) {
        char c = line[ pos++ ];
        if ( c == Delim() ) {
            while( mCollapseSep && ALib::Peek( line, pos ) == Delim() ) {
                pos++;
            }
            row.push_back( Unquote( val ) );
            val = "";
        }
        else if ( c == '\\' ) {
            if ( pos == len ) {
                CSVTHROW( "Escape at end of line" );
            }
            else {
                val += line[ pos++ ];
            }
        }
        else {
            val += c;
        }
    }

    row.push_back( Unquote( val )  );

    BuildCSVRow( row, rv );

}
Example #3
0
// Perform a join on two tables, using two provided strings representing the headers of the respective columns to join on
CSVTable CSVJoiner::join(CSVTable t1, CSVTable t2, std::string c1, std::string c2, Config &config) {
    CSVTable t3;
    CSVRow row;
    row.append( t1.rows[0].begin(), t1.rows[0].end() );
    row.append( t2.rows[0].begin(), t2.rows[0].end() );
    t3.push_back(row);

    int comp1 = t1.get_column_index(c1);
    int comp2 = t2.get_column_index(c2);

    // For left and right joins. Would be great if this was simply a vector of pointers.
    std::vector<CSVRow> remaining_rows1 = t1.rows;
    std::vector<CSVRow> remaining_rows2 = t2.rows;

    for (int i = 1; i < t1.rows.size(); i++) {
        for (int j = 1; j < t2.rows.size(); j++) {
            if( t1.rows[i][comp1] == t2.rows[j][comp2] ) {
                CSVRow row;
                row.append( t1.rows[i].begin(), t1.rows[i].end() );
                row.append( t2.rows[j].begin(), t2.rows[j].end() );
                t3.push_back(row);

                remaining_rows1[i].clear();
                remaining_rows2[j].clear();
            }
        }
    }

    if( config.join_left == true ) {
        for (int i = 1; i < remaining_rows1.size(); i++) {
            if( !remaining_rows1[i].empty() ) {
                CSVRow row = remaining_rows1[i];
                for (int j = 0; j < t2.column_count; j++) {
                    row.push_back("");
                }
                t3.push_back(row);
            }
        }
    }

    if( config.join_right == true ) {
        for (int i = 1; i < remaining_rows2.size(); i++) {
            if( !remaining_rows2[i].empty() ) {
                CSVRow row;
                for (int j = 0; j<t1.column_count; j++) {
                    row.push_back("");
                }
                row.append( remaining_rows2[i].begin(), remaining_rows2[i].end() );
                t3.push_back(row);
            }
        }
    }

    return t3;
}
Example #4
0
void ReadFixedCommand :: MakeRow( const string & line, CSVRow & row ) {
	row.clear();
	unsigned int len = line.size();
	for( unsigned int i = 0; i < mFields.size(); i++ ) {
		if ( mFields[i].first > len ) {
			row.push_back( "" );
		}
		else {
			string val = line.substr( mFields[i].first - 1, mFields[i].second );
			row.push_back( mTrim ? ALib::RTrim( val ) : val );
		}
	}
}
Example #5
0
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;
		}
	}
}
Example #6
0
void JoinCommand :: WriteJoinRows( IOManager & io, const CSVRow & row ) {

	string key = MakeKey( row, true );

	MapType::const_iterator pos = mRowMap.lower_bound( key );
	MapType::const_iterator last = mRowMap.upper_bound( key );

	if ( mOuterJoin && pos == last ) {
		io.WriteRow( row );
	}
	else if ( mInvert ) {
		if ( pos == last ) {
			io.WriteRow( row );
		}
		return;
	}
	else {
		while( pos != last ) {
			const CSVRow & jr = pos->second;
			CSVRow newrow = row;
			for ( unsigned int i = 0; i < jr.size(); i++ ) {
				newrow.push_back( jr[i] );
			}
			io.WriteRow( newrow );
			++pos;
		}
	}
}
Example #7
0
void SummaryCommand :: DoMedian( IOManager & io ) {

	CSVRow r;

	for ( unsigned int i = 0; i < mFields.size(); i++ ) {
		unsigned int col = mFields[i];
		NumSort ns( col );
		std::sort( mRows.begin(), mRows.end(), ns );
		unsigned int sz = mRows.size();
		double d;
		if ( sz % 2 ) {
			unsigned int idx = (sz / 2);
			d = ALib::ToReal( mRows.at(idx).at(col) );
		}
		else {
			unsigned int idx = (sz / 2) - 1;
			double d1 = ALib::ToReal( mRows.at(idx).at(col) );
			double d2 = ALib::ToReal( mRows.at(idx+1).at(col) );
			d = (d1 + d2) /2;
		}

		r.push_back( ALib::Str( d ) );
	}

	io.WriteRow( r );
}
Example #8
0
void SummaryCommand :: DoAvg( IOManager & io ) {
	vector <double> sums;
	SumCols( sums );
	CSVRow r;
	for ( unsigned int i = 0; i < sums.size(); i++ ) {
		r.push_back( ALib::Str( sums.at(i) / mRows.size() ) );
	}
	io.WriteRow( r );
}
Example #9
0
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( "" );
            }
        }
    }
}
Example #10
0
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 ) );
			}
		}
	}
}
Example #11
0
int FileInfoCommand :: Execute( ALib::CommandLine & cmd ) {

	GetSkipOptions( cmd );
	mTwoCols = cmd.HasFlag( FLAG_TWOC );
	mBasename = cmd.HasFlag( FLAG_BASEN );

	IOManager io( cmd );

	CSVRow row;
	while( io.ReadCSV( row ) ) {
		if ( Skip( io, row ) ) {
			continue;
		}
		if ( Pass( io, row ) ) {
			io.WriteRow( row );
			continue;
		}

		string fname = mBasename
						? ALib::Filename( io.CurrentFileName() ).Base()
						: io.CurrentFileName();

		CSVRow outrow;
		if ( mTwoCols ) {
			outrow.push_back( fname );
			outrow.push_back( ALib::Str( io.CurrentLine() ));
		}
		else {
			outrow.push_back( fname + " ("
				+ ALib::Str( io.CurrentLine() ) + ")"
			);
		}
		ALib::operator+=( outrow, row );
		io.WriteRow( outrow );
	}

	return 0;
}
Example #12
0
void AsciiTableCommand :: ProcessFlags( ALib::CommandLine & cmd ) {
	mUseLineSep = cmd.HasFlag( FLAG_SEP );
	mHeadings = ALib::CommaList( cmd.GetValue( FLAG_HEADER, "" ) );
	ALib::CommaList ra = ALib::CommaList( cmd.GetValue( FLAG_RALIGN, "" ) );
	CommaListToIndex( ra, mRightAlign );

	if ( mHeadings.Size() && mHeadings.At(0) != FILE_HEADER) {
		CSVRow r;
		for ( unsigned int i = 0; i < mHeadings.Size() ; i++ ) {
			r.push_back( mHeadings.At( i ) );
		}
		AddRow( r );
	}
}
Example #13
0
void  ReadXMLCommand :: WriteRow( const ALib::XMLElement * r,
										IOManager & io ) {
	CSVRow row;

	for ( unsigned int i = 0; i < r->ChildCount(); i++  ) {
		const ALib::XMLElement * ce = r->ChildElement( i );
		if ( ce && ce->Name() == "td" ) {
			row.push_back( TDToCSV( ce ) );
		}
	}

//	ALib::Dump( std::cout, row );
	io.WriteRow( row );
}
Example #14
0
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 ) );
	}
}
Example #15
0
void AbstractFileSource::readCSV(std::ifstream &input, CSVDatabase &db) {
	String csvLine;
	// read every line from the stream
	while( std::getline(input, csvLine) ){
		std::istringstream csvStream(csvLine);
		CSVRow csvRow;
		String csvCol;
		// read every element from the line that is seperated by commas
		// and put it into the vector or strings
		while( std::getline(csvStream, csvCol, ','))
			csvRow.push_back(csvCol);
		db.push_back(csvRow);
	}

};
Example #16
0
void ExcludeCommand :: Exclude(  CSVRow & r ) const {

	CSVRow out;

	if ( mReverse ) {
		std::reverse( r.begin(), r.end() );
	}

	for ( unsigned int i = 0; i < r.size(); i++ ) {
		if ( ! ALib::Contains( mFields, i ) ) {
			out.push_back( r.at( i ) );
		}
	}

	if ( mReverse ) {
		std::reverse( out.begin(), out.end() );
	}

	r.swap( out );
}
Example #17
0
int CallCommand :: CallOnFields( CSVRow & row, char * buffer ) {
	string fields;
	int fc = 0;
	for ( unsigned int i = 0; i < row.size(); i++ ) {
		if ( mFields.size() == 0 || ALib::Contains( mFields, i ) ) {
			fields += row[i];
			fields += '\0';
			fc++;
		}
	}
	int ofc = 0;
	int rv = mFunc( fc, fields.c_str(), &ofc, buffer, mOutBufSize );
	if ( rv == 0  ) {
		int pos = 0;
		while( ofc-- ) {
			string field = ExtractField( buffer, pos );
			pos++;
			row.push_back( field );
		}
	}
	return rv;
}