core: use C++ structures for weightsystem info

Use std::vector<> instead of fixed size array.
Doesn't do any logic change, even though the back-translation
logic is ominous.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-04-17 17:31:50 +08:00
parent c5bdbdc175
commit 3bfbf12b9a
8 changed files with 58 additions and 72 deletions

View file

@ -1298,7 +1298,7 @@ extern "C" struct dive *fixup_dive(struct dive *dive)
}
update_cylinder_related_info(dive);
for (i = 0; i < dive->weightsystems.nr; i++) {
weightsystem_t *ws = &dive->weightsystems.weightsystems[i];
const weightsystem_t &ws = dive->weightsystems.weightsystems[i];
add_weightsystem_description(ws);
}
/* we should always have a uniq ID as that gets assigned during alloc_dive(),

View file

@ -162,36 +162,28 @@ extern "C" void add_cylinder_description(const cylinder_type_t *type)
type->workingpressure.mbar / 1000);
}
extern "C" void add_weightsystem_description(const weightsystem_t *weightsystem)
void add_weightsystem_description(const weightsystem_t &weightsystem)
{
const char *desc;
int i;
desc = weightsystem->description;
if (!desc)
if (empty_string(weightsystem.description))
return;
auto it = std::find_if(ws_info_table.begin(), ws_info_table.end(),
[&weightsystem](const ws_info &info)
{ return info.name == weightsystem.description; });
if (it != ws_info_table.end()) {
it->weight = weightsystem.weight;
return;
for (i = 0; i < MAX_WS_INFO && ws_info[i].name != NULL; i++) {
if (same_string(ws_info[i].name, desc)) {
ws_info[i].grams = weightsystem->weight.grams;
return;
}
}
if (i < MAX_WS_INFO) {
// FIXME: leaked on exit
ws_info[i].name = strdup(desc);
ws_info[i].grams = weightsystem->weight.grams;
}
ws_info_table.push_back(ws_info { std::string(weightsystem.description), weightsystem.weight });
}
extern "C" struct ws_info_t *get_weightsystem_description(const char *name)
weight_t get_weightsystem_weight(const std::string &name)
{
for (int i = 0; i < MAX_WS_INFO && ws_info[i].name != NULL; i++) {
// Also finds translated names (TODO: should only consider non-user items).
if (same_string(ws_info[i].name, name) ||
same_string(translate("gettextFromC", ws_info[i].name), name))
return &ws_info[i];
}
return NULL;
// Also finds translated names (TODO: should only consider non-user items).
auto it = std::find_if(ws_info_table.begin(), ws_info_table.end(),
[&name](const ws_info &info)
{ return info.name == name || translate("gettextFromC", info.name.c_str()) == name; });
return it != ws_info_table.end() ? it->weight : weight_t();
}
extern "C" weightsystem_t clone_weightsystem(weightsystem_t ws)
@ -363,12 +355,12 @@ extern "C" void reset_tank_info_table(struct tank_info_table *table)
* We hardcode the most common weight system types
* This is a bit odd as the weight system types don't usually encode weight
*/
struct ws_info_t ws_info[MAX_WS_INFO] = {
{ QT_TRANSLATE_NOOP("gettextFromC", "integrated"), 0 },
{ QT_TRANSLATE_NOOP("gettextFromC", "belt"), 0 },
{ QT_TRANSLATE_NOOP("gettextFromC", "ankle"), 0 },
{ QT_TRANSLATE_NOOP("gettextFromC", "backplate"), 0 },
{ QT_TRANSLATE_NOOP("gettextFromC", "clip-on"), 0 },
struct std::vector<ws_info> ws_info_table = {
{ QT_TRANSLATE_NOOP("gettextFromC", "integrated"), weight_t() },
{ QT_TRANSLATE_NOOP("gettextFromC", "belt"), weight_t() },
{ QT_TRANSLATE_NOOP("gettextFromC", "ankle"), weight_t() },
{ QT_TRANSLATE_NOOP("gettextFromC", "backplate"), weight_t() },
{ QT_TRANSLATE_NOOP("gettextFromC", "clip-on"), weight_t() },
};
extern "C" void remove_cylinder(struct dive *dive, int idx)

View file

@ -68,8 +68,6 @@ struct weightsystem_table {
weightsystem_t *weightsystems;
};
#define MAX_WS_INFO (100)
extern enum cylinderuse cylinderuse_from_text(const char *text);
extern void copy_weights(const struct weightsystem_table *s, struct weightsystem_table *d);
extern void copy_cylinders(const struct cylinder_table *s, struct cylinder_table *d);
@ -84,7 +82,6 @@ extern void add_cloned_cylinder(struct cylinder_table *t, cylinder_t cyl);
extern cylinder_t *get_cylinder(const struct dive *d, int idx);
extern cylinder_t *get_or_create_cylinder(struct dive *d, int idx);
extern void add_cylinder_description(const cylinder_type_t *);
extern void add_weightsystem_description(const weightsystem_t *);
extern bool same_weightsystem(weightsystem_t w1, weightsystem_t w2);
extern void remove_cylinder(struct dive *dive, int idx);
extern void remove_weightsystem(struct dive *dive, int idx);
@ -130,15 +127,21 @@ extern void set_tank_info_size(struct tank_info_table *table, const char *name,
extern void set_tank_info_workingpressure(struct tank_info_table *table, const char *name, pressure_t working_pressure);
extern struct tank_info *get_tank_info(struct tank_info_table *table, const char *name);
struct ws_info_t {
const char *name;
int grams;
};
extern struct ws_info_t ws_info[MAX_WS_INFO];
extern struct ws_info_t *get_weightsystem_description(const char *name);
#ifdef __cplusplus
}
#include <memory>
#include <string>
#include <vector>
struct ws_info {
std::string name;
weight_t weight;
};
extern std::vector<ws_info> ws_info_table;
extern weight_t get_weightsystem_weight(const std::string &name); // returns 0 if not found
extern void add_weightsystem_description(const weightsystem_t &);
#endif
#endif // EQUIPMENT_H

View file

@ -131,7 +131,11 @@ typedef struct
typedef struct
{
#ifdef __cplusplus
int grams = 0;
#else
int grams;
#endif
} weight_t;
typedef struct