2009-05-05 Felix Zielcke <fzielcke@z-51.de>
[grub2/phcoder.git] / kern / list.c
blobb879f13207e91b56a51c997a85a1059bf91c596b
1 /* list.c - grub list function */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/list.h>
21 #include <grub/misc.h>
23 void
24 grub_list_push (grub_list_t *head, grub_list_t item)
26 item->next = *head;
27 *head = item;
30 void *
31 grub_list_pop (grub_list_t *head)
33 grub_list_t item;
35 item = *head;
36 if (item)
37 *head = item->next;
39 return item;
42 void
43 grub_list_remove (grub_list_t *head, grub_list_t item)
45 grub_list_t *p, q;
47 for (p = head, q = *p; q; p = &(q->next), q = q->next)
48 if (q == item)
50 *p = q->next;
51 break;
55 int
56 grub_list_iterate (grub_list_t head, grub_list_hook_t hook)
58 grub_list_t p;
60 for (p = head; p; p = p->next)
61 if (hook (p))
62 return 1;
64 return 0;
67 void
68 grub_list_insert (grub_list_t *head, grub_list_t item,
69 grub_list_test_t test)
71 grub_list_t *p, q;
73 for (p = head, q = *p; q; p = &(q->next), q = q->next)
74 if (test (item, q))
75 break;
77 *p = item;
78 item->next = q;
81 void *
82 grub_named_list_find (grub_named_list_t head, const char *name)
84 grub_named_list_t result = 0;
86 auto int list_find (grub_named_list_t item);
87 int list_find (grub_named_list_t item)
89 if (! grub_strcmp (item->name, name))
91 result = item;
92 return 1;
95 return 0;
98 grub_list_iterate (GRUB_AS_LIST (head), (grub_list_hook_t) list_find);
99 return result;
102 void
103 grub_prio_list_insert (grub_prio_list_t *head, grub_prio_list_t nitem)
105 int inactive = 0;
107 auto int test (grub_prio_list_t new_item, grub_prio_list_t item);
108 int test (grub_prio_list_t new_item, grub_prio_list_t item)
110 int r;
112 r = grub_strcmp (new_item->name, item->name);
113 if (r)
114 return (r < 0);
116 if (new_item->prio >= (item->prio & GRUB_PRIO_LIST_PRIO_MASK))
118 item->prio &= ~GRUB_PRIO_LIST_FLAG_ACTIVE;
119 return 1;
122 inactive = 1;
123 return 0;
126 grub_list_insert (GRUB_AS_LIST_P (head), GRUB_AS_LIST (nitem),
127 (grub_list_test_t) test);
128 if (! inactive)
129 nitem->prio |= GRUB_PRIO_LIST_FLAG_ACTIVE;