core: introduce divelog structure

The parser API was very annoying, as a number of tables
to-be-filled were passed in as pointers. The goal of this
commit is to collect all these tables in a single struct.
This should make it (more or less) clear what is actually
written into the divelog files.

Moreover, it should now be rather easy to search for
instances, where the global logfile is accessed (and it
turns out that there are many!).

The divelog struct does not contain the tables as substructs,
but only collects pointers. The idea is that the "divelog.h"
file can be included without all the other files describing
the numerous tables.

To make it easier to use from C++ parts of the code, the
struct implements a constructor and a destructor. Sadly,
we can't use smart pointers, since the pointers are accessed
from C code. Therfore the constructor and destructor are
quite complex.

The whole commit is large, but was mostly an automatic
conversion.

One oddity of note: the divelog structure also contains
the "autogroup" flag, since that is saved in the divelog.
This actually fixes a bug: Before, when importing dives
from a different log, the autogroup flag was overwritten.
This was probably not intended and does not happen anymore.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2022-11-08 21:31:08 +01:00 committed by bstoeger
parent eebb47ec22
commit 9c253ee6c5
81 changed files with 661 additions and 698 deletions

View file

@ -2,6 +2,7 @@
#include "testparse.h"
#include "core/device.h"
#include "core/dive.h"
#include "core/divelog.h"
#include "core/divesite.h"
#include "core/errorhelper.h"
#include "core/trip.h"
@ -85,19 +86,18 @@ int TestParse::parseCSV(int units, std::string file)
xml_params_add_int(&params, "airtempField", -1);
xml_params_add_int(&params, "watertempField", -1);
return parse_manual_file(file.c_str(), &params, &dive_table, &trip_table,
&dive_site_table, &device_table, &filter_preset_table);
return parse_manual_file(file.c_str(), &params, &divelog);
}
int TestParse::parseDivingLog()
{
// Parsing of DivingLog import from SQLite database
struct dive_site *ds = alloc_or_get_dive_site(0xdeadbeef, &dive_site_table);
struct dive_site *ds = alloc_or_get_dive_site(0xdeadbeef, divelog.sites);
ds->name = copy_string("Suomi - - Hälvälä");
int ret = sqlite3_open(SUBSURFACE_TEST_DATA "/dives/TestDivingLog4.1.1.sql", &_sqlite3_handle);
if (ret == 0)
ret = parse_divinglog_buffer(_sqlite3_handle, 0, 0, 0, &dive_table, &trip_table, &dive_site_table, &device_table);
ret = parse_divinglog_buffer(_sqlite3_handle, 0, 0, 0, &divelog);
else
fprintf(stderr, "Can't open sqlite3 db: " SUBSURFACE_TEST_DATA "/dives/TestDivingLog4.1.1.sql");
@ -107,30 +107,28 @@ int TestParse::parseDivingLog()
int TestParse::parseV2NoQuestion()
{
// parsing of a V2 file should work
return parse_file(SUBSURFACE_TEST_DATA "/dives/test40.xml", &dive_table, &trip_table,
&dive_site_table, &device_table, &filter_preset_table);
return parse_file(SUBSURFACE_TEST_DATA "/dives/test40.xml", &divelog);
}
int TestParse::parseV3()
{
// parsing of a V3 files should succeed
return parse_file(SUBSURFACE_TEST_DATA "/dives/test42.xml", &dive_table, &trip_table,
&dive_site_table, &device_table, &filter_preset_table);
return parse_file(SUBSURFACE_TEST_DATA "/dives/test42.xml", &divelog);
}
void TestParse::testParse()
{
QCOMPARE(parseCSV(0, SUBSURFACE_TEST_DATA "/dives/test41.csv"), 0);
fprintf(stderr, "number of dives %d \n", dive_table.nr);
fprintf(stderr, "number of dives %d \n", divelog.dives->nr);
QCOMPARE(parseDivingLog(), 0);
fprintf(stderr, "number of dives %d \n", dive_table.nr);
fprintf(stderr, "number of dives %d \n", divelog.dives->nr);
QCOMPARE(parseV2NoQuestion(), 0);
fprintf(stderr, "number of dives %d \n", dive_table.nr);
fprintf(stderr, "number of dives %d \n", divelog.dives->nr);
QCOMPARE(parseV3(), 0);
fprintf(stderr, "number of dives %d \n", dive_table.nr);
fprintf(stderr, "number of dives %d \n", divelog.dives->nr);
QCOMPARE(save_dives("./testout.ssrf"), 0);
FILE_COMPARE("./testout.ssrf",
@ -140,7 +138,7 @@ void TestParse::testParse()
void TestParse::testParseDM4()
{
QCOMPARE(sqlite3_open(SUBSURFACE_TEST_DATA "/dives/TestDiveDM4.db", &_sqlite3_handle), 0);
QCOMPARE(parse_dm4_buffer(_sqlite3_handle, 0, 0, 0, &dive_table, &trip_table, &dive_site_table, &device_table), 0);
QCOMPARE(parse_dm4_buffer(_sqlite3_handle, 0, 0, 0, &divelog), 0);
QCOMPARE(save_dives("./testdm4out.ssrf"), 0);
FILE_COMPARE("./testdm4out.ssrf",
@ -150,7 +148,7 @@ void TestParse::testParseDM4()
void TestParse::testParseDM5()
{
QCOMPARE(sqlite3_open(SUBSURFACE_TEST_DATA "/dives/TestDiveDM5.db", &_sqlite3_handle), 0);
QCOMPARE(parse_dm5_buffer(_sqlite3_handle, 0, 0, 0, &dive_table, &trip_table, &dive_site_table, &device_table), 0);
QCOMPARE(parse_dm5_buffer(_sqlite3_handle, 0, 0, 0, &divelog), 0);
QCOMPARE(save_dives("./testdm5out.ssrf"), 0);
FILE_COMPARE("./testdm5out.ssrf",
@ -179,18 +177,17 @@ void TestParse::testParseHUDC()
xml_params_add(&params, "hw", "\"DC text\"");
QCOMPARE(parse_csv_file(SUBSURFACE_TEST_DATA "/dives/TestDiveSeabearHUDC.csv",
&params, "csv", &dive_table, &trip_table, &dive_site_table,
&device_table, &filter_preset_table),
&params, "csv", &divelog),
0);
QCOMPARE(dive_table.nr, 1);
QCOMPARE(divelog.dives->nr, 1);
/*
* CSV import uses time and date stamps relative to current
* time, thus we need to use a static (random) timestamp
*/
if (dive_table.nr > 0) {
struct dive *dive = dive_table.dives[dive_table.nr - 1];
if (divelog.dives->nr > 0) {
struct dive *dive = divelog.dives->dives[divelog.dives->nr - 1];
dive->when = 1255152761;
dive->dc.when = 1255152761;
}
@ -225,13 +222,12 @@ void TestParse::testParseNewFormat()
"/dives/")
.append(files.at(i))
.toLatin1()
.data(), &dive_table, &trip_table, &dive_site_table,
&device_table, &filter_preset_table),
.data(), &divelog),
0);
QCOMPARE(dive_table.nr, i + 1);
QCOMPARE(divelog.dives->nr, i + 1);
}
fprintf(stderr, "number of dives %d \n", dive_table.nr);
fprintf(stderr, "number of dives %d \n", divelog.dives->nr);
QCOMPARE(save_dives("./testsbnewout.ssrf"), 0);
// Currently the CSV parse fails
@ -245,9 +241,9 @@ void TestParse::testParseDLD()
QString filename = SUBSURFACE_TEST_DATA "/dives/TestDiveDivelogsDE.DLD";
QVERIFY(readfile(filename.toLatin1().data(), &mem) > 0);
QVERIFY(try_to_open_zip(filename.toLatin1().data(), &dive_table, &trip_table, &dive_site_table, &device_table, &filter_preset_table) > 0);
QVERIFY(try_to_open_zip(filename.toLatin1().data(), &divelog) > 0);
fprintf(stderr, "number of dives from DLD: %d \n", dive_table.nr);
fprintf(stderr, "number of dives from DLD: %d \n", divelog.dives->nr);
// Compare output
QCOMPARE(save_dives("./testdldout.ssrf"), 0);
@ -260,10 +256,8 @@ void TestParse::testParseMerge()
/*
* check that we correctly merge mixed cylinder dives
*/
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/ostc.xml", &dive_table, &trip_table, &dive_site_table,
&device_table, &filter_preset_table), 0);
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/vyper.xml", &dive_table, &trip_table, &dive_site_table,
&device_table, &filter_preset_table), 0);
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/ostc.xml", &divelog), 0);
QCOMPARE(parse_file(SUBSURFACE_TEST_DATA "/dives/vyper.xml", &divelog), 0);
QCOMPARE(save_dives("./testmerge.ssrf"), 0);
FILE_COMPARE("./testmerge.ssrf",
SUBSURFACE_TEST_DATA "/dives/mergedVyperOstc.xml");
@ -303,22 +297,20 @@ int TestParse::parseCSVmanual(int units, std::string file)
xml_params_add_int(&params, "datefmt", 2);
xml_params_add_int(&params, "durationfmt", 2);
xml_params_add_int(&params, "units", units);
return parse_manual_file(file.c_str(), &params, &dive_table, &trip_table,
&dive_site_table, &device_table, &filter_preset_table);
return parse_manual_file(file.c_str(), &params, &divelog);
}
void TestParse::exportCSVDiveDetails()
{
int saved_sac = 0;
parse_file(SUBSURFACE_TEST_DATA "/dives/test25.xml", &dive_table, &trip_table, &dive_site_table,
&device_table, &filter_preset_table);
parse_file(SUBSURFACE_TEST_DATA "/dives/test25.xml", &divelog);
export_dives_xslt("testcsvexportmanual.csv", 0, 0, "xml2manualcsv.xslt", false);
export_dives_xslt("testcsvexportmanualimperial.csv", 0, 1, "xml2manualcsv.xslt", false);
if (dive_table.nr > 0) {
struct dive *dive = dive_table.dives[dive_table.nr - 1];
if (divelog.dives->nr > 0) {
struct dive *dive = divelog.dives->dives[divelog.dives->nr - 1];
saved_sac = dive->sac;
}
clear_dive_file_data();
@ -326,8 +318,8 @@ void TestParse::exportCSVDiveDetails()
parseCSVmanual(1, "testcsvexportmanualimperial.csv");
// We do not currently support reading SAC, thus faking it
if (dive_table.nr > 0) {
struct dive *dive = dive_table.dives[dive_table.nr - 1];
if (divelog.dives->nr > 0) {
struct dive *dive = divelog.dives->dives[divelog.dives->nr - 1];
dive->sac = saved_sac;
}
@ -347,14 +339,13 @@ void TestParse::exportSubsurfaceCSV()
xml_params params;
/* Test SubsurfaceCSV with multiple cylinders */
parse_file(SUBSURFACE_TEST_DATA "/dives/test40.xml", &dive_table, &trip_table,
&dive_site_table, &device_table, &filter_preset_table);
parse_file(SUBSURFACE_TEST_DATA "/dives/test40.xml", &divelog);
export_dives_xslt("testcsvexportmanual-cyl.csv", 0, 0, "xml2manualcsv.xslt", false);
export_dives_xslt("testcsvexportmanualimperial-cyl.csv", 0, 1, "xml2manualcsv.xslt", false);
if (dive_table.nr > 0) {
struct dive *dive = dive_table.dives[dive_table.nr - 1];
if (divelog.dives->nr > 0) {
struct dive *dive = divelog.dives->dives[divelog.dives->nr - 1];
saved_sac = dive->sac;
}
@ -362,12 +353,11 @@ void TestParse::exportSubsurfaceCSV()
xml_params_add_int(&params, "separatorIndex", 0);
xml_params_add_int(&params, "units", 1);
parse_csv_file("testcsvexportmanualimperial-cyl.csv", &params, "SubsurfaceCSV", &dive_table, &trip_table,
&dive_site_table, &device_table, &filter_preset_table);
parse_csv_file("testcsvexportmanualimperial-cyl.csv", &params, "SubsurfaceCSV", &divelog);
// We do not currently support reading SAC, thus faking it
if (dive_table.nr > 0) {
struct dive *dive = dive_table.dives[dive_table.nr - 1];
if (divelog.dives->nr > 0) {
struct dive *dive = divelog.dives->dives[divelog.dives->nr - 1];
dive->sac = saved_sac;
}
@ -396,14 +386,12 @@ int TestParse::parseCSVprofile(int units, std::string file)
xml_params_add_int(&params, "datefmt", 2);
xml_params_add_int(&params, "units", units);
return parse_csv_file(file.c_str(), &params, "csv", &dive_table, &trip_table,
&dive_site_table, &device_table, &filter_preset_table);
return parse_csv_file(file.c_str(), &params, "csv", &divelog);
}
void TestParse::exportCSVDiveProfile()
{
parse_file(SUBSURFACE_TEST_DATA "/dives/test40.xml", &dive_table, &trip_table,
&dive_site_table, &device_table, &filter_preset_table);
parse_file(SUBSURFACE_TEST_DATA "/dives/test40.xml", &divelog);
export_dives_xslt("testcsvexportprofile.csv", 0, 0, "xml2csv.xslt", false);
export_dives_xslt("testcsvexportprofileimperial.csv", 0, 1, "xml2csv.xslt", false);
@ -421,14 +409,13 @@ void TestParse::exportCSVDiveProfile()
void TestParse::exportUDDF()
{
parse_file(SUBSURFACE_TEST_DATA "/dives/test40.xml", &dive_table, &trip_table,
&dive_site_table, &device_table, &filter_preset_table);
parse_file(SUBSURFACE_TEST_DATA "/dives/test40.xml", &divelog);
export_dives_xslt("testuddfexport.uddf", 0, 1, "uddf-export.xslt", false);
clear_dive_file_data();
parse_file("testuddfexport.uddf", &dive_table, &trip_table, &dive_site_table, &device_table, &filter_preset_table);
parse_file("testuddfexport.uddf", &divelog);
export_dives_xslt("testuddfexport2.uddf", 0, 1, "uddf-export.xslt", false);
FILE_COMPARE("testuddfexport.uddf",
@ -472,9 +459,9 @@ void TestParse::parseDL7()
clear_dive_file_data();
QCOMPARE(parse_csv_file(SUBSURFACE_TEST_DATA "/dives/DL7.zxu",
&params, "DL7", &dive_table, &trip_table, &dive_site_table, &device_table, &filter_preset_table),
&params, "DL7", &divelog),
0);
QCOMPARE(dive_table.nr, 3);
QCOMPARE(divelog.dives->nr, 3);
QCOMPARE(save_dives("./testdl7out.ssrf"), 0);
FILE_COMPARE("./testdl7out.ssrf",