| 1 | // %flair:license{
|
---|
| 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
| 4 | // %flair:license}
|
---|
| 5 | #include "file_ui.h"
|
---|
| 6 | #include <stdio.h>
|
---|
| 7 |
|
---|
| 8 | #include <cstring>
|
---|
| 9 | #include <cstdlib>
|
---|
| 10 | #include <fstream>
|
---|
| 11 | #include <iostream>
|
---|
| 12 | #include <QDir>
|
---|
| 13 | #include <QDate>
|
---|
| 14 | #include <QTextStream>
|
---|
| 15 | #include <QGridLayout>
|
---|
| 16 | #include <io_hdfile.h>
|
---|
| 17 | #include <QtEndian>
|
---|
| 18 | #include <QComboBox>
|
---|
| 19 | #include <QPushButton>
|
---|
| 20 | #include <QTextEdit>
|
---|
| 21 | #include <QDialog>
|
---|
| 22 | #include <QStringList>
|
---|
| 23 | #include <QFormLayout>
|
---|
| 24 |
|
---|
| 25 | #ifndef WIN32
|
---|
| 26 | #include <arpa/inet.h>
|
---|
| 27 | #else
|
---|
| 28 | #include <winsock2.h>
|
---|
| 29 | #include <ws2tcpip.h>
|
---|
| 30 | #endif
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 | using namespace std;
|
---|
| 34 |
|
---|
| 35 | file_ui::file_ui()
|
---|
| 36 | {
|
---|
| 37 | dialog=new QDialog();
|
---|
| 38 | dialog->setWindowTitle("log files");
|
---|
| 39 | QGridLayout *main_layout = new QGridLayout(dialog);
|
---|
| 40 | ok_button= new QPushButton("Ok",dialog);
|
---|
| 41 | log_text=new QTextEdit(dialog);
|
---|
| 42 | log_text->setReadOnly(true);
|
---|
| 43 | input_text=new QTextEdit(dialog);
|
---|
| 44 | input_text->setText("add your log comment here");
|
---|
| 45 | input_cleared=false;
|
---|
| 46 |
|
---|
| 47 | ok_button->setEnabled(false);
|
---|
| 48 |
|
---|
| 49 | main_layout->addWidget(log_text,0,0);
|
---|
| 50 | main_layout->addWidget(input_text,1,0);
|
---|
| 51 |
|
---|
| 52 | QWidget *widget=new QWidget(dialog);
|
---|
| 53 | QFormLayout *formLayout = new QFormLayout(widget);
|
---|
| 54 | csv_combo = new QComboBox(widget);
|
---|
| 55 | formLayout->addRow(tr("save all log with following base time"), csv_combo);
|
---|
| 56 | csv_combo->addItem("(no base time)");
|
---|
| 57 | main_layout->addWidget(widget,2,0);
|
---|
| 58 | main_layout->addWidget(ok_button,3,0);
|
---|
| 59 |
|
---|
| 60 | connect(ok_button, SIGNAL(clicked()),this, SLOT(save()), Qt::QueuedConnection);
|
---|
| 61 | connect(this, SIGNAL(showDialog()),dialog, SLOT(show()), Qt::QueuedConnection);
|
---|
| 62 | connect(this, SIGNAL(appendToLog(QString)),log_text, SLOT(append(QString)), Qt::QueuedConnection);
|
---|
| 63 | connect(input_text, SIGNAL(cursorPositionChanged()),this, SLOT(clearInputText()), Qt::QueuedConnection);
|
---|
| 64 |
|
---|
| 65 | file_names=new QStringList();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | file_ui::~file_ui()
|
---|
| 69 | {
|
---|
| 70 | delete dialog;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | void file_ui::log(QString text) {
|
---|
| 74 | appendToLog(text);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void file_ui::addFile(QString file_path) {
|
---|
| 78 | //framework sends dbt file then txt file
|
---|
| 79 | //when we receive txt, we have both files
|
---|
| 80 | //and we can convert it to .csv
|
---|
| 81 | if(file_path.endsWith(".dbt")==true) {
|
---|
| 82 | QString name=file_path.section('/', -1);//remove path for displaying on combobox
|
---|
| 83 | csv_combo->addItem(name.replace(QString(".dbt"), QString(".csv")));
|
---|
| 84 | file_names->append(file_path.replace(QString(".dbt"), QString(".csv")));
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | if(file_path.endsWith(".txt")==true) {
|
---|
| 88 | dbt2csv(file_path.replace(QString(".txt"), QString(".dbt")));
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | if(file_names->size()==1) {
|
---|
| 92 | input_cleared=false;
|
---|
| 93 | showDialog();
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | void file_ui::endOfFiles(void) {
|
---|
| 98 | ok_button->setEnabled(true);
|
---|
| 99 |
|
---|
| 100 | qint64 max_file_size=0;
|
---|
| 101 | for(int i=0;i<file_names->count();i++) {
|
---|
| 102 | QFileInfo info(file_names->at(i));
|
---|
| 103 | if(info.size()>max_file_size) {
|
---|
| 104 | max_file_size=info.size();
|
---|
| 105 | csv_combo->setCurrentIndex(i+1);//first item of combobox is already taken
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | void file_ui::dbt2csv(QString file_path)
|
---|
| 111 | {
|
---|
| 112 | hdfile_t *dbtFile = NULL;
|
---|
| 113 | char *data;
|
---|
| 114 | QStringList data_type;
|
---|
| 115 |
|
---|
| 116 | QString filename=file_path.section('/', -1);//remove path for displaying on logs
|
---|
| 117 | appendToLog(QString("converting %1 to csv").arg(filename));
|
---|
| 118 |
|
---|
| 119 | //open csv file
|
---|
| 120 | QString csv_filename=file_path;
|
---|
| 121 | csv_filename.replace(QString(".dbt"), QString(".csv"));
|
---|
| 122 | QFile csv_file(csv_filename);
|
---|
| 123 |
|
---|
| 124 | if(!csv_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
---|
| 125 | appendToLog(" error opening csv file!");
|
---|
| 126 | return;
|
---|
| 127 | }
|
---|
| 128 | QTextStream out(&csv_file);
|
---|
| 129 |
|
---|
| 130 | //open txt file
|
---|
| 131 | QString txt_filename=file_path;
|
---|
| 132 | txt_filename.replace(QString(".dbt"), QString(".txt"));
|
---|
| 133 | QFile txt_file(txt_filename);
|
---|
| 134 |
|
---|
| 135 | if(!txt_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
---|
| 136 | appendToLog(" error opening txt file!");
|
---|
| 137 | return;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | //read txt file
|
---|
| 141 | QTextStream txt_in(&txt_file);
|
---|
| 142 | txt_in.readLine();//time us
|
---|
| 143 | txt_in.readLine();//time ns
|
---|
| 144 | while(1)
|
---|
| 145 | {
|
---|
| 146 | if(txt_in.atEnd()==true) break;
|
---|
| 147 | QString txt_line=txt_in.readLine();
|
---|
| 148 | data_type.append(txt_line.section("(",-1));//on part de la fin pour trouver la premiere parenthese ouvrante
|
---|
| 149 | //printf("type %s\n",txt_line.section("(",-1).toLocal8Bit().constData());
|
---|
| 150 | }
|
---|
| 151 | txt_file.close();
|
---|
| 152 |
|
---|
| 153 | dbtFile=open_hdfile(file_path.toLocal8Bit().data(),READ_MODE);
|
---|
| 154 |
|
---|
| 155 | if (!dbtFile)
|
---|
| 156 | {
|
---|
| 157 | appendToLog(" error opening dbt file!");
|
---|
| 158 | return;
|
---|
| 159 | }
|
---|
| 160 | data=(char*)malloc(dbtFile->h.DataSize);
|
---|
| 161 | if(data==NULL)
|
---|
| 162 | {
|
---|
| 163 | appendToLog(" error malloc!");
|
---|
| 164 | return;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | bool dataWritten=false;
|
---|
| 168 | while(1)
|
---|
| 169 | {
|
---|
| 170 | road_time_t time;
|
---|
| 171 | road_timerange_t tr = 0;
|
---|
| 172 | int offset=0;
|
---|
| 173 | QTextStream csv_line;
|
---|
| 174 |
|
---|
| 175 | if(read_hdfile(dbtFile,(void*)data,&time,&tr)==0) {
|
---|
| 176 | break;
|
---|
| 177 | }
|
---|
| 178 | dataWritten=true;
|
---|
| 179 |
|
---|
| 180 | out << time << "," << tr;
|
---|
| 181 | for(int i=0;i<data_type.size();i++)
|
---|
| 182 | {
|
---|
| 183 | if(data_type.at(i)=="float)")
|
---|
| 184 | {
|
---|
| 185 | float* value=(float*)(data+offset);
|
---|
| 186 | offset+=sizeof(float);
|
---|
| 187 | out << "," << *value;
|
---|
| 188 | }
|
---|
| 189 | else if(data_type.at(i)=="int8_t)")
|
---|
| 190 | {
|
---|
| 191 | int8_t* value=(int8_t*)(data+offset);
|
---|
| 192 | offset+=sizeof(int8_t);
|
---|
| 193 | float fl=(float)*value;
|
---|
| 194 | out << "," << *value;
|
---|
| 195 | }
|
---|
| 196 | else
|
---|
| 197 | {
|
---|
| 198 | appendToLog(QString(" unhandled type: %1").arg(data_type.at(i)));
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | out << "\n";
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | if(!dataWritten) {
|
---|
| 206 | //empty file!
|
---|
| 207 | out << "0,0";//timr
|
---|
| 208 | for(int i=0;i<data_type.size();i++) {
|
---|
| 209 | out << ",0";
|
---|
| 210 | }
|
---|
| 211 | out << "\n";
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | csv_file.close();
|
---|
| 215 | close_hdfile(dbtFile);
|
---|
| 216 | if(data!=NULL) free(data);
|
---|
| 217 |
|
---|
| 218 | appendToLog(" ok");
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | void file_ui::clearInputText(void){
|
---|
| 222 | if(input_cleared==false) {
|
---|
| 223 | input_cleared=true;
|
---|
| 224 | input_text->clear();
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | void file_ui::save(void)
|
---|
| 229 | {
|
---|
| 230 | save_comment();
|
---|
| 231 | if(csv_combo->currentIndex()!=0)
|
---|
| 232 | {
|
---|
| 233 | save_csv();
|
---|
| 234 | save_txt();
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | log_text->clear();
|
---|
| 238 | input_cleared=true;//avoid clearing it with setText
|
---|
| 239 | input_text->setText("add your log comment here");
|
---|
| 240 | file_names->clear();
|
---|
| 241 | csv_combo->clear();
|
---|
| 242 | csv_combo->addItem(QString("(no base time)"));
|
---|
| 243 |
|
---|
| 244 | dialog->setVisible(false);
|
---|
| 245 | ok_button->setEnabled(false);
|
---|
| 246 | emit finished();
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | void file_ui::save_comment(void)
|
---|
| 250 | {
|
---|
| 251 | QString folder_name=file_names->at(0).section('/', 0,-2);
|
---|
| 252 |
|
---|
| 253 | QString filename=folder_name+"/commentaire.txt";
|
---|
| 254 | QFile file(filename);
|
---|
| 255 | if(!file.open(QIODevice::WriteOnly | QIODevice::Text)) printf("file_ui::save_comment: erreur ouverture fichier %s\n",filename.toLocal8Bit().constData());
|
---|
| 256 | QTextStream out(&file);
|
---|
| 257 |
|
---|
| 258 | out<<input_text->toPlainText();
|
---|
| 259 | file.close();
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | void file_ui::save_csv(void)
|
---|
| 263 | {
|
---|
| 264 | //global csv file
|
---|
| 265 | QString folder_name=file_names->at(0).section('/', 0,-2);
|
---|
| 266 | QString filename=folder_name + "/all_logs.csv" ;
|
---|
| 267 | QFile global_file(filename);
|
---|
| 268 | if(!global_file.open(QIODevice::WriteOnly | QIODevice::Text)) printf("file_ui::save_csv: erreur ouverture fichier %s\n",filename.toLocal8Bit().constData());
|
---|
| 269 | QTextStream out(&global_file);
|
---|
| 270 |
|
---|
| 271 | //reference csv file
|
---|
| 272 | filename=file_names->at(csv_combo->currentIndex()-1);
|
---|
| 273 | QFile ref_file(filename);
|
---|
| 274 | //printf("file_ui::save_csv: ref %s\n",filename.toLocal8Bit().constData());
|
---|
| 275 | if(!ref_file.open(QIODevice::ReadOnly | QIODevice::Text)) printf("file_ui::save_csv: erreur ouverture ficher %s\n",filename.toLocal8Bit().constData());
|
---|
| 276 |
|
---|
| 277 | //other csv files
|
---|
| 278 | int j=0;
|
---|
| 279 | QFile m_file[file_names->count()-1];
|
---|
| 280 | QTextStream m_in[file_names->count()-1];
|
---|
| 281 | for(int i=0;i<file_names->count();i++)
|
---|
| 282 | {
|
---|
| 283 | if(i==csv_combo->currentIndex()-1) continue;
|
---|
| 284 | filename=file_names->at(i);
|
---|
| 285 | m_file[j].setFileName(filename);
|
---|
| 286 | if(!m_file[j].open(QIODevice::ReadOnly | QIODevice::Text)) printf("file_ui::save_csv: erreur ouverture ficher %s\n",filename.toLocal8Bit().constData());
|
---|
| 287 | m_in[j].setDevice(&m_file[j]);
|
---|
| 288 | j++;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | //init
|
---|
| 292 | QTextStream ref_in(&ref_file);
|
---|
| 293 | QString m_line[file_names->count()-1];
|
---|
| 294 | QString m_line_prev[file_names->count()-1];
|
---|
| 295 | for(int i=0;i<file_names->count()-1;i++)
|
---|
| 296 | {
|
---|
| 297 | m_line[i]=m_in[i].readLine();
|
---|
| 298 | m_line_prev[i]=m_line[i];
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | //organize csv files in one file
|
---|
| 302 | while(1)
|
---|
| 303 | {
|
---|
| 304 | if(ref_in.atEnd()==true) break;
|
---|
| 305 | QString ref_line=ref_in.readLine();
|
---|
| 306 |
|
---|
| 307 | qint64 ref_us=ref_line.section(',',0,0).toLongLong();
|
---|
| 308 | int ref_ns=ref_line.section(',',1,1).toInt();
|
---|
| 309 | //printf("ref %lld %i\n",ref_us,ref_ns);
|
---|
| 310 |
|
---|
| 311 | for(int i=0;i<file_names->count()-1;i++)
|
---|
| 312 | {
|
---|
| 313 | qint64 csv_us=m_line[i].section(',',0,0).toLongLong();
|
---|
| 314 | int csv_ns=m_line[i].section(',',1,1).toInt();
|
---|
| 315 | //printf("m %lld %i\n",csv_us,csv_ns);
|
---|
| 316 |
|
---|
| 317 | while(is_greater(ref_us,csv_us,ref_ns,csv_ns)==true)
|
---|
| 318 | {
|
---|
| 319 | m_line_prev[i]=m_line[i];
|
---|
| 320 | if(m_in[i].atEnd()==true) break;
|
---|
| 321 | m_line[i]=m_in[i].readLine();
|
---|
| 322 | csv_us=m_line[i].section(',',0,0).toLongLong();
|
---|
| 323 | csv_ns=m_line[i].section(',',1,1).toInt();
|
---|
| 324 | //printf("m %lld %i\n",csv_us,csv_ns);
|
---|
| 325 | }
|
---|
| 326 | csv_us=m_line_prev[i].section(',',0,0).toLongLong();
|
---|
| 327 | csv_ns=m_line_prev[i].section(',',1,1).toInt();
|
---|
| 328 | //printf("m ok %lld %i\n",csv_us,csv_ns);
|
---|
| 329 |
|
---|
| 330 | ref_line+="," + m_line_prev[i].section(',',2);
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | out<<ref_line << "\n";
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | global_file.close();
|
---|
| 337 | ref_file.close();
|
---|
| 338 | for(int i=0;i<file_names->count()-1;i++) m_file[i].close();
|
---|
| 339 | }
|
---|
| 340 |
|
---|
| 341 | void file_ui::save_txt(void)
|
---|
| 342 | {
|
---|
| 343 | //global txt file
|
---|
| 344 | QString folder_name=file_names->at(0).section('/', 0,-2);
|
---|
| 345 | QString filename=folder_name + "/all_logs.txt";
|
---|
| 346 | QFile global_file(filename);
|
---|
| 347 | if(!global_file.open(QIODevice::WriteOnly | QIODevice::Text)) printf("file_ui::save_txt: erreur ouverture ficher %s\n",filename.toLocal8Bit().constData());
|
---|
| 348 | QTextStream out(&global_file);
|
---|
| 349 |
|
---|
| 350 | //reference txt file
|
---|
| 351 | filename=file_names->at(csv_combo->currentIndex()-1);
|
---|
| 352 | filename.replace(QString(".csv"), QString(".txt"));
|
---|
| 353 | QFile ref_file(filename);
|
---|
| 354 | if(!ref_file.open(QIODevice::ReadOnly | QIODevice::Text)) printf("file_ui::save_txt: erreur ouverture ficher %s\n",filename.toLocal8Bit().constData());
|
---|
| 355 |
|
---|
| 356 | QTextStream ref_in(&ref_file);
|
---|
| 357 | QString current_line=ref_in.readLine();
|
---|
| 358 | int nb_lines=1;
|
---|
| 359 | while(current_line!=NULL)
|
---|
| 360 | {
|
---|
| 361 | out<<current_line << "\n";;
|
---|
| 362 | current_line=ref_in.readLine();
|
---|
| 363 | nb_lines++;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | //other txt files
|
---|
| 367 | for(int i=0;i<file_names->count();i++)
|
---|
| 368 | {
|
---|
| 369 | if(i==csv_combo->currentIndex()-1) continue;
|
---|
| 370 | filename=file_names->at(i);
|
---|
| 371 | filename.replace(QString(".csv"), QString(".txt"));
|
---|
| 372 | QFile txt_file(filename);
|
---|
| 373 | if(!txt_file.open(QIODevice::ReadOnly | QIODevice::Text)) printf("file_ui::save_txt: erreur ouverture ficher %s\n",filename.toLocal8Bit().constData());
|
---|
| 374 | QTextStream txt_in(&txt_file);
|
---|
| 375 | txt_in.readLine();//time us
|
---|
| 376 | txt_in.readLine();//time ns
|
---|
| 377 | current_line=txt_in.readLine();
|
---|
| 378 | while(current_line!=NULL)
|
---|
| 379 | {
|
---|
| 380 | out<< nb_lines << ":" << current_line.section(':',1) << "\n";;
|
---|
| 381 | current_line=txt_in.readLine();
|
---|
| 382 | nb_lines++;
|
---|
| 383 | }
|
---|
| 384 | txt_file.close();
|
---|
| 385 |
|
---|
| 386 | }
|
---|
| 387 | global_file.close();
|
---|
| 388 | ref_file.close();
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 |
|
---|
| 392 | bool file_ui::is_greater(qint64 ref_us,qint64 csv_us,int ref_ns,int csv_ns)
|
---|
| 393 | {
|
---|
| 394 | if(ref_us==csv_us)
|
---|
| 395 | {
|
---|
| 396 | if(ref_ns>csv_ns)
|
---|
| 397 | {
|
---|
| 398 | return true;
|
---|
| 399 | }
|
---|
| 400 | else
|
---|
| 401 | {
|
---|
| 402 | return false;
|
---|
| 403 | }
|
---|
| 404 | }
|
---|
| 405 | if(ref_us>csv_us)
|
---|
| 406 | {
|
---|
| 407 | return true;
|
---|
| 408 | }
|
---|
| 409 | else
|
---|
| 410 | {
|
---|
| 411 | return false;
|
---|
| 412 | }
|
---|
| 413 | }
|
---|