2003-09-18 00:35:57 +04:00
/*
2004-03-30 23:35:44 +04:00
* Copyright ( C ) 2003 - 2004 Sistina Software , Inc . All rights reserved .
* Copyright ( C ) 2004 Red Hat , Inc . All rights reserved .
2003-09-18 00:35:57 +04:00
*
2004-03-30 23:35:44 +04:00
* This file is part of LVM2 .
*
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
* of the GNU General Public License v .2 .
*
* 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 . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
2003-09-18 00:35:57 +04:00
*/
# include "lib.h"
# include "str_list.h"
2005-10-17 03:03:59 +04:00
struct list * str_list_create ( struct dm_pool * mem )
2003-10-16 00:04:29 +04:00
{
struct list * sl ;
2005-10-17 03:03:59 +04:00
if ( ! ( sl = dm_pool_alloc ( mem , sizeof ( struct list ) ) ) ) {
2003-10-16 00:04:29 +04:00
stack ;
return NULL ;
}
list_init ( sl ) ;
return sl ;
}
2005-10-17 03:03:59 +04:00
int str_list_add ( struct dm_pool * mem , struct list * sll , const char * str )
2003-09-18 00:35:57 +04:00
{
struct str_list * sln ;
if ( ! str ) {
stack ;
return 0 ;
}
/* Already in list? */
2003-10-16 00:04:29 +04:00
if ( str_list_match_item ( sll , str ) )
return 1 ;
2003-09-18 00:35:57 +04:00
2005-10-17 03:03:59 +04:00
if ( ! ( sln = dm_pool_alloc ( mem , sizeof ( * sln ) ) ) ) {
2003-09-18 00:35:57 +04:00
stack ;
return 0 ;
}
sln - > str = str ;
2003-10-16 00:04:29 +04:00
list_add ( sll , & sln - > list ) ;
2003-09-18 00:35:57 +04:00
return 1 ;
}
2003-10-16 00:01:12 +04:00
2003-10-16 00:04:29 +04:00
int str_list_del ( struct list * sll , const char * str )
2003-10-16 00:01:12 +04:00
{
struct list * slh , * slht ;
2003-10-16 00:04:29 +04:00
list_iterate_safe ( slh , slht , sll ) {
2003-10-16 00:01:12 +04:00
if ( ! strcmp ( str , list_item ( slh , struct str_list ) - > str ) )
list_del ( slh ) ;
}
return 1 ;
}
2005-10-17 03:03:59 +04:00
int str_list_dup ( struct dm_pool * mem , struct list * sllnew , struct list * sllold )
2004-03-08 18:23:01 +03:00
{
struct str_list * sl ;
list_init ( sllnew ) ;
list_iterate_items ( sl , sllold ) {
if ( ! str_list_add ( mem , sllnew , strdup ( sl - > str ) ) ) {
stack ;
return 0 ;
}
}
return 1 ;
}
/*
* Is item on list ?
*/
2003-10-16 00:04:29 +04:00
int str_list_match_item ( struct list * sll , const char * str )
{
struct str_list * sl ;
list_iterate_items ( sl , sll )
if ( ! strcmp ( str , sl - > str ) )
return 1 ;
return 0 ;
}
2004-03-08 18:23:01 +03:00
/*
* Is at least one item on both lists ?
*/
2003-10-16 00:04:29 +04:00
int str_list_match_list ( struct list * sll , struct list * sll2 )
{
struct str_list * sl ;
list_iterate_items ( sl , sll )
if ( str_list_match_item ( sll2 , sl - > str ) )
return 1 ;
return 0 ;
}
2004-03-08 18:23:01 +03:00
/*
* Do both lists contain the same set of items ?
*/
int str_list_lists_equal ( struct list * sll , struct list * sll2 )
{
struct str_list * sl ;
if ( list_size ( sll ) ! = list_size ( sll2 ) )
return 0 ;
list_iterate_items ( sl , sll )
if ( ! str_list_match_item ( sll2 , sl - > str ) )
return 0 ;
return 1 ;
}