diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2017-05-07 07:52:55 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2017-05-07 07:52:55 -0700 |
commit | d7cf3408e5d2f8c7f5ce25fa2b330cc3e75ff489 (patch) | |
tree | ed03b77b996476acff2ed848f107e4c0e75536e2 /core | |
parent | 75762e5f16bf80c853ecda4fd57a16adcaa22926 (diff) | |
parent | 21cdc647121455a3dd5e3e1dc76c2294fd017369 (diff) | |
download | subsurface-d7cf3408e5d2f8c7f5ce25fa2b330cc3e75ff489.tar.gz |
Merge branch 'seabear-refactor'
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'core')
-rw-r--r-- | core/dive.h | 1 | ||||
-rw-r--r-- | core/file.c | 14 | ||||
-rw-r--r-- | core/qthelper.cpp | 133 | ||||
-rw-r--r-- | core/qthelper.h | 2 | ||||
-rw-r--r-- | core/qthelperfromc.h | 1 |
5 files changed, 151 insertions, 0 deletions
diff --git a/core/dive.h b/core/dive.h index fa6133bd3..4402788c2 100644 --- a/core/dive.h +++ b/core/dive.h @@ -704,6 +704,7 @@ extern int parse_dlf_buffer(unsigned char *buffer, size_t size); extern int parse_file(const char *filename); extern int parse_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate); +extern int parse_seabear_log(const char *filename); extern int parse_seabear_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate); extern int parse_txt_file(const char *filename, const char *csv); extern int parse_manual_file(const char *filename, char **params, int pnr); diff --git a/core/file.c b/core/file.c index 59d00badb..7b24da8e7 100644 --- a/core/file.c +++ b/core/file.c @@ -1056,6 +1056,20 @@ int parse_csv_file(const char *filename, char **params, int pnr, const char *csv } #define SBPARAMS 40 +int parse_seabear_log(const char *filename) +{ + char *params[SBPARAMS]; + int pnr = 0; + + pnr = parse_seabear_header(filename, params, pnr); + + if (parse_seabear_csv_file(filename, params, pnr, "csv") < 0) { + return -1; + } + + return 0; +} + int parse_seabear_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate) { int ret, i; diff --git a/core/qthelper.cpp b/core/qthelper.cpp index 2ba358fc0..45e402fc7 100644 --- a/core/qthelper.cpp +++ b/core/qthelper.cpp @@ -1519,3 +1519,136 @@ QString getUUID() uuidString.replace("{", "").replace("}", ""); return uuidString; } + +int parse_seabear_header(const char *filename, char **params, int pnr) +{ + QFile f(filename); + + f.open(QFile::ReadOnly); + QString parseLine = f.readLine(); + + /* + * Parse dive number from Seabear CSV header + */ + + while ((parseLine = f.readLine().trimmed()).length() > 0 && !f.atEnd()) { + if (parseLine.contains("//DIVE NR: ")) { + params[pnr++] = strdup("diveNro"); + params[pnr++] = strdup(parseLine.replace(QString::fromLatin1("//DIVE NR: "), QString::fromLatin1("")).toUtf8().data()); + break; + } + } + f.seek(0); + + /* + * Parse header - currently only interested in sample + * interval and hardware version. If we have old format + * the interval value is missing from the header. + */ + + while ((parseLine = f.readLine().trimmed()).length() > 0 && !f.atEnd()) { + if (parseLine.contains("//Hardware Version: ")) { + params[pnr++] = strdup("hw"); + params[pnr++] = strdup(parseLine.replace(QString::fromLatin1("//Hardware Version: "), QString::fromLatin1("\"Seabear ")).trimmed().append("\"").toUtf8().data()); + break; + } + } + f.seek(0); + + /* + * Grab the sample interval + */ + + while ((parseLine = f.readLine().trimmed()).length() > 0 && !f.atEnd()) { + if (parseLine.contains("//Log interval: ")) { + params[pnr++] = strdup("delta"); + params[pnr++] = strdup(parseLine.remove(QString::fromLatin1("//Log interval: ")).trimmed().remove(QString::fromLatin1(" s")).toUtf8().data()); + break; + } + } + f.seek(0); + + /* + * Dive mode, can be: OC, APNEA, BOTTOM TIMER, CCR, CCR SENSORBOARD + * Note that we scan over the "Log interval" on purpose + */ + + while ((parseLine = f.readLine().trimmed()).length() > 0 && !f.atEnd()) { + QString needle = "//Mode: "; + if (parseLine.contains(needle)) { + params[pnr++] = strdup("diveMode"); + params[pnr++] = strdup(parseLine.replace(needle, QString::fromLatin1("")).prepend("\"").append("\"").toUtf8().data()); + } + } + f.seek(0); + + while ((parseLine = f.readLine().trimmed()).length() > 0 && !f.atEnd()) { + } + + /* + * Parse CSV fields + */ + + parseLine = f.readLine().trimmed(); + + QStringList currColumns = parseLine.split(';'); + unsigned short index = 0; + Q_FOREACH (QString columnText, currColumns) { + if (columnText == "Time") { + params[pnr++] = strdup("timeField"); + params[pnr++] = intdup(index++); + } else if (columnText == "Depth") { + params[pnr++] = strdup("depthField"); + params[pnr++] = intdup(index++); + } else if (columnText == "Temperature") { + params[pnr++] = strdup("tempField"); + params[pnr++] = intdup(index++); + } else if (columnText == "NDT") { + params[pnr++] = strdup("ndlField"); + params[pnr++] = intdup(index++); + } else if (columnText == "TTS") { + params[pnr++] = strdup("ttsField"); + params[pnr++] = intdup(index++); + } else if (columnText == "pO2_1") { + params[pnr++] = strdup("o2sensor1Field"); + params[pnr++] = intdup(index++); + } else if (columnText == "pO2_2") { + params[pnr++] = strdup("o2sensor2Field"); + params[pnr++] = intdup(index++); + } else if (columnText == "pO2_3") { + params[pnr++] = strdup("o2sensor3Field"); + params[pnr++] = intdup(index++); + } else if (columnText == "Ceiling") { + /* TODO: Add support for dive computer reported ceiling*/ + params[pnr++] = strdup("ceilingField"); + params[pnr++] = intdup(index++); + } else if (columnText == "Tank pressure") { + params[pnr++] = strdup("pressureField"); + params[pnr++] = intdup(index++); + } else { + // We do not know about this value + qDebug() << "Seabear import found an un-handled field: " << columnText; + } + } + + /* Separator is ';' and the index for that in DiveLogImportDialog constructor is 2 */ + params[pnr++] = strdup("separatorIndex"); + params[pnr++] = intdup(2); + + /* And metric units */ + params[pnr++] = strdup("units"); + params[pnr++] = intdup(0); + + params[pnr] = NULL; + f.close(); + return pnr; +} + +char *intdup(int index) +{ + char tmpbuf[21]; + + snprintf(tmpbuf, sizeof(tmpbuf) - 2, "%d", index); + tmpbuf[20] = 0; + return strdup(tmpbuf); +} diff --git a/core/qthelper.h b/core/qthelper.h index e0e4da0c5..ff91b771a 100644 --- a/core/qthelper.h +++ b/core/qthelper.h @@ -47,5 +47,7 @@ extern "C" void subsurface_mkdir(const char *dir); void init_proxy(); QString getUUID(); QStringList imageExtensionFilters(); +char *intdup(int index); +extern "C" int parse_seabear_header(const char *filename, char **params, int pnr); #endif // QTHELPER_H diff --git a/core/qthelperfromc.h b/core/qthelperfromc.h index 4b2ef3d53..91fbde415 100644 --- a/core/qthelperfromc.h +++ b/core/qthelperfromc.h @@ -20,5 +20,6 @@ char *hashfile_name_string(); char *picturedir_string(); const char *subsurface_user_agent(); enum deco_mode decoMode(); +int parse_seabear_header(const char *filename, char **params, int pnr); #endif // QTHELPERFROMC_H |