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

@ -8,6 +8,7 @@
#include "ssrf.h"
#include "subsurface-string.h"
#include "divelist.h"
#include "divelog.h"
#include "file.h"
#include "parse.h"
#include "sample.h"
@ -106,9 +107,7 @@ static char *parse_dan_new_line(char *buf, const char *NL)
}
static int try_to_xslt_open_csv(const char *filename, struct memblock *mem, const char *tag);
static int parse_dan_format(const char *filename, struct xml_params *params, struct dive_table *table,
struct trip_table *trips, struct dive_site_table *sites, struct device_table *devices,
struct filter_preset_table *filter_presets)
static int parse_dan_format(const char *filename, struct xml_params *params, struct divelog *log)
{
int ret = 0, i;
size_t end_ptr = 0;
@ -212,7 +211,7 @@ static int parse_dan_format(const char *filename, struct xml_params *params, str
xml_params_add(params, "waterTemp", tmpbuf);
}
}
ret |= parse_xml_buffer(filename, "<csv></csv>", 11, table, trips, sites, devices, filter_presets, params);
ret |= parse_xml_buffer(filename, "<csv></csv>", 11, log, params);
continue;
}
@ -268,7 +267,7 @@ static int parse_dan_format(const char *filename, struct xml_params *params, str
if (try_to_xslt_open_csv(filename, &mem_csv, "csv"))
return -1;
ret |= parse_xml_buffer(filename, mem_csv.buffer, mem_csv.size, table, trips, sites, devices, filter_presets, params);
ret |= parse_xml_buffer(filename, mem_csv.buffer, mem_csv.size, log, params);
free(mem_csv.buffer);
}
@ -278,9 +277,7 @@ static int parse_dan_format(const char *filename, struct xml_params *params, str
return ret;
}
int parse_csv_file(const char *filename, struct xml_params *params, const char *csvtemplate,
struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites,
struct device_table *devices, struct filter_preset_table *filter_presets)
int parse_csv_file(const char *filename, struct xml_params *params, const char *csvtemplate, struct divelog *log)
{
int ret;
struct memblock mem;
@ -300,7 +297,7 @@ int parse_csv_file(const char *filename, struct xml_params *params, const char *
mem.size = 0;
if (!strcmp("DL7", csvtemplate)) {
return parse_dan_format(filename, params, table, trips, sites, devices, filter_presets);
return parse_dan_format(filename, params, log);
} else if (strcmp(xml_params_get_key(params, 0), "date")) {
time(&now);
timep = localtime(&now);
@ -332,7 +329,7 @@ int parse_csv_file(const char *filename, struct xml_params *params, const char *
fprintf(stderr, "%s/xslt/%s -\n", SUBSURFACE_SOURCE, csvtemplate);
}
#endif
ret = parse_xml_buffer(filename, mem.buffer, mem.size, table, trips, sites, devices, filter_presets, params);
ret = parse_xml_buffer(filename, mem.buffer, mem.size, log, params);
free(mem.buffer);
@ -409,9 +406,8 @@ static int try_to_xslt_open_csv(const char *filename, struct memblock *mem, cons
return 0;
}
int try_to_open_csv(struct memblock *mem, enum csv_format type, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites)
int try_to_open_csv(struct memblock *mem, enum csv_format type, struct divelog *log)
{
UNUSED(sites);
char *p = mem->buffer;
char *header[8];
int i, time;
@ -460,7 +456,7 @@ int try_to_open_csv(struct memblock *mem, enum csv_format type, struct dive_tabl
break;
p = end + 1;
}
record_dive_to_table(dive, table);
record_dive_to_table(dive, log->dives);
return 1;
}
@ -502,15 +498,12 @@ static char *next_mkvi_key(const char *haystack)
return ret;
}
int parse_txt_file(const char *filename, const char *csv, struct dive_table *table, struct trip_table *trips,
struct dive_site_table *sites, struct device_table *devices)
int parse_txt_file(const char *filename, const char *csv, struct divelog *log)
{
UNUSED(sites);
struct memblock memtxt, memcsv;
if (readfile(filename, &memtxt) < 0) {
if (readfile(filename, &memtxt) < 0)
return report_error(translate("gettextFromC", "Failed to read '%s'"), filename);
}
/*
* MkVI stores some information in .txt file but the whole profile and events are stored in .csv file. First
@ -786,7 +779,7 @@ int parse_txt_file(const char *filename, const char *csv, struct dive_table *tab
if (!lineptr || !*lineptr)
break;
}
record_dive_to_table(dive, table);
record_dive_to_table(dive, log->dives);
return 1;
} else {
return 0;
@ -799,17 +792,14 @@ int parse_txt_file(const char *filename, const char *csv, struct dive_table *tab
#define TIMESTR 6
#define SBPARAMS 40
static int parse_seabear_csv_file(const char *filename, struct xml_params *params, const char *csvtemplate,
struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites,
struct device_table *devices, struct filter_preset_table *filter_presets);
int parse_seabear_log(const char *filename, struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites,
struct device_table *devices, struct filter_preset_table *filter_presets)
static int parse_seabear_csv_file(const char *filename, struct xml_params *params, const char *csvtemplate, struct divelog *log);
int parse_seabear_log(const char *filename, struct divelog *log)
{
struct xml_params *params = alloc_xml_params();
int ret;
parse_seabear_header(filename, params);
ret = parse_seabear_csv_file(filename, params, "csv", table, trips, sites, devices, filter_presets) < 0 ? -1 : 0;
ret = parse_seabear_csv_file(filename, params, "csv", log) < 0 ? -1 : 0;
free_xml_params(params);
@ -817,9 +807,7 @@ int parse_seabear_log(const char *filename, struct dive_table *table, struct tri
}
static int parse_seabear_csv_file(const char *filename, struct xml_params *params, const char *csvtemplate,
struct dive_table *table, struct trip_table *trips, struct dive_site_table *sites,
struct device_table *devices, struct filter_preset_table *filter_presets)
static int parse_seabear_csv_file(const char *filename, struct xml_params *params, const char *csvtemplate, struct divelog *log)
{
int ret, i;
struct memblock mem;
@ -937,14 +925,13 @@ static int parse_seabear_csv_file(const char *filename, struct xml_params *param
fprintf(stderr, "xslt/csv2xml.xslt\n");
}
ret = parse_xml_buffer(filename, mem.buffer, mem.size, table, trips, sites, devices, filter_presets, params);
ret = parse_xml_buffer(filename, mem.buffer, mem.size, log, params);
free(mem.buffer);
return ret;
}
int parse_manual_file(const char *filename, struct xml_params *params, struct dive_table *table, struct trip_table *trips,
struct dive_site_table *sites, struct device_table *devices, struct filter_preset_table *filter_presets)
int parse_manual_file(const char *filename, struct xml_params *params, struct divelog *log)
{
struct memblock mem;
time_t now;
@ -980,7 +967,7 @@ int parse_manual_file(const char *filename, struct xml_params *params, struct di
fprintf(stderr, "%s/xslt/manualcsv2xml.xslt -\n", SUBSURFACE_SOURCE);
}
#endif
ret = parse_xml_buffer(filename, mem.buffer, mem.size, table, trips, sites, devices, filter_presets, params);
ret = parse_xml_buffer(filename, mem.buffer, mem.size, log, params);
free(mem.buffer);
return ret;