From 40275ea56bdf2de1515969a407f8c85eb73a6772 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 12 Nov 2022 08:57:56 +0100 Subject: [PATCH] core: use divelog struct in parser state Signed-off-by: Berthold Stoeger --- core/import-cobalt.c | 7 ++----- core/import-divinglog.c | 7 ++----- core/import-seac.c | 5 +---- core/import-shearwater.c | 10 ++-------- core/import-suunto.c | 10 ++-------- core/parse-xml.c | 27 ++++++++++----------------- core/parse.c | 19 ++++++++++--------- core/parse.h | 6 +----- 8 files changed, 30 insertions(+), 61 deletions(-) diff --git a/core/import-cobalt.c b/core/import-cobalt.c index 61f5d36f4..5fd1ace09 100644 --- a/core/import-cobalt.c +++ b/core/import-cobalt.c @@ -201,7 +201,7 @@ static int cobalt_dive(void *param, int columns, char **data, char **column) return 1; } sprintf(tmp, "%s / %s", location, location_site); - add_dive_to_dive_site(state->cur_dive, find_or_create_dive_site_with_name(tmp, state->sites)); + add_dive_to_dive_site(state->cur_dive, find_or_create_dive_site_with_name(tmp, state->log->sites)); free(tmp); } free(location); @@ -229,10 +229,7 @@ int parse_cobalt_buffer(sqlite3 *handle, const char *url, const char *buffer, in struct parser_state state; init_parser_state(&state); - state.target_table = log->dives; - state.trips = log->trips; - state.sites = log->sites; - state.devices = log->devices; + state.log = log; state.sql_handle = handle; char get_dives[] = "select Id,strftime('%s',DiveStartTime),LocationId,'buddy','notes',Units,(MaxDepthPressure*10000/SurfacePressure)-10000,DiveMinutes,SurfacePressure,SerialNumber,'model' from Dive where IsViewDeleted = 0"; diff --git a/core/import-divinglog.c b/core/import-divinglog.c index 2bc974384..69b115f24 100644 --- a/core/import-divinglog.c +++ b/core/import-divinglog.c @@ -282,7 +282,7 @@ static int divinglog_dive(void *param, int columns, char **data, char **column) state->cur_dive->when = (time_t)(atol(data[1])); if (data[2]) - add_dive_to_dive_site(state->cur_dive, find_or_create_dive_site_with_name(data[2], state->sites)); + add_dive_to_dive_site(state->cur_dive, find_or_create_dive_site_with_name(data[2], state->log->sites)); if (data[3]) utf8_string(data[3], &state->cur_dive->buddy); @@ -400,10 +400,7 @@ int parse_divinglog_buffer(sqlite3 *handle, const char *url, const char *buffer, struct parser_state state; init_parser_state(&state); - state.target_table = log->dives; - state.trips = log->trips; - state.sites = log->sites; - state.devices = log->devices; + state.log = log; state.sql_handle = handle; char get_dives[] = "select Number,strftime('%s',Divedate || ' ' || ifnull(Entrytime,'00:00')),Country || ' - ' || City || ' - ' || Place,Buddy,Comments,Depth,Divetime,Divemaster,Airtemp,Watertemp,Weight,Divesuit,Computer,ID,Visibility,SupplyType from Logbook where UUID not in (select UUID from DeletedRecords)"; diff --git a/core/import-seac.c b/core/import-seac.c index a83d112e8..3a921d849 100644 --- a/core/import-seac.c +++ b/core/import-seac.c @@ -273,10 +273,7 @@ int parse_seac_buffer(sqlite3 *handle, const char *url, const char *buffer, int struct parser_state state; init_parser_state(&state); - state.target_table = log->dives; - state.trips = log->trips; - state.sites = log->sites; - state.devices = log->devices; + state.log = log; state.sql_handle = handle; const char *get_dives = "SELECT dive_number, device_sn, date, timezone, time, elapsed_surface_time, dive_type, start_mode, water_type, comment, total_dive_time, max_depth, firmware_version, dive_id FROM headers_dive"; diff --git a/core/import-shearwater.c b/core/import-shearwater.c index 793dc36bb..13ee1ee55 100644 --- a/core/import-shearwater.c +++ b/core/import-shearwater.c @@ -497,10 +497,7 @@ int parse_shearwater_buffer(sqlite3 *handle, const char *url, const char *buffer struct parser_state state; init_parser_state(&state); - state.target_table = log->dives; - state.trips = log->trips; - state.sites = log->sites; - state.devices = log->devices; + state.log = log; state.sql_handle = handle; // So far have not seen any sample rate in Shearwater Desktop @@ -528,10 +525,7 @@ int parse_shearwater_cloud_buffer(sqlite3 *handle, const char *url, const char * struct parser_state state; init_parser_state(&state); - state.target_table = log->dives; - state.trips = log->trips; - state.sites = log->sites; - state.devices = log->devices; + state.log = log; state.sql_handle = handle; char get_dives[] = "select l.number,strftime('%s', DiveDate),location||' / '||site,buddy,notes,imperialUnits,maxDepth,DiveLengthTime,startSurfacePressure,computerSerial,computerModel,d.diveId,l.sampleRateMs / 1000 FROM dive_details AS d JOIN dive_logs AS l ON d.diveId=l.diveId"; diff --git a/core/import-suunto.c b/core/import-suunto.c index c85adcdc0..3616be942 100644 --- a/core/import-suunto.c +++ b/core/import-suunto.c @@ -303,10 +303,7 @@ int parse_dm4_buffer(sqlite3 *handle, const char *url, const char *buffer, int s struct parser_state state; init_parser_state(&state); - state.target_table = log->dives; - state.trips = log->trips; - state.sites = log->sites; - state.devices = log->devices; + state.log = log; state.sql_handle = handle; /* StartTime is converted from Suunto's nano seconds to standard @@ -588,10 +585,7 @@ int parse_dm5_buffer(sqlite3 *handle, const char *url, const char *buffer, int s struct parser_state state; init_parser_state(&state); - state.target_table = log->dives; - state.trips = log->trips; - state.sites = log->sites; - state.devices = log->devices; + state.log = log; state.sql_handle = handle; /* StartTime is converted from Suunto's nano seconds to standard diff --git a/core/parse-xml.c b/core/parse-xml.c index 5bcd4b7e5..b0385eafa 100644 --- a/core/parse-xml.c +++ b/core/parse-xml.c @@ -569,7 +569,7 @@ static void dive_site(char *buffer, struct dive *d, struct parser_state *state) { uint32_t uuid; hex_value(buffer, &uuid); - add_dive_to_dive_site(d, get_dive_site_by_uuid(uuid, state->sites)); + add_dive_to_dive_site(d, get_dive_site_by_uuid(uuid, state->log->sites)); } static void get_notrip(char *buffer, bool *notrip) @@ -982,9 +982,9 @@ static void divinglog_place(char *place, struct dive *d, struct parser_state *st state->city ? state->city : "", state->country ? ", " : "", state->country ? state->country : ""); - ds = get_dive_site_by_name(buffer, state->sites); + ds = get_dive_site_by_name(buffer, state->log->sites); if (!ds) - ds = create_dive_site(buffer, state->sites); + ds = create_dive_site(buffer, state->log->sites); add_dive_to_dive_site(d, ds); // TODO: capture the country / city info in the taxonomy instead @@ -1156,7 +1156,7 @@ static void gps_lat(char *buffer, struct dive *dive, struct parser_state *state) location.lat = parse_degrees(buffer, &end); if (!ds) { - add_dive_to_dive_site(dive, create_dive_site_with_gps(NULL, &location, state->sites)); + add_dive_to_dive_site(dive, create_dive_site_with_gps(NULL, &location, state->log->sites)); } else { if (ds->location.lat.udeg && ds->location.lat.udeg != location.lat.udeg) fprintf(stderr, "Oops, changing the latitude of existing dive site id %8x name %s; not good\n", ds->uuid, ds->name ?: "(unknown)"); @@ -1172,7 +1172,7 @@ static void gps_long(char *buffer, struct dive *dive, struct parser_state *state location.lon = parse_degrees(buffer, &end); if (!ds) { - add_dive_to_dive_site(dive, create_dive_site_with_gps(NULL, &location, state->sites)); + add_dive_to_dive_site(dive, create_dive_site_with_gps(NULL, &location, state->log->sites)); } else { if (ds->location.lon.udeg && ds->location.lon.udeg != location.lon.udeg) fprintf(stderr, "Oops, changing the longitude of existing dive site id %8x name %s; not good\n", ds->uuid, ds->name ?: "(unknown)"); @@ -1202,14 +1202,14 @@ static void gps_in_dive(char *buffer, struct dive *dive, struct parser_state *st parse_location(buffer, &location); if (!ds) { // check if we have a dive site within 20 meters of that gps fix - ds = get_dive_site_by_gps_proximity(&location, 20, state->sites); + ds = get_dive_site_by_gps_proximity(&location, 20, state->log->sites); if (ds) { // found a site nearby; in case it turns out this one had a different name let's // remember the original coordinates so we can create the correct dive site later state->cur_location = location; } else { - ds = create_dive_site_with_gps("", &location, state->sites); + ds = create_dive_site_with_gps("", &location, state->log->sites); } add_dive_to_dive_site(dive, ds); } else { @@ -1761,12 +1761,8 @@ int parse_xml_buffer(const char *url, const char *buffer, int size, struct divel struct parser_state state; init_parser_state(&state); - state.target_table = log->dives; - state.trips = log->trips; - state.sites = log->sites; - state.devices = log->devices; + state.log = log; state.fingerprints = &fingerprint_table; // simply use the global table for now - state.filter_presets = log->filter_presets; doc = xmlReadMemory(res, strlen(res), url, NULL, XML_PARSE_HUGE | XML_PARSE_RECOVER); if (!doc) doc = xmlReadMemory(res, strlen(res), url, "latin1", XML_PARSE_HUGE | XML_PARSE_RECOVER); @@ -1829,10 +1825,7 @@ int parse_dlf_buffer(unsigned char *buffer, size_t size, struct divelog *log) struct parser_state state; init_parser_state(&state); - state.target_table = log->dives; - state.trips = log->trips; - state.sites = log->sites; - state.devices = log->devices; + state.log = log; // Check for the correct file magic if (ptr[0] != 'D' || ptr[1] != 'i' || ptr[2] != 'v' || ptr[3] != 'E') @@ -2245,7 +2238,7 @@ int parse_dlf_buffer(unsigned char *buffer, size_t size, struct divelog *log) /* Measure GPS */ state.cur_location.lat.udeg = (int)((ptr[7] << 24) + (ptr[6] << 16) + (ptr[5] << 8) + (ptr[4] << 0)); state.cur_location.lon.udeg = (int)((ptr[11] << 24) + (ptr[10] << 16) + (ptr[9] << 8) + (ptr[8] << 0)); - add_dive_to_dive_site(state.cur_dive, create_dive_site_with_gps("DLF imported", &state.cur_location, state.sites)); + add_dive_to_dive_site(state.cur_dive, create_dive_site_with_gps("DLF imported", &state.cur_location, state.log->sites)); break; default: break; diff --git a/core/parse.c b/core/parse.c index 657ff64ac..5db945ba8 100644 --- a/core/parse.c +++ b/core/parse.c @@ -9,6 +9,7 @@ #include "parse.h" #include "dive.h" +#include "divelog.h" #include "divesite.h" #include "errorhelper.h" #include "sample.h" @@ -225,7 +226,7 @@ void dc_settings_start(struct parser_state *state) void dc_settings_end(struct parser_state *state) { - create_device_node(state->devices, + create_device_node(state->log->devices, state->cur_settings.dc.model, state->cur_settings.dc.serial_nr, state->cur_settings.dc.nickname); @@ -246,7 +247,7 @@ void dive_site_end(struct parser_state *state) if (!state->cur_dive_site) return; - struct dive_site *ds = alloc_or_get_dive_site(state->cur_dive_site->uuid, state->sites); + struct dive_site *ds = alloc_or_get_dive_site(state->cur_dive_site->uuid, state->log->sites); merge_dive_site(ds, state->cur_dive_site); if (verbose > 3) @@ -265,7 +266,7 @@ void filter_preset_start(struct parser_state *state) void filter_preset_end(struct parser_state *state) { - add_filter_preset_to_table(state->cur_filter, state->filter_presets); + add_filter_preset_to_table(state->cur_filter, state->log->filter_presets); free_filter_preset(state->cur_filter); state->cur_filter = NULL; } @@ -332,7 +333,7 @@ void dive_end(struct parser_state *state) if (!is_dive(state)) { free_dive(state->cur_dive); } else { - record_dive_to_table(state->cur_dive, state->target_table); + record_dive_to_table(state->cur_dive, state->log->dives); if (state->cur_trip) add_dive_to_trip(state->cur_dive, state->cur_trip); } @@ -355,7 +356,7 @@ void trip_end(struct parser_state *state) { if (!state->cur_trip) return; - insert_trip(state->cur_trip, state->trips); + insert_trip(state->cur_trip, state->log->trips); state->cur_trip = NULL; } @@ -504,7 +505,7 @@ void add_dive_site(char *ds_name, struct dive *dive, struct parser_state *state) struct dive_site *ds = dive->dive_site; if (!ds) { // if the dive doesn't have a dive site, check if there's already a dive site by this name - ds = get_dive_site_by_name(buffer, state->sites); + ds = get_dive_site_by_name(buffer, state->log->sites); } if (ds) { // we have a dive site, let's hope there isn't a different name @@ -515,12 +516,12 @@ void add_dive_site(char *ds_name, struct dive *dive, struct parser_state *state) // but wait, we could have gotten this one based on GPS coords and could // have had two different names for the same site... so let's search the other // way around - struct dive_site *exact_match = get_dive_site_by_gps_and_name(buffer, &ds->location, state->sites); + struct dive_site *exact_match = get_dive_site_by_gps_and_name(buffer, &ds->location, state->log->sites); if (exact_match) { unregister_dive_from_dive_site(dive); add_dive_to_dive_site(dive, exact_match); } else { - struct dive_site *newds = create_dive_site(buffer, state->sites); + struct dive_site *newds = create_dive_site(buffer, state->log->sites); unregister_dive_from_dive_site(dive); add_dive_to_dive_site(dive, newds); if (has_location(&state->cur_location)) { @@ -537,7 +538,7 @@ void add_dive_site(char *ds_name, struct dive *dive, struct parser_state *state) add_dive_to_dive_site(dive, ds); } } else { - add_dive_to_dive_site(dive, create_dive_site(buffer, state->sites)); + add_dive_to_dive_site(dive, create_dive_site(buffer, state->log->sites)); } } free(to_free); diff --git a/core/parse.h b/core/parse.h index 2a3db59ca..108959649 100644 --- a/core/parse.h +++ b/core/parse.h @@ -87,12 +87,8 @@ struct parser_state { int sample_rate; struct extra_data cur_extra_data; struct units xml_parsing_units; - struct dive_table *target_table; /* non-owning */ - struct trip_table *trips; /* non-owning */ - struct dive_site_table *sites; /* non-owning */ - struct device_table *devices; /* non-owning */ + struct divelog *log; /* non-owning */ struct fingerprint_table *fingerprints; /* non-owning */ - struct filter_preset_table *filter_presets; /* non-owning */ sqlite3 *sql_handle; /* for SQL based parsers */ event_allocation_t event_allocation;