mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
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:
parent
eebb47ec22
commit
9c253ee6c5
81 changed files with 661 additions and 698 deletions
104
core/divelist.c
104
core/divelist.c
|
|
@ -5,8 +5,9 @@
|
|||
#include "subsurface-string.h"
|
||||
#include "deco.h"
|
||||
#include "device.h"
|
||||
#include "divesite.h"
|
||||
#include "dive.h"
|
||||
#include "divelog.h"
|
||||
#include "divesite.h"
|
||||
#include "event.h"
|
||||
#include "eventname.h"
|
||||
#include "filterpreset.h"
|
||||
|
|
@ -226,7 +227,7 @@ static int calculate_cns(struct dive *dive)
|
|||
return dive->cns;
|
||||
|
||||
divenr = get_divenr(dive);
|
||||
i = divenr >= 0 ? divenr : dive_table.nr;
|
||||
i = divenr >= 0 ? divenr : divelog.dives->nr;
|
||||
#if DECO_CALC_DEBUG & 2
|
||||
if (i >= 0 && i < dive_table.nr)
|
||||
printf("\n\n*** CNS for dive #%d %d\n", i, get_dive(i)->number);
|
||||
|
|
@ -234,7 +235,7 @@ static int calculate_cns(struct dive *dive)
|
|||
printf("\n\n*** CNS for dive #%d\n", i);
|
||||
#endif
|
||||
/* Look at next dive in dive list table and correct i when needed */
|
||||
while (i < dive_table.nr - 1) {
|
||||
while (i < divelog.dives->nr - 1) {
|
||||
struct dive *pdive = get_dive(i);
|
||||
if (!pdive || pdive->when > dive->when)
|
||||
break;
|
||||
|
|
@ -279,7 +280,7 @@ static int calculate_cns(struct dive *dive)
|
|||
#endif
|
||||
}
|
||||
/* Walk forward and add dives and surface intervals to CNS */
|
||||
while (++i < dive_table.nr) {
|
||||
while (++i < divelog.dives->nr) {
|
||||
#if DECO_CALC_DEBUG & 2
|
||||
printf("Check if dive #%d %d will be really added to CNS calc: ", i, get_dive(i)->number);
|
||||
#endif
|
||||
|
|
@ -463,7 +464,7 @@ int init_decompression(struct deco_state *ds, const struct dive *dive, bool in_p
|
|||
return false;
|
||||
|
||||
divenr = get_divenr(dive);
|
||||
i = divenr >= 0 ? divenr : dive_table.nr;
|
||||
i = divenr >= 0 ? divenr : divelog.dives->nr;
|
||||
#if DECO_CALC_DEBUG & 2
|
||||
if (i >= 0 && i < dive_table.nr)
|
||||
printf("\n\n*** Init deco for dive #%d %d\n", i, get_dive(i)->number);
|
||||
|
|
@ -471,7 +472,7 @@ int init_decompression(struct deco_state *ds, const struct dive *dive, bool in_p
|
|||
printf("\n\n*** Init deco for dive #%d\n", i);
|
||||
#endif
|
||||
/* Look at next dive in dive list table and correct i when needed */
|
||||
while (i < dive_table.nr - 1) {
|
||||
while (i < divelog.dives->nr - 1) {
|
||||
struct dive *pdive = get_dive(i);
|
||||
if (!pdive || pdive->when > dive->when)
|
||||
break;
|
||||
|
|
@ -516,7 +517,7 @@ int init_decompression(struct deco_state *ds, const struct dive *dive, bool in_p
|
|||
#endif
|
||||
}
|
||||
/* Walk forward an add dives and surface intervals to deco */
|
||||
while (++i < dive_table.nr) {
|
||||
while (++i < divelog.dives->nr) {
|
||||
#if DECO_CALC_DEBUG & 2
|
||||
printf("Check if dive #%d %d will be really added to deco calc: ", i, get_dive(i)->number);
|
||||
#endif
|
||||
|
|
@ -767,7 +768,7 @@ struct dive *unregister_dive(int idx)
|
|||
/* When removing a dive from the global dive table,
|
||||
* we also have to unregister its fulltext cache. */
|
||||
fulltext_unregister(dive);
|
||||
remove_from_dive_table(&dive_table, idx);
|
||||
remove_from_dive_table(divelog.dives, idx);
|
||||
if (dive->selected)
|
||||
amount_selected--;
|
||||
dive->selected = false;
|
||||
|
|
@ -783,18 +784,18 @@ void delete_single_dive(int idx)
|
|||
return; /* this should never happen */
|
||||
if (dive->selected)
|
||||
deselect_dive(dive);
|
||||
remove_dive_from_trip(dive, &trip_table);
|
||||
remove_dive_from_trip(dive, divelog.trips);
|
||||
unregister_dive_from_dive_site(dive);
|
||||
delete_dive_from_table(&dive_table, idx);
|
||||
delete_dive_from_table(divelog.dives, idx);
|
||||
}
|
||||
|
||||
void process_loaded_dives()
|
||||
{
|
||||
sort_dive_table(&dive_table);
|
||||
sort_trip_table(&trip_table);
|
||||
sort_dive_table(divelog.dives);
|
||||
sort_trip_table(divelog.trips);
|
||||
|
||||
/* Autogroup dives if desired by user. */
|
||||
autogroup_dives(&dive_table, &trip_table);
|
||||
autogroup_dives(divelog.dives, divelog.trips);
|
||||
|
||||
fulltext_populate();
|
||||
|
||||
|
|
@ -876,9 +877,9 @@ static bool try_to_merge_into(struct dive *dive_to_add, int idx, struct dive_tab
|
|||
/* Check if a dive is ranked after the last dive of the global dive list */
|
||||
static bool dive_is_after_last(struct dive *d)
|
||||
{
|
||||
if (dive_table.nr == 0)
|
||||
if (divelog.dives->nr == 0)
|
||||
return true;
|
||||
return dive_less_than(dive_table.dives[dive_table.nr - 1], d);
|
||||
return dive_less_than(divelog.dives->dives[divelog.dives->nr - 1], d);
|
||||
}
|
||||
|
||||
/* Merge dives from "dives_from" into "dives_to". Overlapping dives will be merged,
|
||||
|
|
@ -967,9 +968,7 @@ static bool merge_dive_tables(struct dive_table *dives_from, struct dive_table *
|
|||
/* Merge the dives of the trip "from" and the dive_table "dives_from" into the trip "to"
|
||||
* and dive_table "dives_to". If "prefer_imported" is true, dive data of "from" takes
|
||||
* precedence */
|
||||
void add_imported_dives(struct dive_table *import_table, struct trip_table *import_trip_table,
|
||||
struct dive_site_table *import_sites_table, struct device_table *import_device_table,
|
||||
int flags)
|
||||
void add_imported_dives(struct divelog *import_log, int flags)
|
||||
{
|
||||
int i, idx;
|
||||
struct dive_table dives_to_add = empty_dive_table;
|
||||
|
|
@ -980,7 +979,7 @@ void add_imported_dives(struct dive_table *import_table, struct trip_table *impo
|
|||
|
||||
/* Process imported dives and generate lists of dives
|
||||
* to-be-added and to-be-removed */
|
||||
process_imported_dives(import_table, import_trip_table, import_sites_table, import_device_table,
|
||||
process_imported_dives(import_log->dives, import_log->trips, import_log->sites, import_log->devices,
|
||||
flags, &dives_to_add, &dives_to_remove, &trips_to_add,
|
||||
&dive_sites_to_add, devices_to_add);
|
||||
|
||||
|
|
@ -1004,12 +1003,12 @@ void add_imported_dives(struct dive_table *import_table, struct trip_table *impo
|
|||
|
||||
/* Add new dives */
|
||||
for (i = 0; i < dives_to_add.nr; i++)
|
||||
insert_dive(&dive_table, dives_to_add.dives[i]);
|
||||
insert_dive(divelog.dives, dives_to_add.dives[i]);
|
||||
dives_to_add.nr = 0;
|
||||
|
||||
/* Add new trips */
|
||||
for (i = 0; i < trips_to_add.nr; i++)
|
||||
insert_trip(trips_to_add.trips[i], &trip_table);
|
||||
insert_trip(trips_to_add.trips[i], divelog.trips);
|
||||
trips_to_add.nr = 0;
|
||||
|
||||
/* Add new dive sites */
|
||||
|
|
@ -1020,12 +1019,12 @@ void add_imported_dives(struct dive_table *import_table, struct trip_table *impo
|
|||
/* Add new devices */
|
||||
for (i = 0; i < nr_devices(devices_to_add); i++) {
|
||||
const struct device *dev = get_device(devices_to_add, i);
|
||||
add_to_device_table(&device_table, dev);
|
||||
add_to_device_table(divelog.devices, dev);
|
||||
}
|
||||
|
||||
/* We might have deleted the old selected dive.
|
||||
* Choose the newest dive as selected (if any) */
|
||||
current_dive = dive_table.nr > 0 ? dive_table.dives[dive_table.nr - 1] : NULL;
|
||||
current_dive = divelog.dives->nr > 0 ? divelog.dives->dives[divelog.dives->nr - 1] : NULL;
|
||||
|
||||
free_device_table(devices_to_add);
|
||||
|
||||
|
|
@ -1051,8 +1050,8 @@ bool try_to_merge_trip(struct dive_trip *trip_import, struct dive_table *import_
|
|||
int i;
|
||||
struct dive_trip *trip_old;
|
||||
|
||||
for (i = 0; i < trip_table.nr; i++) {
|
||||
trip_old = trip_table.trips[i];
|
||||
for (i = 0; i < divelog.trips->nr; i++) {
|
||||
trip_old = divelog.trips->trips[i];
|
||||
if (trips_overlap(trip_import, trip_old)) {
|
||||
*sequence_changed |= merge_dive_tables(&trip_import->dives, import_table, &trip_old->dives,
|
||||
prefer_imported, trip_old,
|
||||
|
|
@ -1144,7 +1143,7 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
|
|||
/* Add only the devices that we don't know about yet. */
|
||||
for (i = 0; i < nr_devices(import_device_table); i++) {
|
||||
const struct device *dev = get_device(import_device_table, i);
|
||||
if (!device_exists(&device_table, dev))
|
||||
if (!device_exists(divelog.devices, dev))
|
||||
add_to_device_table(devices_to_add, dev);
|
||||
}
|
||||
|
||||
|
|
@ -1237,7 +1236,7 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
|
|||
/* The remaining dives in import_table are those that don't belong to
|
||||
* a trip and the caller does not want them to be associated to a
|
||||
* new trip. Merge them into the global table. */
|
||||
sequence_changed |= merge_dive_tables(import_table, NULL, &dive_table, flags & IMPORT_PREFER_IMPORTED, NULL,
|
||||
sequence_changed |= merge_dive_tables(import_table, NULL, divelog.dives, flags & IMPORT_PREFER_IMPORTED, NULL,
|
||||
dives_to_add, dives_to_remove, &start_renumbering_at);
|
||||
}
|
||||
|
||||
|
|
@ -1246,13 +1245,13 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
|
|||
* - The last dive in the old dive table had a number itself (if there is a last dive).
|
||||
* - None of the new dives has a number.
|
||||
*/
|
||||
last_old_dive_is_numbered = dive_table.nr == 0 || dive_table.dives[dive_table.nr - 1]->number > 0;
|
||||
last_old_dive_is_numbered = divelog.dives->nr == 0 || divelog.dives->dives[divelog.dives->nr - 1]->number > 0;
|
||||
|
||||
/* We counted the number of merged dives that were added to dives_to_add.
|
||||
* Skip those. Since sequence_changed is false all added dives are *after*
|
||||
* all merged dives. */
|
||||
if (!sequence_changed && last_old_dive_is_numbered && !new_dive_has_number) {
|
||||
nr = dive_table.nr > 0 ? dive_table.dives[dive_table.nr - 1]->number : 0;
|
||||
nr = divelog.dives->nr > 0 ? divelog.dives->dives[divelog.dives->nr - 1]->number : 0;
|
||||
for (i = start_renumbering_at; i < dives_to_add->nr; i++)
|
||||
dives_to_add->dives[i]->number = ++nr;
|
||||
}
|
||||
|
|
@ -1261,9 +1260,9 @@ void process_imported_dives(struct dive_table *import_table, struct trip_table *
|
|||
static struct dive *get_last_valid_dive()
|
||||
{
|
||||
int i;
|
||||
for (i = dive_table.nr - 1; i >= 0; i--) {
|
||||
if (!dive_table.dives[i]->invalid)
|
||||
return dive_table.dives[i];
|
||||
for (i = divelog.dives->nr - 1; i >= 0; i--) {
|
||||
if (!divelog.dives->dives[i]->invalid)
|
||||
return divelog.dives->dives[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
|
@ -1277,7 +1276,7 @@ static struct dive *get_last_valid_dive()
|
|||
*/
|
||||
int get_dive_nr_at_idx(int idx)
|
||||
{
|
||||
if (idx < dive_table.nr)
|
||||
if (idx < divelog.dives->nr)
|
||||
return 0;
|
||||
struct dive *last_dive = get_last_valid_dive();
|
||||
if (!last_dive)
|
||||
|
|
@ -1306,27 +1305,27 @@ void report_datafile_version(int version)
|
|||
int get_dive_id_closest_to(timestamp_t when)
|
||||
{
|
||||
int i;
|
||||
int nr = dive_table.nr;
|
||||
int nr = divelog.dives->nr;
|
||||
|
||||
// deal with pathological cases
|
||||
if (nr == 0)
|
||||
return 0;
|
||||
else if (nr == 1)
|
||||
return dive_table.dives[0]->id;
|
||||
return divelog.dives->dives[0]->id;
|
||||
|
||||
for (i = 0; i < nr && dive_table.dives[i]->when <= when; i++)
|
||||
for (i = 0; i < nr && divelog.dives->dives[i]->when <= when; i++)
|
||||
; // nothing
|
||||
|
||||
// again, capture the two edge cases first
|
||||
if (i == nr)
|
||||
return dive_table.dives[i - 1]->id;
|
||||
return divelog.dives->dives[i - 1]->id;
|
||||
else if (i == 0)
|
||||
return dive_table.dives[0]->id;
|
||||
return divelog.dives->dives[0]->id;
|
||||
|
||||
if (when - dive_table.dives[i - 1]->when < dive_table.dives[i]->when - when)
|
||||
return dive_table.dives[i - 1]->id;
|
||||
if (when - divelog.dives->dives[i - 1]->when < divelog.dives->dives[i]->when - when)
|
||||
return divelog.dives->dives[i - 1]->id;
|
||||
else
|
||||
return dive_table.dives[i]->id;
|
||||
return divelog.dives->dives[i]->id;
|
||||
}
|
||||
|
||||
void clear_dive_file_data()
|
||||
|
|
@ -1334,20 +1333,11 @@ void clear_dive_file_data()
|
|||
fulltext_unregister_all();
|
||||
select_single_dive(NULL); // This is propagate up to the UI and clears all the information.
|
||||
|
||||
while (dive_table.nr)
|
||||
delete_single_dive(0);
|
||||
current_dive = NULL;
|
||||
while (dive_site_table.nr)
|
||||
delete_dive_site(get_dive_site(0, &dive_site_table), &dive_site_table);
|
||||
if (trip_table.nr != 0) {
|
||||
fprintf(stderr, "Warning: trip table not empty in clear_dive_file_data()!\n");
|
||||
trip_table.nr = 0;
|
||||
}
|
||||
clear_divelog(&divelog);
|
||||
|
||||
clear_dive(&displayed_dive);
|
||||
clear_device_table(&device_table);
|
||||
clear_event_names();
|
||||
clear_filter_presets();
|
||||
|
||||
reset_min_datafile_version();
|
||||
clear_git_id();
|
||||
|
|
@ -1418,14 +1408,14 @@ timestamp_t get_surface_interval(timestamp_t when)
|
|||
timestamp_t prev_end;
|
||||
|
||||
/* find previous dive. might want to use a binary search. */
|
||||
for (i = dive_table.nr - 1; i >= 0; --i) {
|
||||
if (dive_table.dives[i]->when < when)
|
||||
for (i = divelog.dives->nr - 1; i >= 0; --i) {
|
||||
if (divelog.dives->dives[i]->when < when)
|
||||
break;
|
||||
}
|
||||
if (i < 0)
|
||||
return -1;
|
||||
|
||||
prev_end = dive_endtime(dive_table.dives[i]);
|
||||
prev_end = dive_endtime(divelog.dives->dives[i]);
|
||||
if (prev_end > when)
|
||||
return 0;
|
||||
return when - prev_end;
|
||||
|
|
@ -1437,11 +1427,11 @@ struct dive *find_next_visible_dive(timestamp_t when)
|
|||
{
|
||||
int i, j;
|
||||
|
||||
if (!dive_table.nr)
|
||||
if (!divelog.dives->nr)
|
||||
return NULL;
|
||||
|
||||
/* we might want to use binary search here */
|
||||
for (i = 0; i < dive_table.nr; i++) {
|
||||
for (i = 0; i < divelog.dives->nr; i++) {
|
||||
if (when <= get_dive(i)->when)
|
||||
break;
|
||||
}
|
||||
|
|
@ -1451,7 +1441,7 @@ struct dive *find_next_visible_dive(timestamp_t when)
|
|||
return get_dive(j);
|
||||
}
|
||||
|
||||
for (j = i; j < dive_table.nr; j++) {
|
||||
for (j = i; j < divelog.dives->nr; j++) {
|
||||
if (!get_dive(j)->hidden_by_filter)
|
||||
return get_dive(j);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue