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 .
2007-08-21 00:55:30 +04:00
* Copyright ( C ) 2004 - 2005 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
2007-08-21 00:55:30 +04:00
* of the GNU Lesser General Public License v .2 .1 .
2004-03-30 23:35:44 +04:00
*
2007-08-21 00:55:30 +04:00
* You should have received a copy of the GNU Lesser General Public License
2004-03-30 23:35:44 +04:00
* 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 ;
}
2007-08-07 13:06:05 +04:00
int str_list_dup ( struct dm_pool * mem , struct list * sllnew ,
const 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 ?
*/
2007-08-07 13:06:05 +04:00
int str_list_match_item ( const struct list * sll , const char * str )
2003-10-16 00:04:29 +04:00
{
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 ?
*/
2007-08-07 13:06:05 +04:00
int str_list_match_list ( const struct list * sll , const struct list * sll2 )
2003-10-16 00:04:29 +04:00
{
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 ?
*/
2007-08-07 13:06:05 +04:00
int str_list_lists_equal ( const struct list * sll , const struct list * sll2 )
2004-03-08 18:23:01 +03:00
{
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 ;
}