Core: dynamically resize weight table

Replace the fixed-size weightsystem table by a dynamically
relocated table. Reuse the table-macros used in other parts
of the code.

The table stores weightsystem entries, not pointers to
weightsystems. Thus, ownership of the description string is
taken when adding a weightsystem. An extra function adds
a cloned weightsystem at the end of the table.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-06-26 17:21:03 +02:00 committed by Dirk Hohndel
parent efdb3503ea
commit a5e7f4253a
25 changed files with 182 additions and 154 deletions

View file

@ -355,8 +355,8 @@ static void free_dive_structures(struct dive *d)
STRUCTURED_LIST_FREE(struct picture, d->picture_list, free_picture); STRUCTURED_LIST_FREE(struct picture, d->picture_list, free_picture);
for (int i = 0; i < MAX_CYLINDERS; i++) for (int i = 0; i < MAX_CYLINDERS; i++)
free((void *)d->cylinder[i].type.description); free((void *)d->cylinder[i].type.description);
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++) clear_weightsystem_table(&d->weightsystems);
free((void *)d->weightsystem[i].description); free(d->weightsystems.weightsystems);
} }
void free_dive(struct dive *d) void free_dive(struct dive *d)
@ -387,6 +387,7 @@ static void copy_dive_nodc(const struct dive *s, struct dive *d)
* relevant components that are referenced through pointers, * relevant components that are referenced through pointers,
* so all the strings and the structured lists */ * so all the strings and the structured lists */
*d = *s; *d = *s;
memset(&d->weightsystems, 0, sizeof(d->weightsystems));
invalidate_dive_cache(d); invalidate_dive_cache(d);
d->buddy = copy_string(s->buddy); d->buddy = copy_string(s->buddy);
d->divemaster = copy_string(s->divemaster); d->divemaster = copy_string(s->divemaster);
@ -394,8 +395,7 @@ static void copy_dive_nodc(const struct dive *s, struct dive *d)
d->suit = copy_string(s->suit); d->suit = copy_string(s->suit);
for (int i = 0; i < MAX_CYLINDERS; i++) for (int i = 0; i < MAX_CYLINDERS; i++)
d->cylinder[i].type.description = copy_string(s->cylinder[i].type.description); d->cylinder[i].type.description = copy_string(s->cylinder[i].type.description);
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++) copy_weights(&s->weightsystems, &d->weightsystems);
d->weightsystem[i].description = copy_string(s->weightsystem[i].description);
STRUCTURED_LIST_COPY(struct picture, s->picture_list, d->picture_list, copy_pl); STRUCTURED_LIST_COPY(struct picture, s->picture_list, d->picture_list, copy_pl);
d->tag_list = taglist_copy(s->tag_list); d->tag_list = taglist_copy(s->tag_list);
} }
@ -432,15 +432,6 @@ struct dive *move_dive(struct dive *s)
if (what._component) \ if (what._component) \
d->_component = copy_string(s->_component) d->_component = copy_string(s->_component)
void copy_weights(const struct dive *s, struct dive *d)
{
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++) {
free((void *)d->weightsystem[i].description);
d->weightsystem[i] = s->weightsystem[i];
d->weightsystem[i].description = copy_string(s->weightsystem[i].description);
}
}
// copy elements, depending on bits in what that are set // copy elements, depending on bits in what that are set
void selective_copy_dive(const struct dive *s, struct dive *d, struct dive_components what, bool clear) void selective_copy_dive(const struct dive *s, struct dive *d, struct dive_components what, bool clear)
{ {
@ -463,7 +454,7 @@ void selective_copy_dive(const struct dive *s, struct dive *d, struct dive_compo
if (what.cylinders) if (what.cylinders)
copy_cylinders(s, d, false); copy_cylinders(s, d, false);
if (what.weights) if (what.weights)
copy_weights(s, d); copy_weights(&s->weightsystems, &d->weightsystems);
} }
#undef CONDITIONAL_COPY_STRING #undef CONDITIONAL_COPY_STRING
@ -515,14 +506,7 @@ int nr_cylinders(const struct dive *dive)
int nr_weightsystems(const struct dive *dive) int nr_weightsystems(const struct dive *dive)
{ {
int nr; return dive->weightsystems.nr;
for (nr = MAX_WEIGHTSYSTEMS; nr; --nr) {
const weightsystem_t *ws = dive->weightsystem + nr - 1;
if (!weightsystem_none(ws))
break;
}
return nr;
} }
/* copy the equipment data part of the cylinders */ /* copy the equipment data part of the cylinders */
@ -1557,8 +1541,8 @@ struct dive *fixup_dive(struct dive *dive)
cyl->end.mbar = 0; cyl->end.mbar = 0;
} }
update_cylinder_related_info(dive); update_cylinder_related_info(dive);
for (i = 0; i < MAX_WEIGHTSYSTEMS; i++) { for (i = 0; i < dive->weightsystems.nr; i++) {
weightsystem_t *ws = dive->weightsystem + i; weightsystem_t *ws = &dive->weightsystems.weightsystems[i];
add_weightsystem_description(ws); add_weightsystem_description(ws);
} }
/* we should always have a uniq ID as that gets assigned during alloc_dive(), /* we should always have a uniq ID as that gets assigned during alloc_dive(),
@ -1922,14 +1906,6 @@ static void merge_events(struct dive *d, struct divecomputer *res,
add_initial_gaschange(d, res, offset, cylinders_map2[0]); add_initial_gaschange(d, res, offset, cylinders_map2[0]);
} }
static void merge_weightsystem_info(weightsystem_t *res, const weightsystem_t *a, const weightsystem_t *b)
{
if (!a->weight.grams)
a = b;
res->weight = a->weight;
res->description = copy_string(a->description);
}
/* get_cylinder_idx_by_use(): Find the index of the first cylinder with a particular CCR use type. /* get_cylinder_idx_by_use(): Find the index of the first cylinder with a particular CCR use type.
* The index returned corresponds to that of the first cylinder with a cylinder_use that * The index returned corresponds to that of the first cylinder with a cylinder_use that
* equals the appropriate enum value [oxygen, diluent, bailout] given by cylinder_use_type. * equals the appropriate enum value [oxygen, diluent, bailout] given by cylinder_use_type.
@ -2307,11 +2283,28 @@ static void merge_cylinders(struct dive *res, const struct dive *a, const struct
} }
} }
/* Check whether a weightsystem table contains a given weightsystem */
static bool has_weightsystem(const struct weightsystem_table *t, const weightsystem_t w)
{
int i;
for (i = 0; i < t->nr; i++) {
if (same_weightsystem(w, t->weightsystems[i]))
return true;
}
return false;
}
static void merge_equipment(struct dive *res, const struct dive *a, const struct dive *b) static void merge_equipment(struct dive *res, const struct dive *a, const struct dive *b)
{ {
int i; int i;
for (i = 0; i < MAX_WEIGHTSYSTEMS; i++) for (i = 0; i < a->weightsystems.nr; i++) {
merge_weightsystem_info(res->weightsystem + i, a->weightsystem + i, b->weightsystem + i); if (!has_weightsystem(&res->weightsystems, a->weightsystems.weightsystems[i]))
add_cloned_weightsystem(&res->weightsystems, a->weightsystems.weightsystems[i]);
}
for (i = 0; i < b->weightsystems.nr; i++) {
if (!has_weightsystem(&res->weightsystems, b->weightsystems.weightsystems[i]))
add_cloned_weightsystem(&res->weightsystems, b->weightsystems.weightsystems[i]);
}
} }
static void merge_temperatures(struct dive *res, const struct dive *a, const struct dive *b) static void merge_temperatures(struct dive *res, const struct dive *a, const struct dive *b)

View file

@ -153,7 +153,7 @@ struct dive {
int rating; int rating;
int visibility; /* 0 - 5 star rating */ int visibility; /* 0 - 5 star rating */
cylinder_t cylinder[MAX_CYLINDERS]; cylinder_t cylinder[MAX_CYLINDERS];
weightsystem_t weightsystem[MAX_WEIGHTSYSTEMS]; struct weightsystem_table weightsystems;
char *suit; char *suit;
int sac, otu, cns, maxcns; int sac, otu, cns, maxcns;
@ -374,7 +374,6 @@ extern void copy_events(const struct divecomputer *s, struct divecomputer *d);
extern void free_events(struct event *ev); extern void free_events(struct event *ev);
extern void copy_cylinders(const struct dive *s, struct dive *d, bool used_only); extern void copy_cylinders(const struct dive *s, struct dive *d, bool used_only);
extern void copy_samples(const struct divecomputer *s, struct divecomputer *d); extern void copy_samples(const struct divecomputer *s, struct divecomputer *d);
extern void copy_weights(const struct dive *s, struct dive *d);
extern bool is_cylinder_used(const struct dive *dive, int idx); extern bool is_cylinder_used(const struct dive *dive, int idx);
extern bool is_cylinder_prot(const struct dive *dive, int idx); extern bool is_cylinder_prot(const struct dive *dive, int idx);
extern void fill_default_cylinder(cylinder_t *cyl); extern void fill_default_cylinder(cylinder_t *cyl);

View file

@ -100,8 +100,8 @@ int total_weight(const struct dive *dive)
int i, total_grams = 0; int i, total_grams = 0;
if (dive) if (dive)
for (i = 0; i < MAX_WEIGHTSYSTEMS; i++) for (i = 0; i < dive->weightsystems.nr; i++)
total_grams += dive->weightsystem[i].weight.grams; total_grams += dive->weightsystems.weightsystems[i].weight.grams;
return total_grams; return total_grams;
} }

View file

@ -17,6 +17,29 @@
#include "display.h" #include "display.h"
#include "divelist.h" #include "divelist.h"
#include "subsurface-string.h" #include "subsurface-string.h"
#include "table.h"
static void free_weightsystem(weightsystem_t w)
{
free((void *)w.description);
}
void copy_weights(const struct weightsystem_table *s, struct weightsystem_table *d)
{
clear_weightsystem_table(d);
for (int i = 0; i < s->nr; i++)
add_cloned_weightsystem(d, s->weightsystems[i]);
}
/* weightsystem table functions */
//static MAKE_GET_IDX(weightsystem_table, weightsystem_t, weightsystems)
static MAKE_GROW_TABLE(weightsystem_table, weightsystem_t, weightsystems)
//static MAKE_GET_INSERTION_INDEX(weightsystem_table, weightsystem_t, weightsystems, weightsystem_less_than)
MAKE_ADD_TO(weightsystem_table, weightsystem_t, weightsystems)
MAKE_REMOVE_FROM(weightsystem_table, weightsystems)
//MAKE_SORT(weightsystem_table, weightsystem_t, weightsystems, comp_weightsystems)
//MAKE_REMOVE(weightsystem_table, weightsystem_t, weightsystem)
MAKE_CLEAR_TABLE(weightsystem_table, weightsystems, weightsystem)
const char *cylinderuse_text[NUM_GAS_USE] = { const char *cylinderuse_text[NUM_GAS_USE] = {
QT_TRANSLATE_NOOP("gettextFromC", "OC-gas"), QT_TRANSLATE_NOOP("gettextFromC", "diluent"), QT_TRANSLATE_NOOP("gettextFromC", "oxygen"), QT_TRANSLATE_NOOP("gettextFromC", "not used") QT_TRANSLATE_NOOP("gettextFromC", "OC-gas"), QT_TRANSLATE_NOOP("gettextFromC", "diluent"), QT_TRANSLATE_NOOP("gettextFromC", "oxygen"), QT_TRANSLATE_NOOP("gettextFromC", "not used")
@ -51,6 +74,7 @@ void add_cylinder_description(const cylinder_type_t *type)
tank_info[i].bar = type->workingpressure.mbar / 1000; tank_info[i].bar = type->workingpressure.mbar / 1000;
} }
} }
void add_weightsystem_description(const weightsystem_t *weightsystem) void add_weightsystem_description(const weightsystem_t *weightsystem)
{ {
const char *desc; const char *desc;
@ -72,10 +96,18 @@ void add_weightsystem_description(const weightsystem_t *weightsystem)
} }
} }
/* Add a clone of a weightsystem to the end of a weightsystem table.
* Cloned in means that the description-string is copied. */
void add_cloned_weightsystem(struct weightsystem_table *t, weightsystem_t ws)
{
weightsystem_t w_clone = { ws.weight, copy_string(ws.description) };
add_to_weightsystem_table(t, t->nr, w_clone);
}
bool same_weightsystem(weightsystem_t w1, weightsystem_t w2) bool same_weightsystem(weightsystem_t w1, weightsystem_t w2)
{ {
return w1->weight.grams == w2->weight.grams && return w1.weight.grams == w2.weight.grams &&
same_string(w1->description, w2->description); same_string(w1.description, w2.description);
} }
bool cylinder_nodata(const cylinder_t *cyl) bool cylinder_nodata(const cylinder_t *cyl)
@ -239,10 +271,7 @@ void remove_cylinder(struct dive *dive, int idx)
void remove_weightsystem(struct dive *dive, int idx) void remove_weightsystem(struct dive *dive, int idx)
{ {
weightsystem_t *ws = dive->weightsystem + idx; remove_from_weightsystem_table(&dive->weightsystems, idx);
int nr = MAX_WEIGHTSYSTEMS - idx - 1;
memmove(ws, ws + 1, nr * sizeof(*ws));
memset(ws + nr, 0, sizeof(*ws));
} }
/* when planning a dive we need to make sure that all cylinders have a sane depth assigned /* when planning a dive we need to make sure that all cylinders have a sane depth assigned

View file

@ -39,12 +39,25 @@ typedef struct
const char *description; /* "integrated", "belt", "ankle" */ const char *description; /* "integrated", "belt", "ankle" */
} weightsystem_t; } weightsystem_t;
/* Table of weightsystems. Attention: this stores weightsystems,
* *not* pointers * to weightsystems. This has two crucial
* consequences:
* 1) Pointers to weightsystems are not stable. They may be
* invalidated if the table is reallocated.
* 2) add_to_weightsystem_table(), etc. takes ownership of the
* weightsystem. Notably of the description string */
struct weightsystem_table {
int nr, allocated;
weightsystem_t *weightsystems;
};
#define MAX_CYLINDERS (20) #define MAX_CYLINDERS (20)
#define MAX_WEIGHTSYSTEMS (6)
#define MAX_TANK_INFO (100) #define MAX_TANK_INFO (100)
#define MAX_WS_INFO (100) #define MAX_WS_INFO (100)
extern int cylinderuse_from_text(const char *text); extern int cylinderuse_from_text(const char *text);
extern void copy_weights(const struct weightsystem_table *s, struct weightsystem_table *d);
extern void add_cloned_weightsystem(struct weightsystem_table *t, weightsystem_t ws);
extern void add_cylinder_description(const cylinder_type_t *); extern void add_cylinder_description(const cylinder_type_t *);
extern void add_weightsystem_description(const weightsystem_t *); extern void add_weightsystem_description(const weightsystem_t *);
extern bool same_weightsystem(weightsystem_t w1, weightsystem_t w2); extern bool same_weightsystem(weightsystem_t w1, weightsystem_t w2);
@ -60,6 +73,10 @@ extern int find_best_gasmix_match(struct gasmix mix, const cylinder_t array[]);
extern void dump_cylinders(struct dive *dive, bool verbose); extern void dump_cylinders(struct dive *dive, bool verbose);
#endif #endif
/* Weightsystem table functions */
extern void clear_weightsystem_table(struct weightsystem_table *);
extern void add_to_weightsystem_table(struct weightsystem_table *, int idx, weightsystem_t ws);
void get_gas_string(struct gasmix gasmix, char *text, int len); void get_gas_string(struct gasmix gasmix, char *text, int len);
const char *gasname(struct gasmix gasmix); const char *gasname(struct gasmix gasmix);

View file

@ -311,8 +311,8 @@ static int divinglog_dive(void *param, int columns, char **data, char **column)
state->cur_dive->watertemp.mkelvin = C_to_mkelvin(atol(data[9])); state->cur_dive->watertemp.mkelvin = C_to_mkelvin(atol(data[9]));
if (data[10]) { if (data[10]) {
state->cur_dive->weightsystem[0].weight.grams = atol(data[10]) * 1000; weightsystem_t ws = { { atol(data[10]) * 1000 }, translate("gettextFromC", "unknown") };
state->cur_dive->weightsystem[0].description = strdup(translate("gettextFromC", "unknown")); add_cloned_weightsystem(&state->cur_dive->weightsystems, ws);
} }
if (data[11]) if (data[11])

View file

@ -330,7 +330,7 @@ static char *parse_keyvalue_entry(void (*fn)(void *, const char *, const char *)
return line; return line;
} }
static int cylinder_index, weightsystem_index; static int cylinder_index;
static int o2pressure_sensor; static int o2pressure_sensor;
static void parse_cylinder_keyvalue(void *_cylinder, const char *key, const char *value) static void parse_cylinder_keyvalue(void *_cylinder, const char *key, const char *value)
@ -424,21 +424,19 @@ static void parse_weightsystem_keyvalue(void *_ws, const char *key, const char *
static void parse_dive_weightsystem(char *line, struct membuffer *str, void *_dive) static void parse_dive_weightsystem(char *line, struct membuffer *str, void *_dive)
{ {
struct dive *dive = _dive; struct dive *dive = _dive;
weightsystem_t *ws = dive->weightsystem + weightsystem_index; weightsystem_t ws = { 0 };
if (weightsystem_index >= MAX_WEIGHTSYSTEMS) ws.description = get_utf8(str);
return;
weightsystem_index++;
ws->description = get_utf8(str);
for (;;) { for (;;) {
char c; char c;
while (isspace(c = *line)) while (isspace(c = *line))
line++; line++;
if (!c) if (!c)
break; break;
line = parse_keyvalue_entry(parse_weightsystem_keyvalue, ws, line); line = parse_keyvalue_entry(parse_weightsystem_keyvalue, &ws, line);
} }
add_to_weightsystem_table(&dive->weightsystems, dive->weightsystems.nr, ws);
} }
static int match_action(char *line, struct membuffer *str, void *data, static int match_action(char *line, struct membuffer *str, void *data,
@ -1506,7 +1504,8 @@ static int parse_dive_entry(git_repository *repo, const git_tree_entry *entry, c
return report_error("Unable to read dive file"); return report_error("Unable to read dive file");
if (*suffix) if (*suffix)
dive->number = atoi(suffix+1); dive->number = atoi(suffix+1);
cylinder_index = weightsystem_index = 0; cylinder_index = 0;
clear_weightsystem_table(&dive->weightsystems);
o2pressure_sensor = 1; o2pressure_sensor = 1;
for_each_line(blob, dive_parser, active_dive); for_each_line(blob, dive_parser, active_dive);
git_blob_free(blob); git_blob_free(blob);

View file

@ -1309,14 +1309,12 @@ static void try_to_fill_dive(struct dive *dive, const char *name, char *buf, str
return; return;
if (MATCH_STATE("airpressure.dive", pressure, &dive->surface_pressure)) if (MATCH_STATE("airpressure.dive", pressure, &dive->surface_pressure))
return; return;
if (state->cur_ws_index < MAX_WEIGHTSYSTEMS) { if (MATCH("description.weightsystem", utf8_string, &dive->weightsystems.weightsystems[dive->weightsystems.nr - 1].description))
if (MATCH("description.weightsystem", utf8_string, &dive->weightsystem[state->cur_ws_index].description)) return;
return; if (MATCH_STATE("weight.weightsystem", weight, &dive->weightsystems.weightsystems[dive->weightsystems.nr - 1].weight))
if (MATCH_STATE("weight.weightsystem", weight, &dive->weightsystem[state->cur_ws_index].weight)) return;
return; if (MATCH_STATE("weight", weight, &dive->weightsystems.weightsystems[dive->weightsystems.nr - 1].weight))
if (MATCH_STATE("weight", weight, &dive->weightsystem[state->cur_ws_index].weight)) return;
return;
}
if (state->cur_cylinder_index < MAX_CYLINDERS) { if (state->cur_cylinder_index < MAX_CYLINDERS) {
if (MATCH("size.cylinder", cylindersize, &dive->cylinder[state->cur_cylinder_index].type.size)) if (MATCH("size.cylinder", cylindersize, &dive->cylinder[state->cur_cylinder_index].type.size))
return; return;

View file

@ -257,7 +257,6 @@ void dive_end(struct parser_state *state)
state->cur_location.lat.udeg = 0; state->cur_location.lat.udeg = 0;
state->cur_location.lon.udeg = 0; state->cur_location.lon.udeg = 0;
state->cur_cylinder_index = 0; state->cur_cylinder_index = 0;
state->cur_ws_index = 0;
} }
void trip_start(struct parser_state *state) void trip_start(struct parser_state *state)
@ -299,11 +298,12 @@ void cylinder_end(struct parser_state *state)
void ws_start(struct parser_state *state) void ws_start(struct parser_state *state)
{ {
weightsystem_t w = { {0}, "" };
add_cloned_weightsystem(&state->cur_dive->weightsystems, w);
} }
void ws_end(struct parser_state *state) void ws_end(struct parser_state *state)
{ {
state->cur_ws_index++;
} }
/* /*

View file

@ -54,7 +54,7 @@ struct parser_state {
bool in_settings; bool in_settings;
bool in_userid; bool in_userid;
struct tm cur_tm; struct tm cur_tm;
int cur_cylinder_index, cur_ws_index; int cur_cylinder_index;
int lastcylinderindex, next_o2_sensor; int lastcylinderindex, next_o2_sensor;
int o2pressure_sensor; int o2pressure_sensor;
int sample_rate; int sample_rate;

View file

@ -165,9 +165,9 @@ static void save_weightsystem_info(struct membuffer *b, struct dive *dive)
nr = nr_weightsystems(dive); nr = nr_weightsystems(dive);
for (i = 0; i < nr; i++) { for (i = 0; i < nr; i++) {
weightsystem_t *ws = dive->weightsystem + i; weightsystem_t ws = dive->weightsystems.weightsystems[i];
int grams = ws->weight.grams; int grams = ws.weight.grams;
const char *description = ws->description; const char *description = ws.description;
put_string(b, "weightsystem"); put_string(b, "weightsystem");
put_milli(b, " weight=", grams, "kg"); put_milli(b, " weight=", grams, "kg");

View file

@ -106,9 +106,9 @@ static void put_weightsystem_HTML(struct membuffer *b, struct dive *dive)
char *separator = ""; char *separator = "";
for (i = 0; i < nr; i++) { for (i = 0; i < nr; i++) {
weightsystem_t *ws = dive->weightsystem + i; weightsystem_t ws = dive->weightsystems.weightsystems[i];
int grams = ws->weight.grams; int grams = ws.weight.grams;
const char *description = ws->description; const char *description = ws.description;
put_string(b, separator); put_string(b, separator);
separator = ", "; separator = ", ";

View file

@ -205,9 +205,9 @@ static void save_weightsystem_info(struct membuffer *b, struct dive *dive)
nr = nr_weightsystems(dive); nr = nr_weightsystems(dive);
for (i = 0; i < nr; i++) { for (i = 0; i < nr; i++) {
weightsystem_t *ws = dive->weightsystem + i; weightsystem_t ws = dive->weightsystems.weightsystems[i];
int grams = ws->weight.grams; int grams = ws.weight.grams;
const char *description = ws->description; const char *description = ws.description;
put_format(b, " <weightsystem"); put_format(b, " <weightsystem");
put_milli(b, " weight='", grams, " kg'"); put_milli(b, " weight='", grams, " kg'");

View file

@ -14,7 +14,7 @@ enum returnPressureSelector {START_PRESSURE, END_PRESSURE};
static QString getFormattedWeight(struct dive *dive, unsigned int idx) static QString getFormattedWeight(struct dive *dive, unsigned int idx)
{ {
weightsystem_t *weight = &dive->weightsystem[idx]; weightsystem_t *weight = &dive->weightsystems.weightsystems[idx];
if (!weight->description) if (!weight->description)
return QString(); return QString();
QString fmt = QString(weight->description); QString fmt = QString(weight->description);
@ -231,7 +231,7 @@ QString DiveObjectHelper::sac() const
QString DiveObjectHelper::weightList() const QString DiveObjectHelper::weightList() const
{ {
QString weights; QString weights;
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++) { for (int i = 0; i < m_dive->weightsystems.nr; i++) {
QString w = getFormattedWeight(m_dive, i); QString w = getFormattedWeight(m_dive, i);
if (w.isEmpty()) if (w.isEmpty())
continue; continue;
@ -243,7 +243,7 @@ QString DiveObjectHelper::weightList() const
QStringList DiveObjectHelper::weights() const QStringList DiveObjectHelper::weights() const
{ {
QStringList weights; QStringList weights;
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++) { for (int i = 0; i < m_dive->weightsystems.nr; i++) {
QString w = getFormattedWeight(m_dive, i); QString w = getFormattedWeight(m_dive, i);
if (w.isEmpty()) if (w.isEmpty())
continue; continue;
@ -254,12 +254,12 @@ QStringList DiveObjectHelper::weights() const
bool DiveObjectHelper::singleWeight() const bool DiveObjectHelper::singleWeight() const
{ {
return weightsystem_none(&m_dive->weightsystem[1]); return m_dive->weightsystems.nr == 1;
} }
QString DiveObjectHelper::weight(int idx) const QString DiveObjectHelper::weight(int idx) const
{ {
if ( (idx < 0) || idx > MAX_WEIGHTSYSTEMS ) if ((idx < 0) || idx >= m_dive->weightsystems.nr)
return QString(); return QString();
return getFormattedWeight(m_dive, idx); return getFormattedWeight(m_dive, idx);
} }

View file

@ -50,13 +50,13 @@
} \ } \
} }
#define MAKE_REMOVE_FROM(table_type, array_name) \ #define MAKE_REMOVE_FROM(table_type, array_name) \
void remove_from_##table_type(struct table_type *table, int idx) \ void remove_from_##table_type(struct table_type *table, int idx) \
{ \ { \
int i; \ int i; \
for (i = idx; i < table->nr - 1; i++) \ for (i = idx; i < table->nr - 1; i++) \
table->array_name[i] = table->array_name[i + 1]; \ table->array_name[i] = table->array_name[i + 1]; \
table->array_name[--table->nr] = NULL; \ memset(&table->array_name[--table->nr], 0, sizeof(table->array_name[0])); \
} }
#define MAKE_GET_IDX(table_type, item_type, array_name) \ #define MAKE_GET_IDX(table_type, item_type, array_name) \

View file

@ -194,7 +194,7 @@ static void uemis_get_weight(char *buffer, weightsystem_t *weight, int diveid)
weight->weight.grams = uemis_get_weight_unit(diveid) ? weight->weight.grams = uemis_get_weight_unit(diveid) ?
lbs_to_grams(ascii_strtod(buffer, NULL)) : lbs_to_grams(ascii_strtod(buffer, NULL)) :
lrint(ascii_strtod(buffer, NULL) * 1000); lrint(ascii_strtod(buffer, NULL) * 1000);
weight->description = strdup(translate("gettextFromC", "unknown")); weight->description = translate("gettextFromC", "unknown");
} }
static struct dive *uemis_start_dive(uint32_t deviceid) static struct dive *uemis_start_dive(uint32_t deviceid)
@ -815,7 +815,9 @@ static void parse_tag(struct dive *dive, char *tag, char *val)
} else if (!strcmp(tag, "altitude")) { } else if (!strcmp(tag, "altitude")) {
uemis_get_index(val, &dive->dc.surface_pressure.mbar); uemis_get_index(val, &dive->dc.surface_pressure.mbar);
} else if (!strcmp(tag, "f32Weight")) { } else if (!strcmp(tag, "f32Weight")) {
uemis_get_weight(val, &dive->weightsystem[0], dive->dc.diveid); weightsystem_t ws;
uemis_get_weight(val, &ws, dive->dc.diveid);
add_cloned_weightsystem(&dive->weightsystems, ws);
} else if (!strcmp(tag, "notes")) { } else if (!strcmp(tag, "notes")) {
uemis_add_string(val, &dive->notes, " "); uemis_add_string(val, &dive->notes, " ");
} else if (!strcmp(tag, "u8DiveSuit")) { } else if (!strcmp(tag, "u8DiveSuit")) {

View file

@ -654,7 +654,7 @@ PasteState::PasteState(dive *dIn, const dive *data, dive_components what) : d(dI
tags(nullptr) tags(nullptr)
{ {
memset(&cylinders[0], 0, sizeof(cylinders)); memset(&cylinders[0], 0, sizeof(cylinders));
memset(&weightsystems[0], 0, sizeof(weightsystems)); clear_weightsystem_table(&weightsystems);
if (what.notes) if (what.notes)
notes = data->notes; notes = data->notes;
if (what.divemaster) if (what.divemaster)
@ -675,12 +675,8 @@ PasteState::PasteState(dive *dIn, const dive *data, dive_components what) : d(dI
for (int i = 0; i < MAX_CYLINDERS; ++i) for (int i = 0; i < MAX_CYLINDERS; ++i)
copy_cylinder(data->cylinder[i], cylinders[i]); copy_cylinder(data->cylinder[i], cylinders[i]);
} }
if (what.weights) { if (what.weights)
for (int i = 0; i < MAX_WEIGHTSYSTEMS; ++i) { copy_weights(&data->weightsystems, &weightsystems);
weightsystems[i] = data->weightsystem[i];
weightsystems[i].description = copy_string(data->weightsystem[i].description);
}
}
} }
PasteState::~PasteState() PasteState::~PasteState()
@ -688,8 +684,8 @@ PasteState::~PasteState()
taglist_free(tags); taglist_free(tags);
for (cylinder_t &c: cylinders) for (cylinder_t &c: cylinders)
free((void *)c.type.description); free((void *)c.type.description);
for (weightsystem_t &w: weightsystems) clear_weightsystem_table(&weightsystems);
free((void *)w.description); free(weightsystems.weightsystems);
} }
void PasteState::swap(dive_components what) void PasteState::swap(dive_components what)
@ -713,7 +709,7 @@ void PasteState::swap(dive_components what)
if (what.cylinders) if (what.cylinders)
std::swap(cylinders, d->cylinder); std::swap(cylinders, d->cylinder);
if (what.weights) if (what.weights)
std::swap(weightsystems, d->weightsystem); std::swap(weightsystems, d->weightsystems);
} }
// ***** Paste ***** // ***** Paste *****

View file

@ -236,7 +236,7 @@ struct PasteState {
int visibility; int visibility;
tag_entry *tags; tag_entry *tags;
cylinder_t cylinders[MAX_CYLINDERS]; cylinder_t cylinders[MAX_CYLINDERS];
weightsystem_t weightsystems[MAX_WEIGHTSYSTEMS]; struct weightsystem_table weightsystems;
PasteState(dive *d, const dive *data, dive_components what); // Read data from dive data for dive d PasteState(dive *d, const dive *data, dive_components what); // Read data from dive data for dive d
~PasteState(); ~PasteState();

View file

@ -447,16 +447,12 @@ void DiveLogExportDialog::export_TeX(const char *filename, const bool selected_o
put_format(&buf, "\n%% Weighting information:\n"); put_format(&buf, "\n%% Weighting information:\n");
qty_weight = 0; qty_weight = 0;
total_weight = 0; total_weight = 0;
for (i = 0; i < MAX_WEIGHTSYSTEMS; i++){ for (i = 0; i < dive->weightsystems.nr; i++) {
if (dive->weightsystem[i].weight.grams){ weightsystem_t w = dive->weightsystems.weightsystems[i];
put_format(&buf, "\\def\\%sweight%ctype{%s}\n", ssrf, 'a' + i, dive->weightsystem[i].description); put_format(&buf, "\\def\\%sweight%ctype{%s}\n", ssrf, 'a' + i, w.description);
put_format(&buf, "\\def\\%sweight%camt{%.3f\\%sweightunit}\n", ssrf, 'a' + i, get_weight_units(dive->weightsystem[i].weight.grams, NULL, &unit), ssrf); put_format(&buf, "\\def\\%sweight%camt{%.3f\\%sweightunit}\n", ssrf, 'a' + i, get_weight_units(w.weight.grams, NULL, &unit), ssrf);
qty_weight += 1; qty_weight += 1;
total_weight += get_weight_units(dive->weightsystem[i].weight.grams, NULL, &unit); total_weight += get_weight_units(w.weight.grams, NULL, &unit);
} else {
put_format(&buf, "\\def\\%sweight%ctype{}\n", ssrf, 'a' + i);
put_format(&buf, "\\def\\%sweight%camt{}\n", ssrf, 'a' + i);
}
} }
put_format(&buf, "\\def\\%sqtyweights{%d}\n", ssrf, qty_weight); put_format(&buf, "\\def\\%sqtyweights{%d}\n", ssrf, qty_weight);
put_format(&buf, "\\def\\%stotalweight{%.2f\\%sweightunit}\n", ssrf, total_weight, ssrf); put_format(&buf, "\\def\\%stotalweight{%.2f\\%sweightunit}\n", ssrf, total_weight, ssrf);

View file

@ -518,9 +518,9 @@ void DiveComponentSelection::buttonClicked(QAbstractButton *button)
if (what->weights) { if (what->weights) {
int w; int w;
text << tr("Weights:\n"); text << tr("Weights:\n");
for (w = 0; w < MAX_WEIGHTSYSTEMS; w++) { for (w = 0; w < displayed_dive.weightsystems.nr; w++) {
if (displayed_dive.weightsystem[w].weight.grams) weightsystem_t ws = displayed_dive.weightsystems.weightsystems[w];
text << displayed_dive.weightsystem[w].description << displayed_dive.weightsystem[w].weight.grams / 1000 << "kg\n"; text << ws.description << ws.weight.grams / 1000 << "kg\n";
} }
} }
clipboard->setText(cliptext); clipboard->setText(cliptext);

View file

@ -172,6 +172,10 @@ static QVector<dive *> getSelectedDivesCurrentLast()
return res; return res;
} }
// TODO: This is only a temporary function until undo of weightsystems is implemented.
// Therefore it is not worth putting it in a header.
extern bool weightsystems_equal(const dive *d1, const dive *d2);
void TabDiveEquipment::acceptChanges() void TabDiveEquipment::acceptChanges()
{ {
bool do_replot = false; bool do_replot = false;
@ -232,17 +236,10 @@ void TabDiveEquipment::acceptChanges()
if (weightModel->changed) { if (weightModel->changed) {
mark_divelist_changed(true); mark_divelist_changed(true);
MODIFY_DIVES(selectedDives, MODIFY_DIVES(selectedDives,
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++) { if (weightsystems_equal(mydive, cd))
if (mydive != cd && (same_string(mydive->weightsystem[i].description, cd->weightsystem[i].description))) { copy_weights(&displayed_dive.weightsystems, &mydive->weightsystems);
mydive->weightsystem[i] = displayed_dive.weightsystem[i];
mydive->weightsystem[i].description = copy_string(displayed_dive.weightsystem[i].description);
}
}
); );
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++) { copy_weights(&displayed_dive.weightsystems, &cd->weightsystems);
cd->weightsystem[i] = displayed_dive.weightsystem[i];
cd->weightsystem[i].description = copy_string(displayed_dive.weightsystem[i].description);
}
} }
if (do_replot) if (do_replot)

View file

@ -669,6 +669,17 @@ void MainTab::acceptChanges()
editMode = NONE; editMode = NONE;
} }
bool weightsystems_equal(const dive *d1, const dive *d2)
{
if (d1->weightsystems.nr != d2->weightsystems.nr)
return false;
for (int i = 0; i < d1->weightsystems.nr; ++i) {
if (!same_weightsystem(d1->weightsystems.weightsystems[0], d2->weightsystems.weightsystems[i]))
return false;
}
return true;
}
void MainTab::rejectChanges() void MainTab::rejectChanges()
{ {
EditMode lastMode = editMode; EditMode lastMode = editMode;
@ -676,7 +687,7 @@ void MainTab::rejectChanges()
if (lastMode != NONE && current_dive && if (lastMode != NONE && current_dive &&
(modified || (modified ||
memcmp(&current_dive->cylinder[0], &displayed_dive.cylinder[0], sizeof(cylinder_t) * MAX_CYLINDERS) || memcmp(&current_dive->cylinder[0], &displayed_dive.cylinder[0], sizeof(cylinder_t) * MAX_CYLINDERS) ||
memcmp(&current_dive->weightsystem[0], &displayed_dive.weightsystem[0], sizeof(weightsystem_t) * MAX_WEIGHTSYSTEMS))) { !weightsystems_equal(current_dive, &displayed_dive))) {
if (QMessageBox::warning(MainWindow::instance(), TITLE_OR_TEXT(tr("Discard the changes?"), if (QMessageBox::warning(MainWindow::instance(), TITLE_OR_TEXT(tr("Discard the changes?"),
tr("You are about to discard your changes.")), tr("You are about to discard your changes.")),
QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Discard) != QMessageBox::Discard) { QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Discard) != QMessageBox::Discard) {

View file

@ -1031,10 +1031,10 @@ void QMLManager::commitChanges(QString diveId, QString date, QString location, Q
} }
// not sure what we'd do if there was more than one weight system // not sure what we'd do if there was more than one weight system
// defined - for now just ignore that case // defined - for now just ignore that case
if (weightsystem_none(&d->weightsystem[1])) { if (d->weightsystems.nr == 1) {
if (myDive->sumWeight() != weight) { if (myDive->sumWeight() != weight) {
diveChanged = true; diveChanged = true;
d->weightsystem[0].weight.grams = parseWeightToGrams(weight); d->weightsystems.weightsystems[0].weight.grams = parseWeightToGrams(weight);
} }
} }
// start and end pressures for first cylinder only // start and end pressures for first cylinder only

View file

@ -18,14 +18,14 @@ WeightModel::WeightModel(QObject *parent) : CleanerTableModel(parent),
weightsystem_t *WeightModel::weightSystemAt(const QModelIndex &index) weightsystem_t *WeightModel::weightSystemAt(const QModelIndex &index)
{ {
return &displayed_dive.weightsystem[index.row()]; return &displayed_dive.weightsystems.weightsystems[index.row()];
} }
void WeightModel::remove(const QModelIndex &index) void WeightModel::remove(const QModelIndex &index)
{ {
if (index.column() != REMOVE) if (index.column() != REMOVE)
return; return;
beginRemoveRows(QModelIndex(), index.row(), index.row()); // yah, know, ugly. beginRemoveRows(QModelIndex(), index.row(), index.row());
rows--; rows--;
remove_weightsystem(&displayed_dive, index.row()); remove_weightsystem(&displayed_dive, index.row());
changed = true; changed = true;
@ -34,18 +34,17 @@ void WeightModel::remove(const QModelIndex &index)
void WeightModel::clear() void WeightModel::clear()
{ {
if (rows > 0) { beginResetModel();
beginRemoveRows(QModelIndex(), 0, rows - 1); rows = 0;
endRemoveRows(); endResetModel();
}
} }
QVariant WeightModel::data(const QModelIndex &index, int role) const QVariant WeightModel::data(const QModelIndex &index, int role) const
{ {
if (!index.isValid() || index.row() >= MAX_WEIGHTSYSTEMS) if (!index.isValid() || index.row() >= displayed_dive.weightsystems.nr)
return QVariant(); return QVariant();
weightsystem_t *ws = &displayed_dive.weightsystem[index.row()]; const weightsystem_t *ws = &displayed_dive.weightsystems.weightsystems[index.row()];
switch (role) { switch (role) {
case Qt::FontRole: case Qt::FontRole:
@ -79,7 +78,7 @@ QVariant WeightModel::data(const QModelIndex &index, int role) const
// so we only implement the two columns we care about // so we only implement the two columns we care about
void WeightModel::passInData(const QModelIndex &index, const QVariant &value) void WeightModel::passInData(const QModelIndex &index, const QVariant &value)
{ {
weightsystem_t *ws = &displayed_dive.weightsystem[index.row()]; weightsystem_t *ws = &displayed_dive.weightsystems.weightsystems[index.row()];
if (index.column() == WEIGHT) { if (index.column() == WEIGHT) {
if (ws->weight.grams != value.toInt()) { if (ws->weight.grams != value.toInt()) {
ws->weight.grams = value.toInt(); ws->weight.grams = value.toInt();
@ -92,7 +91,7 @@ void WeightModel::passInData(const QModelIndex &index, const QVariant &value)
bool WeightModel::setData(const QModelIndex &index, const QVariant &value, int role) bool WeightModel::setData(const QModelIndex &index, const QVariant &value, int role)
{ {
QString vString = value.toString(); QString vString = value.toString();
weightsystem_t *ws = &displayed_dive.weightsystem[index.row()]; weightsystem_t *ws = &displayed_dive.weightsystems.weightsystems[index.row()];
switch (index.column()) { switch (index.column()) {
case TYPE: case TYPE:
if (!value.isNull()) { if (!value.isNull()) {
@ -142,11 +141,10 @@ int WeightModel::rowCount(const QModelIndex&) const
void WeightModel::add() void WeightModel::add()
{ {
if (rows >= MAX_WEIGHTSYSTEMS)
return;
int row = rows; int row = rows;
weightsystem_t ws { {0}, "" };
beginInsertRows(QModelIndex(), row, row); beginInsertRows(QModelIndex(), row, row);
add_cloned_weightsystem(&displayed_dive.weightsystems, ws);
rows++; rows++;
changed = true; changed = true;
endInsertRows(); endInsertRows();
@ -154,17 +152,9 @@ void WeightModel::add()
void WeightModel::updateDive() void WeightModel::updateDive()
{ {
clear(); beginResetModel();
rows = 0; rows = displayed_dive.weightsystems.nr;
for (int i = 0; i < MAX_WEIGHTSYSTEMS; i++) { endResetModel();
if (!weightsystem_none(&displayed_dive.weightsystem[i])) {
rows = i + 1;
}
}
if (rows > 0) {
beginInsertRows(QModelIndex(), 0, rows - 1);
endInsertRows();
}
} }
void WeightModel::weightsystemsReset(const QVector<dive *> &dives) void WeightModel::weightsystemsReset(const QVector<dive *> &dives)
@ -174,8 +164,8 @@ void WeightModel::weightsystemsReset(const QVector<dive *> &dives)
if (!current_dive || std::find(dives.begin(), dives.end(), current_dive) == dives.end()) if (!current_dive || std::find(dives.begin(), dives.end(), current_dive) == dives.end())
return; return;
// Copy the cylinders from the current dive to the displayed dive. // Copy the weights from the current dive to the displayed dive.
copy_weights(current_dive, &displayed_dive); copy_weights(&current_dive->weightsystems, &displayed_dive.weightsystems);
// And update the model.. // And update the model..
updateDive(); updateDive();

View file

@ -1063,7 +1063,8 @@ void smartrak_import(const char *file, struct dive_table *divetable)
/* No DC related data */ /* No DC related data */
smtkdive->visibility = strtod(col[coln(VISIBILITY)]->bind_ptr, NULL) > 25 ? 5 : lrint(strtod(col[13]->bind_ptr, NULL) / 5); smtkdive->visibility = strtod(col[coln(VISIBILITY)]->bind_ptr, NULL) > 25 ? 5 : lrint(strtod(col[13]->bind_ptr, NULL) / 5);
smtkdive->weightsystem[0].weight.grams = lrint(strtod(col[coln(WEIGHT)]->bind_ptr, NULL) * 1000); weightsystem_t ws = { {lrint(strtod(col[coln(WEIGHT)]->bind_ptr, NULL) * 1000)}, "" };
add_cloned_weightsystem(&smtkdive->weightsystems, ws);
smtkdive->suit = copy_string(suit_list[atoi(col[coln(SUITIDX)]->bind_ptr) - 1]); smtkdive->suit = copy_string(suit_list[atoi(col[coln(SUITIDX)]->bind_ptr) - 1]);
smtk_build_location(mdb_clon, col[coln(SITEIDX)]->bind_ptr, &smtkdive->dive_site); smtk_build_location(mdb_clon, col[coln(SITEIDX)]->bind_ptr, &smtkdive->dive_site);
smtkdive->buddy = smtk_locate_buddy(mdb_clon, col[0]->bind_ptr, buddy_list); smtkdive->buddy = smtk_locate_buddy(mdb_clon, col[0]->bind_ptr, buddy_list);