diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-06-26 17:21:03 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2019-07-18 06:01:07 -0700 |
commit | a5e7f4253ac98ad18354973fda7049e9daaea8eb (patch) | |
tree | 16b95a18d0a1af296e4645d6b7a204b5b325ff01 /core/equipment.c | |
parent | efdb3503eadd7e47cb64b1c252e50488d2e9d0fe (diff) | |
download | subsurface-a5e7f4253ac98ad18354973fda7049e9daaea8eb.tar.gz |
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>
Diffstat (limited to 'core/equipment.c')
-rw-r--r-- | core/equipment.c | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/core/equipment.c b/core/equipment.c index 91db0cb81..5bf13713c 100644 --- a/core/equipment.c +++ b/core/equipment.c @@ -17,6 +17,29 @@ #include "display.h" #include "divelist.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] = { 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; } } + void add_weightsystem_description(const weightsystem_t *weightsystem) { 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) { - return w1->weight.grams == w2->weight.grams && - same_string(w1->description, w2->description); + return w1.weight.grams == w2.weight.grams && + same_string(w1.description, w2.description); } 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) { - weightsystem_t *ws = dive->weightsystem + idx; - int nr = MAX_WEIGHTSYSTEMS - idx - 1; - memmove(ws, ws + 1, nr * sizeof(*ws)); - memset(ws + nr, 0, sizeof(*ws)); + remove_from_weightsystem_table(&dive->weightsystems, idx); } /* when planning a dive we need to make sure that all cylinders have a sane depth assigned |