changeset 24552:f10aba5592c6

The other day while struct hiding, I noticed a for loop that was checking g_list_length() as the loop conditional. I decided to check all our calls to g_list_length() to see which ones I could clean up without too much work.
author Richard Laager <rlaager@wiktel.com>
date Thu, 27 Nov 2008 05:54:09 +0000
parents cd70713a996d
children 2675fda76ddf 9bdd3ab8087f
files finch/libgnt/gnttextview.c finch/libgnt/gntwm.c libpurple/protocols/jabber/buddy.c libpurple/protocols/jabber/si.c libpurple/protocols/myspace/message.c libpurple/protocols/sametime/sametime.c libpurple/protocols/yahoo/yahoochat.c libpurple/request.c pidgin/gtkblist.c pidgin/gtknotify.c pidgin/gtkrequest.c pidgin/gtksavedstatuses.c pidgin/plugins/history.c
diffstat 13 files changed, 45 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/finch/libgnt/gnttextview.c	Thu Nov 27 05:51:03 2008 +0000
+++ b/finch/libgnt/gnttextview.c	Thu Nov 27 05:54:09 2008 +0000
@@ -67,6 +67,7 @@
 gnt_text_view_draw(GntWidget *widget)
 {
 	GntTextView *view = GNT_TEXT_VIEW(widget);
+	int n;
 	int i = 0;
 	GList *lines;
 	int rows, scrcol;
@@ -76,10 +77,11 @@
 	wbkgd(widget->window, gnt_color_pair(GNT_COLOR_NORMAL));
 	werase(widget->window);
 
+	n = g_list_length(view->list);
 	if ((view->flags & GNT_TEXT_VIEW_TOP_ALIGN) &&
-			g_list_length(view->list) < widget->priv.height) {
+			n < widget->priv.height) {
 		GList *now = view->list;
-		comp = widget->priv.height - g_list_length(view->list);
+		comp = widget->priv.height - n;
 		view->list = g_list_nth_prev(view->list, comp);
 		if (!view->list) {
 			view->list = g_list_first(now);
@@ -236,6 +238,7 @@
 static char *
 gnt_text_view_get_p(GntTextView *view, int x, int y)
 {
+	int n;
 	int i = 0;
 	GntWidget *wid = GNT_WIDGET(view);
 	GntTextLine *line;
@@ -244,10 +247,11 @@
 	GntTextSegment *seg;
 	gchar *pos;
 
+	n = g_list_length(view->list);
 	y = wid->priv.height - y;
-	if (g_list_length(view->list) < y) {
+	if (n < y) {
 		x = 0;
-		y = g_list_length(view->list) - 1;
+		y = n - 1;
 	}
 
 	lines = g_list_nth(view->list, y - 1);
--- a/finch/libgnt/gntwm.c	Thu Nov 27 05:51:03 2008 +0000
+++ b/finch/libgnt/gntwm.c	Thu Nov 27 05:54:09 2008 +0000
@@ -201,7 +201,7 @@
 	GString *text = g_string_new("act: ");
 	if (message)
 		gnt_widget_destroy(message);
-	if (g_list_length(act) == 0)
+	if (!act)
 		return;
 	for (iter = act; iter; iter = iter->next) {
 		GntWS *ws = iter->data;
@@ -927,6 +927,7 @@
 	GntWidget *tree, *win;
 	GList *iter;
 	GntWM *wm = GNT_WM(bindable);
+	int n;
 	if (wm->_list.window || wm->menu)
 		return TRUE;
 
@@ -950,8 +951,9 @@
 				gnt_tree_create_row(GNT_TREE(tree), action->label), NULL);
 	}
 	g_signal_connect(G_OBJECT(tree), "activate", G_CALLBACK(action_list_activate), wm);
-	gnt_widget_set_size(tree, 0, g_list_length(wm->acts));
-	gnt_widget_set_position(win, 0, getmaxy(stdscr) - 3 - g_list_length(wm->acts));
+	n = g_list_length(wm->acts);
+	gnt_widget_set_size(tree, 0, n);
+	gnt_widget_set_position(win, 0, getmaxy(stdscr) - 3 - n);
 
 	gnt_widget_show(win);
 	return TRUE;
--- a/libpurple/protocols/jabber/buddy.c	Thu Nov 27 05:51:03 2008 +0000
+++ b/libpurple/protocols/jabber/buddy.c	Thu Nov 27 05:54:09 2008 +0000
@@ -966,7 +966,7 @@
 		}
 #endif
 	} else {
-		gboolean multiple_resources = jbi->jb->resources && (g_list_length(jbi->jb->resources) > 1);
+		gboolean multiple_resources = jbi->jb->resources && jbi->jb->resources->next;
 
 		for(resources = jbi->jb->resources; resources; resources = resources->next) {
 			char *purdy = NULL;
--- a/libpurple/protocols/jabber/si.c	Thu Nov 27 05:51:03 2008 +0000
+++ b/libpurple/protocols/jabber/si.c	Thu Nov 27 05:54:09 2008 +0000
@@ -1085,7 +1085,7 @@
 
 			purple_notify_error(jsx->js->gc, _("File Send Failed"), _("File Send Failed"), msg);
 			g_free(msg);
-		} else if(g_list_length(jb->resources) == 1) {
+		} else if(!jb->resources->next) {
 			/* only 1 resource online (probably our most common case)
 			 * so no need to ask who to send to */
 			jbr = jb->resources->data;
--- a/libpurple/protocols/myspace/message.c	Thu Nov 27 05:51:03 2008 +0000
+++ b/libpurple/protocols/myspace/message.c	Thu Nov 27 05:54:09 2008 +0000
@@ -613,6 +613,7 @@
 		const gchar *sep, 
 		const gchar *begin, const gchar *end)
 {
+	int num_items;
 	gchar **strings;
 	gchar **strings_tmp;
 	gchar *joined;
@@ -621,8 +622,10 @@
 
 	g_return_val_if_fail(msg != NULL, NULL);
 
+	num_items = g_list_length(msg);
+
 	/* Add one for NULL terminator for g_strjoinv(). */
-	strings = (gchar **)g_new0(gchar *, g_list_length(msg) + 1);
+	strings = (gchar **)g_new0(gchar *, num_items + 1);
 
 	strings_tmp = strings;
 	g_list_foreach(msg, gf, &strings_tmp);
@@ -632,7 +635,7 @@
 	g_free(joined);
 
 	/* Clean up. */
-	for (i = 0; i < g_list_length(msg); ++i) {
+	for (i = 0; i < num_items; ++i) {
 		g_free(strings[i]);
 	}
 
--- a/libpurple/protocols/sametime/sametime.c	Thu Nov 27 05:51:03 2008 +0000
+++ b/libpurple/protocols/sametime/sametime.c	Thu Nov 27 05:54:09 2008 +0000
@@ -4415,7 +4415,7 @@
     res = results->data;
 
   if(!code && res && res->matches) {
-    if(g_list_length(res->matches) == 1) {
+    if(!res->matches->next) {
       struct mwResolveMatch *match = res->matches->data;
       
       /* only one? that might be the right one! */
--- a/libpurple/protocols/yahoo/yahoochat.c	Thu Nov 27 05:51:03 2008 +0000
+++ b/libpurple/protocols/yahoo/yahoochat.c	Thu Nov 27 05:54:09 2008 +0000
@@ -513,12 +513,12 @@
 
 	c = purple_find_chat(gc, YAHOO_CHAT_ID);
 
-	if (room && (!c || purple_conv_chat_has_left(PURPLE_CONV_CHAT(c))) && members &&
-	   ((g_list_length(members) > 1) ||
+	if (room && (!c || purple_conv_chat_has_left(PURPLE_CONV_CHAT(c))) &&
+	    members && (members->next ||
 	     !g_ascii_strcasecmp(members->data, purple_connection_get_display_name(gc)))) {
-		int i;
+		GList *l;
 		GList *flags = NULL;
-		for (i = 0; i < g_list_length(members); i++)
+		for (l = members; l; l = l->next)
 			flags = g_list_append(flags, GINT_TO_POINTER(PURPLE_CBFLAGS_NONE));
 		if (c && purple_conv_chat_has_left(PURPLE_CONV_CHAT(c))) {
 			/* this might be a hack, but oh well, it should nicely */
--- a/libpurple/request.c	Thu Nov 27 05:51:03 2008 +0000
+++ b/libpurple/request.c	Thu Nov 27 05:54:09 2008 +0000
@@ -887,7 +887,7 @@
 	purple_request_field_list_clear_selected(field);
 
 	if (!purple_request_field_list_get_multi_select(field) &&
-		g_list_length(items) > 1)
+		items && items->next)
 	{
 		purple_debug_warning("request",
 						   "More than one item added to non-multi-select "
--- a/pidgin/gtkblist.c	Thu Nov 27 05:51:03 2008 +0000
+++ b/pidgin/gtkblist.c	Thu Nov 27 05:54:09 2008 +0000
@@ -3320,6 +3320,7 @@
 	if (PURPLE_BLIST_NODE_IS_CHAT(node))
 	{
 		PurpleChat *chat;
+		GList *connections;
 		GList *cur;
 		struct proto_chat_entry *pce;
 		char *name, *value;
@@ -3330,7 +3331,8 @@
 		prpl = purple_find_prpl(purple_account_get_protocol_id(chat->account));
 		prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);
 
-		if (g_list_length(purple_connections_get_all()) > 1)
+		connections = purple_connections_get_all();
+		if (connections && connections->next)
 		{
 			tmp = g_markup_escape_text(chat->account->username, -1);
 			g_string_append_printf(str, _("<b>Account:</b> %s"), tmp);
@@ -3400,6 +3402,7 @@
 		PurpleBuddy *b;
 		PurplePresence *presence;
 		PurpleNotifyUserInfo *user_info;
+		GList *connections;
 		char *tmp;
 		time_t idle_secs, signon;
 
@@ -3421,7 +3424,8 @@
 		user_info = purple_notify_user_info_new();
 
 		/* Account */
-		if (full && g_list_length(purple_connections_get_all()) > 1)
+		connections = purple_connections_get_all();
+		if (full && connections && connections->next)
 		{
 			tmp = g_markup_escape_text(purple_account_get_username(
 									   purple_buddy_get_account(b)), -1);
--- a/pidgin/gtknotify.c	Thu Nov 27 05:51:03 2008 +0000
+++ b/pidgin/gtknotify.c	Thu Nov 27 05:54:09 2008 +0000
@@ -730,7 +730,6 @@
 	GtkListStore *model = data->model;
 	GtkTreeIter iter;
 	GdkPixbuf *pixbuf;
-	guint col_num;
 	GList *row, *column;
 	guint n;
 
@@ -738,9 +737,6 @@
 
 	pixbuf = pidgin_create_prpl_icon(purple_connection_get_account(gc), 0.5);
 
-	/* +1 is for the automagically created Status column. */
-	col_num = g_list_length(results->columns) + 1;
-
 	for (row = results->rows; row != NULL; row = row->next) {
 
 		gtk_list_store_append(model, &iter);
@@ -776,6 +772,7 @@
 	guint col_num;
 	GList *columniter;
 	guint i;
+	GList *l;
 
 	GtkWidget *vbox;
 	GtkWidget *label;
@@ -869,8 +866,8 @@
 		i++;
 	}
 
-	for (i = 0; i < g_list_length(results->buttons); i++) {
-		PurpleNotifySearchButton *b = g_list_nth_data(results->buttons, i);
+	for (l = results->buttons; l; l = l->next) {
+		PurpleNotifySearchButton *b = l->data;
 		GtkWidget *button = NULL;
 		switch (b->type) {
 			case PURPLE_NOTIFY_BUTTON_LABELED:
--- a/pidgin/gtkrequest.c	Thu Nov 27 05:51:03 2008 +0000
+++ b/pidgin/gtkrequest.c	Thu Nov 27 05:54:09 2008 +0000
@@ -853,12 +853,11 @@
 create_choice_field(PurpleRequestField *field)
 {
 	GtkWidget *widget;
-	GList *labels;
+	GList *labels = purple_request_field_choice_get_labels(field);
+	int num_labels = g_list_length(labels);
 	GList *l;
 
-	labels = purple_request_field_choice_get_labels(field);
-
-	if (g_list_length(labels) > 5)
+	if (num_labels > 5)
 	{
 		GtkWidget *menu;
 		GtkWidget *item;
@@ -892,7 +891,7 @@
 		GtkWidget *radio;
 		gint i;
 
-		if (g_list_length(labels) == 2)
+		if (num_labels == 2)
 			box = gtk_hbox_new(FALSE, PIDGIN_HIG_BOX_SPACE);
 		else
 			box = gtk_vbox_new(FALSE, 0);
--- a/pidgin/gtksavedstatuses.c	Thu Nov 27 05:51:03 2008 +0000
+++ b/pidgin/gtksavedstatuses.c	Thu Nov 27 05:54:09 2008 +0000
@@ -331,7 +331,8 @@
 	}
 	g_list_free(sel_paths);
 
-	if (g_list_length(sel_titles) == 1) {
+	g_return_if_fail(sel_titles != NULL);
+	if (!sel_titles->next) {
 		title = g_strdup_printf(_("Are you sure you want to delete %s?"),
 				(const gchar *)sel_titles->data);
 		handle = purple_savedstatus_find(sel_titles->data);
--- a/pidgin/plugins/history.c	Thu Nov 27 05:51:03 2008 +0000
+++ b/pidgin/plugins/history.c	Thu Nov 27 05:54:09 2008 +0000
@@ -47,10 +47,11 @@
 
 	convtype = purple_conversation_get_type(c);
 	gtkconv = PIDGIN_CONVERSATION(c);
-	if (gtkconv == NULL)
-		return;
+	g_return_if_fail(gtkconv != NULL);
 
-	if (convtype == PURPLE_CONV_TYPE_IM && g_list_length(gtkconv->convs) < 2)
+	/* An IM which is the first active conversation. */
+	g_return_if_fail(gtkconv->convs != NULL);
+	if (convtype == PURPLE_CONV_TYPE_IM && !gtkconv->convs->next)
 	{
 		GSList *buddies;
 		GSList *cur;