int DateReadCommand :: Execute( ALib::CommandLine & cmd ) { GetSkipOptions( cmd ); ProcessFlags( cmd ); IOManager io( cmd ); CSVRow row; while( io.ReadCSV( row ) ) { if ( Skip( io, row ) ) { continue; } if( Pass( io, row ) ) { io.WriteRow( row ); continue; } if ( ConvertDates( row ) ) { io.WriteRow( row ); } } return 0; }
int EvalCommand :: Execute( ALib::CommandLine & cmd ) { GetSkipOptions( cmd ); IOManager io( cmd ); CSVRow row; mDiscardInput = cmd.HasFlag( FLAG_DISCARD ); GetExpressions( cmd ); while( io.ReadCSV( row ) ) { if ( Skip( io, row ) ) { continue; } if ( ! Pass( io, row ) ) { SetParams( row, io ); if ( mDiscardInput ) { row.clear(); } Evaluate( row ); } io.WriteRow( row ); } return 0; }
int TrimCommand :: Execute( ALib::CommandLine & cmd ) { GetSkipOptions( cmd ); if ( cmd.HasFlag( FLAG_TRLEAD ) || cmd.HasFlag( FLAG_TRTRAIL ) ) { mTrimLead = cmd.HasFlag( FLAG_TRLEAD ); mTrimTrail = cmd.HasFlag( FLAG_TRTRAIL ); } else { mTrimLead = mTrimTrail = true; } if ( cmd.HasFlag( FLAG_WIDTH ) ) { GetWidths( cmd.GetValue( FLAG_WIDTH ) ); } ALib::CommaList cl( cmd.GetValue( FLAG_COLS, "" ) ); CommaListToIndex( cl, mFields ); IOManager io( cmd ); CSVRow row; while( io.ReadCSV( row ) ) { if ( Skip( row ) ) { continue; } if ( ! Pass( row ) ) { Trim( row ); } io.WriteRow( row ); } return 0; }
int TemplateCommand :: Execute( ALib::CommandLine & cmd ) { GetSkipOptions( cmd ); ReadTemplate( cmd ); if ( cmd.HasFlag( FLAG_FNAMES ) ) { mFileTemplate = cmd.GetValue( FLAG_FNAMES ); } IOManager io( cmd ); CSVRow row; while( io.ReadCSV( row ) ) { if ( Skip( row ) ) { continue; } if ( mFileTemplate.empty() ) { io.Out() << ReplaceColumns( mTemplate, row ); } else { FileOut( row ); } } return 0; }
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; }
int AsciiTableCommand :: Execute( ALib::CommandLine & cmd ) { GetSkipOptions( cmd ); ProcessFlags( cmd ); IOManager io( cmd ); CSVRow row; while( io.ReadCSV( row ) ) { if ( ! Skip( row ) ) { AddRow( row ); } } OutputTable( io.Out() ); return 0; }
int EchoCommand :: Execute( ALib::CommandLine & cmd ) { GetSkipOptions( cmd ); IOManager io( cmd ); CSVRow row; while( io.ReadCSV( row ) ) { if ( ! Skip( row ) ) { io.WriteRow( row ); } } return 0; }
int DSVWriteCommand :: Execute( ALib::CommandLine & cmd ) { GetSkipOptions( cmd ); ReadFlags( cmd ); IOManager io( cmd ); CSVRow row; while( io.ReadCSV( row ) ) { if ( ! Skip( row ) ) { io.Out() << MakeDSV( row ) << "\n"; } } return 0; }
int ValidateCommand :: Execute( ALib::CommandLine & cmd ) { GetSkipOptions( cmd ); GetOutputMode( cmd ); ReadValidationFile( cmd ); IOManager io( cmd ); CSVRow row; // we optionally return an error code to the shell if validation failed int errtotal = 0; bool errcode = cmd.HasFlag( FLAG_ERRCODE ); while( io.ReadCSV( row ) ) { if ( Skip( io, row ) ) { continue; } int errcount = 0; for ( unsigned int i = 0; i < mRules.size(); i++ ) { ValidationRule::Results res = mRules[i]->Apply( row ); if ( res.size() && mOutMode == Reports ) { Report( io, res, errcount ); errcount++; errtotal += errcount; continue; } if ( res.size() ) { errcount++; if ( mOutMode == Fails ) { io.WriteRow( row ); break; } } } if ( mOutMode == Passes && errcount == 0 ) { io.WriteRow( row ); } errtotal += errcount; } // exit code of 2 indicates program detected invalid data return errtotal && errcode ? 2 : 0; }
int RemoveNewlineCommand :: Execute( ALib::CommandLine & cmd ) { GetSkipOptions( cmd ); ProcessFlags( cmd ); IOManager io( cmd ); CSVRow row; while( io.ReadCSV( row ) ) { if ( Skip( row ) ) { continue; } if ( ! Pass( row ) ) { RemoveNewlines( row ); } io.WriteRow( row ); } return 0; }
int CallCommand :: Execute( ALib::CommandLine & cmd ) { GetSkipOptions( cmd ); ProcessFlags( cmd ); std::vector <char> outbuf( mOutBufSize ); HMODULE dll = LoadLibrary( mDLL.c_str() ); if ( dll == NULL ) { CSVTHROW( "LoadLibrary call on " << mDLL << " failed" ); } mFunc = (FuncType) GetProcAddress( dll, mFuncName.c_str() ); if ( mFunc == NULL ) { CSVTHROW( "Cannot load function " << mFuncName << "from DLL " << mDLL ); } IOManager io( cmd ); CSVRow row; while( io.ReadCSV( row ) ) { if ( ! Skip( io, row ) ) { continue; } if ( Pass( io, row ) ) { io.WriteRow( row ); continue; } int rv = CallOnFields( row, &outbuf[0] ); if ( rv == 0 ) { io.WriteRow( row ); } else if ( rv > 0 ) { CSVTHROW( mFuncName << " returned error code " << rv ); } else { // do nothing - negative values just mean skip output } } return 0; }
void SQLCommand :: GetCommonValues( ALib::CommandLine & cmd ) { GetSkipOptions( cmd ); mTable = cmd.GetValue( FLAG_TABLE ); if ( mTable == "" ) { CSVTHROW( "Need table name specified by " << FLAG_TABLE << " flag" ); } if ( ! cmd.HasFlag( FLAG_SQLSEP ) ) { mSep ="\n;\n"; } else { mSep = ALib::UnEscape( cmd.GetValue( FLAG_SQLSEP ) ); } string noq = cmd.GetValue( FLAG_NOQUOTE, "" ); if ( ! ALib::IsEmpty( noq ) ) { CommaListToIndex( ALib::CommaList( noq ), mNoQuote ); } mQuoteNulls = cmd.HasFlag( FLAG_QNULLS ); mEmptyNulls = cmd.HasFlag( FLAG_ENULLS ); }
int TruncPadBase :: Execute( ALib::CommandLine & cmd ) { GetSkipOptions( cmd ); string ps = cmd.GetValue( FLAG_PAD ); ALib::CommaList padding( ps ); unsigned int ncols = padding.Size(); bool ncolspec = false; // use explicit size or not if ( ncols == 0 || cmd.HasFlag( FLAG_NUM ) ) { if ( ! cmd.HasFlag( FLAG_NUM ) ) { CSVTHROW( "Need -n flag to specify field count" ); } ncolspec = true; string nv = cmd.GetValue( FLAG_NUM ); if ( ALib::ToInteger( nv, "-n flag needs integer value" ) < 0 ) { CSVTHROW( FLAG_NUM << " needs value greater or equal to zero" ); } ncols = ALib::ToInteger( nv ); } IOManager io( cmd ); CSVRow row; while( io.ReadCSV( row ) ) { if ( Skip( io, row ) ) { continue; } if ( ! Pass( io, row ) ) { unsigned int nc = ncolspec ? ncols : row.size() + padding.Size(); ProcessRow( row, nc, padding ); } io.WriteRow( row ); } return 0; }
int WriteFixedCommand :: Execute( ALib::CommandLine & cmd ) { GetSkipOptions( cmd ); BuildFields( cmd ); IOManager io( cmd ); if ( cmd.HasFlag( FLAG_RULER ) ) { io.Out() << Ruler() << "\n"; } CSVRow row; while( io.ReadCSV( row ) ) { if ( Skip( row ) ) { continue; } string line = MakeFixedOutput( row ); io.Out() << line << "\n"; } return 0; }
int ExcludeCommand :: Execute( ALib::CommandLine & cmd ) { GetSkipOptions( cmd ); ProcessFlags( cmd ); IOManager io( cmd ); CSVRow row; while( io.ReadCSV( row ) ) { if ( Skip( row ) ) { continue; } if ( ! Pass( row ) ) { if ( EvalExprOnRow( io, row ) ) { Exclude( row ); } } io.WriteRow( row ); } return 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; }
int FileSplitCommand :: Execute( ALib::CommandLine & cmd ) { GetSkipOptions( cmd ); ProcessFlags( cmd ); IOManager io( cmd ); CSVRow row; while( io.ReadCSV( row ) ) { if ( Skip( row ) ) { continue; } if ( Pass( row ) ) { io.WriteRow( row ); } else { WriteRow( io, row ); } } mOutFile.flush(); return 0; }