diff --git a/fvwm/infostore.c b/fvwm/infostore.c index 6ba561ced..08ed372a6 100644 --- a/fvwm/infostore.c +++ b/fvwm/infostore.c @@ -37,8 +37,6 @@ /* ---------------------------- local types -------------------------------- */ -static MetaInfo *mi_store; - /* ---------------------------- forward declarations ----------------------- */ static void delete_metainfo(const char *); @@ -47,23 +45,19 @@ static void delete_metainfo(const char *); /* ---------------------------- exported variables (globals) --------------- */ -/* ---------------------------- local functions ---------------------------- */ - -MetaInfo *new_metainfo(void) -{ - MetaInfo *mi; - - mi = fxcalloc(1, sizeof *mi); +struct meta_infos meta_info_q; - return mi; -} +/* ---------------------------- local functions ---------------------------- */ 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 +72,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 = NULL; - 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,56 +105,32 @@ 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; } -int get_metainfo_length(void) -{ - MetaInfo *mi; - int count; - - count = 0; - - for(mi = mi_store; mi; mi = mi->next) - count++; - - return count; -} - -MetaInfo * -get_metainfo(void) -{ - return mi_store; -} - void print_infostore(void) { MetaInfo *mi; - fvwm_debug(__func__, "Current items in infostore (key, value):\n\n"); - - if (get_metainfo_length() == 0) - { + if (TAILQ_EMPTY(&meta_info_q)) { fvwm_debug(__func__, "No items are currently stored in the infostore.\n"); return; } - for(mi = mi_store; mi; mi = mi->next) - { + 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); - } return; - } /* ---------------------------- interface functions ------------------------ */ @@ -189,6 +155,9 @@ void CMD_InfoStoreAdd(F_CMD_ARGS) if (!key || !value) { fvwm_debug(__func__, "Bad arguments given."); + if (key) + free(key); + return; } @@ -218,11 +187,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..70e4334eb 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,8 @@ 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..30cc4a611 100644 --- a/fvwm/session.c +++ b/fvwm/session.c @@ -177,13 +177,13 @@ SaveGlobalState(FILE *f) f, " [STYLE] %i %i\n", Scr.gs.do_emulate_mwm, Scr.gs.do_emulate_win); - if (get_metainfo_length() > 0) { - MetaInfo *mi = get_metainfo(), *mi_i; + if (!TAILQ_EMPTY(&meta_info_q)) { + 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); } }