X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=util.c;h=325c2a2b8ab397dfcd7fd360d3abf0d4a3109d7b;hb=419e5080f7a94b1a5dc132fcea344181825a777c;hp=bf07b0283576cfc2a373609946c9120b18e1b4e5;hpb=b241210419fdf7f6c4e4cc881ff834c641d9bdd5;p=wmpus diff --git a/util.c b/util.c index bf07b02..325c2a2 100644 --- a/util.c +++ b/util.c @@ -1,9 +1,27 @@ +/* + * Copyright (C) 2011 Andy Spencer + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + #include #include #include #include "util.h" +/* Doubly linked lists */ list_t *list_insert(list_t *next, void *data) { list_t *node = new0(list_t); @@ -15,10 +33,22 @@ list_t *list_insert(list_t *next, void *data) return node; } +void list_insert_after(list_t *prev, void *data) +{ + // prev must be valid, + // as we cannot return the original list head + list_t *node = new0(list_t); + node->data = data; + node->prev = prev; + node->next = prev->next; + prev->next = node; + if (node->next) node->next->prev = node; +} + list_t *list_append(list_t *head, void *data) { list_t *last = head; - while (last->next) + while (last && last->next) last = last->next; list_t *node = new0(list_t); node->data = data; @@ -45,8 +75,19 @@ int list_length(list_t *node) return len; } -void list_move(list_t *node, int offset) +list_t *list_last(list_t *list) +{ + while (list && list->next) + list = list->next; + return list; +} + +list_t *list_find(list_t *list, void *data) { + for (list_t *cur = list; cur; cur = cur->next) + if (cur->data == data) + return cur; + return NULL; } /* Misc */