From 771199a2e585b8ed7b9934429ebcc5fef7f5ae2e Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Sat, 12 Jan 2008 14:50:15 +0100 Subject: [PATCH] add list header --- Makefile.am | 1 + list.h | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 list.h diff --git a/Makefile.am b/Makefile.am index 420dfeeb..191fa9d1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -83,6 +83,7 @@ awesome_SOURCES = \ tag.c tag.h \ util.c util.h \ xutil.c xutil.h \ + list.h \ config.c config.h \ screen.c screen.h \ statusbar.c statusbar.h \ diff --git a/list.h b/list.h new file mode 100644 index 00000000..21ff4344 --- /dev/null +++ b/list.h @@ -0,0 +1,117 @@ +/* + * list.h - useful list handling header + * + * Copyright © 2008 Julien Danjou + * Copyright © 2006 Pierre Habouzit + * + * 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 2 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#ifndef AWESOME_LIST_H +#define AWESOME_LIST_H + +#define DO_SLIST(type, prefix, dtor) \ + static inline type *prefix##_list_pop(type **list) \ + { \ + if (*list) \ + { \ + type *res = *list; \ + *list = res->next; \ + res->next = NULL; \ + return res; \ + } \ + return NULL; \ + } \ + static inline void prefix##_list_push(type **list, type *item) \ + { \ + item->next = *list; \ + *list = item; \ + } \ + \ + static inline type **prefix##_list_last(type **list) \ + { \ + while (*list && (*list)->next) \ + list = &(*list)->next; \ + return list; \ + } \ + \ + static inline void prefix##_list_append(type **list, type *item) \ + { \ + if(*(list = prefix##_list_last(list))) \ + (*list)->next = item; \ + else \ + (*list) = item; \ + } \ + static inline type *prefix##_list_rev(type *list) \ + { \ + type *l = NULL; \ + while (list) \ + prefix##_list_push(&l, prefix##_list_pop(&list)); \ + return l; \ + } \ + \ + static inline type **prefix##_list_init(type **list) \ + { \ + *list = NULL; \ + return list; \ + } \ + static inline void prefix##_list_wipe(type **list) \ + { \ + while (*list) \ + { \ + type *item = prefix##_list_pop(list); \ + dtor(&item); \ + } \ + } \ + static inline void prefix##_list_swap(type **list, type *item1, \ + type *item2) \ + { \ + type *i1n = item1->next; \ + type *i2n = item2->next; \ + item1->next = i2n == item1 ? item2 : i2n; \ + item2->next = i1n == item2 ? item1 : i1n; \ + \ + if(*list == item1) \ + *list = item2; \ + else if(*list == item2) \ + *list = item1; \ + } \ + static inline type *prefix##_list_prev(type **list, type *item) \ + { \ + type *tmp; \ + if(*list == item) return NULL; \ + for(tmp = *list; tmp && tmp->next != item; tmp = tmp->next); \ + return tmp; \ + } \ + static inline type *prefix##_list_prev_cycle(type **list, type *item) \ + { \ + type *tmp = prefix##_list_prev(list, item); \ + if(!tmp) \ + tmp = *prefix##_list_last(list); \ + return tmp; \ + } \ + static inline void prefix##_list_detach(type **list, type *item) \ + { \ + type *prev = prefix##_list_prev(list, item); \ + if(prev) \ + prev->next = item->next; \ + if(item == *list) \ + *list = item->next; \ + item->next = NULL; \ + } \ + +#endif +// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80