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 ); } }
void CSVIO::read(std::vector<std::vector<double> >& out){ CSVRow row; std::vector<string> rowStr; row.read(ifs); rowStr = row.getData(); std::vector<double> tVec(rowStr.size()); int rowindex = 0; while(!ifs.fail() && !ifs.eof() && (rowindex < nrow)){ row.read(ifs); rowStr = row.getData(); for(int i = 0; i < rowStr.size(); i++){ tVec[i] = std::stof(rowStr.at(i)); } out.push_back(tVec); ++rowindex; rowStr.clear(); } }
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 ); }
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; }
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; }
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; }
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; }
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 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 ); }
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 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 ShuffleCommand ::ShuffleFields( CSVRow & row ) { std::vector <string> f; for( unsigned int i = 0; i < mFields.size(); i++ ) { unsigned int fi = mFields[i]; if ( fi >= row.size() ) { CSVTHROW( "Invalid field index: " << fi + 1 ); } else { f.push_back( row[fi] ); } } std::random_shuffle( f.begin(), f.end() ); unsigned int ri = 0; for( unsigned int i = 0; i < mFields.size(); i++ ) { unsigned int fi = mFields[i]; row[fi] = f[ri++]; } }
void TemplateCommand :: HandleSpecialChars( const string & tplate, char c, unsigned int & pos, const CSVRow & row, string & out ) { char t = tplate[ pos++ ]; if ( c == '\\' ) { switch( t ) { case '\n': case '\r': CSVTHROW( "Invalid escape at end of line" ); case 'n': out += '\n'; break; case 't': out += '\t'; break; default: out += t; } } else { // its a brace string ns; while( t != POUTRO) { if ( t == '\n' || t == '\r' ) { CSVTHROW( "Missing closing brace" ); } ns += t; t = tplate[ pos++ ]; } if( ns.size() && ns[0] == EVALCHR ) { // it's an expression out += Eval( row, ns ); return; } if ( ! ALib::IsInteger( ns ) ) { CSVTHROW( "Invalid placeholder: " << "{" << ns << "}" ); } int n = ALib::ToInteger( ns ) - 1; // to zero based if ( n < 0 ) { CSVTHROW( "Invalid placeholder: " << "{" << ns << "}" ); } if ( (unsigned int) n < row.size() ) { out += row[ n ]; } } }
void TrimCommand :: Trim( CSVRow & row ) { for ( unsigned int i = 0; i < row.size(); i++ ) { if ( mFields.size() == 0 || ALib::Contains( mFields, i ) ) { if ( mTrimLead && mTrimTrail ) { row[i] = ALib::Trim( row[i] ); } else if ( mTrimLead ) { row[i] = ALib::LTrim( row[i] ); } else { row[i] = ALib::RTrim( row[i] ); } } if ( mWidths.size() ) { if ( mFields.size() == 0 || ALib::Contains( mFields, i ) ) { Chop( row, i ); } } } }
string SQLInsertCommand :: CreateValues( const CSVRow & row ) const { string vals = ""; 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 ( vals != "" ) { vals += ", "; } string field = EmptyToNull( row[fi] ); vals += ( DoSQLQuote( i ) && ! NoNullQuote( field ) ) ? ALib::SQuote( ALib::SQLQuote( field ) ) : field; } return " VALUES( " + vals + ")"; }
void RemoveNewlineCommand :: RemoveNewlines( CSVRow & row ) { for ( unsigned int i = 0; i < row.size(); i++ ) { if ( (mFields.size() == 0 || ALib::Contains( mFields, i ) ) && row[i].find_first_of( "\n" ) != std::string::npos ) { string s; for ( unsigned int j = 0; j < row[i].size(); j++ ) { if ( row[i][j] == '\n' ) { if ( mExcludeAfter ) { break; } else { s += mSep; } } else { s += row[i][j]; } } row[i] = s; } } }
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) ); } } } } }
int32_t main(int32_t argc, char **argv) { uint32_t retVal = 0; int recIndex=1; bool log=false; if((argc != 2 && argc != 3 && argc != 4) || (argc==4 && string(argv[1]).compare("-l")!=0)) { errorMessage(string(argv[0])); retVal = 1; } else if(argc==2 && string(argv[1]).compare("-h")==0) { helpMessage(string(argv[0])); retVal = 0; } else { // if -l option is set if(argc==4 || (argc==3 && string(argv[1]).compare("-l")==0)) { ++recIndex; log=true; } // Use command line parameter as file for playback; string recordingFile(argv[recIndex]); stringstream recordingFileUrl; recordingFileUrl << "file://" << recordingFile; // Location of the recording file. URL url(recordingFileUrl.str()); // Do we want to rewind the stream on EOF? const bool AUTO_REWIND = false; // Size of the memory buffer that should fit at least the size of one frame. const uint32_t MEMORY_SEGMENT_SIZE = 1024 * 768; // Number of memory segments (one is enough as we are running sychronously). const uint32_t NUMBER_OF_SEGMENTS = 1; // Run player in synchronous mode without data caching in background. const bool THREADING = false; // Construct the player. Player player(url, AUTO_REWIND, MEMORY_SEGMENT_SIZE, NUMBER_OF_SEGMENTS, THREADING); // The next container from the recording. Container nextContainer; // Using OpenCV's IplImage data structure to simply playback the data. IplImage *image = NULL; // Create the OpenCV playback window. cvNamedWindow("CaroloCup-CameraPlayback", CV_WINDOW_AUTOSIZE); // This flag indicates whether we have attached already to the shared // memory containing the sequence of captured images. bool hasAttachedToSharedImageMemory = false; // Using this variable, we will access the captured images while // also having convenient automated system resource management. SharedPointer<SharedMemory> sharedImageMemory; ifstream file(argv[recIndex+1]); CSVRow row; // read out the header row row.readNextRow(file); uint32_t frameNumber=1, csvFN; int32_t VPx,VPy,BLx,BLy,BRx,BRy,TLx,TLy,TRx,TRy; stringstream frameMessage; stringstream VPMessage; frameMessage.str(string()); VPMessage.str(string()); bool fbf=false; // Main data processing loop. while (player.hasMoreData()) { // Read next entry from recording. nextContainer = player.getNextContainerToBeSent(); // Data type SHARED_IMAGE contains a SharedImage data structure that // provides meta-information about the captured image. if (nextContainer.getDataType() == Container::SHARED_IMAGE) { // Read the data structure to retrieve information about the image. SharedImage si = nextContainer.getData<SharedImage>(); // Check if we have already attached to the shared memory. if (!hasAttachedToSharedImageMemory) { sharedImageMemory = SharedMemoryFactory::attachToSharedMemory(si.getName()); // Toggle the flag as we have now attached to the shared memory. hasAttachedToSharedImageMemory = true; } // Check if we could successfully attach to the shared memory. if (sharedImageMemory->isValid()) { // Using a scoped lock to get exclusive access. { Lock l(sharedImageMemory); if (image == NULL) { // Create the IplImage header data and access the shared memory for the actual image data. image = cvCreateImageHeader(cvSize(si.getWidth(), si.getHeight()), IPL_DEPTH_8U, si.getBytesPerPixel()); // Let the IplImage point to the shared memory containing the captured image. image->imageData = static_cast<char*>(sharedImageMemory->getSharedMemory()); } } // Show the image using OpenCV. // if csv parameter is set if(argc==4 || (argc==3 && string(argv[1]).compare("-l")!=0)) { if(! row.readNextRow(file)) break; while(row[0].compare("")==0) if(! row.readNextRow(file)) break; sscanf(row[0].c_str(), "%d", &csvFN); if(frameNumber==csvFN) { Mat img = cvarrToMat(image); frameMessage.str(string()); VPMessage.str(string()); sscanf(row[9].c_str(), "%d", &VPx); sscanf(row[10].c_str(), "%d", &VPy); frameMessage<<"Frame "<<frameNumber; VPMessage<<"Vanishing Point ("<<VPx<<","<<VPy<<")"; setLabel(img, frameMessage.str(), cvPoint(30,45)); setLabel(img, VPMessage.str(), cvPoint(30,60)); if(log) cout << frameNumber << ", " << VPx << ", " << VPy <<endl; // print support points and lines sscanf(row[1].c_str(), "%d", &BLx); sscanf(row[2].c_str(), "%d", &BLy);BLy+=60; sscanf(row[3].c_str(), "%d", &TLx); sscanf(row[4].c_str(), "%d", &TLy);TLy+=60; sscanf(row[5].c_str(), "%d", &TRx); sscanf(row[6].c_str(), "%d", &TRy);TRy+=60; sscanf(row[7].c_str(), "%d", &BRx); sscanf(row[8].c_str(), "%d", &BRy);BRy+=60; circle(img, Point(BLx,BLy), 5, CV_RGB(255, 255, 255), CV_FILLED); circle(img, Point(TLx,TLy), 5, CV_RGB(255, 255, 255), CV_FILLED); circle(img, Point(TRx,TRy), 5, CV_RGB(255, 255, 255), CV_FILLED); circle(img, Point(BRx,BRy), 5, CV_RGB(255, 255, 255), CV_FILLED); double slope1 = static_cast<double>(TLy-BLy)/static_cast<double>(TLx-BLx); double slope2 = static_cast<double>(TRy-BRy)/static_cast<double>(TRx-BRx); Point p1(0,0), q1(img.cols,img.rows); Point p2(0,0), q2(img.cols,img.rows); p1.y = -(BLx-p1.x) * slope1 + BLy; q1.y = -(TLx-q1.x) * slope1 + TLy; p2.y = -(BRx-p2.x) * slope2 + BRy; q2.y = -(TRx-q2.x) * slope2 + TRy; line(img,p1,q1,CV_RGB(255, 255, 255),1,CV_AA); line(img,p2,q2,CV_RGB(255, 255, 255),1,CV_AA); imshow("CaroloCup-CameraPlayback", img); } } else cvShowImage("CaroloCup-CameraPlayback", image); // Let the image render before proceeding to the next image. char c = cvWaitKey(10); // Check if the user wants to stop the replay by pressing ESC or pause it by pressing SPACE (needed also to go frame-by-frame). if (static_cast<uint8_t>(c) == 27) break; else if (static_cast<uint8_t>(c) == 32 || fbf) { do { c = cvWaitKey(); }while(c!='n' && static_cast<uint8_t>(c) != 32 && static_cast<uint8_t>(c) != 27); if (static_cast<uint8_t>(c) == 27) break; // ESC else if (static_cast<uint8_t>(c) == 32) fbf=false; // SPACE -> continue else if (c=='n') fbf=true; // pressed 'n' -> next frame } ++frameNumber; } } } // maybe print EOF message && wait for user input? // Release IplImage data structure. cvReleaseImage(&image); // Close playback window. cvDestroyWindow("CaroloCup-CameraPlayback"); // The shared memory will be automatically released. } // Return error code. return retVal; }
void GMCWriter::applyGMCVariantForTDD(TDDTable * tdd, GMCDocument * doc, std::string type, std::string hz, std::string frameConf, std::string specSubfConf ) { // Find column int columnIndex = -1; std::string cellType = type == "Indoor" ? "very_small" : "small"; CSVRow * cellTypeRow = tdd->getParameterRowByName("cellType"); CSVRow * hzRow = tdd->getParameterRowByName("chBw (MHz)"); CSVRow * frameConfRow = tdd->getParameterRowByName("tddFrameConf"); CSVRow * specSubfConfRow = tdd->getParameterRowByName("tddSpecSubfConf"); if ( cellTypeRow == NULL || hzRow == NULL || frameConfRow == NULL || specSubfConfRow == NULL ) return; int cellsCount = cellTypeRow->getCells().size(); for ( int i = 1; i < cellsCount; i ++ ) { if ( cellTypeRow->getCell(i) == cellType && hzRow->getCell(i) == hz && frameConfRow->getCell(i) == frameConf && specSubfConfRow->getCell(i) == specSubfConf) { columnIndex = i; break; } } if ( columnIndex == -1 ) return; // Apply changes to LNCEL vector<GMCManagedObject*> mocs = doc->getMocsByClassName("LNCEL"); for ( vector<GMCManagedObject*>::iterator it = mocs.begin(); it != mocs.end(); it ++ ) { vector<CSVRow*> parameterRows = tdd->getParameterRows(); for ( vector<CSVRow*>::iterator itParRow = parameterRows.begin(); itParRow != parameterRows.end(); itParRow ++ ) { GMCManagedObject * m = *it; CSVRow * parRow = *itParRow; GMCManagedObjectParameter * p = m->getParameterByName(parRow->getCell(0)); if ( p != NULL ) { modifyParameterSimpleTypeFromSameGmc(doc, p, parRow->getCell(columnIndex)); } } } }
string PivotCommand :: GetFact( const CSVRow & row ) const { if ( mFact >= row.size() ) { CSVTHROW( "Invalid fact index: " << mFact ); } return row[mFact]; }
void GMCWriter::applyGMCVariantForFDDOutdoor(FDDOutdoorTable * fdd, GMCDocument * doc, std::string hz ) { // Find column int columnIndex = -1; CSVRow * hzRow = fdd->getParameterRowByName("dlChBw"); if ( hzRow == NULL ) return; int cellsCount = hzRow->getCells().size(); for ( int i = 1; i < cellsCount; i ++ ) { if ( hzRow->getCell(i) == hz ) { columnIndex = i; break; } } if ( columnIndex == -1 ) return; // Apply changes to LNCEL vector<GMCManagedObject*> mocs = doc->getMocsByClassName("LNCEL"); for ( vector<GMCManagedObject*>::iterator it = mocs.begin(); it != mocs.end(); it ++ ) { vector<CSVRow*> parameterRows = fdd->getParameterRows(); for ( vector<CSVRow*>::iterator itParRow = parameterRows.begin(); itParRow != parameterRows.end(); itParRow ++ ) { GMCManagedObject * m = *it; CSVRow * parRow = *itParRow; GMCManagedObjectParameter * p = m->getParameterByName(parRow->getCell(3)); if ( p != NULL ) { if ( parRow->getCell(3) != "earfcnDL" && parRow->getCell(3) != "earfcnUL" ) { if ( parRow->getCell(1) != "" ) { GMCManagedObjectParameter * compPar = m->getParameterByName(parRow->getCell(1)); if ( compPar != NULL ) modifyParameterComplexTypeFromSameGmc(doc, compPar, parRow->getCell(3), parRow->getCell(columnIndex)); } else { if ( XmlElementReader::getName(p->getElement()) == "p" ) modifyParameterSimpleTypeFromSameGmc(doc, p, parRow->getCell(columnIndex)); } } } } } }
void printRow(const CSVRow& row) { printf("[%d:%lu] ", row.getIndex(), row.getCount(true)); UnicodeString temp; CSVFormatter::formatRow(row, temp, sepchar); std::cout<<temp<<std::endl; }
void DataRecorder::Record(CSVRow row, std::string identifier) { assert(row.m_cellData.size() == m_streams[identifier]->m_columns); m_streams[identifier]->m_stream << row.GetRow(); }