From 30976c05a42909c3ec2c15d4d627ab45602825e5 Mon Sep 17 00:00:00 2001 From: Miika Turkia Date: Thu, 5 May 2016 09:26:12 +0300 Subject: Parse txt formatted log files This parses .txt log files produced by Dataplus and Oceanlog software. Signed-off-by: Miika Turkia Signed-off-by: Dirk Hohndel --- desktop-widgets/divelogimportdialog.cpp | 74 ++++++++++++++++++++++++++++++++- desktop-widgets/divelogimportdialog.h | 2 + 2 files changed, 74 insertions(+), 2 deletions(-) (limited to 'desktop-widgets') diff --git a/desktop-widgets/divelogimportdialog.cpp b/desktop-widgets/divelogimportdialog.cpp index c8402abb9..0a964b003 100644 --- a/desktop-widgets/divelogimportdialog.cpp +++ b/desktop-widgets/divelogimportdialog.cpp @@ -342,6 +342,7 @@ DiveLogImportDialog::DiveLogImportDialog(QStringList fn, QWidget *parent) : QDia column = 0; delta = "0"; hw = ""; + txtLog = false; /* Add indexes of XSLTs requiring special handling to the list */ specialCSV << SENSUS; @@ -489,8 +490,21 @@ void DiveLogImportDialog::loadFileContents(int value, whatChanged triggeredBy) ui->knownImports->setCurrentText("DL7"); ui->CSVUnits->setCurrentText(units); blockSignals(false); + } else if (firstLine.contains("Life Time Dive")) { + txtLog = true; + + while ((firstLine = f.readLine().trimmed()).length() >= 0 && !f.atEnd()) { + if (firstLine.contains("Dive Profile")) { + f.readLine(); + break; + } + } + firstLine = f.readLine().trimmed(); + } + + // Special handling for APD Log Viewer if ((triggeredBy == KNOWNTYPES && (value == APD || value == APD2)) || (triggeredBy == INITIAL && fileNames.first().endsWith(".apd", Qt::CaseInsensitive))) { apd=true; @@ -694,6 +708,13 @@ void DiveLogImportDialog::loadFileContents(int value, whatChanged triggeredBy) break; } } + } else if (txtLog) { + while ((firstLine = f.readLine().trimmed()).length() >= 0 && !f.atEnd()) { + if (firstLine.contains("Dive Profile")) { + firstLine = f.readLine().trimmed(); + break; + } + } } while (rows < 10 && !f.atEnd()) { @@ -768,6 +789,51 @@ int DiveLogImportDialog::setup_csv_params(QStringList r, char **params, int pnr) return pnr; } +int DiveLogImportDialog::parseTxtHeader(QString fileName, char **params, int pnr) +{ + QFile f(fileNames.first()); + QString date; + QString time; + QString line; + + f.open(QFile::ReadOnly); + while ((line = f.readLine().trimmed()).length() >= 0 && !f.atEnd()) { + if (line.contains("Dive Profile")) { + f.readLine(); + break; + } else if (line.contains("Dive Date: ")) { + date = line.replace(QString::fromLatin1("Dive Date: "), QString::fromLatin1("")); + + if (date.contains('-')) { + QStringList fmtDate = date.split('-'); + date = fmtDate[0] + fmtDate[1] + fmtDate[2]; + } else if (date.contains('/')) { + QStringList fmtDate = date.split('/'); + date = fmtDate[2] + fmtDate[0] + fmtDate[1]; + } else { + QStringList fmtDate = date.split('.'); + date = fmtDate[2] + fmtDate[1] + fmtDate[0]; + } + } else if (line.contains("Elapsed Dive Time: ")) { + // Skipping dive duration for now + } else if (line.contains("Dive Time: ")) { + time = line.replace(QString::fromLatin1("Dive Time: "), QString::fromLatin1("")); + + if (time.contains(':')) { + QStringList fmtTime = time.split(':'); + time = fmtTime[0] + fmtTime[1]; + + } + } + } + f.close(); + + params[pnr++] = strdup("date"); + params[pnr++] = strdup(date.toLatin1()); + params[pnr++] = strdup("time"); + params[pnr++] = strdup(time.toLatin1()); + return pnr; +} void DiveLogImportDialog::on_buttonBox_accepted() { @@ -832,7 +898,9 @@ void DiveLogImportDialog::on_buttonBox_accepted() int pnr = 0; QRegExp apdRe("^.*[/\\][0-9a-zA-Z]*_([0-9]{6})_([0-9]{6})\\.apd"); - if (apdRe.exactMatch(fileNames[i])) { + if (txtLog) { + pnr = parseTxtHeader(fileNames[i], params, pnr); + } else if (apdRe.exactMatch(fileNames[i])) { params[pnr++] = strdup("date"); params[pnr++] = strdup("20" + apdRe.cap(1).toLatin1()); params[pnr++] = strdup("time"); @@ -906,7 +974,9 @@ void DiveLogImportDialog::on_buttonBox_accepted() int pnr = 0; QRegExp apdRe("^.*[/\\][0-9a-zA-Z]*_([0-9]{6})_([0-9]{6})\\.apd"); - if (apdRe.exactMatch(fileNames[i])) { + if (txtLog) { + pnr = parseTxtHeader(fileNames[i], params, pnr); + } else if (apdRe.exactMatch(fileNames[i])) { params[pnr++] = strdup("date"); params[pnr++] = strdup("20" + apdRe.cap(1).toLatin1()); params[pnr++] = strdup("time"); diff --git a/desktop-widgets/divelogimportdialog.h b/desktop-widgets/divelogimportdialog.h index 750bc10fa..8c752844d 100644 --- a/desktop-widgets/divelogimportdialog.h +++ b/desktop-widgets/divelogimportdialog.h @@ -87,6 +87,7 @@ slots: void loadFileContentsKnownTypesSelected(int value); void loadFileContents(int value, enum whatChanged triggeredBy); int setup_csv_params(QStringList r, char **params, int pnr); + int parseTxtHeader(QString fileName, char **params, int pnr); private: bool selector; @@ -97,6 +98,7 @@ private: ColumnNameResult *resultModel; QString delta; QString hw; + bool txtLog; struct CSVAppConfig { QString name; -- cgit v1.2.3-70-g09d2