mirror of
https://github.com/subsurface/subsurface.git
synced 2024-12-01 06:30:26 +00:00
52050fdc92
We used to save dive computer information only if that dive computer was actually used in any of the dives we saved. But we can simplify the code if we just always save any dive computers we know about. And it does allow for some usage cases where you have nicknames for other peoples computers that you may not actively use, but you want to see if you end up loading multiple XML files in one go. So there's just no compelling reason to not just save all the info we have. And this will make it less painful to remove the "use system config for dive computer nicknames", because you can also use this to continue to gather dive computer info in a separate XML file if you want to. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
89 lines
2.2 KiB
C
89 lines
2.2 KiB
C
#include <string.h>
|
|
#include "dive.h"
|
|
#include "device.h"
|
|
|
|
static struct device_info *device_info_list;
|
|
|
|
struct device_info *head_of_device_info_list(void)
|
|
{
|
|
return device_info_list;
|
|
}
|
|
|
|
static int match_device_info(struct device_info *entry, const char *model, uint32_t deviceid)
|
|
{
|
|
return !strcmp(entry->model, model) && entry->deviceid == deviceid;
|
|
}
|
|
|
|
/* just find the entry for this divecomputer */
|
|
struct device_info *get_device_info(const char *model, uint32_t deviceid)
|
|
{
|
|
struct device_info *known = device_info_list;
|
|
|
|
/* a 0 deviceid doesn't get a nickname - those come from development
|
|
* versions of Subsurface that didn't store the deviceid in the divecomputer entries */
|
|
if (!deviceid || !model)
|
|
return NULL;
|
|
while (known) {
|
|
if (match_device_info(known, model, deviceid))
|
|
return known;
|
|
known = known->next;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/* Get an existing device info model or create a new one if valid */
|
|
struct device_info *create_device_info(const char *model, uint32_t deviceid)
|
|
{
|
|
struct device_info *entry;
|
|
|
|
if (!deviceid || !model || !*model)
|
|
return NULL;
|
|
entry = get_device_info(model, deviceid);
|
|
if (entry)
|
|
return entry;
|
|
entry = calloc(1, sizeof(*entry));
|
|
if (entry) {
|
|
entry->model = strdup(model);
|
|
entry->deviceid = deviceid;
|
|
entry->next = device_info_list;
|
|
device_info_list = entry;
|
|
}
|
|
return entry;
|
|
}
|
|
|
|
/* do we have a DIFFERENT divecomputer of the same model? */
|
|
struct device_info *get_different_device_info(const char *model, uint32_t deviceid)
|
|
{
|
|
struct device_info *known = device_info_list;
|
|
|
|
/* a 0 deviceid matches any DC of the same model - those come from development
|
|
* versions of Subsurface that didn't store the deviceid in the divecomputer entries */
|
|
if (!deviceid)
|
|
return NULL;
|
|
if (!model)
|
|
model = "";
|
|
while (known) {
|
|
if (known->model && !strcmp(known->model, model) &&
|
|
known->deviceid != deviceid)
|
|
return known;
|
|
known = known->next;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
struct device_info *remove_device_info(const char *model, uint32_t deviceid)
|
|
{
|
|
struct device_info *entry, **p;
|
|
|
|
if (!deviceid || !model || !*model)
|
|
return NULL;
|
|
p = &device_info_list;
|
|
while ((entry = *p) != NULL) {
|
|
if (match_device_info(entry, model, deviceid)) {
|
|
*p = entry->next;
|
|
break;
|
|
}
|
|
p = &entry->next;
|
|
}
|
|
return entry;
|
|
}
|