mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
core: turn dive-trip location and notes into std::string
Simpler memory management. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
parent
2fd226964c
commit
3ee41328f9
29 changed files with 157 additions and 179 deletions
|
|
@ -131,7 +131,6 @@ set(SUBSURFACE_CORE_LIB_SRCS
|
|||
metrics.cpp
|
||||
metrics.h
|
||||
ostctools.cpp
|
||||
owning_ptrs.h
|
||||
parse-gpx.cpp
|
||||
parse-xml.cpp
|
||||
parse.cpp
|
||||
|
|
|
|||
|
|
@ -1752,7 +1752,7 @@ static void merge_temperatures(struct dive *res, const struct dive *a, const str
|
|||
*/
|
||||
static struct dive_trip *get_preferred_trip(const struct dive *a, const struct dive *b)
|
||||
{
|
||||
dive_trip_t *atrip, *btrip;
|
||||
dive_trip *atrip, *btrip;
|
||||
|
||||
/* If only one dive has a trip, choose that */
|
||||
atrip = a->divetrip;
|
||||
|
|
@ -1769,13 +1769,13 @@ static struct dive_trip *get_preferred_trip(const struct dive *a, const struct d
|
|||
return atrip;
|
||||
|
||||
/* Otherwise, look at the trip data and pick the "better" one */
|
||||
if (!atrip->location)
|
||||
if (atrip->location.empty())
|
||||
return btrip;
|
||||
if (!btrip->location)
|
||||
if (btrip->location.empty())
|
||||
return atrip;
|
||||
if (!atrip->notes)
|
||||
if (atrip->notes.empty())
|
||||
return btrip;
|
||||
if (!btrip->notes)
|
||||
if (btrip->notes.empty())
|
||||
return atrip;
|
||||
|
||||
/*
|
||||
|
|
|
|||
|
|
@ -693,7 +693,7 @@ void insert_dive(struct dive_table *table, struct dive *d)
|
|||
static void autogroup_dives(struct dive_table *table, struct trip_table *trip_table_arg)
|
||||
{
|
||||
int from, to;
|
||||
dive_trip_t *trip;
|
||||
dive_trip *trip;
|
||||
int i, j;
|
||||
bool alloc;
|
||||
|
||||
|
|
@ -1003,10 +1003,10 @@ void add_imported_dives(struct divelog *import_log, int flags)
|
|||
* Returns true if trip was merged. In this case, the trip will be
|
||||
* freed.
|
||||
*/
|
||||
bool try_to_merge_trip(struct dive_trip *trip_import, struct dive_table *import_table, bool prefer_imported,
|
||||
/* output parameters: */
|
||||
struct dive_table *dives_to_add, struct dive_table *dives_to_remove,
|
||||
bool *sequence_changed, int *start_renumbering_at)
|
||||
static bool try_to_merge_trip(dive_trip *trip_import, struct dive_table *import_table, bool prefer_imported,
|
||||
/* output parameters: */
|
||||
struct dive_table *dives_to_add, struct dive_table *dives_to_remove,
|
||||
bool *sequence_changed, int *start_renumbering_at)
|
||||
{
|
||||
int i;
|
||||
struct dive_trip *trip_old;
|
||||
|
|
@ -1066,7 +1066,7 @@ void process_imported_dives(struct divelog *import_log, int flags,
|
|||
device_table &devices_to_add)
|
||||
{
|
||||
int i, j, nr, start_renumbering_at = 0;
|
||||
struct dive_trip *trip_import, *new_trip;
|
||||
dive_trip *new_trip;
|
||||
bool sequence_changed = false;
|
||||
bool new_dive_has_number = false;
|
||||
bool last_old_dive_is_numbered;
|
||||
|
|
@ -1141,7 +1141,7 @@ void process_imported_dives(struct divelog *import_log, int flags,
|
|||
* will be imported so do a simple n*m loop until someone complains.
|
||||
*/
|
||||
for (i = 0; i < import_log->trips->nr; i++) {
|
||||
trip_import = import_log->trips->trips[i];
|
||||
dive_trip *trip_import = import_log->trips->trips[i];
|
||||
if ((flags & IMPORT_MERGE_ALL_TRIPS) || trip_import->autogen) {
|
||||
if (try_to_merge_trip(trip_import, import_log->dives.get(), flags & IMPORT_PREFER_IMPORTED, dives_to_add, dives_to_remove,
|
||||
&sequence_changed, &start_renumbering_at))
|
||||
|
|
|
|||
|
|
@ -837,7 +837,7 @@ static bool has_locations(const filter_constraint &c, const struct dive *d)
|
|||
{
|
||||
QStringList diveLocations;
|
||||
if (d->divetrip)
|
||||
diveLocations.push_back(QString(d->divetrip->location).trimmed());
|
||||
diveLocations.push_back(QString::fromStdString(d->divetrip->location).trimmed());
|
||||
|
||||
if (d->dive_site)
|
||||
diveLocations.push_back(QString::fromStdString(d->dive_site->name).trimmed());
|
||||
|
|
|
|||
|
|
@ -139,7 +139,7 @@ static std::vector<QString> getWords(const dive *d)
|
|||
}
|
||||
// TODO: We should index trips separately!
|
||||
if (d->divetrip)
|
||||
tokenize(d->divetrip->location, res);
|
||||
tokenize(QString::fromStdString(d->divetrip->location), res);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ struct git_parser_state {
|
|||
git_repository *repo = nullptr;
|
||||
struct divecomputer *active_dc = nullptr;
|
||||
std::unique_ptr<dive> active_dive;
|
||||
dive_trip_t *active_trip = nullptr;
|
||||
std::unique_ptr<dive_trip> active_trip;
|
||||
std::string fulltext_mode;
|
||||
std::string fulltext_query;
|
||||
std::string filter_constraint_type;
|
||||
|
|
@ -889,10 +889,10 @@ static void parse_trip_time(char *, struct git_parser_state *)
|
|||
{ }
|
||||
|
||||
static void parse_trip_location(char *, struct git_parser_state *state)
|
||||
{ state->active_trip->location = get_first_converted_string_c(state); }
|
||||
{ state->active_trip->location = get_first_converted_string(state); }
|
||||
|
||||
static void parse_trip_notes(char *, struct git_parser_state *state)
|
||||
{ state->active_trip->notes = get_first_converted_string_c(state); }
|
||||
{ state->active_trip->notes = get_first_converted_string(state); }
|
||||
|
||||
static void parse_settings_autogroup(char *, struct git_parser_state *state)
|
||||
{
|
||||
|
|
@ -1381,12 +1381,10 @@ static void for_each_line(git_blob *blob, line_fn_t *fn, struct git_parser_state
|
|||
|
||||
static void finish_active_trip(struct git_parser_state *state)
|
||||
{
|
||||
dive_trip_t *trip = state->active_trip;
|
||||
auto &trip = state->active_trip;
|
||||
|
||||
if (trip) {
|
||||
state->active_trip = NULL;
|
||||
insert_trip(trip, state->log->trips.get());
|
||||
}
|
||||
if (trip)
|
||||
insert_trip(trip.release(), state->log->trips.get());
|
||||
}
|
||||
|
||||
static void finish_active_dive(struct git_parser_state *state)
|
||||
|
|
@ -1403,7 +1401,7 @@ static void create_new_dive(timestamp_t when, struct git_parser_state *state)
|
|||
state->active_dive->when = when;
|
||||
|
||||
if (state->active_trip)
|
||||
add_dive_to_trip(state->active_dive.get(), state->active_trip);
|
||||
add_dive_to_trip(state->active_dive.get(), state->active_trip.get());
|
||||
}
|
||||
|
||||
static bool validate_date(int yyyy, int mm, int dd)
|
||||
|
|
@ -1433,7 +1431,7 @@ static int dive_trip_directory(const char *root, const char *name, struct git_pa
|
|||
if (!validate_date(yyyy, mm, dd))
|
||||
return GIT_WALK_SKIP;
|
||||
finish_active_trip(state);
|
||||
state->active_trip = alloc_trip();
|
||||
state->active_trip = std::make_unique<dive_trip>();
|
||||
return GIT_WALK_OK;
|
||||
}
|
||||
|
||||
|
|
@ -1785,7 +1783,7 @@ static int parse_filter_preset(struct git_parser_state *state, const git_tree_en
|
|||
static int walk_tree_file(const char *root, const git_tree_entry *entry, struct git_parser_state *state)
|
||||
{
|
||||
auto &dive = state->active_dive;
|
||||
dive_trip_t *trip = state->active_trip;
|
||||
auto &trip = state->active_trip;
|
||||
const char *name = git_tree_entry_name(entry);
|
||||
if (verbose > 1)
|
||||
report_info("git load handling file %s\n", name);
|
||||
|
|
@ -1815,7 +1813,7 @@ static int walk_tree_file(const char *root, const git_tree_entry *entry, struct
|
|||
return parse_settings_entry(state, entry);
|
||||
break;
|
||||
}
|
||||
report_error("Unknown file %s%s (%p %p)", root, name, dive.get(), trip);
|
||||
report_error("Unknown file %s%s (%p %p)", root, name, dive.get(), trip.get());
|
||||
return GIT_WALK_SKIP;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@
|
|||
#include "file.h"
|
||||
#include "format.h"
|
||||
#include "libdivecomputer.h"
|
||||
#include "owning_ptrs.h"
|
||||
|
||||
/*
|
||||
* Fills a device_data_t structure with known dc data and a descriptor.
|
||||
|
|
|
|||
|
|
@ -9,16 +9,4 @@
|
|||
#include <memory>
|
||||
#include <cstdlib>
|
||||
|
||||
struct dive_trip;
|
||||
|
||||
void free_trip(struct dive_trip *);
|
||||
|
||||
// Classes used to automatically call the appropriate free_*() function for owning pointers that go out of scope.
|
||||
struct TripDeleter {
|
||||
void operator()(dive_trip *t) { free_trip(t); }
|
||||
};
|
||||
|
||||
// Owning pointers to dive, dive_trip, dive_site and event objects.
|
||||
using OwningTripPtr = std::unique_ptr<dive_trip, TripDeleter>;
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1385,13 +1385,13 @@ static void try_to_fill_dive(struct dive *dive, const char *name, char *buf, str
|
|||
}
|
||||
|
||||
/* We're in the top-level trip xml. Try to convert whatever value to a trip value */
|
||||
static void try_to_fill_trip(dive_trip_t *dive_trip, const char *name, char *buf, struct parser_state *state)
|
||||
static void try_to_fill_trip(dive_trip *dive_trip, const char *name, char *buf, struct parser_state *state)
|
||||
{
|
||||
start_match("trip", name, buf);
|
||||
|
||||
if (MATCH("location", utf8_string, &dive_trip->location))
|
||||
if (MATCH("location", utf8_string_std, &dive_trip->location))
|
||||
return;
|
||||
if (MATCH("notes", utf8_string, &dive_trip->notes))
|
||||
if (MATCH("notes", utf8_string_std, &dive_trip->notes))
|
||||
return;
|
||||
|
||||
nonmatch("trip", name, buf);
|
||||
|
|
@ -1528,7 +1528,7 @@ static bool entry(const char *name, char *buf, struct parser_state *state)
|
|||
return true;
|
||||
}
|
||||
if (state->cur_trip) {
|
||||
try_to_fill_trip(state->cur_trip, name, buf, state);
|
||||
try_to_fill_trip(state->cur_trip.get(), name, buf, state);
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -19,14 +19,8 @@
|
|||
#include "device.h"
|
||||
#include "gettext.h"
|
||||
|
||||
parser_state::parser_state()
|
||||
{
|
||||
}
|
||||
|
||||
parser_state::~parser_state()
|
||||
{
|
||||
free_trip(cur_trip);
|
||||
}
|
||||
parser_state::parser_state() = default;
|
||||
parser_state::~parser_state() = default;
|
||||
|
||||
/*
|
||||
* If we don't have an explicit dive computer,
|
||||
|
|
@ -275,7 +269,7 @@ void dive_end(struct parser_state *state)
|
|||
return;
|
||||
if (is_dive(state)) {
|
||||
if (state->cur_trip)
|
||||
add_dive_to_trip(state->cur_dive.get(), state->cur_trip);
|
||||
add_dive_to_trip(state->cur_dive.get(), state->cur_trip.get());
|
||||
record_dive_to_table(state->cur_dive.release(), state->log->dives.get());
|
||||
}
|
||||
state->cur_dive.reset();
|
||||
|
|
@ -289,7 +283,7 @@ void trip_start(struct parser_state *state)
|
|||
if (state->cur_trip)
|
||||
return;
|
||||
dive_end(state);
|
||||
state->cur_trip = alloc_trip();
|
||||
state->cur_trip = std::make_unique<dive_trip>();
|
||||
memset(&state->cur_tm, 0, sizeof(state->cur_tm));
|
||||
}
|
||||
|
||||
|
|
@ -297,8 +291,7 @@ void trip_end(struct parser_state *state)
|
|||
{
|
||||
if (!state->cur_trip)
|
||||
return;
|
||||
insert_trip(state->cur_trip, state->log->trips.get());
|
||||
state->cur_trip = NULL;
|
||||
insert_trip(state->cur_trip.release(), state->log->trips.get());
|
||||
}
|
||||
|
||||
void picture_start(struct parser_state *state)
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ struct parser_state {
|
|||
std::unique_ptr<dive> cur_dive; /* owning */
|
||||
std::unique_ptr<dive_site> cur_dive_site; /* owning */
|
||||
location_t cur_location;
|
||||
struct dive_trip *cur_trip = nullptr; /* owning */
|
||||
std::unique_ptr<dive_trip> cur_trip; /* owning */
|
||||
struct sample *cur_sample = nullptr; /* non-owning */
|
||||
struct picture cur_picture; /* owning */
|
||||
std::unique_ptr<filter_preset> cur_filter; /* owning */
|
||||
|
|
|
|||
|
|
@ -706,11 +706,12 @@ static int save_one_dive(git_repository *repo, struct dir *tree, struct dive *di
|
|||
* similar.
|
||||
*/
|
||||
#define MAXTRIPNAME 15
|
||||
static void create_trip_name(dive_trip_t *trip, struct membuffer *name, struct tm *tm)
|
||||
static void create_trip_name(dive_trip *trip, struct membuffer *name, struct tm *tm)
|
||||
{
|
||||
put_format(name, "%02u-", tm->tm_mday);
|
||||
if (trip->location) {
|
||||
char ascii_loc[MAXTRIPNAME+1], *p = trip->location;
|
||||
if (!trip->location.empty()) {
|
||||
char ascii_loc[MAXTRIPNAME+1];
|
||||
const char *p = trip->location.c_str();
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAXTRIPNAME; ) {
|
||||
|
|
@ -740,7 +741,7 @@ static void create_trip_name(dive_trip_t *trip, struct membuffer *name, struct t
|
|||
put_string(name, "trip");
|
||||
}
|
||||
|
||||
static int save_trip_description(git_repository *repo, struct dir *dir, dive_trip_t *trip, struct tm *tm)
|
||||
static int save_trip_description(git_repository *repo, struct dir *dir, dive_trip *trip, struct tm *tm)
|
||||
{
|
||||
int ret;
|
||||
git_oid blob_id;
|
||||
|
|
@ -751,8 +752,8 @@ static int save_trip_description(git_repository *repo, struct dir *dir, dive_tri
|
|||
put_format(&desc, "time %02u:%02u:%02u\n",
|
||||
tm->tm_hour, tm->tm_min, tm->tm_sec);
|
||||
|
||||
show_utf8(&desc, "location ", trip->location, "\n");
|
||||
show_utf8(&desc, "notes ", trip->notes, "\n");
|
||||
show_utf8(&desc, "location ", trip->location.c_str(), "\n");
|
||||
show_utf8(&desc, "notes ", trip->notes.c_str(), "\n");
|
||||
|
||||
ret = git_blob_create_frombuffer(&blob_id, repo, desc.buffer, desc.len);
|
||||
if (ret)
|
||||
|
|
@ -779,7 +780,7 @@ static void verify_shared_date(timestamp_t when, struct tm *tm)
|
|||
#define MIN_TIMESTAMP (0)
|
||||
#define MAX_TIMESTAMP (0x7fffffffffffffff)
|
||||
|
||||
static int save_one_trip(git_repository *repo, struct dir *tree, dive_trip_t *trip, struct tm *tm, bool cached_ok)
|
||||
static int save_one_trip(git_repository *repo, struct dir *tree, dive_trip *trip, struct tm *tm, bool cached_ok)
|
||||
{
|
||||
int i;
|
||||
struct dive *dive;
|
||||
|
|
@ -982,7 +983,7 @@ static int create_git_tree(git_repository *repo, struct dir *root, bool select_o
|
|||
{
|
||||
int i;
|
||||
struct dive *dive;
|
||||
dive_trip_t *trip;
|
||||
dive_trip *trip;
|
||||
|
||||
git_storage_update_progress(translate("gettextFromC", "Start saving data"));
|
||||
save_settings(repo, root);
|
||||
|
|
@ -1104,7 +1105,7 @@ static void create_commit_message(struct membuffer *msg, bool create_empty)
|
|||
} else if (!changes_made.empty()) {
|
||||
put_format(msg, "Changes made: \n\n%s\n", changes_made.c_str());
|
||||
} else if (dive) {
|
||||
dive_trip_t *trip = dive->divetrip;
|
||||
dive_trip *trip = dive->divetrip;
|
||||
std::string location = get_dive_location(dive);
|
||||
if (location.empty())
|
||||
location = "no location";
|
||||
|
|
@ -1114,8 +1115,8 @@ static void create_commit_message(struct membuffer *msg, bool create_empty)
|
|||
nr = dive->number;
|
||||
|
||||
put_format(msg, "dive %d: %s", nr, location.c_str());
|
||||
if (trip && !empty_string(trip->location) && location != trip->location)
|
||||
put_format(msg, " (%s)", trip->location);
|
||||
if (trip && !trip->location.empty() && location != trip->location)
|
||||
put_format(msg, " (%s)", trip->location.c_str());
|
||||
put_format(msg, "\n");
|
||||
for (auto &dc: dive->dcs) {
|
||||
if (!dc.model.empty()) {
|
||||
|
|
|
|||
|
|
@ -405,7 +405,7 @@ static void write_no_trip(struct membuffer *b, int *dive_no, bool selected_only,
|
|||
put_format(b, "]}\n\n");
|
||||
}
|
||||
|
||||
static void write_trip(struct membuffer *b, dive_trip_t *trip, int *dive_no, bool selected_only, const char *photos_dir, const bool list_only, char *sep)
|
||||
static void write_trip(struct membuffer *b, dive_trip *trip, int *dive_no, bool selected_only, const char *photos_dir, const bool list_only, char *sep)
|
||||
{
|
||||
const struct dive *dive;
|
||||
const char *separator = "";
|
||||
|
|
@ -421,7 +421,7 @@ static void write_trip(struct membuffer *b, dive_trip_t *trip, int *dive_no, boo
|
|||
found_sel_dive = 1;
|
||||
put_format(b, "%c {", *sep);
|
||||
(*sep) = ',';
|
||||
write_attribute(b, "name", trip->location, ", ");
|
||||
write_attribute(b, "name", trip->location.c_str(), ", ");
|
||||
put_format(b, "\"dives\":[");
|
||||
}
|
||||
put_string(b, separator);
|
||||
|
|
@ -438,7 +438,7 @@ static void write_trips(struct membuffer *b, const char *photos_dir, bool select
|
|||
{
|
||||
int i, dive_no = 0;
|
||||
const struct dive *dive;
|
||||
dive_trip_t *trip;
|
||||
dive_trip *trip;
|
||||
char sep_ = ' ';
|
||||
char *sep = &sep_;
|
||||
|
||||
|
|
|
|||
|
|
@ -543,16 +543,16 @@ int save_dive(FILE *f, struct dive *dive, bool anonymize)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static void save_trip(struct membuffer *b, dive_trip_t *trip, bool anonymize)
|
||||
static void save_trip(struct membuffer *b, dive_trip *trip, bool anonymize)
|
||||
{
|
||||
int i;
|
||||
struct dive *dive;
|
||||
|
||||
put_format(b, "<trip");
|
||||
show_date(b, trip_date(trip));
|
||||
show_utf8(b, trip->location, " location=\'", "\'", 1);
|
||||
show_utf8(b, trip->location.c_str(), " location=\'", "\'", 1);
|
||||
put_format(b, ">\n");
|
||||
show_utf8(b, trip->notes, "<notes>", "</notes>\n", 0);
|
||||
show_utf8(b, trip->notes.c_str(), "<notes>", "</notes>\n", 0);
|
||||
|
||||
/*
|
||||
* Incredibly cheesy: we want to save the dives sorted, and they
|
||||
|
|
@ -642,7 +642,7 @@ static void save_dives_buffer(struct membuffer *b, bool select_only, bool anonym
|
|||
{
|
||||
int i;
|
||||
struct dive *dive;
|
||||
dive_trip_t *trip;
|
||||
dive_trip *trip;
|
||||
|
||||
put_format(b, "<divelog program='subsurface' version='%d'>\n<settings>\n", DATAFORMAT_VERSION);
|
||||
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ stats_summary calculate_stats_summary(bool selected_only)
|
|||
int current_year = -1;
|
||||
int current_month = 0;
|
||||
int prev_month = 0, prev_year = 0;
|
||||
dive_trip_t *trip_ptr = nullptr;
|
||||
dive_trip *trip_ptr = nullptr;
|
||||
|
||||
stats_summary out;
|
||||
|
||||
|
|
|
|||
|
|
@ -309,7 +309,7 @@ QString formatTripTitle(const dive_trip *trip)
|
|||
|
||||
QDateTime localTime = timestampToDateTime(when);
|
||||
|
||||
QString prefix = !empty_string(trip->location) ? QString(trip->location) + ", " : QString();
|
||||
QString prefix = !trip->location.empty() ? QString::fromStdString(trip->location) + ", " : QString();
|
||||
if (getday)
|
||||
return prefix + loc.toString(localTime, prefs.date_format);
|
||||
else
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
#ifdef DEBUG_TRIP
|
||||
void dump_trip_list()
|
||||
{
|
||||
dive_trip_t *trip;
|
||||
dive_trip *trip;
|
||||
int i = 0;
|
||||
timestamp_t last_time = 0;
|
||||
|
||||
|
|
@ -24,7 +24,7 @@ void dump_trip_list()
|
|||
printf("\n\ntrip_table OUT OF ORDER!!!\n\n\n");
|
||||
printf("%s trip %d to \"%s\" on %04u-%02u-%02u %02u:%02u:%02u (%d dives - %p)\n",
|
||||
trip->autogen ? "autogen " : "",
|
||||
i + 1, trip->location,
|
||||
i + 1, trip->location.c_str(),
|
||||
tm.tm_year, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
|
||||
trip->dives.nr, trip);
|
||||
last_time = trip_date(trip);
|
||||
|
|
@ -33,15 +33,19 @@ void dump_trip_list()
|
|||
}
|
||||
#endif
|
||||
|
||||
/* free resources associated with a trip structure */
|
||||
void free_trip(dive_trip_t *trip)
|
||||
dive_trip::dive_trip() : id(dive_getUniqID())
|
||||
{
|
||||
if (trip) {
|
||||
free(trip->location);
|
||||
free(trip->notes);
|
||||
free(trip->dives.dives);
|
||||
free(trip);
|
||||
}
|
||||
}
|
||||
|
||||
dive_trip::~dive_trip()
|
||||
{
|
||||
free(dives.dives);
|
||||
}
|
||||
|
||||
/* free resources associated with a trip structure */
|
||||
void free_trip(dive_trip *trip)
|
||||
{
|
||||
delete trip;
|
||||
}
|
||||
|
||||
/* Trip table functions */
|
||||
|
|
@ -87,7 +91,7 @@ bool is_trip_before_after(const struct dive *dive, bool before)
|
|||
|
||||
/* Add dive to a trip. Caller is responsible for removing dive
|
||||
* from trip beforehand. */
|
||||
void add_dive_to_trip(struct dive *dive, dive_trip_t *trip)
|
||||
void add_dive_to_trip(struct dive *dive, dive_trip *trip)
|
||||
{
|
||||
if (dive->divetrip == trip)
|
||||
return;
|
||||
|
|
@ -103,7 +107,7 @@ void add_dive_to_trip(struct dive *dive, dive_trip_t *trip)
|
|||
*/
|
||||
struct dive_trip *unregister_dive_from_trip(struct dive *dive)
|
||||
{
|
||||
dive_trip_t *trip = dive->divetrip;
|
||||
dive_trip *trip = dive->divetrip;
|
||||
|
||||
if (!trip)
|
||||
return NULL;
|
||||
|
|
@ -113,7 +117,7 @@ struct dive_trip *unregister_dive_from_trip(struct dive *dive)
|
|||
return trip;
|
||||
}
|
||||
|
||||
static void delete_trip(dive_trip_t *trip, struct trip_table *trip_table_arg)
|
||||
static void delete_trip(dive_trip *trip, struct trip_table *trip_table_arg)
|
||||
{
|
||||
remove_trip(trip, trip_table_arg);
|
||||
free_trip(trip);
|
||||
|
|
@ -126,15 +130,13 @@ void remove_dive_from_trip(struct dive *dive, struct trip_table *trip_table_arg)
|
|||
delete_trip(trip, trip_table_arg);
|
||||
}
|
||||
|
||||
dive_trip_t *alloc_trip()
|
||||
dive_trip *alloc_trip()
|
||||
{
|
||||
dive_trip_t *res = (dive_trip_t *)calloc(1, sizeof(dive_trip_t));
|
||||
res->id = dive_getUniqID();
|
||||
return res;
|
||||
return new dive_trip;
|
||||
}
|
||||
|
||||
/* insert the trip into the trip table */
|
||||
void insert_trip(dive_trip_t *dive_trip, struct trip_table *trip_table_arg)
|
||||
void insert_trip(struct dive_trip *dive_trip, struct trip_table *trip_table_arg)
|
||||
{
|
||||
int idx = trip_table_get_insertion_index(trip_table_arg, dive_trip);
|
||||
add_to_trip_table(trip_table_arg, idx, dive_trip);
|
||||
|
|
@ -143,12 +145,12 @@ void insert_trip(dive_trip_t *dive_trip, struct trip_table *trip_table_arg)
|
|||
#endif
|
||||
}
|
||||
|
||||
dive_trip_t *create_trip_from_dive(struct dive *dive)
|
||||
dive_trip *create_trip_from_dive(struct dive *dive)
|
||||
{
|
||||
dive_trip_t *trip;
|
||||
dive_trip *trip;
|
||||
|
||||
trip = alloc_trip();
|
||||
trip->location = copy_string(get_dive_location(dive).c_str());
|
||||
trip->location = get_dive_location(dive);
|
||||
|
||||
return trip;
|
||||
}
|
||||
|
|
@ -163,10 +165,10 @@ dive_trip_t *create_trip_from_dive(struct dive *dive)
|
|||
* exist, allocate a new trip. The bool "*allocated" is set to true
|
||||
* if a new trip was allocated.
|
||||
*/
|
||||
dive_trip_t *get_trip_for_new_dive(struct dive *new_dive, bool *allocated)
|
||||
dive_trip *get_trip_for_new_dive(struct dive *new_dive, bool *allocated)
|
||||
{
|
||||
struct dive *d;
|
||||
dive_trip_t *trip;
|
||||
dive_trip *trip;
|
||||
int i;
|
||||
|
||||
/* Find dive that is within TRIP_THRESHOLD of current dive */
|
||||
|
|
@ -190,7 +192,7 @@ dive_trip_t *get_trip_for_new_dive(struct dive *new_dive, bool *allocated)
|
|||
}
|
||||
|
||||
/* lookup of trip in main trip_table based on its id */
|
||||
dive_trip_t *get_trip_by_uniq_id(int tripId)
|
||||
dive_trip *get_trip_by_uniq_id(int tripId)
|
||||
{
|
||||
for (int i = 0; i < divelog.trips->nr; i++) {
|
||||
if (divelog.trips->trips[i]->id == tripId)
|
||||
|
|
@ -221,7 +223,7 @@ bool trips_overlap(const struct dive_trip *t1, const struct dive_trip *t2)
|
|||
* manually injects the new trips. If there are no dives to be autogrouped,
|
||||
* return NULL.
|
||||
*/
|
||||
dive_trip_t *get_dives_to_autogroup(struct dive_table *table, int start, int *from, int *to, bool *allocated)
|
||||
dive_trip *get_dives_to_autogroup(struct dive_table *table, int start, int *from, int *to, bool *allocated)
|
||||
{
|
||||
int i;
|
||||
struct dive *lastdive = NULL;
|
||||
|
|
@ -231,7 +233,7 @@ dive_trip_t *get_dives_to_autogroup(struct dive_table *table, int start, int *fr
|
|||
*/
|
||||
for (i = start; i < table->nr; i++) {
|
||||
struct dive *dive = table->dives[i];
|
||||
dive_trip_t *trip;
|
||||
dive_trip *trip;
|
||||
|
||||
if (dive->divetrip) {
|
||||
lastdive = dive;
|
||||
|
|
@ -265,9 +267,8 @@ dive_trip_t *get_dives_to_autogroup(struct dive_table *table, int start, int *fr
|
|||
if (dive->divetrip || dive->notrip ||
|
||||
dive->when >= lastdive->when + TRIP_THRESHOLD)
|
||||
break;
|
||||
std::string location = get_dive_location(dive);
|
||||
if (!location.empty() && !trip->location)
|
||||
trip->location = copy_string(get_dive_location(dive).c_str());
|
||||
if (trip->location.empty())
|
||||
trip->location = get_dive_location(dive);
|
||||
lastdive = dive;
|
||||
}
|
||||
return trip;
|
||||
|
|
@ -277,21 +278,21 @@ dive_trip_t *get_dives_to_autogroup(struct dive_table *table, int start, int *fr
|
|||
return NULL;
|
||||
}
|
||||
|
||||
/* Out of two strings, copy the string that is not empty (if any). */
|
||||
static char *copy_non_empty_string(const char *a, const char *b)
|
||||
/* Out of two strings, get the string that is not empty (if any). */
|
||||
static std::string non_empty_string(const std::string &a, const std::string &b)
|
||||
{
|
||||
return copy_string(empty_string(b) ? a : b);
|
||||
return b.empty() ? a : b;
|
||||
}
|
||||
|
||||
/* This combines the information of two trips, generating a
|
||||
* new trip. To support undo, we have to preserve the old trips. */
|
||||
dive_trip_t *combine_trips(struct dive_trip *trip_a, struct dive_trip *trip_b)
|
||||
dive_trip *combine_trips(struct dive_trip *trip_a, struct dive_trip *trip_b)
|
||||
{
|
||||
dive_trip_t *trip;
|
||||
dive_trip *trip;
|
||||
|
||||
trip = alloc_trip();
|
||||
trip->location = copy_non_empty_string(trip_a->location, trip_b->location);
|
||||
trip->notes = copy_non_empty_string(trip_a->notes, trip_b->notes);
|
||||
trip->location = non_empty_string(trip_a->location, trip_b->location);
|
||||
trip->notes = non_empty_string(trip_a->notes, trip_b->notes);
|
||||
|
||||
return trip;
|
||||
}
|
||||
|
|
|
|||
41
core/trip.h
41
core/trip.h
|
|
@ -4,17 +4,22 @@
|
|||
|
||||
#include "divelist.h"
|
||||
|
||||
typedef struct dive_trip
|
||||
#include <string>
|
||||
|
||||
struct dive_trip
|
||||
{
|
||||
char *location;
|
||||
char *notes;
|
||||
struct dive_table dives;
|
||||
std::string location;
|
||||
std::string notes;
|
||||
struct dive_table dives = {};
|
||||
int id; /* unique ID for this trip: used to pass trips through QML. */
|
||||
/* Used by the io-routines to mark trips that have already been written. */
|
||||
bool saved;
|
||||
bool autogen;
|
||||
bool selected;
|
||||
} dive_trip_t;
|
||||
bool saved = false;
|
||||
bool autogen = false;
|
||||
bool selected = false;
|
||||
|
||||
dive_trip();
|
||||
~dive_trip();
|
||||
};
|
||||
|
||||
typedef struct trip_table {
|
||||
int nr, allocated;
|
||||
|
|
@ -23,13 +28,13 @@ typedef struct trip_table {
|
|||
|
||||
static const trip_table_t empty_trip_table = { 0, 0, (struct dive_trip **)0 };
|
||||
|
||||
extern void add_dive_to_trip(struct dive *, dive_trip_t *);
|
||||
extern void add_dive_to_trip(struct dive *, dive_trip *);
|
||||
extern struct dive_trip *unregister_dive_from_trip(struct dive *dive);
|
||||
extern void remove_dive_from_trip(struct dive *dive, struct trip_table *trip_table_arg);
|
||||
|
||||
extern void insert_trip(dive_trip_t *trip, struct trip_table *trip_table_arg);
|
||||
extern int remove_trip(const dive_trip_t *trip, struct trip_table *trip_table_arg);
|
||||
extern void free_trip(dive_trip_t *trip);
|
||||
extern void insert_trip(dive_trip *trip, struct trip_table *trip_table_arg);
|
||||
extern int remove_trip(const dive_trip *trip, struct trip_table *trip_table_arg);
|
||||
extern void free_trip(dive_trip *trip);
|
||||
extern timestamp_t trip_date(const struct dive_trip *trip);
|
||||
extern timestamp_t trip_enddate(const struct dive_trip *trip);
|
||||
|
||||
|
|
@ -37,14 +42,14 @@ extern bool trip_less_than(const struct dive_trip *a, const struct dive_trip *b)
|
|||
extern int comp_trips(const struct dive_trip *a, const struct dive_trip *b);
|
||||
extern void sort_trip_table(struct trip_table *table);
|
||||
|
||||
extern dive_trip_t *alloc_trip();
|
||||
extern dive_trip_t *create_trip_from_dive(struct dive *dive);
|
||||
extern dive_trip_t *get_dives_to_autogroup(struct dive_table *table, int start, int *from, int *to, bool *allocated);
|
||||
extern dive_trip_t *get_trip_for_new_dive(struct dive *new_dive, bool *allocated);
|
||||
extern dive_trip_t *get_trip_by_uniq_id(int tripId);
|
||||
extern dive_trip *alloc_trip();
|
||||
extern dive_trip *create_trip_from_dive(struct dive *dive);
|
||||
extern dive_trip *get_dives_to_autogroup(struct dive_table *table, int start, int *from, int *to, bool *allocated);
|
||||
extern dive_trip *get_trip_for_new_dive(struct dive *new_dive, bool *allocated);
|
||||
extern dive_trip *get_trip_by_uniq_id(int tripId);
|
||||
extern bool trips_overlap(const struct dive_trip *t1, const struct dive_trip *t2);
|
||||
|
||||
extern dive_trip_t *combine_trips(struct dive_trip *trip_a, struct dive_trip *trip_b);
|
||||
extern dive_trip *combine_trips(struct dive_trip *trip_a, struct dive_trip *trip_b);
|
||||
extern bool is_trip_before_after(const struct dive *dive, bool before);
|
||||
extern bool trip_is_single_day(const struct dive_trip *trip);
|
||||
extern int trip_shown_dives(const struct dive_trip *trip);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue