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 .
2012-02-08 16:52:58 +04:00
* Copyright ( C ) 2004 - 2012 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"
2008-11-04 01:14:30 +03:00
struct dm_list * str_list_create ( struct dm_pool * mem )
2003-10-16 00:04:29 +04:00
{
2008-11-04 01:14:30 +03:00
struct dm_list * sl ;
2003-10-16 00:04:29 +04:00
2009-07-27 15:00:17 +04:00
if ( ! ( sl = dm_pool_alloc ( mem , sizeof ( struct dm_list ) ) ) ) {
log_errno ( ENOMEM , " str_list allocation failed " ) ;
return NULL ;
}
2003-10-16 00:04:29 +04:00
2008-11-04 01:14:30 +03:00
dm_list_init ( sl ) ;
2003-10-16 00:04:29 +04:00
return sl ;
}
2008-11-04 01:14:30 +03:00
int str_list_add ( struct dm_pool * mem , struct dm_list * sll , const char * str )
2003-09-18 00:35:57 +04:00
{
struct str_list * sln ;
2008-01-30 16:19:47 +03:00
if ( ! str )
return_0 ;
2003-09-18 00:35:57 +04:00
/* 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
2008-01-30 16:19:47 +03:00
if ( ! ( sln = dm_pool_alloc ( mem , sizeof ( * sln ) ) ) )
return_0 ;
2003-09-18 00:35:57 +04:00
sln - > str = str ;
2008-11-04 01:14:30 +03:00
dm_list_add ( sll , & sln - > list ) ;
2003-09-18 00:35:57 +04:00
return 1 ;
}
2003-10-16 00:01:12 +04:00
2012-02-08 16:52:58 +04:00
void str_list_del ( struct dm_list * sll , const char * str )
2003-10-16 00:01:12 +04:00
{
2008-11-04 01:14:30 +03:00
struct dm_list * slh , * slht ;
2003-10-16 00:01:12 +04:00
2012-02-08 16:52:58 +04:00
dm_list_iterate_safe ( slh , slht , sll )
2008-11-04 01:14:30 +03:00
if ( ! strcmp ( str , dm_list_item ( slh , struct str_list ) - > str ) )
dm_list_del ( slh ) ;
2003-10-16 00:01:12 +04:00
}
2008-11-04 01:14:30 +03:00
int str_list_dup ( struct dm_pool * mem , struct dm_list * sllnew ,
const struct dm_list * sllold )
2004-03-08 18:23:01 +03:00
{
struct str_list * sl ;
2008-11-04 01:14:30 +03:00
dm_list_init ( sllnew ) ;
2004-03-08 18:23:01 +03:00
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( sl , sllold ) {
2008-01-30 16:19:47 +03:00
if ( ! str_list_add ( mem , sllnew , dm_pool_strdup ( mem , sl - > str ) ) )
return_0 ;
2004-03-08 18:23:01 +03:00
}
return 1 ;
}
/*
* Is item on list ?
*/
2008-11-04 01:14:30 +03:00
int str_list_match_item ( const struct dm_list * sll , const char * str )
2003-10-16 00:04:29 +04:00
{
struct str_list * sl ;
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( sl , sll )
2003-10-16 00:04:29 +04:00
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 ?
2010-11-09 15:34:40 +03:00
* If tag_matched is non - NULL , it is set to the tag that matched .
2004-03-08 18:23:01 +03:00
*/
2010-11-11 15:32:33 +03:00
int str_list_match_list ( const struct dm_list * sll , const struct dm_list * sll2 , const char * * tag_matched )
2003-10-16 00:04:29 +04:00
{
struct str_list * sl ;
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( sl , sll )
2010-11-09 15:34:40 +03:00
if ( str_list_match_item ( sll2 , sl - > str ) ) {
if ( tag_matched )
* tag_matched = sl - > str ;
return 1 ;
}
2003-10-16 00:04:29 +04:00
return 0 ;
}
2004-03-08 18:23:01 +03:00
/*
* Do both lists contain the same set of items ?
*/
2008-11-04 01:14:30 +03:00
int str_list_lists_equal ( const struct dm_list * sll , const struct dm_list * sll2 )
2004-03-08 18:23:01 +03:00
{
struct str_list * sl ;
2008-11-04 01:14:30 +03:00
if ( dm_list_size ( sll ) ! = dm_list_size ( sll2 ) )
2004-03-08 18:23:01 +03:00
return 0 ;
2008-11-04 01:14:30 +03:00
dm_list_iterate_items ( sl , sll )
2004-03-08 18:23:01 +03:00
if ( ! str_list_match_item ( sll2 , sl - > str ) )
return 0 ;
return 1 ;
}