void EvalCommand :: GetExpressions( ALib::CommandLine & cmd ) { int i = 2; // skip exe name and command name while( i < cmd.Argc() ) { if ( cmd.Argv( i ) == FLAG_EXPR || cmd.Argv( i ) == FLAG_IF ) { mIsIf.push_back( cmd.Argv( i ) == FLAG_IF ); if ( i + 1 >= cmd.Argc() ) { CSVTHROW( "Missing expression" ); } i++; string expr = cmd.Argv( i ); ALib::Expression ex; string emsg = ex.Compile( expr ); if ( emsg != "" ) { CSVTHROW( emsg + " in " + expr ); } mFieldExprs.push_back( FieldEx( -1, ex ) ); } else if ( cmd.Argv( i ) == FLAG_REMOVE ) { mIsIf.push_back( false ); if ( mDiscardInput ) { CSVTHROW( "Cannot specify both " << FLAG_REMOVE << " and " << FLAG_DISCARD ); } if ( i + 1 >= cmd.Argc() ) { CSVTHROW( "Missimg field/expression" ); } i++; string::size_type pos = cmd.Argv(i).find_first_of( "," ); if ( pos == string::npos ) { CSVTHROW( "Invalid field/index pair: " << cmd.Argv(i) ); } string field = cmd.Argv(i).substr( 0, pos ); string expr = cmd.Argv(i).substr( pos + 1 ); if ( ! ALib::IsInteger( field ) ) { CSVTHROW( "Invalid field (need integer): " << field ); } int n = ALib::ToInteger( field ); if ( n <= 0 ) { CSVTHROW( "Invalid field (must be greater than zero): " << field ); } ALib::Expression ex; string emsg = ex.Compile( expr ); if ( emsg != "" ) { CSVTHROW( emsg + " in " + expr ); } mFieldExprs.push_back( FieldEx( n - 1, ex ) ); } i++; } if ( mFieldExprs.size() == 0 ) { CSVTHROW( "Need at least one of -e or -r options" ); } }
int EditCommand :: Execute( ALib::CommandLine & cmd ) { GetSkipOptions( cmd ); for ( int i = 2; i < cmd.Argc(); i++ ) { if ( cmd.Argv( i ) == FLAG_EDIT ) { AddSubCmd( cmd.Argv( i + 1 ) ); i++; } } ALib::CommaList cl( cmd.GetValue( FLAG_COLS, "" ) ); CommaListToIndex( cl, mCols ); IOManager io( cmd ); CSVRow row; while( io.ReadCSV( row ) ) { if ( Skip( io, row ) ) { continue; } if ( ! Pass( io, row ) ) { EditRow( row ); } io.WriteRow( row ); } return 0; }