xref: /netbsd-src/external/gpl2/lvm2/dist/lib/datastruct/str_list.c (revision 7c604eea85b4f330dc75ffe65e947f4d73758aa0)
1*7c604eeaShaad /*	$NetBSD: str_list.c,v 1.1.1.2 2009/12/02 00:26:32 haad Exp $	*/
256a34939Shaad 
356a34939Shaad /*
456a34939Shaad  * Copyright (C) 2003-2004 Sistina Software, Inc. All rights reserved.
556a34939Shaad  * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
656a34939Shaad  *
756a34939Shaad  * This file is part of LVM2.
856a34939Shaad  *
956a34939Shaad  * This copyrighted material is made available to anyone wishing to use,
1056a34939Shaad  * modify, copy, or redistribute it subject to the terms and conditions
1156a34939Shaad  * of the GNU Lesser General Public License v.2.1.
1256a34939Shaad  *
1356a34939Shaad  * You should have received a copy of the GNU Lesser General Public License
1456a34939Shaad  * along with this program; if not, write to the Free Software Foundation,
1556a34939Shaad  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1656a34939Shaad  */
1756a34939Shaad 
1856a34939Shaad #include "lib.h"
1956a34939Shaad #include "str_list.h"
2056a34939Shaad 
str_list_create(struct dm_pool * mem)2156a34939Shaad struct dm_list *str_list_create(struct dm_pool *mem)
2256a34939Shaad {
2356a34939Shaad 	struct dm_list *sl;
2456a34939Shaad 
25*7c604eeaShaad 	if (!(sl = dm_pool_alloc(mem, sizeof(struct dm_list)))) {
26*7c604eeaShaad 		log_errno(ENOMEM, "str_list allocation failed");
27*7c604eeaShaad 		return NULL;
28*7c604eeaShaad 	}
2956a34939Shaad 
3056a34939Shaad 	dm_list_init(sl);
3156a34939Shaad 
3256a34939Shaad 	return sl;
3356a34939Shaad }
3456a34939Shaad 
str_list_add(struct dm_pool * mem,struct dm_list * sll,const char * str)3556a34939Shaad int str_list_add(struct dm_pool *mem, struct dm_list *sll, const char *str)
3656a34939Shaad {
3756a34939Shaad 	struct str_list *sln;
3856a34939Shaad 
3956a34939Shaad 	if (!str)
4056a34939Shaad 		return_0;
4156a34939Shaad 
4256a34939Shaad 	/* Already in list? */
4356a34939Shaad 	if (str_list_match_item(sll, str))
4456a34939Shaad 		return 1;
4556a34939Shaad 
4656a34939Shaad 	if (!(sln = dm_pool_alloc(mem, sizeof(*sln))))
4756a34939Shaad 		return_0;
4856a34939Shaad 
4956a34939Shaad 	sln->str = str;
5056a34939Shaad 	dm_list_add(sll, &sln->list);
5156a34939Shaad 
5256a34939Shaad 	return 1;
5356a34939Shaad }
5456a34939Shaad 
str_list_del(struct dm_list * sll,const char * str)5556a34939Shaad int str_list_del(struct dm_list *sll, const char *str)
5656a34939Shaad {
5756a34939Shaad 	struct dm_list *slh, *slht;
5856a34939Shaad 
5956a34939Shaad 	dm_list_iterate_safe(slh, slht, sll) {
6056a34939Shaad 		if (!strcmp(str, dm_list_item(slh, struct str_list)->str))
6156a34939Shaad 			 dm_list_del(slh);
6256a34939Shaad 	}
6356a34939Shaad 
6456a34939Shaad 	return 1;
6556a34939Shaad }
6656a34939Shaad 
str_list_dup(struct dm_pool * mem,struct dm_list * sllnew,const struct dm_list * sllold)6756a34939Shaad int str_list_dup(struct dm_pool *mem, struct dm_list *sllnew,
6856a34939Shaad 		 const struct dm_list *sllold)
6956a34939Shaad {
7056a34939Shaad 	struct str_list *sl;
7156a34939Shaad 
7256a34939Shaad 	dm_list_init(sllnew);
7356a34939Shaad 
7456a34939Shaad 	dm_list_iterate_items(sl, sllold) {
7556a34939Shaad 		if (!str_list_add(mem, sllnew, dm_pool_strdup(mem, sl->str)))
7656a34939Shaad 			return_0;
7756a34939Shaad 	}
7856a34939Shaad 
7956a34939Shaad 	return 1;
8056a34939Shaad }
8156a34939Shaad 
8256a34939Shaad /*
8356a34939Shaad  * Is item on list?
8456a34939Shaad  */
str_list_match_item(const struct dm_list * sll,const char * str)8556a34939Shaad int str_list_match_item(const struct dm_list *sll, const char *str)
8656a34939Shaad {
8756a34939Shaad 	struct str_list *sl;
8856a34939Shaad 
8956a34939Shaad 	dm_list_iterate_items(sl, sll)
9056a34939Shaad 	    if (!strcmp(str, sl->str))
9156a34939Shaad 		return 1;
9256a34939Shaad 
9356a34939Shaad 	return 0;
9456a34939Shaad }
9556a34939Shaad 
9656a34939Shaad /*
9756a34939Shaad  * Is at least one item on both lists?
9856a34939Shaad  */
str_list_match_list(const struct dm_list * sll,const struct dm_list * sll2)9956a34939Shaad int str_list_match_list(const struct dm_list *sll, const struct dm_list *sll2)
10056a34939Shaad {
10156a34939Shaad 	struct str_list *sl;
10256a34939Shaad 
10356a34939Shaad 	dm_list_iterate_items(sl, sll)
10456a34939Shaad 	    if (str_list_match_item(sll2, sl->str))
10556a34939Shaad 		return 1;
10656a34939Shaad 
10756a34939Shaad 	return 0;
10856a34939Shaad }
10956a34939Shaad 
11056a34939Shaad /*
11156a34939Shaad  * Do both lists contain the same set of items?
11256a34939Shaad  */
str_list_lists_equal(const struct dm_list * sll,const struct dm_list * sll2)11356a34939Shaad int str_list_lists_equal(const struct dm_list *sll, const struct dm_list *sll2)
11456a34939Shaad {
11556a34939Shaad 	struct str_list *sl;
11656a34939Shaad 
11756a34939Shaad 	if (dm_list_size(sll) != dm_list_size(sll2))
11856a34939Shaad 		return 0;
11956a34939Shaad 
12056a34939Shaad 	dm_list_iterate_items(sl, sll)
12156a34939Shaad 	    if (!str_list_match_item(sll2, sl->str))
12256a34939Shaad 		return 0;
12356a34939Shaad 
12456a34939Shaad 	return 1;
12556a34939Shaad }
126