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