Parser: add trip_table parameter to parsing functions

To allow parsing into arbitrary trip_tables, add the corresponding
parameter to the parsing functions and the parser state. Currently,
all callers pass the global trip_table so there should be no change
in functionality. These arguments will be replaced in subsequent commits.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-11-27 00:25:15 +01:00 committed by Dirk Hohndel
parent ec37c71f5e
commit 7e33369dc8
28 changed files with 157 additions and 143 deletions

View file

@ -75,7 +75,7 @@ out:
}
static void zip_read(struct zip_file *file, const char *filename, struct dive_table *table)
static void zip_read(struct zip_file *file, const char *filename, struct dive_table *table, struct trip_table *trips)
{
int size = 1024, n, read = 0;
char *mem = malloc(size);
@ -86,11 +86,11 @@ static void zip_read(struct zip_file *file, const char *filename, struct dive_ta
mem = realloc(mem, size);
}
mem[read] = 0;
(void) parse_xml_buffer(filename, mem, read, table, NULL);
(void) parse_xml_buffer(filename, mem, read, table, trips, NULL);
free(mem);
}
int try_to_open_zip(const char *filename, struct dive_table *table)
int try_to_open_zip(const char *filename, struct dive_table *table, struct trip_table *trips)
{
int success = 0;
/* Grr. libzip needs to re-open the file, it can't take a buffer */
@ -105,7 +105,7 @@ int try_to_open_zip(const char *filename, struct dive_table *table)
/* skip parsing the divelogs.de pictures */
if (strstr(zip_get_name(zip, index, 0), "pictures/"))
continue;
zip_read(file, filename, table);
zip_read(file, filename, table, trips);
zip_fclose(file);
success++;
}
@ -126,7 +126,7 @@ static int db_test_func(void *param, int columns, char **data, char **column)
}
static int try_to_open_db(const char *filename, struct memblock *mem, struct dive_table *table)
static int try_to_open_db(const char *filename, struct memblock *mem, struct dive_table *table, struct trip_table *trips)
{
sqlite3 *handle;
char dm4_test[] = "select count(*) from sqlite_master where type='table' and name='Dive' and sql like '%ProfileBlob%'";
@ -147,7 +147,7 @@ static int try_to_open_db(const char *filename, struct memblock *mem, struct div
/* Testing if DB schema resembles Suunto DM5 database format */
retval = sqlite3_exec(handle, dm5_test, &db_test_func, 0, NULL);
if (!retval) {
retval = parse_dm5_buffer(handle, filename, mem->buffer, mem->size, table);
retval = parse_dm5_buffer(handle, filename, mem->buffer, mem->size, table, trips);
sqlite3_close(handle);
return retval;
}
@ -155,7 +155,7 @@ static int try_to_open_db(const char *filename, struct memblock *mem, struct div
/* Testing if DB schema resembles Suunto DM4 database format */
retval = sqlite3_exec(handle, dm4_test, &db_test_func, 0, NULL);
if (!retval) {
retval = parse_dm4_buffer(handle, filename, mem->buffer, mem->size, table);
retval = parse_dm4_buffer(handle, filename, mem->buffer, mem->size, table, trips);
sqlite3_close(handle);
return retval;
}
@ -163,7 +163,7 @@ static int try_to_open_db(const char *filename, struct memblock *mem, struct div
/* Testing if DB schema resembles Shearwater database format */
retval = sqlite3_exec(handle, shearwater_test, &db_test_func, 0, NULL);
if (!retval) {
retval = parse_shearwater_buffer(handle, filename, mem->buffer, mem->size, table);
retval = parse_shearwater_buffer(handle, filename, mem->buffer, mem->size, table, trips);
sqlite3_close(handle);
return retval;
}
@ -171,7 +171,7 @@ static int try_to_open_db(const char *filename, struct memblock *mem, struct div
/* Testing if DB schema resembles Shearwater cloud database format */
retval = sqlite3_exec(handle, shearwater_cloud_test, &db_test_func, 0, NULL);
if (!retval) {
retval = parse_shearwater_cloud_buffer(handle, filename, mem->buffer, mem->size, table);
retval = parse_shearwater_cloud_buffer(handle, filename, mem->buffer, mem->size, table, trips);
sqlite3_close(handle);
return retval;
}
@ -179,7 +179,7 @@ static int try_to_open_db(const char *filename, struct memblock *mem, struct div
/* Testing if DB schema resembles Atomic Cobalt database format */
retval = sqlite3_exec(handle, cobalt_test, &db_test_func, 0, NULL);
if (!retval) {
retval = parse_cobalt_buffer(handle, filename, mem->buffer, mem->size, table);
retval = parse_cobalt_buffer(handle, filename, mem->buffer, mem->size, table, trips);
sqlite3_close(handle);
return retval;
}
@ -187,7 +187,7 @@ static int try_to_open_db(const char *filename, struct memblock *mem, struct div
/* Testing if DB schema resembles Divinglog database format */
retval = sqlite3_exec(handle, divinglog_test, &db_test_func, 0, NULL);
if (!retval) {
retval = parse_divinglog_buffer(handle, filename, mem->buffer, mem->size, table);
retval = parse_divinglog_buffer(handle, filename, mem->buffer, mem->size, table, trips);
sqlite3_close(handle);
return retval;
}
@ -213,7 +213,8 @@ static int try_to_open_db(const char *filename, struct memblock *mem, struct div
*
* Followed by the data values (all comma-separated, all one long line).
*/
static int open_by_filename(const char *filename, const char *fmt, struct memblock *mem, struct dive_table *table)
static int open_by_filename(const char *filename, const char *fmt, struct memblock *mem,
struct dive_table *table, struct trip_table *trips)
{
// hack to be able to provide a comment for the translated string
static char *csv_warning = QT_TRANSLATE_NOOP3("gettextFromC",
@ -222,38 +223,38 @@ static int open_by_filename(const char *filename, const char *fmt, struct memblo
/* Suunto Dive Manager files: SDE, ZIP; divelogs.de files: DLD */
if (!strcasecmp(fmt, "SDE") || !strcasecmp(fmt, "ZIP") || !strcasecmp(fmt, "DLD"))
return try_to_open_zip(filename, table);
return try_to_open_zip(filename, table, trips);
/* CSV files */
if (!strcasecmp(fmt, "CSV"))
return report_error(translate("gettextFromC", csv_warning), filename);
/* Truly nasty intentionally obfuscated Cochran Anal software */
if (!strcasecmp(fmt, "CAN"))
return try_to_open_cochran(filename, mem, table);
return try_to_open_cochran(filename, mem, table, trips);
/* Cochran export comma-separated-value files */
if (!strcasecmp(fmt, "DPT"))
return try_to_open_csv(mem, CSV_DEPTH, table);
return try_to_open_csv(mem, CSV_DEPTH, table, trips);
if (!strcasecmp(fmt, "LVD"))
return try_to_open_liquivision(filename, mem, table);
return try_to_open_liquivision(filename, mem, table, trips);
if (!strcasecmp(fmt, "TMP"))
return try_to_open_csv(mem, CSV_TEMP, table);
return try_to_open_csv(mem, CSV_TEMP, table, trips);
if (!strcasecmp(fmt, "HP1"))
return try_to_open_csv(mem, CSV_PRESSURE, table);
return try_to_open_csv(mem, CSV_PRESSURE, table, trips);
return 0;
}
static int parse_file_buffer(const char *filename, struct memblock *mem, struct dive_table *table)
static int parse_file_buffer(const char *filename, struct memblock *mem, struct dive_table *table, struct trip_table *trips)
{
int ret;
char *fmt = strrchr(filename, '.');
if (fmt && (ret = open_by_filename(filename, fmt + 1, mem, table)) != 0)
if (fmt && (ret = open_by_filename(filename, fmt + 1, mem, table, trips)) != 0)
return ret;
if (!mem->size || !mem->buffer)
return report_error("Out of memory parsing file %s\n", filename);
return parse_xml_buffer(filename, mem->buffer, mem->size, table, NULL);
return parse_xml_buffer(filename, mem->buffer, mem->size, table, trips, NULL);
}
int check_git_sha(const char *filename, struct git_repository **git_p, const char **branch_p)
@ -290,7 +291,7 @@ int check_git_sha(const char *filename, struct git_repository **git_p, const cha
return 1;
}
int parse_file(const char *filename, struct dive_table *table)
int parse_file(const char *filename, struct dive_table *table, struct trip_table *trips)
{
struct git_repository *git;
const char *branch = NULL;
@ -336,7 +337,7 @@ int parse_file(const char *filename, struct dive_table *table)
fmt = strrchr(filename, '.');
if (fmt && (!strcasecmp(fmt + 1, "DB") || !strcasecmp(fmt + 1, "BAK") || !strcasecmp(fmt + 1, "SQL"))) {
if (!try_to_open_db(filename, &mem, table)) {
if (!try_to_open_db(filename, &mem, table, trips)) {
free(mem.buffer);
return 0;
}
@ -344,14 +345,14 @@ int parse_file(const char *filename, struct dive_table *table)
/* Divesoft Freedom */
if (fmt && (!strcasecmp(fmt + 1, "DLF"))) {
ret = parse_dlf_buffer(mem.buffer, mem.size, table);
ret = parse_dlf_buffer(mem.buffer, mem.size, table, trips);
free(mem.buffer);
return ret;
}
/* DataTrak/Wlog */
if (fmt && !strcasecmp(fmt + 1, "LOG")) {
ret = datatrak_import(&mem, table);
ret = datatrak_import(&mem, table, trips);
free(mem.buffer);
return ret;
}
@ -359,11 +360,11 @@ int parse_file(const char *filename, struct dive_table *table)
/* OSTCtools */
if (fmt && (!strcasecmp(fmt + 1, "DIVE"))) {
free(mem.buffer);
ostctools_import(filename, table);
ostctools_import(filename, table, trips);
return 0;
}
ret = parse_file_buffer(filename, &mem, table);
ret = parse_file_buffer(filename, &mem, table, trips);
free(mem.buffer);
return ret;
}