core: keep tank infos in a dynamic table

The list of known tank types were kept in a fixed size table.
Instead, use a dynamic table with our horrendous table macros.
This is more flexible and sensible.

While doing this, clean up the TankInfoModel, which was leaking
memory.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-12-11 22:34:35 +01:00 committed by Dirk Hohndel
parent 2e328c7633
commit 50b11024d6
12 changed files with 154 additions and 141 deletions

View file

@ -1207,9 +1207,9 @@ EditCylinder::EditCylinder(int index, cylinder_t cylIn, EditCylinderType typeIn,
// Try to untranslate the cylinder type // Try to untranslate the cylinder type
QString description = cylIn.type.description; QString description = cylIn.type.description;
for (int i = 0; i < MAX_TANK_INFO && tank_info[i].name; ++i) { for (int i = 0; i < tank_info_table.nr; ++i) {
if (gettextFromC::tr(tank_info[i].name) == description) { if (gettextFromC::tr(tank_info_table.infos[i].name) == description) {
description = tank_info[i].name; description = tank_info_table.infos[i].name;
break; break;
} }
} }

View file

@ -130,14 +130,12 @@ static int dtrak_prepare_data(int model, device_data_t *dev_data)
*/ */
static const char *cyl_type_by_size(int size) static const char *cyl_type_by_size(int size)
{ {
struct tank_info_t *ti = tank_info; for (int i = 0; i < tank_info_table.nr; ++i) {
const struct tank_info *ti = &tank_info_table.infos[i];
while (ti->ml != size && ti < tank_info + MAX_TANK_INFO) if (ti->ml == size)
ti++; return ti->name;
if (ti == tank_info + MAX_TANK_INFO) }
return ""; return "";
else
return ti->name;
} }
/* /*

View file

@ -1395,6 +1395,8 @@ void clear_dive_file_data()
reset_min_datafile_version(); reset_min_datafile_version();
clear_git_id(); clear_git_id();
reset_tank_info_table(&tank_info_table);
/* Inform frontend of reset data. This should reset all the models. */ /* Inform frontend of reset data. This should reset all the models. */
emit_reset_signal(); emit_reset_signal();
} }

View file

@ -51,6 +51,11 @@ void copy_cylinders(const struct cylinder_table *s, struct cylinder_table *d)
add_cloned_cylinder(d, s->cylinders[i]); add_cloned_cylinder(d, s->cylinders[i]);
} }
static void free_tank_info(struct tank_info info)
{
free((void *)info.name);
}
/* weightsystem table functions */ /* weightsystem table functions */
//static MAKE_GET_IDX(weightsystem_table, weightsystem_t, weightsystems) //static MAKE_GET_IDX(weightsystem_table, weightsystem_t, weightsystems)
static MAKE_GROW_TABLE(weightsystem_table, weightsystem_t, weightsystems) static MAKE_GROW_TABLE(weightsystem_table, weightsystem_t, weightsystems)
@ -71,6 +76,12 @@ static MAKE_REMOVE_FROM(cylinder_table, cylinders)
//MAKE_REMOVE(cylinder_table, cylinder_t, cylinder) //MAKE_REMOVE(cylinder_table, cylinder_t, cylinder)
MAKE_CLEAR_TABLE(cylinder_table, cylinders, cylinder) MAKE_CLEAR_TABLE(cylinder_table, cylinders, cylinder)
/* tank_info table functions */
static MAKE_GROW_TABLE(tank_info_table, tank_info_t, infos)
static MAKE_ADD_TO(tank_info_table, tank_info_t, infos)
//static MAKE_REMOVE_FROM(tank_info_table, infos)
MAKE_CLEAR_TABLE(tank_info_table, infos, tank_info)
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")
}; };
@ -84,25 +95,32 @@ int cylinderuse_from_text(const char *text)
return -1; return -1;
} }
/* Add a metric or an imperial tank info structure. Copies the passed-in string. */
void add_tank_info_metric(struct tank_info_table *table, const char *name, int ml, int bar)
{
struct tank_info info = { strdup(name), .ml = ml, .bar = bar };
add_to_tank_info_table(table, table->nr, info);
}
void add_tank_info_imperial(struct tank_info_table *table, const char *name, int cuft, int psi)
{
struct tank_info info = { strdup(name), .cuft = cuft, .psi = psi };
add_to_tank_info_table(table, table->nr, info);
}
/* placeholders for a few functions that we need to redesign for the Qt UI */ /* placeholders for a few functions that we need to redesign for the Qt UI */
void add_cylinder_description(const cylinder_type_t *type) void add_cylinder_description(const cylinder_type_t *type)
{ {
const char *desc; const char *desc = type->description;
int i; if (empty_string(desc))
desc = type->description;
if (!desc)
return; return;
for (i = 0; i < MAX_TANK_INFO && tank_info[i].name != NULL; i++) { for (int i = 0; i < tank_info_table.nr; i++) {
if (strcmp(tank_info[i].name, desc) == 0) if (strcmp(tank_info_table.infos[i].name, desc) == 0)
return; return;
} }
if (i < MAX_TANK_INFO) { // FIXME: leaked on exit
// FIXME: leaked on exit add_tank_info_metric(&tank_info_table, desc, type->size.mliter,
tank_info[i].name = strdup(desc); type->workingpressure.mbar / 1000);
tank_info[i].ml = type->size.mliter;
tank_info[i].bar = type->workingpressure.mbar / 1000;
}
} }
void add_weightsystem_description(const weightsystem_t *weightsystem) void add_weightsystem_description(const weightsystem_t *weightsystem)
@ -229,62 +247,63 @@ int find_best_gasmix_match(struct gasmix mix, const struct cylinder_table *cylin
* we should pick up any other names from the dive * we should pick up any other names from the dive
* logs directly. * logs directly.
*/ */
struct tank_info_t tank_info[MAX_TANK_INFO] = { struct tank_info_table tank_info_table;
void reset_tank_info_table(struct tank_info_table *table)
{
clear_tank_info_table(table);
/* Need an empty entry for the no-cylinder case */ /* Need an empty entry for the no-cylinder case */
{ "", }, add_tank_info_metric(table, "", 0, 0);
/* Size-only metric cylinders */ /* Size-only metric cylinders */
{ "10.0", .ml = 10000 }, add_tank_info_metric(table, "10.0", 10000, 0);
{ "11.1", .ml = 11100 }, add_tank_info_metric(table, "11.1", 11100, 0);
/* Most common AL cylinders */ /* Most common AL cylinders */
{ "AL40", .cuft = 40, .psi = 3000 }, add_tank_info_metric(table, "AL40", 40, 3000);
{ "AL50", .cuft = 50, .psi = 3000 }, add_tank_info_metric(table, "AL50", 50, 3000);
{ "AL63", .cuft = 63, .psi = 3000 }, add_tank_info_metric(table, "AL63", 63, 3000);
{ "AL72", .cuft = 72, .psi = 3000 }, add_tank_info_metric(table, "AL72", 72, 3000);
{ "AL80", .cuft = 80, .psi = 3000 }, add_tank_info_metric(table, "AL80", 80, 3000);
{ "AL100", .cuft = 100, .psi = 3300 }, add_tank_info_metric(table, "AL100", 100, 3300);
/* Metric AL cylinders */ /* Metric AL cylinders */
{ "ALU7", .ml = 7000, .bar = 200 }, add_tank_info_metric(table, "ALU7", 7000, 200);
/* Somewhat common LP steel cylinders */ /* Somewhat common LP steel cylinders */
{ "LP85", .cuft = 85, .psi = 2640 }, add_tank_info_imperial(table, "LP85", 85, 2640);
{ "LP95", .cuft = 95, .psi = 2640 }, add_tank_info_imperial(table, "LP95", 95, 2640);
{ "LP108", .cuft = 108, .psi = 2640 }, add_tank_info_imperial(table, "LP108", 108, 2640);
{ "LP121", .cuft = 121, .psi = 2640 }, add_tank_info_imperial(table, "LP121", 121, 2640);
/* Somewhat common HP steel cylinders */ /* Somewhat common HP steel cylinders */
{ "HP65", .cuft = 65, .psi = 3442 }, add_tank_info_imperial(table, "HP65", 65, 3442);
{ "HP80", .cuft = 80, .psi = 3442 }, add_tank_info_imperial(table, "HP80", 80, 3442);
{ "HP100", .cuft = 100, .psi = 3442 }, add_tank_info_imperial(table, "HP100", 100, 3442);
{ "HP117", .cuft = 117, .psi = 3442 }, add_tank_info_imperial(table, "HP117", 117, 3442);
{ "HP119", .cuft = 119, .psi = 3442 }, add_tank_info_imperial(table, "HP119", 119, 3442);
{ "HP130", .cuft = 130, .psi = 3442 }, add_tank_info_imperial(table, "HP130", 130, 3442);
/* Common European steel cylinders */ /* Common European steel cylinders */
{ "3 232 bar", .ml = 3000, .bar = 232 }, add_tank_info_metric(table, "3 232 bar", 3000, 232);
{ "3 300 bar", .ml = 3000, .bar = 300 }, add_tank_info_metric(table, "3 300 bar", 3000, 300);
{ "10 200 bar", .ml = 10000, .bar = 200 }, add_tank_info_metric(table, "10 200 bar", 10000, 200);
{ "10 232 bar", .ml = 10000, .bar = 232 }, add_tank_info_metric(table, "10 232 bar", 10000, 232);
{ "10 300 bar", .ml = 10000, .bar = 300 }, add_tank_info_metric(table, "10 300 bar", 10000, 300);
{ "12 200 bar", .ml = 12000, .bar = 200 }, add_tank_info_metric(table, "12 200 bar", 12000, 200);
{ "12 232 bar", .ml = 12000, .bar = 232 }, add_tank_info_metric(table, "12 232 bar", 12000, 232);
{ "12 300 bar", .ml = 12000, .bar = 300 }, add_tank_info_metric(table, "12 300 bar", 12000, 300);
{ "15 200 bar", .ml = 15000, .bar = 200 }, add_tank_info_metric(table, "15 200 bar", 15000, 200);
{ "15 232 bar", .ml = 15000, .bar = 232 }, add_tank_info_metric(table, "15 232 bar", 15000, 232);
{ "D7 300 bar", .ml = 14000, .bar = 300 }, add_tank_info_metric(table, "D7 300 bar", 14000, 300);
{ "D8.5 232 bar", .ml = 17000, .bar = 232 }, add_tank_info_metric(table, "D8.5 232 bar", 17000, 232);
{ "D12 232 bar", .ml = 24000, .bar = 232 }, add_tank_info_metric(table, "D12 232 bar", 24000, 232);
{ "D13 232 bar", .ml = 26000, .bar = 232 }, add_tank_info_metric(table, "D13 232 bar", 26000, 232);
{ "D15 232 bar", .ml = 30000, .bar = 232 }, add_tank_info_metric(table, "D15 232 bar", 30000, 232);
{ "D16 232 bar", .ml = 32000, .bar = 232 }, add_tank_info_metric(table, "D16 232 bar", 32000, 232);
{ "D18 232 bar", .ml = 36000, .bar = 232 }, add_tank_info_metric(table, "D18 232 bar", 36000, 232);
{ "D20 232 bar", .ml = 40000, .bar = 232 }, add_tank_info_metric(table, "D20 232 bar", 40000, 232);
}
/* We'll fill in more from the dive log dynamically */
{ NULL, }
};
/* /*
* We hardcode the most common weight system types * We hardcode the most common weight system types
@ -402,30 +421,27 @@ cylinder_t *get_or_create_cylinder(struct dive *d, int idx)
void fill_default_cylinder(const struct dive *dive, cylinder_t *cyl) void fill_default_cylinder(const struct dive *dive, cylinder_t *cyl)
{ {
const char *cyl_name = prefs.default_cylinder; const char *cyl_name = prefs.default_cylinder;
struct tank_info_t *ti = tank_info;
pressure_t pO2 = {.mbar = lrint(prefs.modpO2 * 1000.0)}; pressure_t pO2 = {.mbar = lrint(prefs.modpO2 * 1000.0)};
if (!cyl_name) if (!cyl_name)
return; return;
while (ti->name != NULL && ti < tank_info + MAX_TANK_INFO) { for (int i = 0; i < tank_info_table.nr; ++i) {
if (strcmp(ti->name, cyl_name) == 0) struct tank_info *ti = &tank_info_table.infos[i];
break; if (strcmp(ti->name, cyl_name) == 0) {
ti++; cyl->type.description = strdup(ti->name);
if (ti->ml) {
cyl->type.size.mliter = ti->ml;
cyl->type.workingpressure.mbar = ti->bar * 1000;
} else {
cyl->type.workingpressure.mbar = psi_to_mbar(ti->psi);
if (ti->psi)
cyl->type.size.mliter = lrint(cuft_to_l(ti->cuft) * 1000 / bar_to_atm(psi_to_bar(ti->psi)));
}
// MOD of air
cyl->depth = gas_mod(cyl->gasmix, pO2, dive, 1);
return;
}
} }
if (ti->name == NULL)
/* didn't find it */
return;
cyl->type.description = strdup(ti->name);
if (ti->ml) {
cyl->type.size.mliter = ti->ml;
cyl->type.workingpressure.mbar = ti->bar * 1000;
} else {
cyl->type.workingpressure.mbar = psi_to_mbar(ti->psi);
if (ti->psi)
cyl->type.size.mliter = lrint(cuft_to_l(ti->cuft) * 1000 / bar_to_atm(psi_to_bar(ti->psi)));
}
// MOD of air
cyl->depth = gas_mod(cyl->gasmix, pO2, dive, 1);
} }
cylinder_t create_new_cylinder(const struct dive *d) cylinder_t create_new_cylinder(const struct dive *d)

View file

@ -68,7 +68,6 @@ struct weightsystem_table {
weightsystem_t *weightsystems; weightsystem_t *weightsystems;
}; };
#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);
@ -110,11 +109,21 @@ extern void add_cylinder(struct cylinder_table *, int idx, cylinder_t cyl);
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);
struct tank_info_t { typedef struct tank_info {
const char *name; const char *name;
int cuft, ml, psi, bar; int cuft, ml, psi, bar;
} tank_info_t;
struct tank_info_table {
int nr, allocated;
struct tank_info *infos;
}; };
extern struct tank_info_t tank_info[MAX_TANK_INFO];
extern struct tank_info_table tank_info_table;
extern void reset_tank_info_table(struct tank_info_table *table);
extern void clear_tank_info_table(struct tank_info_table *table);
extern void add_tank_info_metric(struct tank_info_table *table, const char *name, int ml, int bar);
extern void add_tank_info_imperial(struct tank_info_table *table, const char *name, int cuft, int psi);
struct ws_info_t { struct ws_info_t {
const char *name; const char *name;

View file

@ -241,8 +241,8 @@ QStringList getFullCylinderList()
addStringToSortedList(cylinders, get_cylinder(d, j)->type.description); addStringToSortedList(cylinders, get_cylinder(d, j)->type.description);
} }
for (int ti = 0; ti < MAX_TANK_INFO; ti++) for (int ti = 0; ti < tank_info_table.nr; ti++)
addStringToSortedList(cylinders, tank_info[ti].name); addStringToSortedList(cylinders, tank_info_table.infos[ti].name);
return cylinders; return cylinders;
} }

View file

@ -24,9 +24,10 @@ void PreferencesEquipment::refreshSettings()
{ {
ui->display_unused_tanks->setChecked(prefs.display_unused_tanks); ui->display_unused_tanks->setChecked(prefs.display_unused_tanks);
ui->default_cylinder->clear(); ui->default_cylinder->clear();
for (int i = 0; i < MAX_TANK_INFO && tank_info[i].name != NULL; i++) { for (int i = 0; i < tank_info_table.nr; i++) {
ui->default_cylinder->addItem(tank_info[i].name); const tank_info &ti = tank_info_table.infos[i];
if (qPrefEquipment::default_cylinder() == tank_info[i].name) ui->default_cylinder->addItem(ti.name);
if (qPrefEquipment::default_cylinder() == ti.name)
ui->default_cylinder->setCurrentIndex(i); ui->default_cylinder->setCurrentIndex(i);
} }
} }

View file

@ -1156,20 +1156,20 @@ void QMLManager::commitChanges(QString diveId, QString number, QString date, QSt
// info for first cylinder // info for first cylinder
if (myDive.getCylinder != usedCylinder) { if (myDive.getCylinder != usedCylinder) {
diveChanged = true; diveChanged = true;
unsigned long i;
int size = 0, wp = 0, j = 0, k = 0; int size = 0, wp = 0, j = 0, k = 0;
for (j = 0; k < usedCylinder.length(); j++) { for (j = 0; k < usedCylinder.length(); j++) {
if (state != "add" && !is_cylinder_used(d, j)) if (state != "add" && !is_cylinder_used(d, j))
continue; continue;
for (i = 0; i < MAX_TANK_INFO && tank_info[i].name != NULL; i++) { for (int i = 0; i < tank_info_table.nr; i++) {
if (tank_info[i].name == usedCylinder[k] ) { const tank_info &ti = tank_info_table.infos[i];
if (tank_info[i].ml > 0){ if (ti.name == usedCylinder[k] ) {
size = tank_info[i].ml; if (ti.ml > 0){
wp = tank_info[i].bar * 1000; size = ti.ml;
wp = ti.bar * 1000;
} else { } else {
size = (int) (cuft_to_l(tank_info[i].cuft) * 1000 / bar_to_atm(psi_to_bar(tank_info[i].psi))); size = (int) (cuft_to_l(ti.cuft) * 1000 / bar_to_atm(psi_to_bar(ti.psi)));
wp = psi_to_mbar(tank_info[i].psi); wp = psi_to_mbar(ti.psi);
} }
break; break;
} }
@ -1816,8 +1816,8 @@ QStringList QMLManager::cylinderInit() const
} }
} }
for (unsigned long ti = 0; ti < MAX_TANK_INFO && tank_info[ti].name != NULL; ti++) { for (int ti = 0; ti < tank_info_table.nr; ti++) {
QString cyl = tank_info[ti].name; QString cyl = tank_info_table.infos[ti].name;
if (cyl == "") if (cyl == "")
continue; continue;
cylinders << cyl; cylinders << cyl;

View file

@ -13,8 +13,9 @@ TankInfoModel *TankInfoModel::instance()
bool TankInfoModel::insertRows(int, int count, const QModelIndex &parent) bool TankInfoModel::insertRows(int, int count, const QModelIndex &parent)
{ {
beginInsertRows(parent, rowCount(), rowCount()); beginInsertRows(parent, rowCount(), rowCount() + count - 1);
rows += count; for (int i = 0; i < count; ++i)
add_tank_info_metric(&tank_info_table, "", 0, 0);
endInsertRows(); endInsertRows();
return true; return true;
} }
@ -23,64 +24,55 @@ bool TankInfoModel::setData(const QModelIndex &index, const QVariant &value, int
{ {
//WARN Seems wrong, we need to check for role == Qt::EditRole //WARN Seems wrong, we need to check for role == Qt::EditRole
if (index.row() < 0 || index.row() > MAX_TANK_INFO - 1) if (index.row() < 0 || index.row() >= tank_info_table.nr )
return false; return false;
struct tank_info_t *info = &tank_info[index.row()]; struct tank_info &info = tank_info_table.infos[index.row()];
switch (index.column()) { switch (index.column()) {
case DESCRIPTION: case DESCRIPTION:
info->name = strdup(value.toByteArray().data()); free((void *)info.name);
info.name = strdup(value.toByteArray().data());
break; break;
case ML: case ML:
info->ml = value.toInt(); info.ml = value.toInt();
break; break;
case BAR: case BAR:
info->bar = value.toInt(); info.bar = value.toInt();
break; break;
} }
emit dataChanged(index, index); emit dataChanged(index, index);
return true; return true;
} }
void TankInfoModel::clear()
{
}
QVariant TankInfoModel::data(const QModelIndex &index, int role) const QVariant TankInfoModel::data(const QModelIndex &index, int role) const
{ {
QVariant ret; if (!index.isValid() || index.row() < 0 || index.row() >= tank_info_table.nr)
if (!index.isValid() || index.row() < 0 || index.row() > MAX_TANK_INFO - 1) { return QVariant();
return ret; if (role == Qt::FontRole)
}
if (role == Qt::FontRole) {
return defaultModelFont(); return defaultModelFont();
}
if (role == Qt::DisplayRole || role == Qt::EditRole) { if (role == Qt::DisplayRole || role == Qt::EditRole) {
struct tank_info_t *info = &tank_info[index.row()]; const struct tank_info &info = tank_info_table.infos[index.row()];
int ml = info->ml; int ml = info.ml;
double bar = (info->psi) ? psi_to_bar(info->psi) : info->bar; double bar = (info.psi) ? psi_to_bar(info.psi) : info.bar;
if (info->cuft && info->psi) if (info.cuft && info.psi)
ml = lrint(cuft_to_l(info->cuft) * 1000 / bar_to_atm(bar)); ml = lrint(cuft_to_l(info.cuft) * 1000 / bar_to_atm(bar));
switch (index.column()) { switch (index.column()) {
case BAR: case BAR:
ret = bar * 1000; return bar * 1000;
break;
case ML: case ML:
ret = ml; return ml;
break;
case DESCRIPTION: case DESCRIPTION:
ret = QString(info->name); return info.name;
break;
} }
} }
return ret; return QVariant();
} }
int TankInfoModel::rowCount(const QModelIndex&) const int TankInfoModel::rowCount(const QModelIndex&) const
{ {
return rows; return tank_info_table.nr;
} }
TankInfoModel::TankInfoModel() TankInfoModel::TankInfoModel()
@ -94,8 +86,5 @@ TankInfoModel::TankInfoModel()
void TankInfoModel::update() void TankInfoModel::update()
{ {
beginResetModel(); beginResetModel();
rows = 0;
for (struct tank_info_t *info = tank_info; info->name && info < tank_info + MAX_TANK_INFO; info++, rows++)
;
endResetModel(); endResetModel();
} }

View file

@ -22,13 +22,9 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override;
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
void clear();
public public
slots: slots:
void update(); void update();
private:
int rows;
}; };
#endif #endif

View file

@ -77,6 +77,7 @@ int main(int argc, char **argv)
setup_system_prefs(); setup_system_prefs();
copy_prefs(&default_prefs, &prefs); copy_prefs(&default_prefs, &prefs);
fill_computer_list(); fill_computer_list();
reset_tank_info_table(&tank_info_table);
parse_xml_init(); parse_xml_init();
taglist_init_global(); taglist_init_global();
init_ui(); init_ui();

View file

@ -56,6 +56,7 @@ int main(int argc, char **argv)
default_prefs.units = IMPERIAL_units; default_prefs.units = IMPERIAL_units;
copy_prefs(&default_prefs, &prefs); copy_prefs(&default_prefs, &prefs);
fill_computer_list(); fill_computer_list();
reset_tank_info_table(&tank_info_table);
parse_xml_init(); parse_xml_init();
taglist_init_global(); taglist_init_global();