Cleaned up all list iterations, added macro named LIST_FOREACH.

Previously you would have to do something like this:

for (type foo = (type) list_get_first(list); foo; foo = (type) list_get_next(list)
{
    /* code */
}

Now, you can instead write this as:

LIST_FOREACH(type, foo, list,
{
    /* code */
})

Basically, boilerplate stuff including the casting is gone.
This commit is contained in:
Jan Vidar Krey 2013-03-22 00:58:14 +01:00
parent 50e720861e
commit b81bb2cbd9
15 changed files with 84 additions and 131 deletions

View File

@ -283,23 +283,19 @@ struct adc_message* adc_msg_copy(const struct adc_message* cmd)
if (cmd->feature_cast_include)
{
copy->feature_cast_include = list_create();
tmp = list_get_first(cmd->feature_cast_include);
while (tmp)
LIST_FOREACH(char*, tmp, cmd->feature_cast_include,
{
list_append(copy->feature_cast_include, hub_strdup(tmp));
tmp = list_get_next(cmd->feature_cast_include);
}
});
}
if (cmd->feature_cast_exclude)
{
copy->feature_cast_exclude = list_create();
tmp = list_get_first(cmd->feature_cast_exclude);
while (tmp)
LIST_FOREACH(char*, tmp, cmd->feature_cast_exclude,
{
list_append(copy->feature_cast_exclude, hub_strdup(tmp));
tmp = list_get_next(cmd->feature_cast_exclude);
}
});
}
ADC_MSG_ASSERT(copy);

View File

@ -330,13 +330,11 @@ struct auth_info* acl_get_access_info(struct hub_info* hub, const char* name)
}
#define STR_LIST_CONTAINS(LIST, STR) \
str = (char*) list_get_first(LIST); \
while (str) \
LIST_FOREACH(char*, str, LIST, \
{ \
if (strcasecmp(str, STR) == 0) \
return 1; \
str = (char*) list_get_next(LIST); \
} \
}); \
return 0
int acl_is_cid_banned(struct acl_handle* handle, const char* data)
@ -400,34 +398,28 @@ int acl_user_unban_cid(struct acl_handle* handle, const char* cid)
int acl_is_ip_banned(struct acl_handle* handle, const char* ip_address)
{
struct ip_addr_encap raw;
struct ip_range* info = (struct ip_range*) list_get_first(handle->networks);
ip_convert_to_binary(ip_address, &raw);
struct ip_range* info;
while (info)
ip_convert_to_binary(ip_address, &raw);
LIST_FOREACH(struct ip_range*, info, handle->networks,
{
if (ip_in_range(&raw, info))
{
return 1;
}
info = (struct ip_range*) list_get_next(handle->networks);
}
});
return 0;
}
int acl_is_ip_nat_override(struct acl_handle* handle, const char* ip_address)
{
struct ip_addr_encap raw;
struct ip_range* info = (struct ip_range*) list_get_first(handle->nat_override);
ip_convert_to_binary(ip_address, &raw);
struct ip_range* info;
while (info)
ip_convert_to_binary(ip_address, &raw);
LIST_FOREACH(struct ip_range*, info, handle->nat_override,
{
if (ip_in_range(&raw, info))
{
return 1;
}
info = (struct ip_range*) list_get_next(handle->nat_override);
}
});
return 0;
}

View File

@ -1,6 +1,6 @@
/*
* uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2012, Jan Vidar Krey
* Copyright (C) 2007-2013, Jan Vidar Krey
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -26,7 +26,7 @@ static void hub_command_args_free(struct hub_command* cmd)
if (!cmd->args)
return;
for (data = (struct hub_command_arg_data*) list_get_first(cmd->args); data; data = (struct hub_command_arg_data*) list_get_next(cmd->args))
LIST_FOREACH(struct hub_command_arg_data*, data, cmd->args,
{
switch (data->type)
{
@ -39,7 +39,7 @@ static void hub_command_args_free(struct hub_command* cmd)
default:
break;
}
}
});
list_clear(cmd->args, hub_free);
list_destroy(cmd->args);
@ -77,8 +77,7 @@ static enum command_parse_status command_extract_arguments(struct hub_info* hub,
if (greedy)
{
size = 1;
for (tmp = (char*) list_get_first(tokens); tmp; tmp = (char*) list_get_next(tokens))
size += (strlen(tmp) + 1);
LIST_FOREACH(char*, tmp, tokens, { size += (strlen(tmp) + 1); });
token = hub_malloc_zero(size);
while ((tmp = list_get_first(tokens)))

View File

@ -98,14 +98,14 @@ struct command_handle* command_handler_lookup(struct command_base* cbase, const
struct command_handle* handler = NULL;
size_t prefix_len = strlen(prefix);
for (handler = (struct command_handle*) list_get_first(cbase->handlers); handler; handler = (struct command_handle*) list_get_next(cbase->handlers))
LIST_FOREACH(struct command_handle*, handler, cbase->handlers,
{
if (prefix_len != handler->length)
continue;
if (!memcmp(prefix, handler->prefix, handler->length))
return handler;
}
});
return NULL;
}
@ -292,7 +292,7 @@ static int command_help(struct command_base* cbase, struct hub_user* user, struc
{
cbuf_append(buf, "Available commands:\n");
for (command = (struct command_handle*) list_get_first(cbase->handlers); command; command = (struct command_handle*) list_get_next(cbase->handlers))
LIST_FOREACH(struct command_handle*, command, cbase->handlers,
{
if (command_is_available(command, user->credentials))
{
@ -301,7 +301,7 @@ static int command_help(struct command_base* cbase, struct hub_user* user, struc
cbuf_append(buf, " ");
cbuf_append_format(buf, " - %s\n", command->description);
}
}
});
}
else
{
@ -432,12 +432,10 @@ static int command_whoip(struct command_base* cbase, struct hub_user* user, stru
buf = cbuf_create(128 + ((MAX_NICK_LEN + INET6_ADDRSTRLEN + 5) * ret));
cbuf_append_format(buf, "*** %s: Found %d match%s:\n", cmd->prefix, ret, ((ret != 1) ? "es" : ""));
u = (struct hub_user*) list_get_first(users);
while (u)
LIST_FOREACH(struct hub_user*, u, users,
{
cbuf_append_format(buf, "%s (%s)\n", u->id.nick, user_get_address(u));
u = (struct hub_user*) list_get_next(users);
}
});
cbuf_append(buf, "\n");
send_message(cbase, user, buf);
@ -462,8 +460,7 @@ static int command_broadcast(struct command_base* cbase, struct hub_user* user,
memcpy(from_sid, sid_to_string(user->id.sid), sizeof(from_sid));
memcpy(pm_flag + 2, from_sid, sizeof(from_sid));
target = (struct hub_user*) list_get_first(cbase->hub->users->list);
while (target)
LIST_FOREACH(struct hub_user*, target, cbase->hub->users->list,
{
if (target != user)
{
@ -480,8 +477,7 @@ static int command_broadcast(struct command_base* cbase, struct hub_user* user,
route_to_user(cbase->hub, target, command);
adc_msg_free(command);
}
target = (struct hub_user*) list_get_next(cbase->hub->users->list);
}
});
cbuf_append_format(buf, "*** %s: Delivered to " PRINTF_SIZE_T " user%s", cmd->prefix, recipients, (recipients != 1 ? "s" : ""));
send_message(cbase, user, buf);
@ -513,8 +509,7 @@ static int command_log(struct command_base* cbase, struct hub_user* user, struct
command_status(cbase, user, cmd, buf);
buf = cbuf_create(MAX_HELP_LINE);
log = (struct hub_logout_info*) list_get_first(messages);
while (log)
LIST_FOREACH(struct hub_logout_info*, log, messages,
{
const char* address = ip_convert_to_string(&log->addr);
int show = 0;
@ -538,8 +533,7 @@ static int command_log(struct command_base* cbase, struct hub_user* user, struct
send_message(cbase, user, buf);
buf = cbuf_create(MAX_HELP_LINE);
}
log = (struct hub_logout_info*) list_get_next(messages);
}
});
if (search_len)
{

View File

@ -77,15 +77,13 @@ int event_queue_process(struct event_queue* queue)
/* lock primary queue, and handle the primary queue messages. */
queue->locked = 1;
data = (struct event_data*) list_get_first(queue->q1);
while (data)
LIST_FOREACH(struct event_data*, data, queue->q1,
{
#ifdef EQ_DEBUG
eq_debug("EXEC", data);
#endif
queue->callback(queue->callback_data, data);
data = (struct event_data*) list_get_next(queue->q1);
}
});
list_clear(queue->q1, event_queue_cleanup_callback);
uhub_assert(list_size(queue->q1) == 0);

View File

@ -40,14 +40,11 @@ static int plugin_command_dispatch(struct command_base* cbase, struct hub_user*
LOG_PLUGIN("plugin_command_dispatch: cmd=%s", cmd->prefix);
cmdh = (struct plugin_command_handle*) list_get_first(data->commands);
while (cmdh)
LIST_FOREACH(struct plugin_command_handle*, cmdh, data->commands,
{
if (strcmp(cmdh->prefix, cmd->prefix) == 0)
return cmdh->handler(plugin, puser, pcommand);
cmdh = (struct plugin_command_handle*) list_get_next(data->commands);
}
});
return 0;
}

View File

@ -27,13 +27,12 @@
PLUGIN_DEBUG(HUB, # FUNCNAME) \
if (HUB->plugins && HUB->plugins->loaded) \
{ \
struct plugin_handle* plugin = (struct plugin_handle*) list_get_first(HUB->plugins->loaded); \
while (plugin) \
struct plugin_handle* plugin;\
LIST_FOREACH(struct plugin_handle*, plugin, HUB->plugins->loaded, \
{ \
if (plugin->funcs.FUNCNAME) \
CODE \
plugin = (struct plugin_handle*) list_get_next(HUB->plugins->loaded); \
} \
}); \
}
#define PLUGIN_INVOKE_STATUS_1(HUB, FUNCNAME, ARG1) \

View File

@ -139,12 +139,11 @@ int route_flush_pipeline(struct hub_info* hub, struct hub_user* u)
int route_to_all(struct hub_info* hub, struct adc_message* command) /* iterate users */
{
struct hub_user* user = (struct hub_user*) list_get_first(hub->users->list);
while (user)
struct hub_user* user;
LIST_FOREACH(struct hub_user*, user, hub->users->list,
{
route_to_user(hub, user, command);
user = (struct hub_user*) list_get_next(hub->users->list);
}
});
return 0;
}
@ -154,47 +153,38 @@ int route_to_subscribers(struct hub_info* hub, struct adc_message* command) /* i
int do_send;
char* tmp;
struct hub_user* user = (struct hub_user*) list_get_first(hub->users->list);
while (user)
struct hub_user* user;
LIST_FOREACH(struct hub_user*, user, hub->users->list,
{
if (user->feature_cast)
{
do_send = 1;
tmp = list_get_first(command->feature_cast_include);
while (tmp)
LIST_FOREACH(char*, tmp, command->feature_cast_include,
{
if (!user_have_feature_cast_support(user, tmp))
{
do_send = 0;
break;
}
tmp = list_get_next(command->feature_cast_include);;
}
});
if (!do_send) {
user = (struct hub_user*) list_get_next(hub->users->list);
if (!do_send)
continue;
}
tmp = list_get_first(command->feature_cast_exclude);
while (tmp)
LIST_FOREACH(char*, tmp, command->feature_cast_exclude,
{
if (user_have_feature_cast_support(user, tmp))
{
do_send = 0;
break;
}
tmp = list_get_next(command->feature_cast_exclude);
}
});
if (do_send)
{
route_to_user(hub, user, command);
}
}
user = (struct hub_user*) list_get_next(hub->users->list);
}
});
return 0;
}
@ -214,16 +204,13 @@ int route_info_message(struct hub_info* hub, struct hub_user* u)
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_IPV4_ADDR);
adc_msg_add_named_argument(cmd, ADC_INF_FLAG_IPV4_ADDR, address);
user = (struct hub_user*) list_get_first(hub->users->list);
while (user)
LIST_FOREACH(struct hub_user*, user, hub->users->list,
{
if (user_is_nat_override(user))
route_to_user(hub, user, cmd);
else
route_to_user(hub, user, u->info);
user = (struct hub_user*) list_get_next(hub->users->list);
}
});
adc_msg_free(cmd);
}
return 0;

View File

@ -232,15 +232,12 @@ void user_support_remove(struct hub_user* user, int fourcc)
int user_have_feature_cast_support(struct hub_user* user, char feature[4])
{
char* tmp = list_get_first(user->feature_cast);
while (tmp)
char* tmp;
LIST_FOREACH(char*, tmp, user->feature_cast,
{
if (strncmp(tmp, feature, 4) == 0)
return 1;
tmp = list_get_next(user->feature_cast);
}
});
return 0;
}

View File

@ -120,42 +120,39 @@ struct hub_user* uman_get_user_by_sid(struct hub_user_manager* users, sid_t sid)
struct hub_user* uman_get_user_by_cid(struct hub_user_manager* users, const char* cid)
{
struct hub_user* user = (struct hub_user*) list_get_first(users->list); /* iterate users - only on incoming INF msg */
while (user)
struct hub_user* user;
LIST_FOREACH(struct hub_user*, user, users->list,
{
if (strcmp(user->id.cid, cid) == 0)
return user;
user = (struct hub_user*) list_get_next(users->list);
}
});
return NULL;
}
struct hub_user* uman_get_user_by_nick(struct hub_user_manager* users, const char* nick)
{
struct hub_user* user = (struct hub_user*) list_get_first(users->list); /* iterate users - only on incoming INF msg */
while (user)
struct hub_user* user;
LIST_FOREACH(struct hub_user*, user, users->list,
{
if (strcmp(user->id.nick, nick) == 0)
return user;
user = (struct hub_user*) list_get_next(users->list);
}
});
return NULL;
}
size_t uman_get_user_by_addr(struct hub_user_manager* users, struct linked_list* target, struct ip_range* range)
{
size_t num = 0;
struct hub_user* user = (struct hub_user*) list_get_first(users->list); /* iterate users - only on incoming INF msg */
while (user)
struct hub_user* user;
LIST_FOREACH(struct hub_user*, user, users->list,
{
if (ip_in_range(&user->id.addr, range))
{
list_append(target, user);
num++;
}
user = (struct hub_user*) list_get_next(users->list);
}
});
return num;
}
@ -164,8 +161,8 @@ int uman_send_user_list(struct hub_info* hub, struct hub_user_manager* users, st
int ret = 1;
struct hub_user* user;
user_flag_set(target, flag_user_list);
user = (struct hub_user*) list_get_first(users->list); /* iterate users - only on INF or PAS msg */
while (user)
LIST_FOREACH(struct hub_user*, user, users->list,
{
if (user_is_logged_in(user))
{
@ -173,8 +170,7 @@ int uman_send_user_list(struct hub_info* hub, struct hub_user_manager* users, st
if (!ret)
break;
}
user = (struct hub_user*) list_get_next(users->list);
}
});
#if 0
FIXME: FIXME FIXME handle send queue excess

View File

@ -120,7 +120,7 @@ void net_dns_process()
uhub_mutex_lock(&g_dns->mutex);
LOG_DUMP("net_dns_process(): jobs=%d, results=%d", (int) list_size(g_dns->jobs), (int) list_size(g_dns->results));
for (result = (struct net_dns_result*) list_get_first(g_dns->results); result; result = (struct net_dns_result*) list_get_next(g_dns->results))
LIST_FOREACH(struct net_dns_result*, result, g_dns->results,
{
struct net_dns_job* job = result->job;
#ifdef DEBUG_LOOKUP_TIME
@ -146,7 +146,7 @@ void net_dns_process()
result->job = NULL;
free_job(job);
}
}
});
list_clear(g_dns->results, &dummy_free);
uhub_mutex_unlock(&g_dns->mutex);
@ -273,14 +273,14 @@ extern struct net_dns_job* net_dns_gethostbyaddr(struct ip_addr_encap* ipaddr, n
static struct net_dns_job* find_and_remove_job(struct net_dns_job* job)
{
struct net_dns_job* it;
for (it = (struct net_dns_job*) list_get_first(g_dns->jobs); it; it = (struct net_dns_job*) list_get_next(g_dns->jobs))
LIST_FOREACH(struct net_dns_job*, it, g_dns->jobs,
{
if (it == job)
{
list_remove(g_dns->jobs, it);
return job;
}
}
});
return NULL;
}
@ -288,14 +288,14 @@ static struct net_dns_job* find_and_remove_job(struct net_dns_job* job)
static struct net_dns_result* find_and_remove_result(struct net_dns_job* job)
{
struct net_dns_result* it;
for (it = (struct net_dns_result*) list_get_first(g_dns->results); it; it = (struct net_dns_result*) list_get_next(g_dns->results))
LIST_FOREACH(struct net_dns_result*, it, g_dns->results,
{
if (it->job == job)
{
list_remove(g_dns->results, it);
return it;
}
}
});
return NULL;
}

View File

@ -177,16 +177,15 @@ static void unload_acl(struct acl_data* data)
static plugin_st get_user(struct plugin_handle* plugin, const char* nickname, struct auth_info* data)
{
struct acl_data* acl = (struct acl_data*) plugin->ptr;
struct auth_info* info = (struct auth_info*) list_get_first(acl->users);
while (info)
struct auth_info* info;
LIST_FOREACH(struct auth_info*, info, acl->users,
{
if (strcasecmp((char*)info->nickname, nickname) == 0)
{
memcpy(data, info, sizeof(struct auth_info));
return st_allow;
}
info = (struct auth_info*) list_get_next(acl->users);
}
});
if (acl->exclusive)
return st_deny;
return st_default;

View File

@ -79,16 +79,14 @@ static size_t get_messages(struct chat_history_data* data, size_t num, struct cb
skiplines = total - num;
cbuf_append(outbuf, "\n");
message = (char*) list_get_first(messages);
while (message)
LIST_FOREACH(char*, message, messages,
{
if (--skiplines < 0)
{
cbuf_append(outbuf, message);
lines++;
}
message = (char*) list_get_next(messages);
}
});
cbuf_append(outbuf, "\n");
return lines;
}

View File

@ -65,17 +65,14 @@ static void on_message(struct ADC_chat_message* chat)
lines = list_create();
ret = split_string(chat->message, "\n", lines, 1);
line = (char*) list_get_first(lines);
ret = 0;
while (line)
LIST_FOREACH(char*, line, lines,
{
if (ret > 0)
printf(" ");
printf("%s\n", line);
ret++;
line = (char*) list_get_next(lines);
}
});
list_clear(lines, &hub_free);
list_destroy(lines);

View File

@ -57,5 +57,9 @@ extern void* list_get_prev(struct linked_list*);
extern struct node* list_get_first_node(struct linked_list*);
extern struct node* list_get_last_node(struct linked_list*);
#define LIST_FOREACH(TYPE, ITEM, LIST, BLOCK) \
for (ITEM = (TYPE) list_get_first(LIST); ITEM; ITEM = (TYPE) list_get_next(LIST)) \
BLOCK
#endif /* HAVE_UHUB_LINKED_LIST_H */