]> Pileus Git - wmpus/blob - util.c
Add floating layer
[wmpus] / util.c
1 /*
2  * Copyright (C) 2011 Andy Spencer <andy753421@gmail.com>
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdarg.h>
21
22 #include "util.h"
23
24 /* Doubly linked lists */
25 list_t *list_insert(list_t *next, void *data)
26 {
27         list_t *node = new0(list_t);
28         node->data = data;
29         node->next = next;
30         node->prev = next ? next->prev : NULL;
31         if (node->next) node->next->prev = node;
32         if (node->prev) node->prev->next = node;
33         return node;
34 }
35
36 void list_insert_after(list_t *prev, void *data)
37 {
38         // prev must be valid,
39         // as we cannot return the original list head
40         list_t *node = new0(list_t);
41         node->data = data;
42         node->prev = prev;
43         node->next = prev->next;
44         prev->next = node;
45         if (node->next) node->next->prev = node;
46 }
47
48 list_t *list_append(list_t *head, void *data)
49 {
50         list_t *last = head;
51         while (last && last->next)
52                 last = last->next;
53         list_t *node = new0(list_t);
54         node->data = data;
55         node->prev = last;
56         if (last) last->next = node;
57         return last ? head : node;
58 }
59
60 list_t *list_remove(list_t *head, list_t *node)
61 {
62         list_t *next = node->next;
63         list_t *prev = node->prev;
64         if (next) next->prev = prev;
65         if (prev) prev->next = next;
66         free(node);
67         return head == node ? next : head;
68 }
69
70 int list_length(list_t *node)
71 {
72         int len = 0;
73         for (; node; node = node->next)
74                 len++;
75         return len;
76 }
77
78 list_t *list_last(list_t *list)
79 {
80         while (list && list->next)
81                 list = list->next;
82         return list;
83 }
84
85 list_t *list_find(list_t *list, void *data)
86 {
87         for (list_t *cur = list; cur; cur = cur->next)
88                 if (cur->data == data)
89                         return cur;
90         return NULL;
91 }
92
93 /* Misc */
94 int error(char *fmt, ...)
95 {
96         va_list ap;
97         va_start(ap, fmt);
98         fprintf(stderr, "Error: ");
99         vfprintf(stderr, fmt, ap);
100         fprintf(stderr, "\n");
101         va_end(ap);
102         exit(1);
103         return 0;
104 }