All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Vesa Jääskeläinen" <chaac@nic.fi>
To: The development of GRUB 2 <grub-devel@gnu.org>
Subject: Re: Handlers
Date: Fri, 29 Aug 2008 22:43:13 +0300	[thread overview]
Message-ID: <48B85151.3030408@nic.fi> (raw)
In-Reply-To: <878wufj1qm.fsf@xs4all.nl>

[-- Attachment #1: Type: text/plain, Size: 464 bytes --]

Marco Gerards wrote:
> If I knew a better way, I would have used it.  I think the
> alternatives will all end up in either code duplication or a loss of
> type safety that now is at least present through warnings.  But please
> understand that you only have to use the macros, not change them :-)
> 
> Suggestions are welcome.

Hi,

Well... How about something like this?

It only requires one cast and no macros at all.

Thanks,
Vesa Jääskeläinen

[-- Attachment #2: list.c --]
[-- Type: text/plain, Size: 2051 bytes --]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct grub_list 
{
	struct grub_list *next;
} grub_list_t;

typedef struct
{
	/* Has to be first */
	grub_list_t entry;

	/* Just some data */
	char name[32];
} grub_fs_t;

void grub_list_add(grub_list_t **list, grub_list_t *item)
{
	item->next = *list;
	*list = item;
}

void grub_list_remove(grub_list_t **list, grub_list_t *item)
{
	grub_list_t *prev = 0;
	grub_list_t *tmp = *list;
	
	while (tmp)
	{
		if (tmp == item)
		{
			if (! prev)
			{
				*list = tmp->next;
				tmp->next = 0;
				return;
			}
			else
			{
				prev->next = tmp->next;
				tmp->next = 0;
				return;
			}
		}
		
		prev = tmp;
		tmp = tmp->next;
	}
}

int grub_list_iterate(grub_list_t *list, int (*hook) (const grub_list_t *item))
{
	while (list)
	{
		if (hook(list))
			return 1;

		list = list->next;
	}

	return 0;
}

int grub_fs_print (const grub_list_t *list_item)
{
	grub_fs_t *item = (grub_fs_t *)list_item;

	printf("'%s'\n", item->name);

	return 0;
}

int main()
{
	grub_fs_t item1;
	grub_fs_t item2;
	grub_fs_t item3;

	grub_list_t *fs_list = 0;

	memset(&item1, 0, sizeof(item1));
	memset(&item2, 0, sizeof(item2));
	memset(&item3, 0, sizeof(item3));

	strcpy(item1.name, "item 1");
	strcpy(item2.name, "item 2");
	strcpy(item3.name, "item 3");

	grub_list_add(&fs_list, &item1.entry);
	grub_list_add(&fs_list, &item2.entry);
	grub_list_add(&fs_list, &item3.entry);

	printf("---\n");
	grub_list_iterate(fs_list, grub_fs_print);
	
	printf("---\n");
	grub_list_remove(&fs_list, &item2.entry);
	grub_list_iterate(fs_list, grub_fs_print);

	printf("---\n");
	grub_list_remove(&fs_list, &item2.entry);
	grub_list_iterate(fs_list, grub_fs_print);

	printf("---\n");
	grub_list_remove(&fs_list, &item1.entry);
	grub_list_iterate(fs_list, grub_fs_print);

	printf("---\n");
	grub_list_remove(&fs_list, &item3.entry);
	grub_list_iterate(fs_list, grub_fs_print);

	printf("---\n");
	grub_list_remove(&fs_list, &item3.entry);
	grub_list_iterate(fs_list, grub_fs_print);
	printf("---\n");

	return 0;
}

  reply	other threads:[~2008-08-29 19:43 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-29 12:36 Handlers Marco Gerards
2008-08-29 13:27 ` Handlers Neal H. Walfield
2008-08-29 14:46   ` Handlers Marco Gerards
2008-08-29 19:13     ` Handlers Neal H. Walfield
2008-08-29 14:58 ` Handlers Vesa Jääskeläinen
2008-08-29 15:10   ` Handlers Marco Gerards
2008-08-29 19:43     ` Vesa Jääskeläinen [this message]
2008-08-30  1:27       ` Handlers Marco Gerards
2008-08-30 14:55         ` Handlers Vesa Jääskeläinen
2008-08-30 12:45 ` Handlers Robert Millan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=48B85151.3030408@nic.fi \
    --to=chaac@nic.fi \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.