From fbe17e620e7b92b2af0d65d7b4c8fe688f2b6058 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Tue, 17 Aug 2021 11:05:20 -0700 Subject: core: add get_or_add helper for dc table This makes it much easier to manipulate dc nickname entries. In order for that to work we can't simply remove entries with empty nickname (but that isn't needed, anyway, as the code that saves XML or git already handles that case correctly). Signed-off-by: Dirk Hohndel --- core/device.cpp | 19 ++++++++++++------- core/device.h | 1 + 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/core/device.cpp b/core/device.cpp index 42c2be004..a01893f60 100644 --- a/core/device.cpp +++ b/core/device.cpp @@ -44,6 +44,18 @@ extern "C" const struct device *get_device_for_dc(const struct device_table *tab return it != dcs.end() && same_device(*it, dev) ? &*it : NULL; } +extern "C" int get_or_add_device_for_dc(struct device_table *table, const struct divecomputer *dc) +{ + if (!dc->model || !dc->serial) + return -1; + const struct device *dev = get_device_for_dc(table, dc); + if (dev) { + auto it = std::lower_bound(table->devices.begin(), table->devices.end(), *dev); + return it - table->devices.begin(); + } + return create_device_node(table, dc->model, dc->serial, ""); +} + extern "C" bool device_exists(const struct device_table *device_table, const struct device *dev) { auto it = std::lower_bound(device_table->devices.begin(), device_table->devices.end(), *dev); @@ -71,16 +83,9 @@ static int addDC(std::vector &dcs, const std::string &m, const std::stri if (verbose) it->showchanges(n); // Update any non-existent fields from the old entry - if (n.empty()) { - dcs.erase(it); - return -1; - } it->nickName = n; return it - dcs.begin(); } else { - if (n.empty()) - return -1; - dev.deviceId = calculate_string_hash(s.c_str()); dcs.insert(it, dev); return it - dcs.begin(); diff --git a/core/device.h b/core/device.h index 6bfb85e3c..9e0a90ac9 100644 --- a/core/device.h +++ b/core/device.h @@ -25,6 +25,7 @@ const char *get_dc_nickname(const struct divecomputer *dc); extern bool device_used_by_selected_dive(const struct device *dev); extern const struct device *get_device_for_dc(const struct device_table *table, const struct divecomputer *dc); +extern int get_or_add_device_for_dc(struct device_table *table, const struct divecomputer *dc); extern bool device_exists(const struct device_table *table, const struct device *dev); extern int add_to_device_table(struct device_table *table, const struct device *dev); // returns index extern int remove_device(struct device_table *table, const struct device *dev); // returns index or -1 if not found -- cgit v1.2.3-70-g09d2