source: flair-src/trunk/tools/FlairGCS/src/file_ui.cpp@ 204

Last change on this file since 204 was 51, checked in by Sanahuja Guillaume, 8 years ago

gps

File size: 11.6 KB
Line 
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
32using namespace std;
33
34file_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;
44
45 ok_button->setEnabled(false);
46
47 main_layout->addWidget(log_text, 0, 0);
48 main_layout->addWidget(input_text, 1, 0);
49
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);
57
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);
66
67 file_names = new QStringList();
68}
69
70file_ui::~file_ui() { delete dialog; }
71
72void file_ui::log(QString text) { appendToLog(text); }
73
74void file_ui::addFile(QString file_path) {
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 }
84
85 if (file_path.endsWith(".txt") == true) {
86 dbt2csv(file_path.replace(QString(".txt"), QString(".dbt")));
87 }
88
89 if (file_names->size() == 1) {
90 input_cleared = false;
91 showDialog();
92 }
93}
94
95void file_ui::endOfFiles(void) {
96 ok_button->setEnabled(true);
97
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
105 }
106 }
107}
108
109void file_ui::dbt2csv(QString file_path) {
110 hdfile_t *dbtFile = NULL;
111 char *data;
112 QStringList data_type;
113
114 QString filename =
115 file_path.section('/', -1); // remove path for displaying on logs
116 appendToLog(QString("converting %1 to csv").arg(filename));
117
118 // open csv file
119 QString csv_filename = file_path;
120 csv_filename.replace(QString(".dbt"), QString(".csv"));
121 QFile csv_file(csv_filename);
122
123 if (!csv_file.open(QIODevice::WriteOnly | QIODevice::Text)) {
124 appendToLog(" error opening csv file!");
125 return;
126 }
127 QTextStream out(&csv_file);
128
129 // open txt file
130 QString txt_filename = file_path;
131 txt_filename.replace(QString(".dbt"), QString(".txt"));
132 QFile txt_file(txt_filename);
133
134 if (!txt_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
135 appendToLog(" error opening txt file!");
136 return;
137 }
138
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();
153
154 dbtFile = open_hdfile(file_path.toLocal8Bit().data(), READ_MODE);
155
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 }
165
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;
172
173 if (read_hdfile(dbtFile, (void *)data, &time, &tr) == 0) {
174 break;
175 }
176 dataWritten = true;
177
178 out << time << "," << tr;
179 for (int i = 0; i < data_type.size(); i++) {
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)") {
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;
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;
196 } else {
197 appendToLog(QString(" unhandled type: %1").arg(data_type.at(i)));
198 }
199 }
200
201 out << "\n";
202 }
203
204 if (!dataWritten) {
205 // empty file!
206 out << "0,0"; // timr
207 for (int i = 0; i < data_type.size(); i++) {
208 out << ",0";
209 }
210 out << "\n";
211 }
212
213 csv_file.close();
214 close_hdfile(dbtFile);
215 if (data != NULL)
216 free(data);
217
218 appendToLog(" ok");
219}
220
221void file_ui::clearInputText(void) {
222 if (input_cleared == false) {
223 input_cleared = true;
224 input_text->clear();
225 }
226}
227
228void file_ui::save(void) {
229 save_comment();
230 if (csv_combo->currentIndex() != 0) {
231 save_csv();
232 save_txt();
233 }
234
235 log_text->clear();
236 input_cleared = true; // avoid clearing it with setText
237 input_text->setText("add your log comment here");
238 file_names->clear();
239 csv_combo->clear();
240 csv_combo->addItem(QString("(no base time)"));
241
242 dialog->setVisible(false);
243 ok_button->setEnabled(false);
244 emit finished();
245}
246
247void file_ui::save_comment(void) {
248 QString folder_name = file_names->at(0).section('/', 0, -2);
249
250 QString filename = folder_name + "/commentaire.txt";
251 QFile file(filename);
252 if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
253 printf("file_ui::save_comment: erreur ouverture fichier %s\n",
254 filename.toLocal8Bit().constData());
255 QTextStream out(&file);
256
257 out << input_text->toPlainText();
258 file.close();
259}
260
261void file_ui::save_csv(void) {
262 // global csv file
263 QString folder_name = file_names->at(0).section('/', 0, -2);
264 QString filename = folder_name + "/all_logs.csv";
265 QFile global_file(filename);
266 if (!global_file.open(QIODevice::WriteOnly | QIODevice::Text))
267 printf("file_ui::save_csv: erreur ouverture fichier %s\n",
268 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))
276 printf("file_ui::save_csv: erreur ouverture ficher %s\n",
277 filename.toLocal8Bit().constData());
278
279 // other csv files
280 int j = 0;
281 QFile m_file[file_names->count() - 1];
282 QTextStream m_in[file_names->count() - 1];
283 for (int i = 0; i < file_names->count(); i++) {
284 if (i == csv_combo->currentIndex() - 1)
285 continue;
286 filename = file_names->at(i);
287 m_file[j].setFileName(filename);
288 if (!m_file[j].open(QIODevice::ReadOnly | QIODevice::Text))
289 printf("file_ui::save_csv: erreur ouverture ficher %s\n",
290 filename.toLocal8Bit().constData());
291 m_in[j].setDevice(&m_file[j]);
292 j++;
293 }
294
295 // init
296 QTextStream ref_in(&ref_file);
297 QString m_line[file_names->count() - 1];
298 QString m_line_prev[file_names->count() - 1];
299 for (int i = 0; i < file_names->count() - 1; i++) {
300 m_line[i] = m_in[i].readLine();
301 m_line_prev[i] = m_line[i];
302 }
303
304 // organize csv files in one file
305 while (1) {
306 if (ref_in.atEnd() == true)
307 break;
308 QString ref_line = ref_in.readLine();
309
310 qint64 ref_us = ref_line.section(',', 0, 0).toLongLong();
311 int ref_ns = ref_line.section(',', 1, 1).toInt();
312 // printf("ref %lld %i\n",ref_us,ref_ns);
313
314 for (int i = 0; i < file_names->count() - 1; i++) {
315 qint64 csv_us = m_line[i].section(',', 0, 0).toLongLong();
316 int csv_ns = m_line[i].section(',', 1, 1).toInt();
317 // printf("m %lld %i\n",csv_us,csv_ns);
318
319 while (is_greater(ref_us, csv_us, ref_ns, csv_ns) == true) {
320 m_line_prev[i] = m_line[i];
321 if (m_in[i].atEnd() == true)
322 break;
323 m_line[i] = m_in[i].readLine();
324 csv_us = m_line[i].section(',', 0, 0).toLongLong();
325 csv_ns = m_line[i].section(',', 1, 1).toInt();
326 // printf("m %lld %i\n",csv_us,csv_ns);
327 }
328 csv_us = m_line_prev[i].section(',', 0, 0).toLongLong();
329 csv_ns = m_line_prev[i].section(',', 1, 1).toInt();
330 // printf("m ok %lld %i\n",csv_us,csv_ns);
331
332 ref_line += "," + m_line_prev[i].section(',', 2);
333 }
334
335 out << ref_line << "\n";
336 }
337
338 global_file.close();
339 ref_file.close();
340 for (int i = 0; i < file_names->count() - 1; i++)
341 m_file[i].close();
342}
343
344void file_ui::save_txt(void) {
345 // global txt file
346 QString folder_name = file_names->at(0).section('/', 0, -2);
347 QString filename = folder_name + "/all_logs.txt";
348 QFile global_file(filename);
349 if (!global_file.open(QIODevice::WriteOnly | QIODevice::Text))
350 printf("file_ui::save_txt: erreur ouverture ficher %s\n",
351 filename.toLocal8Bit().constData());
352 QTextStream out(&global_file);
353
354 // reference txt file
355 filename = file_names->at(csv_combo->currentIndex() - 1);
356 filename.replace(QString(".csv"), QString(".txt"));
357 QFile ref_file(filename);
358 if (!ref_file.open(QIODevice::ReadOnly | QIODevice::Text))
359 printf("file_ui::save_txt: erreur ouverture ficher %s\n",
360 filename.toLocal8Bit().constData());
361
362 QTextStream ref_in(&ref_file);
363 QString current_line = ref_in.readLine();
364 int nb_lines = 1;
365 while (current_line != NULL) {
366 out << current_line << "\n";
367 ;
368 current_line = ref_in.readLine();
369 nb_lines++;
370 }
371
372 // other txt files
373 for (int i = 0; i < file_names->count(); i++) {
374 if (i == csv_combo->currentIndex() - 1)
375 continue;
376 filename = file_names->at(i);
377 filename.replace(QString(".csv"), QString(".txt"));
378 QFile txt_file(filename);
379 if (!txt_file.open(QIODevice::ReadOnly | QIODevice::Text))
380 printf("file_ui::save_txt: erreur ouverture ficher %s\n",
381 filename.toLocal8Bit().constData());
382 QTextStream txt_in(&txt_file);
383 txt_in.readLine(); // time us
384 txt_in.readLine(); // time ns
385 current_line = txt_in.readLine();
386 while (current_line != NULL) {
387 out << nb_lines << ":" << current_line.section(':', 1) << "\n";
388 ;
389 current_line = txt_in.readLine();
390 nb_lines++;
391 }
392 txt_file.close();
393 }
394 global_file.close();
395 ref_file.close();
396}
397
398bool file_ui::is_greater(qint64 ref_us, qint64 csv_us, int ref_ns, int csv_ns) {
399 if (ref_us == csv_us) {
400 if (ref_ns > csv_ns) {
401 return true;
402 } else {
403 return false;
404 }
405 }
406 if (ref_us > csv_us) {
407 return true;
408 } else {
409 return false;
410 }
411}
Note: See TracBrowser for help on using the repository browser.