123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- #include "dialogivctocsv.h"
- #include "ui_dialogivctocsv.h"
- #include <QFileDialog>
- #include <QMessageBox>
- #include <QDateTime>
- #include <functional>
- Dialogivctocsv::Dialogivctocsv(QWidget *parent) :
- QDialog(parent),
- ui(new Ui::Dialogivctocsv)
- {
- ui->setupUi(this);
- ui->checkBox->setChecked(false);
- ui->comboBox->addItem("ms");
- ui->comboBox->addItem("s");
- setWindowTitle("ivc to csv");
- }
- Dialogivctocsv::~Dialogivctocsv()
- {
- delete ui;
- }
- class ivcdataunit
- {
- public:
- qint64 ntime;
- double fvalue;
- };
- class ivcdata
- {
- public:
- std::string strvarname;
- size_t hashname;
- std::vector<ivcdataunit> mvectordata;
- int nnowpos = 0;
- };
- void Dialogivctocsv::on_pushButton_Convert_clicked()
- {
- QString strpath = QFileDialog::getOpenFileName(this,"Load ivc File",".","*.ivc");
- if(strpath.isEmpty())return;
- QString strpathSave = QFileDialog::getSaveFileName(this,"Save CSV File",".","*.csv");
- if(strpathSave.isEmpty())return;
- QFile xFileivc;
- QFile xFilecsv;
- if(strpathSave.right(4) != ".csv")
- {
- strpathSave.append(".csv");
- }
- xFileivc.setFileName(strpath);
- xFilecsv.setFileName(strpathSave);
- if(!xFileivc.open(QIODevice::ReadOnly))
- {
- QMessageBox::warning(this,"Waring","Can't Open IVC File.",QMessageBox::YesAll);
- return;
- }
- if(!xFilecsv.open(QIODevice::ReadWrite))
- {
- xFileivc.close();
- QMessageBox::warning(this,"Warning","Can't Open CSV File.",QMessageBox::YesAll);
- return;
- }
- int ntimemode = ui->comboBox->currentIndex();
- bool bFillData = ui->checkBox->isChecked();
- int ndatasize;
- iv::ivchart::ivchartarray xivarray;
- std::vector<ivcdata> xvectorivcdata;
- int nRead = xFileivc.read((char *)&ndatasize,sizeof(int));
- while(nRead == sizeof(int))
- {
- if(ndatasize<=0)break;
- char * str = new char[ndatasize];
- nRead = xFileivc.read(str,ndatasize);
- if(nRead == ndatasize)
- {
- if(xivarray.ParseFromArray(str,ndatasize))
- {
- unsigned int i;
- for(i=0;i<xivarray.xivchartunit_size();i++)
- {
- iv::ivchart::ivchartunit * punit = xivarray.mutable_xivchartunit(i);
- std::hash<std::string > h;
- size_t hash_unit = h(punit->strvarname());
- unsigned int j;
- bool bhave = false;
- for(j=0;j<xvectorivcdata.size();j++)
- {
- if(xvectorivcdata[j].hashname == hash_unit)
- {
- ivcdataunit iu;
- iu.ntime = punit->timex();
- iu.fvalue = punit->fvalue();
- xvectorivcdata[j].mvectordata.push_back(iu);
- bhave = true;
- break;
- }
- }
- if(bhave == false)
- {
- ivcdata xdata;
- xdata.hashname = hash_unit;
- xdata.strvarname = punit->strvarname();
- ivcdataunit iu;
- iu.ntime = punit->timex();
- iu.fvalue = punit->fvalue();
- xdata.mvectordata.push_back(iu);
- xvectorivcdata.push_back(xdata);
- }
- }
- }
- }
- else
- {
- break;
- }
- nRead = xFileivc.read((char *)&ndatasize,sizeof(int));
- }
- char strout[10000];
- char strtem[1000];
- double fDiv = 1;
- if(ntimemode == 1)fDiv = 1000.0;
- snprintf(strout,10000,"Time ; AbsTime ; RelTime");
- unsigned int i;
- double * xvectorlastvalue = new double[xvectorivcdata.size()];
- for(i=0;i<xvectorivcdata.size();i++)
- {
- snprintf(strtem,1000," ; %s",xvectorivcdata[i].strvarname.data());
- strncat(strout,strtem,10000);
- xvectorivcdata[i].nnowpos = 0;
- xvectorlastvalue[i] = -123456.6789;
- }
- snprintf(strtem,1000,"\n");
- strncat(strout,strtem,10000);
- xFilecsv.write(strout,strnlen(strout,10000));
- qint64 nstarttime = xvectorivcdata[0].mvectordata[0].ntime;
- qint64 nnowtime = nstarttime;
- bool bComplete = false;
- while(bComplete == false)
- {
- snprintf(strout,10000,"%s ; %lld ; %f ",QDateTime::fromMSecsSinceEpoch(nnowtime).toString("yyyy-MM-dd hh:mm:ss:zzz").toLatin1().data(),
- nnowtime,((double)(nnowtime - nstarttime))/fDiv);
- for(i=0;i<xvectorivcdata.size();i++)
- {
- if(xvectorivcdata[i].nnowpos <xvectorivcdata[i].mvectordata.size())
- {
- ivcdataunit * pdata = &xvectorivcdata[i].mvectordata[xvectorivcdata[i].nnowpos];
- if(nnowtime == pdata->ntime)
- {
- snprintf(strtem,1000," ; %f",pdata->fvalue);
- strncat(strout,strtem,10000);
- xvectorlastvalue[i] = pdata->fvalue;
- xvectorivcdata[i].nnowpos++;
- }
- else
- {
- if((bFillData)&&(xvectorlastvalue[i] != -123456.6789))
- {
- snprintf(strtem,1000," ; %f",xvectorlastvalue[i]);
- strncat(strout,strtem,10000);
- }
- else
- {
- snprintf(strtem,1000," ; ");
- strncat(strout,strtem,10000);
- }
- }
- }
- else
- {
- snprintf(strtem,1000," ; ");
- strncat(strout,strtem,10000);
- }
- }
- snprintf(strtem,1000,"\n");
- strncat(strout,strtem,10000);
- xFilecsv.write(strout,strnlen(strout,10000));
- bool bFindNewNow = false;
- for(i=0;i<xvectorivcdata.size();i++)
- {
- if(xvectorivcdata[i].nnowpos <xvectorivcdata[i].mvectordata.size())
- {
- ivcdataunit * pdata = &xvectorivcdata[i].mvectordata[xvectorivcdata[i].nnowpos];
- if(bFindNewNow == false)
- {
- bFindNewNow = true;
- nnowtime = pdata->ntime;
- }
- else
- {
- if(nnowtime > pdata->ntime)
- {
- nnowtime = pdata->ntime;
- }
- }
- }
- }
- if(bFindNewNow == false)
- {
- bComplete = true;
- break;
- }
- }
- xFilecsv.close();
- QMessageBox::information(this,"Info","Covert Complete.",QMessageBox::YesAll);
- }
|