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