// rdcollect.cpp // // Collect and combine log exports into a single file. // // (C) Copyright 2010,2016 Fred Gleason // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License version 2 as // published by the Free Software Foundation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public // License along with this program; if not, write to the Free Software // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. // #include #include #include #include #include #include #include MainObject::MainObject(QObject *parent) :QObject(parent) { bool ok; int err; std::vector line_index; hours_offset=0; minutes_offset=3; seconds_offset=6; // // Read Command Options // RDCmdSwitch *cmd= new RDCmdSwitch(qApp->argc(),qApp->argv(),"rdcollect",RDCOLLECT_USAGE); for(unsigned i=0;ikeys();i++) { if(cmd->key(i)=="--source-file") { source_files.push_back(cmd->value(i)); cmd->setProcessed(i,true); } if(cmd->key(i)=="--destination-file") { destination_file=cmd->value(i); cmd->setProcessed(i,true); } if(cmd->key(i)=="--hours-offset") { hours_offset=cmd->value(i).toUInt(&ok); cmd->setProcessed(i,true); } if(cmd->key(i)=="--minutes-offset") { minutes_offset=cmd->value(i).toUInt(&ok); cmd->setProcessed(i,true); } if(cmd->key(i)=="--seconds-offset") { seconds_offset=cmd->value(i).toUInt(&ok); cmd->setProcessed(i,true); } } if(!cmd->allProcessed()) { fprintf(stderr,"rdcollect: unknown option\n"); exit(256); } if(source_files.size()==0) { fprintf(stderr,"rdcollect: missing --source-file argument\n"); exit(256); } if(destination_file.isEmpty()) { fprintf(stderr,"rdcollect: missing --destination-file argument\n"); exit(256); } // // Process Data // QStringList src_lines; for(int i=0;i0) { lines->push_back(QString(line).left(strlen(line)-1)); } file.close(); } void MainObject::SortLines(QStringList *lines,std::vector *index) { std::vector start_times; // // Initialize Index // for(int i=0;isize();i++) { index->push_back(i); start_times.push_back(ReadTime((*lines)[i])); } // // Sort // bool modified=true; while(modified) { modified=false; for(int i=1;isize();i++) { if(start_times[i-1]>start_times[i]) { QTime time=start_times[i-1]; start_times[i-1]=start_times[i]; start_times[i]=time; unsigned line=index->at(i-1); index->at(i-1)=index->at(i); index->at(i)=line; modified=true; } } } } int MainObject::WriteOutputFile(const QString &filename, const QStringList &lines, std::vector *index) { FILE *f=NULL; if((f=fopen(filename,"w"))==NULL) { return errno; } for(int i=0;iat(i)]); } fclose(f); return 0; } void MainObject::AddDirs(const QString &path,QStringList *dirs) { QDir dir(path); dir.setFilter(QDir::Dirs); QStringList list=dir.entryList(); for(int i=0;ipush_back(path+"/"+list[i]); AddDirs(path+"/"+list[i],dirs); } } } QTime MainObject::ReadTime(const QString &line) { return QTime(line.mid(hours_offset,2).toInt(), line.mid(minutes_offset,2).toInt(), line.mid(seconds_offset,2).toInt()); } int main(int argc,char *argv[]) { QApplication a(argc,argv,false); new MainObject(); return a.exec(); }