diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2018-03-25 10:01:36 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2018-03-25 10:01:36 +0200 |
commit | 8f3c9a73784ddba6dcf1ed942725e595183235ac (patch) | |
tree | 5b2c61cec7f4fe1811d5d91451b295b9387bb051 /src/plugins/irc/irc-modelist.c | |
parent | 126a8259248e477e3c9d8affd0f22b3197b790ee (diff) | |
download | weechat-8f3c9a73784ddba6dcf1ed942725e595183235ac.zip |
irc: reorder modelist functions
Diffstat (limited to 'src/plugins/irc/irc-modelist.c')
-rw-r--r-- | src/plugins/irc/irc-modelist.c | 328 |
1 files changed, 164 insertions, 164 deletions
diff --git a/src/plugins/irc/irc-modelist.c b/src/plugins/irc/irc-modelist.c index ec7099acf..6b814ca92 100644 --- a/src/plugins/irc/irc-modelist.c +++ b/src/plugins/irc/irc-modelist.c @@ -34,132 +34,169 @@ /* - * Checks if a modelist item pointer is valid for a modelist. + * Checks if a modelist pointer is valid for a channel. * * Returns: - * 1: item exists for modelist - * 0: item does not exist for modelist + * 1: modelist exists for channel + * 0: modelist does not exist for channel */ int -irc_modelist_item_valid (struct t_irc_modelist *modelist, - struct t_irc_modelist_item *item) +irc_modelist_valid (struct t_irc_channel *channel, + struct t_irc_modelist *modelist) { - struct t_irc_modelist_item *ptr_item; + struct t_irc_modelist *ptr_modelist; - if (!modelist || !item) + if (!channel || !modelist) return 0; - for (ptr_item = modelist->items; ptr_item; - ptr_item = ptr_item->next_item) + for (ptr_modelist = channel->modelists; ptr_modelist; + ptr_modelist = ptr_modelist->next_modelist) { - if (ptr_item == item) + if (ptr_modelist == modelist) return 1; } - /* item not found */ + /* modelist not found */ return 0; } /* - * Creates a new item in a modelist. + * Searches for a modelist by type. * - * Returns pointer to new item, NULL if error. + * Returns pointer to modelist found, NULL if not found. */ -struct t_irc_modelist_item * -irc_modelist_item_new (struct t_irc_modelist *modelist, - const char *mask, const char *setter, time_t datetime) +struct t_irc_modelist * +irc_modelist_search (struct t_irc_channel *channel, char type) { - struct t_irc_modelist_item *new_item; + struct t_irc_modelist *ptr_modelist; - /* alloc memory for new item */ - if ((new_item = malloc (sizeof (*new_item))) == NULL) + if (!channel) + return NULL; + + for (ptr_modelist = channel->modelists; ptr_modelist; + ptr_modelist = ptr_modelist->next_modelist) + { + if (ptr_modelist->type == type) + return ptr_modelist; + } + return NULL; +} + +/* + * Creates a new modelist in a channel. + * + * Returns pointer to new modelist, NULL if error. + */ + +struct t_irc_modelist * +irc_modelist_new (struct t_irc_channel *channel, char type) +{ + struct t_irc_modelist *new_modelist; + + /* alloc memory for new modelist */ + if ((new_modelist = malloc (sizeof (*new_modelist))) == NULL) { weechat_printf (NULL, - _("%s%s: cannot allocate new modelist item"), + _("%s%s: cannot allocate new modelist"), weechat_prefix ("error"), IRC_PLUGIN_NAME); return NULL; } - /* initialize new item */ - new_item->number = (modelist->last_item) ? - modelist->last_item->number + 1 : 0; - new_item->mask = strdup (mask); - new_item->setter = (setter) ? strdup (setter) : NULL; - new_item->datetime = datetime; + /* initialize new modelist */ + new_modelist->type = type; + new_modelist->state = IRC_MODELIST_STATE_EMPTY; + new_modelist->items = NULL; + new_modelist->last_item = NULL; - /* add new item to modelist */ - new_item->prev_item = modelist->last_item; - new_item->next_item = NULL; - if (modelist->items) - (modelist->last_item)->next_item = new_item; + /* add new modelist to channel */ + new_modelist->prev_modelist = channel->last_modelist; + new_modelist->next_modelist = NULL; + if (channel->modelists) + (channel->last_modelist)->next_modelist = new_modelist; else - modelist->items = new_item; - modelist->last_item = new_item; - - if ((modelist->state == IRC_MODELIST_STATE_EMPTY) || - (modelist->state == IRC_MODELIST_STATE_RECEIVED)) - { - modelist->state = IRC_MODELIST_STATE_MODIFIED; - } + channel->modelists = new_modelist; + channel->last_modelist = new_modelist; - /* all is OK, return address of new item */ - return new_item; + /* all is OK, return address of new modelist */ + return new_modelist; } /* - * Frees an item and remove it from modelist. + * Frees a modelist and remove it from channel. */ void -irc_modelist_item_free (struct t_irc_modelist *modelist, - struct t_irc_modelist_item *item) +irc_modelist_free (struct t_irc_channel *channel, + struct t_irc_modelist *modelist) { - struct t_irc_modelist_item *new_items; + struct t_irc_modelist *new_modelists; - if (!modelist || !item) + if (!channel || !modelist) return; - /* remove item from modelist list */ - if (modelist->last_item == item) - modelist->last_item = item->prev_item; - if (item->prev_item) + /* remove modelist from channel modelists */ + if (channel->last_modelist == modelist) + channel->last_modelist = modelist->prev_modelist; + if (modelist->prev_modelist) { - (item->prev_item)->next_item = item->next_item; - new_items = modelist->items; + (modelist->prev_modelist)->next_modelist = modelist->next_modelist; + new_modelists = channel->modelists; } else - new_items = item->next_item; + new_modelists = modelist->next_modelist; - if (item->next_item) - (item->next_item)->prev_item = item->prev_item; + if (modelist->next_modelist) + (modelist->next_modelist)->prev_modelist = modelist->prev_modelist; - /* free item data */ - if (item->mask) - free (item->mask); - if (item->setter) - free (item->setter); - free (item); + /* free linked lists */ + irc_modelist_item_free_all (modelist); - modelist->items = new_items; + free (modelist); - if (modelist->state == IRC_MODELIST_STATE_RECEIVED) - modelist->state = IRC_MODELIST_STATE_MODIFIED; + channel->modelists = new_modelists; } /* - * Frees all items for a modelist. + * Frees all modelists for a channel. */ void -irc_modelist_item_free_all (struct t_irc_modelist *modelist) +irc_modelist_free_all (struct t_irc_channel *channel) { - while (modelist->items) + while (channel->modelists) { - irc_modelist_item_free (modelist, modelist->items); + irc_modelist_free (channel, channel->modelists); } - modelist->state = IRC_MODELIST_STATE_EMPTY; +} + +/* + * Checks if a modelist item pointer is valid for a modelist. + * + * Returns: + * 1: item exists for modelist + * 0: item does not exist for modelist + */ + +int +irc_modelist_item_valid (struct t_irc_modelist *modelist, + struct t_irc_modelist_item *item) +{ + struct t_irc_modelist_item *ptr_item; + + if (!modelist || !item) + return 0; + + for (ptr_item = modelist->items; ptr_item; + ptr_item = ptr_item->next_item) + { + if (ptr_item == item) + return 1; + } + + /* item not found */ + return 0; } /* @@ -209,141 +246,104 @@ irc_modelist_item_number (struct t_irc_modelist *modelist, int number) } /* - * Checks if a modelist pointer is valid for a channel. - * - * Returns: - * 1: modelist exists for channel - * 0: modelist does not exist for channel - */ - -int -irc_modelist_valid (struct t_irc_channel *channel, - struct t_irc_modelist *modelist) -{ - struct t_irc_modelist *ptr_modelist; - - if (!channel || !modelist) - return 0; - - for (ptr_modelist = channel->modelists; ptr_modelist; - ptr_modelist = ptr_modelist->next_modelist) - { - if (ptr_modelist == modelist) - return 1; - } - - /* modelist not found */ - return 0; -} - -/* - * Creates a new modelist in a channel. + * Creates a new item in a modelist. * - * Returns pointer to new modelist, NULL if error. + * Returns pointer to new item, NULL if error. */ -struct t_irc_modelist * -irc_modelist_new (struct t_irc_channel *channel, char type) +struct t_irc_modelist_item * +irc_modelist_item_new (struct t_irc_modelist *modelist, + const char *mask, const char *setter, time_t datetime) { - struct t_irc_modelist *new_modelist; + struct t_irc_modelist_item *new_item; - /* alloc memory for new modelist */ - if ((new_modelist = malloc (sizeof (*new_modelist))) == NULL) + /* alloc memory for new item */ + if ((new_item = malloc (sizeof (*new_item))) == NULL) { weechat_printf (NULL, - _("%s%s: cannot allocate new modelist"), + _("%s%s: cannot allocate new modelist item"), weechat_prefix ("error"), IRC_PLUGIN_NAME); return NULL; } - /* initialize new modelist */ - new_modelist->type = type; - new_modelist->state = IRC_MODELIST_STATE_EMPTY; - new_modelist->items = NULL; - new_modelist->last_item = NULL; + /* initialize new item */ + new_item->number = (modelist->last_item) ? + modelist->last_item->number + 1 : 0; + new_item->mask = strdup (mask); + new_item->setter = (setter) ? strdup (setter) : NULL; + new_item->datetime = datetime; - /* add new modelist to channel */ - new_modelist->prev_modelist = channel->last_modelist; - new_modelist->next_modelist = NULL; - if (channel->modelists) - (channel->last_modelist)->next_modelist = new_modelist; + /* add new item to modelist */ + new_item->prev_item = modelist->last_item; + new_item->next_item = NULL; + if (modelist->items) + (modelist->last_item)->next_item = new_item; else - channel->modelists = new_modelist; - channel->last_modelist = new_modelist; + modelist->items = new_item; + modelist->last_item = new_item; - /* all is OK, return address of new modelist */ - return new_modelist; + if ((modelist->state == IRC_MODELIST_STATE_EMPTY) || + (modelist->state == IRC_MODELIST_STATE_RECEIVED)) + { + modelist->state = IRC_MODELIST_STATE_MODIFIED; + } + + /* all is OK, return address of new item */ + return new_item; } /* - * Frees a modelist and remove it from channel. + * Frees an item and remove it from modelist. */ void -irc_modelist_free (struct t_irc_channel *channel, - struct t_irc_modelist *modelist) +irc_modelist_item_free (struct t_irc_modelist *modelist, + struct t_irc_modelist_item *item) { - struct t_irc_modelist *new_modelists; + struct t_irc_modelist_item *new_items; - if (!channel || !modelist) + if (!modelist || !item) return; - /* remove modelist from channel modelists */ - if (channel->last_modelist == modelist) - channel->last_modelist = modelist->prev_modelist; - if (modelist->prev_modelist) + /* remove item from modelist list */ + if (modelist->last_item == item) + modelist->last_item = item->prev_item; + if (item->prev_item) { - (modelist->prev_modelist)->next_modelist = modelist->next_modelist; - new_modelists = channel->modelists; + (item->prev_item)->next_item = item->next_item; + new_items = modelist->items; } else - new_modelists = modelist->next_modelist; + new_items = item->next_item; - if (modelist->next_modelist) - (modelist->next_modelist)->prev_modelist = modelist->prev_modelist; + if (item->next_item) + (item->next_item)->prev_item = item->prev_item; - /* free linked lists */ - irc_modelist_item_free_all (modelist); + /* free item data */ + if (item->mask) + free (item->mask); + if (item->setter) + free (item->setter); + free (item); - free (modelist); + modelist->items = new_items; - channel->modelists = new_modelists; + if (modelist->state == IRC_MODELIST_STATE_RECEIVED) + modelist->state = IRC_MODELIST_STATE_MODIFIED; } /* - * Frees all modelists for a channel. + * Frees all items for a modelist. */ void -irc_modelist_free_all (struct t_irc_channel *channel) -{ - while (channel->modelists) - { - irc_modelist_free (channel, channel->modelists); - } -} - -/* - * Searches for a modelist by type. - * - * Returns pointer to modelist found, NULL if not found. - */ - -struct t_irc_modelist * -irc_modelist_search (struct t_irc_channel *channel, char type) +irc_modelist_item_free_all (struct t_irc_modelist *modelist) { - struct t_irc_modelist *ptr_modelist; - - if (!channel) - return NULL; - - for (ptr_modelist = channel->modelists; ptr_modelist; - ptr_modelist = ptr_modelist->next_modelist) + while (modelist->items) { - if (ptr_modelist->type == type) - return ptr_modelist; + irc_modelist_item_free (modelist, modelist->items); } - return NULL; + modelist->state = IRC_MODELIST_STATE_EMPTY; } /* |