diff options
-rw-r--r-- | core/device.cpp | 19 | ||||
-rw-r--r-- | 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<device> &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 |