2004-10-08 12:13:00 +04:00
/*
Unix SMB / CIFS implementation .
2005-02-04 07:58:48 +03:00
Copyright ( C ) Andrew Tridgell 2005
2005-09-26 21:42:12 +04:00
Copyright ( C ) Jelmer Vernooij 2005
2004-10-08 12:13:00 +04:00
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 . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include "includes.h"
2005-02-04 07:58:48 +03:00
/*
build a null terminated list of strings from a input string and a
2005-08-30 17:58:48 +04:00
separator list . The separator list must contain characters less than
2005-02-04 07:58:48 +03:00
or equal to 0x2f for this to work correctly on multi - byte strings
*/
2005-02-08 04:02:56 +03:00
const char * * str_list_make ( TALLOC_CTX * mem_ctx , const char * string , const char * sep )
2005-02-04 07:58:48 +03:00
{
int num_elements = 0 ;
2005-02-08 04:02:56 +03:00
const char * * ret = NULL ;
2004-10-08 12:13:00 +04:00
2005-02-04 07:58:48 +03:00
if ( sep = = NULL ) {
sep = LIST_SEP ;
}
2004-10-08 12:13:00 +04:00
2005-02-15 05:24:57 +03:00
ret = talloc_array ( mem_ctx , const char * , 1 ) ;
2005-02-04 07:58:48 +03:00
if ( ret = = NULL ) {
2004-10-08 12:13:00 +04:00
return NULL ;
}
2005-02-04 07:58:48 +03:00
while ( string & & * string ) {
size_t len = strcspn ( string , sep ) ;
2005-02-08 04:02:56 +03:00
const char * * ret2 ;
2004-10-08 12:13:00 +04:00
2005-02-04 07:58:48 +03:00
if ( len = = 0 ) {
string + = strspn ( string , sep ) ;
continue ;
}
2005-02-08 04:02:56 +03:00
ret2 = talloc_realloc ( mem_ctx , ret , const char * , num_elements + 2 ) ;
2005-02-04 07:58:48 +03:00
if ( ret2 = = NULL ) {
talloc_free ( ret ) ;
2004-10-08 12:13:00 +04:00
return NULL ;
}
2005-02-04 07:58:48 +03:00
ret = ret2 ;
ret [ num_elements ] = talloc_strndup ( ret , string , len ) ;
if ( ret [ num_elements ] = = NULL ) {
talloc_free ( ret ) ;
return NULL ;
}
num_elements + + ;
string + = len ;
2004-10-08 12:13:00 +04:00
}
2005-02-04 07:58:48 +03:00
ret [ num_elements ] = NULL ;
return ret ;
2004-10-08 12:13:00 +04:00
}
2005-09-26 21:42:12 +04:00
/* build a null terminated list of strings from an argv-like input string
Entries are seperated by spaces and can be enclosed by quotes .
Does NOT support escaping
*/
2005-09-26 22:16:23 +04:00
const char * * str_list_make_shell ( TALLOC_CTX * mem_ctx , const char * string , const char * sep )
2005-09-26 21:42:12 +04:00
{
int num_elements = 0 ;
const char * * ret = NULL ;
ret = talloc_array ( mem_ctx , const char * , 1 ) ;
if ( ret = = NULL ) {
return NULL ;
}
2005-09-26 22:16:23 +04:00
if ( sep = = NULL )
sep = " \t \n \r " ;
2005-09-26 21:42:12 +04:00
while ( string & & * string ) {
2005-09-26 22:16:23 +04:00
size_t len = strcspn ( string , sep ) ;
2005-09-26 21:42:12 +04:00
char * element ;
const char * * ret2 ;
if ( len = = 0 ) {
2005-09-26 22:16:23 +04:00
string + = strspn ( string , sep ) ;
2005-09-26 21:42:12 +04:00
continue ;
}
if ( * string = = ' \" ' ) {
string + + ;
len = strcspn ( string , " \" " ) ;
element = talloc_strndup ( ret , string , len ) ;
string + = len + 1 ;
} else {
element = talloc_strndup ( ret , string , len ) ;
string + = len ;
}
if ( element = = NULL ) {
talloc_free ( ret ) ;
return NULL ;
}
ret2 = talloc_realloc ( mem_ctx , ret , const char * , num_elements + 2 ) ;
if ( ret2 = = NULL ) {
talloc_free ( ret ) ;
return NULL ;
}
ret = ret2 ;
ret [ num_elements ] = element ;
num_elements + + ;
}
ret [ num_elements ] = NULL ;
return ret ;
}
2005-08-30 17:58:48 +04:00
/* join a list back to one string */
char * str_list_join ( TALLOC_CTX * mem_ctx , const char * * list , char seperator )
{
char * ret = NULL ;
int i ;
if ( list [ 0 ] = = NULL )
return talloc_strdup ( mem_ctx , " " ) ;
ret = talloc_strdup ( mem_ctx , list [ 0 ] ) ;
for ( i = 1 ; list [ i ] ; i + + ) {
ret = talloc_asprintf_append ( ret , " %c%s " , seperator , list [ i ] ) ;
}
return ret ;
}
2005-09-26 21:42:12 +04:00
/* join a list back to one (shell-like) string; entries
* seperated by spaces , using quotes where necessary */
2005-09-26 22:16:23 +04:00
char * str_list_join_shell ( TALLOC_CTX * mem_ctx , const char * * list , char sep )
2005-09-26 21:42:12 +04:00
{
char * ret = NULL ;
int i ;
if ( list [ 0 ] = = NULL )
return talloc_strdup ( mem_ctx , " " ) ;
if ( strchr ( list [ 0 ] , ' ' ) | | strlen ( list [ 0 ] ) = = 0 )
ret = talloc_asprintf ( mem_ctx , " \" %s \" " , list [ 0 ] ) ;
else
ret = talloc_strdup ( mem_ctx , list [ 0 ] ) ;
for ( i = 1 ; list [ i ] ; i + + ) {
if ( strchr ( list [ i ] , ' ' ) | | strlen ( list [ i ] ) = = 0 )
2005-09-26 22:16:23 +04:00
ret = talloc_asprintf_append ( ret , " %c \" %s \" " , sep , list [ i ] ) ;
2005-09-26 21:42:12 +04:00
else
2005-09-26 22:16:23 +04:00
ret = talloc_asprintf_append ( ret , " %c%s " , sep , list [ i ] ) ;
2005-09-26 21:42:12 +04:00
}
return ret ;
}
2005-08-30 17:58:48 +04:00
2005-02-04 07:58:48 +03:00
/*
return the number of elements in a string list
*/
size_t str_list_length ( const char * * list )
2004-10-08 12:13:00 +04:00
{
2005-02-04 07:58:48 +03:00
size_t ret ;
for ( ret = 0 ; list & & list [ ret ] ; ret + + ) /* noop */ ;
return ret ;
}
2004-10-08 12:13:00 +04:00
2005-02-04 07:58:48 +03:00
/*
copy a string list
*/
2005-02-08 04:02:56 +03:00
const char * * str_list_copy ( TALLOC_CTX * mem_ctx , const char * * list )
2005-02-04 07:58:48 +03:00
{
int i ;
2005-02-08 04:02:56 +03:00
const char * * ret = talloc_array ( mem_ctx , const char * , str_list_length ( list ) + 1 ) ;
2005-02-04 07:58:48 +03:00
if ( ret = = NULL ) return NULL ;
for ( i = 0 ; list & & list [ i ] ; i + + ) {
ret [ i ] = talloc_strdup ( ret , list [ i ] ) ;
if ( ret [ i ] = = NULL ) {
talloc_free ( ret ) ;
return NULL ;
}
2004-10-08 12:13:00 +04:00
}
2005-02-04 07:58:48 +03:00
ret [ i ] = NULL ;
return ret ;
2004-10-08 12:13:00 +04:00
}
2005-02-04 07:58:48 +03:00
/*
2004-10-08 12:13:00 +04:00
Return true if all the elements of the list match exactly .
2005-02-04 07:58:48 +03:00
*/
BOOL str_list_equal ( const char * * list1 , const char * * list2 )
2004-10-08 12:13:00 +04:00
{
2005-02-04 07:58:48 +03:00
int i ;
2004-10-08 12:13:00 +04:00
2005-02-04 07:58:48 +03:00
if ( list1 = = NULL | | list2 = = NULL ) {
2004-10-08 12:13:00 +04:00
return ( list1 = = list2 ) ;
2005-02-04 07:58:48 +03:00
}
2004-10-08 12:13:00 +04:00
2005-02-04 07:58:48 +03:00
for ( i = 0 ; list1 [ i ] & & list2 [ i ] ; i + + ) {
if ( strcmp ( list1 [ i ] , list2 [ i ] ) ! = 0 ) {
2004-10-08 12:13:00 +04:00
return False ;
2005-02-04 07:58:48 +03:00
}
}
if ( list1 [ i ] | | list2 [ i ] ) {
return False ;
2004-10-08 12:13:00 +04:00
}
return True ;
}
2005-02-14 12:15:24 +03:00
/*
add an entry to a string list
*/
const char * * str_list_add ( const char * * list , const char * s )
{
size_t len = str_list_length ( list ) ;
const char * * ret ;
ret = talloc_realloc ( NULL , list , const char * , len + 2 ) ;
if ( ret = = NULL ) return NULL ;
ret [ len ] = talloc_strdup ( ret , s ) ;
if ( ret [ len ] = = NULL ) return NULL ;
ret [ len + 1 ] = NULL ;
return ret ;
}
/*
remove an entry from a string list
*/
void str_list_remove ( const char * * list , const char * s )
{
int i ;
for ( i = 0 ; list [ i ] ; i + + ) {
if ( strcmp ( list [ i ] , s ) = = 0 ) break ;
}
if ( ! list [ i ] ) return ;
for ( ; list [ i ] ; i + + ) {
list [ i ] = list [ i + 1 ] ;
}
}
/*
return True if a string is in a list
*/
BOOL str_list_check ( const char * * list , const char * s )
{
int i ;
for ( i = 0 ; list [ i ] ; i + + ) {
if ( strcmp ( list [ i ] , s ) = = 0 ) return True ;
}
return False ;
}
2005-05-28 12:47:01 +04:00
/*
return True if a string is in a list , case insensitively
*/
BOOL str_list_check_ci ( const char * * list , const char * s )
{
int i ;
for ( i = 0 ; list [ i ] ; i + + ) {
if ( strcasecmp ( list [ i ] , s ) = = 0 ) return True ;
}
return False ;
}