From ec38d3708df24a23a46f19707f0f5afd44969c92 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Wed, 9 Jan 2013 12:07:09 -0800 Subject: [PATCH] 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 Signed-off-by: Dirk Hohndel --- Makefile | 11 +++-- device.c | 94 +++++++++++++++++++++++++++++++++++++++++ device.h | 19 +++++++++ dive.h | 1 - gtk-gui.c | 122 ++++++----------------------------------------------- save-xml.c | 3 +- 6 files changed, 135 insertions(+), 115 deletions(-) create mode 100644 device.c create mode 100644 device.h diff --git a/Makefile b/Makefile index 900a2c89a..7c1c89d76 100644 --- a/Makefile +++ b/Makefile @@ -132,7 +132,7 @@ MSGOBJS=$(addprefix share/locale/,$(MSGLANGS:.po=.UTF-8/LC_MESSAGES/subsurface.m OBJS = main.o dive.o time.o profile.o info.o equipment.o divelist.o deco.o planner.o \ parse-xml.o save-xml.o libdivecomputer.o print.o uemis.o uemis-downloader.o \ - gtk-gui.o statistics.o file.o cochran.o $(OSSUPPORT).o $(RESFILE) + gtk-gui.o statistics.o file.o cochran.o device.o $(OSSUPPORT).o $(RESFILE) $(NAME): $(OBJS) $(MSGOBJS) $(CC) $(LDFLAGS) -o $(NAME) $(OBJS) $(LIBS) @@ -214,7 +214,7 @@ cochran.o: cochran.c dive.h file.h parse-xml.o: parse-xml.c dive.h $(CC) $(CFLAGS) $(GLIB2CFLAGS) $(XML2CFLAGS) $(XSLT) -c parse-xml.c -save-xml.o: save-xml.c dive.h +save-xml.o: save-xml.c dive.h device.h $(CC) $(CFLAGS) $(GLIB2CFLAGS) $(XML2CFLAGS) -c save-xml.c dive.o: dive.c dive.h @@ -250,12 +250,12 @@ deco.o: deco.c dive.h planner.o: planner.c dive.h divelist.h display-gtk.h $(CC) $(CFLAGS) $(GTK2CFLAGS) $(GLIB2CFLAGS) -c planner.c -libdivecomputer.o: libdivecomputer.c dive.h display.h display-gtk.h libdivecomputer.h +libdivecomputer.o: libdivecomputer.c dive.h display.h display-gtk.h libdivecomputer.h device.h $(CC) $(CFLAGS) $(GTK2CFLAGS) $(GLIB2CFLAGS) $(XML2CFLAGS) \ $(LIBDIVECOMPUTERCFLAGS) \ -c libdivecomputer.c -gtk-gui.o: gtk-gui.c dive.h display.h divelist.h display-gtk.h libdivecomputer.h Makefile +gtk-gui.o: gtk-gui.c dive.h display.h divelist.h display-gtk.h libdivecomputer.h device.h Makefile $(CC) $(CFLAGS) $(GTK2CFLAGS) $(GLIB2CFLAGS) $(GCONF2CFLAGS) $(XML2CFLAGS) \ $(LIBDIVECOMPUTERCFLAGS) \ -DVERSION_STRING='"v$(VERSION)"' \ @@ -267,6 +267,9 @@ uemis.o: uemis.c dive.h uemis.h uemis-downloader.o: uemis-downloader.c dive.h uemis.h $(CC) $(CFLAGS) $(GTK2CFLAGS) $(GLIB2CFLAGS) $(XML2CFLAGS) -c uemis-downloader.c +device.o: device.c device.h dive.h + $(CC) $(CFLAGS) $(GLIB2CFLAGS) -c device.c + $(OSSUPPORT).o: $(OSSUPPORT).c display-gtk.h $(CC) $(CFLAGS) $(OSSUPPORT_CFLAGS) -c $(OSSUPPORT).c 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 +#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; +} diff --git a/device.h b/device.h new file mode 100644 index 000000000..5a207f256 --- /dev/null +++ b/device.h @@ -0,0 +1,19 @@ +#ifndef DEVICE_INFO_H +#define DEVICE_INFO_H + +struct device_info { + const char *model; + uint32_t deviceid; + + const char *nickname; + struct device_info *next; + gboolean saved; +}; + +extern struct device_info *get_device_info(const char *model, uint32_t deviceid); +extern struct device_info *get_different_device_info(const char *model, uint32_t deviceid); +extern struct device_info *create_device_info(const char *model, uint32_t deviceid); +extern struct device_info *remove_device_info(const char *model, uint32_t deviceid); +extern void clear_device_saved_status(void); + +#endif diff --git a/dive.h b/dive.h index bf2a4b841..0151c6a72 100644 --- a/dive.h +++ b/dive.h @@ -544,7 +544,6 @@ extern const char *get_dc_nickname(const char *model, uint32_t deviceid); extern void remember_dc(const char *model, uint32_t deviceid, const char *nickname, gboolean change_conf); extern gboolean dc_was_saved(struct divecomputer *dc); extern void mark_dc_saved(struct divecomputer *dc); -extern void clear_dc_saved_status(void); extern void set_autogroup(gboolean value); extern int total_weight(struct dive *); diff --git a/gtk-gui.c b/gtk-gui.c index a89ac35dd..de5128cf7 100644 --- a/gtk-gui.c +++ b/gtk-gui.c @@ -19,6 +19,7 @@ #include "display.h" #include "display-gtk.h" #include "uemis.h" +#include "device.h" #include "libdivecomputer.h" @@ -41,15 +42,6 @@ struct preferences prefs = { FALSE, FALSE, FALSE, 0.30, 0.75 }; -struct device_info { - const char *model; - uint32_t deviceid; - - const char *nickname; - struct device_info *next; - gboolean saved; -}; -static struct device_info *device_info_list; char *nicknamestring; static GtkWidget *dive_profile; @@ -2194,25 +2186,6 @@ void set_filename(const char *filename, gboolean force) existing_filename = NULL; } -/* just find the entry for this divecomputer */ -static struct device_info *get_device_info(const char *model, int 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) - return NULL; - if (!model) - model = ""; - while (known) { - if (!strcmp(known->model, model) && known->deviceid == deviceid) - return known; - known = known->next; - } - return NULL; -} - const char *get_dc_nickname(const char *model, uint32_t deviceid) { struct device_info *known = get_device_info(model, deviceid); @@ -2238,36 +2211,6 @@ void mark_dc_saved(struct divecomputer *dc) nn_entry->saved = TRUE; } -void clear_dc_saved_status() -{ - 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? */ -static struct device_info *get_different_dc_nicknameentry(const char *model, int 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; -} - /* no curly braces or commas, please */ static char *cleanedup_nickname(const char *nickname, int len) { @@ -2332,21 +2275,13 @@ bail: void remove_dc(const char *model, uint32_t deviceid, gboolean change_conf) { - struct device_info *nn_entry, **prevp = &device_info_list; char pattern[160]; char *nnstring, *brace; + struct device_info *entry; - if (!deviceid || !model || !*model) + entry = remove_device_info(model, deviceid); + if (!entry) return; - nn_entry = get_device_info(model, deviceid); - if (!nn_entry) - return; - while (prevp && *prevp != nn_entry) - prevp = &(*prevp)->next; - if (!prevp) - /* that should be impossible */ - goto bail; - (*prevp) = nn_entry->next; /* now remove it from the config string */ snprintf(pattern, sizeof(pattern), "{%08x,%s", deviceid, model); @@ -2362,40 +2297,24 @@ void remove_dc(const char *model, uint32_t deviceid, gboolean change_conf) if (change_conf) subsurface_set_conf("dc_nicknames", PREF_STRING, nicknamestring); -#if defined(NICKNAME_DEBUG) - struct device_info *nn_entry = device_info_list; - fprintf(debugfile, "nicknames:\n"); - while (nn_entry) { - fprintf(debugfile, "id %8x model %s nickname %s\n", nn_entry->deviceid, nn_entry->model, - nn_entry->nickname && *nn_entry->nickname ? nn_entry->nickname : "(none)"); - nn_entry = nn_entry->next; - } - fprintf(debugfile, "----------\n"); -#endif - bail: - free(nn_entry); + free(entry); } void remember_dc(const char *model, uint32_t deviceid, const char *nickname, gboolean change_conf) { - /* we don't want to record entries with a deviceid of 0; those are from earlier - * development versions of Subsurface before we stored the hash in the divecomputer - * entries... we don't know which actual divecomputer those entries are from */ - if (!deviceid) + struct device_info *nn_entry; + + nn_entry = create_device_info(model, deviceid); + if (!nn_entry) return; - if (!nickname) - nickname = ""; - if (!get_dc_nickname(model, deviceid)) { + + /* No existing nickname? */ + if (!nn_entry->nickname) { char buffer[160]; - struct device_info *nn_entry = malloc(sizeof(struct device_info)); - nn_entry->deviceid = deviceid; - nn_entry->model = strdup(model); /* make sure there are no curly braces or commas in the string and that * it will fit in the buffer */ nn_entry->nickname = cleanedup_nickname(nickname, sizeof(buffer) - 13 - strlen(model)); - nn_entry->next = device_info_list; - device_info_list = nn_entry; if (*nickname != '\0') snprintf(buffer, sizeof(buffer), "{%08x,%s,%s}", deviceid, model, nn_entry->nickname); else @@ -2403,26 +2322,11 @@ void remember_dc(const char *model, uint32_t deviceid, const char *nickname, gbo nicknamestring = realloc(nicknamestring, strlen(nicknamestring) + strlen(buffer) + 1); strcat(nicknamestring, buffer); } else { - /* modify existing entry */ - struct device_info *nn_entry = get_device_info(model, deviceid); - if (!nn_entry->model || !*nn_entry->model) - nn_entry->model = model; nn_entry->nickname = cleanedup_nickname(nickname, 80); replace_nickname_nicknamestring(model, deviceid, nickname); } if (change_conf) subsurface_set_conf("dc_nicknames", PREF_STRING, nicknamestring); - -#if defined(NICKNAME_DEBUG) - struct device_info *nn_entry = device_info_list; - fprintf(debugfile, "nicknames:\n"); - while (nn_entry) { - fprintf(debugfile, "id %8x model %s nickname %s\n", nn_entry->deviceid, nn_entry->model, - nn_entry->nickname && *nn_entry->nickname ? nn_entry->nickname : "(none)"); - nn_entry = nn_entry->next; - } - fprintf(debugfile, "----------\n"); -#endif } void set_dc_nickname(struct dive *dive) @@ -2440,7 +2344,7 @@ void set_dc_nickname(struct dive *dive) fprintf(debugfile, "set_dc_nickname for model %s deviceid %8x\n", dc->model ? : "", dc->deviceid); #endif if (get_dc_nickname(dc->model, dc->deviceid) == NULL) { - struct device_info *nn_entry = get_different_dc_nicknameentry(dc->model, dc->deviceid); + struct device_info *nn_entry = get_different_device_info(dc->model, dc->deviceid); if (!nn_entry) { /* just remember the dive computer without setting a nickname */ if (dc->model) diff --git a/save-xml.c b/save-xml.c index 7b5cd2b10..465010cd1 100644 --- a/save-xml.c +++ b/save-xml.c @@ -6,6 +6,7 @@ #include #include "dive.h" +#include "device.h" static void show_milli(FILE *f, const char *pre, int value, const char *unit, const char *post) { @@ -524,7 +525,7 @@ void save_dives(const char *filename) fprintf(f, "\n\n", VERSION); /* save the dive computer nicknames, if any */ - clear_dc_saved_status(); + clear_device_saved_status(); for_each_dive(i, dive) { struct divecomputer *dc = &dive->dc; while (dc) {