diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2013-01-09 12:07:09 -0800 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2013-01-09 16:19:38 -0800 |
commit | ec38d3708df24a23a46f19707f0f5afd44969c92 (patch) | |
tree | e41066bcc58df363d4c8f241279a95558e5a79ca /device.c | |
parent | 33c67cc61960545216e31fa2c420cdac94da03f0 (diff) | |
download | subsurface-ec38d3708df24a23a46f19707f0f5afd44969c92.tar.gz |
Move device_info handling into a new 'device.c' file
The legacy nickname wrappers (that use the device_info structure) are
left in gtk-gui.c. We can slowly start moving away from them, we don't
want to start exporting that thing as some kind of generic interface.
This isn't a pure code movement - because we leave the legacy interfaces
alone, there are a few new interfaces in device.c (like "create a new
device_info entry") that were embedded into the legacy "create nickname"
code, and needed to be abstracted out.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'device.c')
-rw-r--r-- | device.c | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/device.c b/device.c new file mode 100644 index 000000000..39f742acb --- /dev/null +++ b/device.c @@ -0,0 +1,94 @@ +#include <string.h> +#include "dive.h" +#include "device.h" + +static struct device_info *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; +} + +void clear_device_saved_status(void) +{ + struct device_info *nn_entry = device_info_list; + + while (nn_entry) { + nn_entry->saved = FALSE; + nn_entry = nn_entry->next; + } +} + +/* 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; +} |