diff --git a/fvwm/infostore.c b/fvwm/infostore.c index 6ba561ced..6f7360dcb 100644 --- a/fvwm/infostore.c +++ b/fvwm/infostore.c @@ -37,10 +37,9 @@ /* ---------------------------- local types -------------------------------- */ -static MetaInfo *mi_store; - /* ---------------------------- forward declarations ----------------------- */ +struct meta_infos meta_info_q; static void delete_metainfo(const char *); /* ---------------------------- local variables ---------------------------- */ @@ -49,21 +48,15 @@ static void delete_metainfo(const char *); /* ---------------------------- local functions ---------------------------- */ -MetaInfo *new_metainfo(void) -{ - MetaInfo *mi; - - mi = fxcalloc(1, sizeof *mi); - - return mi; -} - void insert_metainfo(char *key, char *value) { MetaInfo *mi; MetaInfo *mi_new; - for (mi = mi_store; mi; mi = mi->next) + if (TAILQ_EMPTY(&meta_info_q)) + TAILQ_INIT(&meta_info_q); + + TAILQ_FOREACH(mi, &meta_info_q, entry) { if (StrEquals(mi->key, key)) { @@ -78,36 +71,32 @@ void insert_metainfo(char *key, char *value) } /* It's a new item, add it to the list. */ - mi_new = new_metainfo(); + mi_new = fxcalloc(1, sizeof *mi_new); mi_new->key = fxstrdup(key); CopyString(&mi_new->value, value); - mi_new->next = mi_store; - mi_store = mi_new; + TAILQ_INSERT_TAIL(&meta_info_q, mi_new, entry); return; } static void delete_metainfo(const char *key) { - MetaInfo *mi_current, *mi_prev; - mi_prev = NULL; + MetaInfo *mi; - for(mi_current = mi_store; mi_current != NULL; - mi_prev = mi_current, mi_current = mi_current->next) + TAILQ_FOREACH(mi, &meta_info_q, entry) { - if (StrEquals(mi_current->key, key)) { - if (mi_prev == NULL) - mi_store = mi_current->next; - else - mi_prev->next = mi_current->next; + if (StrEquals(mi->key, key)) + break; + } - free(mi_current->key); - free(mi_current->value); - free(mi_current); + if (mi != NULL) + { + TAILQ_REMOVE(&meta_info_q, mi, entry); - break; - } + free(mi->key); + free(mi->value); + free(mi); } return; @@ -115,12 +104,12 @@ static void delete_metainfo(const char *key) inline char *get_metainfo_value(const char *key) { - MetaInfo *mi_current; + MetaInfo *mi; - for(mi_current = mi_store; mi_current; mi_current = mi_current->next) + TAILQ_FOREACH(mi, &meta_info_q, entry) { - if (StrEquals(mi_current->key, key)) - return mi_current->value; + if (StrEquals(mi->key, key)) + return mi->value; } return NULL; @@ -129,42 +118,34 @@ inline char *get_metainfo_value(const char *key) int get_metainfo_length(void) { MetaInfo *mi; - int count; + int count = 0; - count = 0; - - for(mi = mi_store; mi; mi = mi->next) + TAILQ_FOREACH(mi, &meta_info_q, entry) count++; return count; } -MetaInfo * -get_metainfo(void) -{ - return mi_store; -} - void print_infostore(void) { MetaInfo *mi; + bool not_found = 1; fvwm_debug(__func__, "Current items in infostore (key, value):\n\n"); + TAILQ_FOREACH(mi, &meta_info_q, entry) + { + fvwm_debug(__func__, "%s\t%s\n", mi->key, mi->value); + not_found = 0; + } - if (get_metainfo_length() == 0) + if (not_found) { fvwm_debug(__func__, "No items are currently stored in the infostore.\n"); return; } - for(mi = mi_store; mi; mi = mi->next) - { - fvwm_debug(__func__, "%s\t%s\n", mi->key, mi->value); - } - return; - } /* ---------------------------- interface functions ------------------------ */ @@ -189,6 +170,9 @@ void CMD_InfoStoreAdd(F_CMD_ARGS) if (!key || !value) { fvwm_debug(__func__, "Bad arguments given."); + if (key) + free(key); + return; } @@ -218,11 +202,13 @@ void CMD_InfoStoreRemove(F_CMD_ARGS) void CMD_InfoStoreClear(F_CMD_ARGS) { - MetaInfo *mi; + MetaInfo *mi, *mi2; - if (get_metainfo_length() == 0) - return; - - for (mi = mi_store; mi; mi = mi->next) - delete_metainfo(mi->key); + TAILQ_FOREACH_SAFE(mi, &meta_info_q, entry, mi2) + { + TAILQ_REMOVE(&meta_info_q, mi, entry); + free(mi->key); + free(mi->value); + free(mi); + } } diff --git a/fvwm/infostore.h b/fvwm/infostore.h index 96f029483..55eb6bad8 100644 --- a/fvwm/infostore.h +++ b/fvwm/infostore.h @@ -11,13 +11,15 @@ /* ---------------------------- type definitions --------------------------- */ -typedef struct MetaInfo +typedef struct meta_info { char *key; char *value; - struct MetaInfo *next; + TAILQ_ENTRY(meta_info) entry; } MetaInfo; +TAILQ_HEAD(meta_infos, meta_info); +extern struct meta_infos meta_info_q; /* ---------------------------- forward declarations ----------------------- */ @@ -25,11 +27,9 @@ typedef struct MetaInfo /* ---------------------------- interface functions ------------------------ */ -MetaInfo *new_metainfo(void); void insert_metainfo(char *, char *); char *get_metainfo_value(const char *); int get_metainfo_length(void); -MetaInfo *get_metainfo(void); void print_infostore(void); #endif /* FVWM_INFOSTORE_H */ diff --git a/fvwm/session.c b/fvwm/session.c index 5ec4fdd51..fcff94491 100644 --- a/fvwm/session.c +++ b/fvwm/session.c @@ -178,12 +178,12 @@ SaveGlobalState(FILE *f) Scr.gs.do_emulate_win); if (get_metainfo_length() > 0) { - MetaInfo *mi = get_metainfo(), *mi_i; + MetaInfo *mi; fprintf(f, " [INFOSTORE]\n"); - for (mi_i = mi; mi_i; mi_i = mi_i->next) { - fprintf(f, " [KEY] %s\n", mi_i->key); - fprintf(f, " [VALUE] %s\n", mi_i->value); + TAILQ_FOREACH(mi, &meta_info_q, entry) { + fprintf(f, " [KEY] %s\n", mi->key); + fprintf(f, " [VALUE] %s\n", mi->value); } }