core: turn C dive-table into an owning table

This is a humongous commit, because it touches all parts of the
code. It removes the last user of our horrible TABLE macros, which
simulate std::vector<> in a very clumsy way.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-06-07 10:25:09 +02:00 committed by bstoeger
parent f00c30ad4a
commit b95ac3f79c
73 changed files with 1030 additions and 1230 deletions

View file

@ -43,13 +43,13 @@ static void copy_image_and_overwrite(const std::string &cfileName, const std::st
report_info("copy of %s to %s failed", cfileName.c_str(), newName.c_str());
}
static void save_photos(struct membuffer *b, const char *photos_dir, const struct dive *dive)
static void save_photos(struct membuffer *b, const char *photos_dir, const struct dive &dive)
{
if (dive->pictures.empty())
if (dive.pictures.empty())
return;
const char *separator = "\"photos\":[";
for (auto &picture: dive->pictures) {
for (auto &picture: dive.pictures) {
put_string(b, separator);
separator = ", ";
std::string fname = get_file_name(local_file_path(picture));
@ -61,11 +61,11 @@ static void save_photos(struct membuffer *b, const char *photos_dir, const struc
put_string(b, "],");
}
static void write_divecomputers(struct membuffer *b, const struct dive *dive)
static void write_divecomputers(struct membuffer *b, const struct dive &dive)
{
put_string(b, "\"divecomputers\":[");
const char *separator = "";
for (auto &dc: dive->dcs) {
for (auto &dc: dive.dcs) {
put_string(b, separator);
separator = ", ";
put_format(b, "{");
@ -83,17 +83,17 @@ static void write_divecomputers(struct membuffer *b, const struct dive *dive)
put_string(b, "],");
}
static void write_dive_status(struct membuffer *b, const struct dive *dive)
static void write_dive_status(struct membuffer *b, const struct dive &dive)
{
put_format(b, "\"sac\":\"%d\",", dive->sac);
put_format(b, "\"otu\":\"%d\",", dive->otu);
put_format(b, "\"cns\":\"%d\",", dive->cns);
put_format(b, "\"sac\":\"%d\",", dive.sac);
put_format(b, "\"otu\":\"%d\",", dive.otu);
put_format(b, "\"cns\":\"%d\",", dive.cns);
}
static void put_HTML_bookmarks(struct membuffer *b, const struct dive *dive)
static void put_HTML_bookmarks(struct membuffer *b, const struct dive &dive)
{
const char *separator = "\"events\":[";
for (const auto &ev: dive->dcs[0].events) {
for (const auto &ev: dive.dcs[0].events) {
put_string(b, separator);
separator = ", ";
put_string(b, "{\"name\":\"");
@ -106,13 +106,13 @@ static void put_HTML_bookmarks(struct membuffer *b, const struct dive *dive)
put_string(b, "],");
}
static void put_weightsystem_HTML(struct membuffer *b, const struct dive *dive)
static void put_weightsystem_HTML(struct membuffer *b, const struct dive &dive)
{
put_string(b, "\"Weights\":[");
const char *separator = "";
for (auto &ws: dive->weightsystems) {
for (auto &ws: dive.weightsystems) {
int grams = ws.weight.grams;
put_string(b, separator);
@ -125,14 +125,14 @@ static void put_weightsystem_HTML(struct membuffer *b, const struct dive *dive)
put_string(b, "],");
}
static void put_cylinder_HTML(struct membuffer *b, const struct dive *dive)
static void put_cylinder_HTML(struct membuffer *b, const struct dive &dive)
{
const char *separator = "\"Cylinders\":[";
if (dive->cylinders.empty())
if (dive.cylinders.empty())
put_string(b, separator);
for (auto &cyl: dive->cylinders) {
for (auto &cyl: dive.cylinders) {
put_format(b, "%s{", separator);
separator = ", ";
write_attribute(b, "Type", cyl.type.description.c_str(), ", ");
@ -172,25 +172,25 @@ static void put_cylinder_HTML(struct membuffer *b, const struct dive *dive)
}
static void put_HTML_samples(struct membuffer *b, const struct dive *dive)
static void put_HTML_samples(struct membuffer *b, const struct dive &dive)
{
put_format(b, "\"maxdepth\":%d,", dive->dcs[0].maxdepth.mm);
put_format(b, "\"duration\":%d,", dive->dcs[0].duration.seconds);
put_format(b, "\"maxdepth\":%d,", dive.dcs[0].maxdepth.mm);
put_format(b, "\"duration\":%d,", dive.dcs[0].duration.seconds);
if (dive->dcs[0].samples.empty())
if (dive.dcs[0].samples.empty())
return;
const char *separator = "\"samples\":[";
for (auto &s: dive->dcs[0].samples) {
for (auto &s: dive.dcs[0].samples) {
put_format(b, "%s[%d,%d,%d,%d]", separator, s.time.seconds, s.depth.mm, s.pressure[0].mbar, s.temperature.mkelvin);
separator = ", ";
}
put_string(b, "],");
}
static void put_HTML_coordinates(struct membuffer *b, const struct dive *dive)
static void put_HTML_coordinates(struct membuffer *b, const struct dive &dive)
{
struct dive_site *ds = get_dive_site_for_dive(dive);
struct dive_site *ds = get_dive_site_for_dive(&dive);
if (!ds)
return;
degrees_t latitude = ds->location.lat;
@ -206,10 +206,10 @@ static void put_HTML_coordinates(struct membuffer *b, const struct dive *dive)
put_string(b, "},");
}
void put_HTML_date(struct membuffer *b, const struct dive *dive, const char *pre, const char *post)
void put_HTML_date(struct membuffer *b, const struct dive &dive, const char *pre, const char *post)
{
struct tm tm;
utc_mkdate(dive->when, &tm);
utc_mkdate(dive.when, &tm);
put_format(b, "%s%04u-%02u-%02u%s", pre, tm.tm_year, tm.tm_mon + 1, tm.tm_mday, post);
}
@ -219,11 +219,11 @@ void put_HTML_quoted(struct membuffer *b, const char *text)
put_quoted(b, text, is_attribute, is_html);
}
void put_HTML_notes(struct membuffer *b, const struct dive *dive, const char *pre, const char *post)
void put_HTML_notes(struct membuffer *b, const struct dive &dive, const char *pre, const char *post)
{
put_string(b, pre);
if (!dive->notes.empty())
put_HTML_quoted(b, dive->notes.c_str());
if (!dive.notes.empty())
put_HTML_quoted(b, dive.notes.c_str());
else
put_string(b, "--");
put_string(b, post);
@ -263,24 +263,24 @@ void put_HTML_weight_units(struct membuffer *b, unsigned int grams, const char *
put_format(b, "%s%.1f %s%s", pre, value, unit, post);
}
void put_HTML_time(struct membuffer *b, const struct dive *dive, const char *pre, const char *post)
void put_HTML_time(struct membuffer *b, const struct dive &dive, const char *pre, const char *post)
{
struct tm tm;
utc_mkdate(dive->when, &tm);
utc_mkdate(dive.when, &tm);
put_format(b, "%s%02u:%02u:%02u%s", pre, tm.tm_hour, tm.tm_min, tm.tm_sec, post);
}
void put_HTML_depth(struct membuffer *b, const struct dive *dive, const char *pre, const char *post)
void put_HTML_depth(struct membuffer *b, const struct dive &dive, const char *pre, const char *post)
{
const char *unit;
double value;
const struct units *units_p = get_units();
if (!dive->maxdepth.mm) {
if (!dive.maxdepth.mm) {
put_format(b, "%s--%s", pre, post);
return;
}
value = get_depth_units(dive->maxdepth.mm, NULL, &unit);
value = get_depth_units(dive.maxdepth.mm, NULL, &unit);
switch (units_p->length) {
case units::METERS:
@ -293,41 +293,41 @@ void put_HTML_depth(struct membuffer *b, const struct dive *dive, const char *pr
}
}
void put_HTML_airtemp(struct membuffer *b, const struct dive *dive, const char *pre, const char *post)
void put_HTML_airtemp(struct membuffer *b, const struct dive &dive, const char *pre, const char *post)
{
const char *unit;
double value;
if (!dive->airtemp.mkelvin) {
if (!dive.airtemp.mkelvin) {
put_format(b, "%s--%s", pre, post);
return;
}
value = get_temp_units(dive->airtemp.mkelvin, &unit);
value = get_temp_units(dive.airtemp.mkelvin, &unit);
put_format(b, "%s%.1f %s%s", pre, value, unit, post);
}
void put_HTML_watertemp(struct membuffer *b, const struct dive *dive, const char *pre, const char *post)
void put_HTML_watertemp(struct membuffer *b, const struct dive &dive, const char *pre, const char *post)
{
const char *unit;
double value;
if (!dive->watertemp.mkelvin) {
if (!dive.watertemp.mkelvin) {
put_format(b, "%s--%s", pre, post);
return;
}
value = get_temp_units(dive->watertemp.mkelvin, &unit);
value = get_temp_units(dive.watertemp.mkelvin, &unit);
put_format(b, "%s%.1f %s%s", pre, value, unit, post);
}
static void put_HTML_tags(struct membuffer *b, const struct dive *dive, const char *pre, const char *post)
static void put_HTML_tags(struct membuffer *b, const struct dive &dive, const char *pre, const char *post)
{
put_string(b, pre);
if (dive->tags.empty())
if (dive.tags.empty())
put_string(b, "[\"--\"");
const char *separator = "[";
for (const divetag *tag: dive->tags) {
for (const divetag *tag: dive.tags) {
put_format(b, "%s\"", separator);
separator = ", ";
put_HTML_quoted(b, tag->name.c_str());
@ -338,30 +338,30 @@ static void put_HTML_tags(struct membuffer *b, const struct dive *dive, const ch
}
/* if exporting list_only mode, we neglect exporting the samples, bookmarks and cylinders */
static void write_one_dive(struct membuffer *b, const struct dive *dive, const char *photos_dir, int *dive_no, bool list_only)
static void write_one_dive(struct membuffer *b, const struct dive &dive, const char *photos_dir, int *dive_no, bool list_only)
{
put_string(b, "{");
put_format(b, "\"number\":%d,", *dive_no);
put_format(b, "\"subsurface_number\":%d,", dive->number);
put_format(b, "\"subsurface_number\":%d,", dive.number);
put_HTML_date(b, dive, "\"date\":\"", "\",");
put_HTML_time(b, dive, "\"time\":\"", "\",");
write_attribute(b, "location", get_dive_location(dive).c_str(), ", ");
write_attribute(b, "location", get_dive_location(&dive).c_str(), ", ");
put_HTML_coordinates(b, dive);
put_format(b, "\"rating\":%d,", dive->rating);
put_format(b, "\"visibility\":%d,", dive->visibility);
put_format(b, "\"current\":%d,", dive->current);
put_format(b, "\"wavesize\":%d,", dive->wavesize);
put_format(b, "\"surge\":%d,", dive->surge);
put_format(b, "\"chill\":%d,", dive->chill);
put_format(b, "\"rating\":%d,", dive.rating);
put_format(b, "\"visibility\":%d,", dive.visibility);
put_format(b, "\"current\":%d,", dive.current);
put_format(b, "\"wavesize\":%d,", dive.wavesize);
put_format(b, "\"surge\":%d,", dive.surge);
put_format(b, "\"chill\":%d,", dive.chill);
put_format(b, "\"dive_duration\":\"%u:%02u min\",",
FRACTION_TUPLE(dive->duration.seconds, 60));
FRACTION_TUPLE(dive.duration.seconds, 60));
put_string(b, "\"temperature\":{");
put_HTML_airtemp(b, dive, "\"air\":\"", "\",");
put_HTML_watertemp(b, dive, "\"water\":\"", "\"");
put_string(b, " },");
write_attribute(b, "buddy", dive->buddy.c_str(), ", ");
write_attribute(b, "diveguide", dive->diveguide.c_str(), ", ");
write_attribute(b, "suit", dive->suit.c_str(), ", ");
write_attribute(b, "buddy", dive.buddy.c_str(), ", ");
write_attribute(b, "diveguide", dive.diveguide.c_str(), ", ");
write_attribute(b, "suit", dive.suit.c_str(), ", ");
put_HTML_tags(b, dive, "\"tags\":", ",");
if (!list_only) {
put_cylinder_HTML(b, dive);
@ -380,12 +380,10 @@ static void write_one_dive(struct membuffer *b, const struct dive *dive, const c
static void write_no_trip(struct membuffer *b, int *dive_no, bool selected_only, const char *photos_dir, const bool list_only, char *sep)
{
int i;
const struct dive *dive;
const char *separator = "";
bool found_sel_dive = 0;
for_each_dive (i, dive) {
for (auto &dive: divelog.dives) {
// write dive if it doesn't belong to any trip and the dive is selected
// or we are in exporting all dives mode.
if (!dive->divetrip && (dive->selected || !selected_only)) {
@ -398,7 +396,7 @@ static void write_no_trip(struct membuffer *b, int *dive_no, bool selected_only,
}
put_string(b, separator);
separator = ", ";
write_one_dive(b, dive, photos_dir, dive_no, list_only);
write_one_dive(b, *dive, photos_dir, dive_no, list_only);
}
}
if (found_sel_dive)
@ -424,7 +422,7 @@ static void write_trip(struct membuffer *b, dive_trip *trip, int *dive_no, bool
}
put_string(b, separator);
separator = ", ";
write_one_dive(b, dive, photos_dir, dive_no, list_only);
write_one_dive(b, *dive, photos_dir, dive_no, list_only);
}
// close the trip object if contain dives.
@ -434,15 +432,14 @@ static void write_trip(struct membuffer *b, dive_trip *trip, int *dive_no, bool
static void write_trips(struct membuffer *b, const char *photos_dir, bool selected_only, const bool list_only)
{
int i, dive_no = 0;
const struct dive *dive;
int dive_no = 0;
char sep_ = ' ';
char *sep = &sep_;
for (auto &trip: *divelog.trips)
trip->saved = 0;
for_each_dive (i, dive) {
for (auto &dive: divelog.dives) {
dive_trip *trip = dive->divetrip;
/*Continue if the dive have no trips or we have seen this trip before*/