diff options
author | cvsadm <cvsadm> | 2005-01-21 00:44:34 +0000 |
---|---|---|
committer | cvsadm <cvsadm> | 2005-01-21 00:44:34 +0000 |
commit | b2093e3016027d6b5cf06b3f91f30769bfc099e2 (patch) | |
tree | cf58939393a9032182c4fbc4441164a9456e82f8 /ldap/servers/slapd/attrlist.c | |
download | ds-b2093e3016027d6b5cf06b3f91f30769bfc099e2.tar.gz ds-b2093e3016027d6b5cf06b3f91f30769bfc099e2.tar.xz ds-b2093e3016027d6b5cf06b3f91f30769bfc099e2.zip |
Moving NSCP Directory Server from DirectoryBranch to TRUNK, initial drop. (foxworth)ldapserver7x
Diffstat (limited to 'ldap/servers/slapd/attrlist.c')
-rw-r--r-- | ldap/servers/slapd/attrlist.c | 253 |
1 files changed, 253 insertions, 0 deletions
diff --git a/ldap/servers/slapd/attrlist.c b/ldap/servers/slapd/attrlist.c new file mode 100644 index 00000000..e57e473c --- /dev/null +++ b/ldap/servers/slapd/attrlist.c @@ -0,0 +1,253 @@ +/** BEGIN COPYRIGHT BLOCK + * Copyright 2001 Sun Microsystems, Inc. + * Portions copyright 1999, 2001-2003 Netscape Communications Corporation. + * All rights reserved. + * END COPYRIGHT BLOCK **/ + +#include "slap.h" + +void +attrlist_free(Slapi_Attr *alist) +{ + Slapi_Attr *a, *next; + for ( a = alist; a != NULL; a = next ) + { + next = a->a_next; + slapi_attr_free( &a ); + } +} + +/* + * Search for the attribute. + * If not found then create it, + * and add it to the end of the list. + * Return 0 for found, 1 for created. + */ +int +attrlist_find_or_create(Slapi_Attr **alist, const char *type, Slapi_Attr ***a) +{ + return attrlist_find_or_create_locking_optional(alist, type, a, PR_TRUE, PR_TRUE); +} + +int +attrlist_find_or_create_locking_optional(Slapi_Attr **alist, const char *type, Slapi_Attr ***a, PRBool use_lock, PRBool ref_count) +{ + int rc= 0; /* found */ + if ( *a==NULL ) + { + for ( *a = alist; **a != NULL; *a = &(**a)->a_next ) { + if ( strcasecmp( (**a)->a_type, type ) == 0 ) { + break; + } + } + } + + if( **a==NULL ) + { + **a = slapi_attr_new(); + slapi_attr_init_locking_optional(**a, type, use_lock, ref_count); + rc= 1; /* created */ + } + return rc; +} + +/* + * attrlist_merge - merge the given type and value with the list of + * attributes in attrs. + */ +void +attrlist_merge(Slapi_Attr **alist, const char *type, struct berval **vals) +{ + Slapi_Value **values= NULL; + valuearray_init_bervalarray(vals,&values); /* JCM SLOW FUNCTION */ + attrlist_merge_valuearray(alist,type,values); + valuearray_free(&values); +} + + +/* + * attrlist_merge_valuearray - merge the given type and value with the list of + * attributes in attrs. + */ +void +attrlist_merge_valuearray(Slapi_Attr **alist, const char *type, Slapi_Value **vals) +{ + Slapi_Attr **a= NULL; + attrlist_find_or_create(alist, type, &a); + valueset_add_valuearray( &(*a)->a_present_values, vals ); +} + + +/* + * attrlist_find - find and return attribute type in list a + */ + +Slapi_Attr * +attrlist_find(Slapi_Attr *a, const char *type) +{ + for ( ; a != NULL; a = a->a_next ) { + if ( strcasecmp( a->a_type, type ) == 0 ) { + return( a ); + } + } + + return( NULL ); +} + + +/* + * attrlist_count_subtypes + * + * Returns a count attributes which conform to type + * in the attr list a. This count includes all subtypes of + * type + */ +int +attrlist_count_subtypes(Slapi_Attr *a, const char *type) +{ + int counter = 0; + + for ( ; a != NULL; a = a->a_next ) { + if ( slapi_attr_type_cmp( type , a->a_type, SLAPI_TYPE_CMP_SUBTYPE) == 0 ) { + counter++; + } + } + + return( counter ); +} + +/* + * attrlist_find_ex + * + * Finds the first subtype in the list which matches "type" + * starting at the beginning or hint depending on whether + * hint has a value + * + * It is intended that hint be zero when first called and then + * passed back in on subsequent calls until 0 is returned to mark + * the end of the filtered list + */ +Slapi_Attr * +attrlist_find_ex( + Slapi_Attr *a, + const char *type, + int *type_name_disposition, /* pass null if you're not interested */ + char** actual_type_name, /* pass null if you're not interested */ + void **hint +) +{ + Slapi_Attr **attr_cursor = (Slapi_Attr **)hint; + + if (type_name_disposition) *type_name_disposition = 0; + if (actual_type_name) *actual_type_name = NULL; + + if(*attr_cursor == NULL) + *attr_cursor = a; /* start at the beginning of the list */ + else + *attr_cursor = (*attr_cursor)->a_next; + + while(*attr_cursor != NULL) { + + /* Determine whether the two types are related:*/ + if ( slapi_attr_type_cmp( type , (*attr_cursor)->a_type, SLAPI_TYPE_CMP_SUBTYPE) == 0 ) { + /* We got a match. Now figure out if we matched because it was a subtype */ + if (type_name_disposition) { + if ( 0 == slapi_attr_type_cmp( type , (*attr_cursor)->a_type, SLAPI_TYPE_CMP_EXACT) ) { + *type_name_disposition = SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_EXACTLY_OR_ALIAS; + } else { + *type_name_disposition = SLAPI_VIRTUALATTRS_TYPE_NAME_MATCHED_SUBTYPE; + } + } + + if (actual_type_name) { + *actual_type_name = (*attr_cursor)->a_type; + } + + a = *attr_cursor; /* the attribute to return */ + return( a ); + + } + + *attr_cursor = (*attr_cursor)->a_next; /* no match, move cursor */ + } + + return( NULL ); +} + +/* + * attr_remove - remove the attribute from the list of attributes + */ +Slapi_Attr * +attrlist_remove(Slapi_Attr **attrs, const char *type) +{ + Slapi_Attr **a; + Slapi_Attr *save= NULL; + for ( a = attrs; *a != NULL; a = &(*a)->a_next ) + { + if ( strcasecmp( (*a)->a_type, type ) == 0 ) + { + break; + } + } + if (*a != NULL) + { + save = *a; + *a = (*a)->a_next; + } + return save; +} + +void +attrlist_add(Slapi_Attr **attrs, Slapi_Attr *a) +{ + a->a_next= *attrs; + *attrs= a; +} + +/* + * attrlist_delete - delete the attribute type in list pointed to by attrs + * return 0 deleted ok + * 1 not found in list a + * -1 something bad happened + */ + +int +attrlist_delete(Slapi_Attr **attrs, const char *type) +{ + Slapi_Attr **a; + Slapi_Attr *save; + + for ( a = attrs; *a != NULL; a = &(*a)->a_next ) { + if ( strcasecmp( (*a)->a_type, type ) == 0 ) { + break; + } + } + + if ( *a == NULL ) { + return( 1 ); + } + + save = *a; + *a = (*a)->a_next; + slapi_attr_free( &save ); + + return( 0 ); +} + +/* + * attrlist_replace - replace the attribute value(s) with this value(s) + */ +void attrlist_replace(Slapi_Attr **alist, const char *type, struct berval **vals) +{ + Slapi_Attr **a = NULL; + Slapi_Value **values = NULL; + + if (vals == NULL || vals[0] == NULL) { + (void)attrlist_delete(alist, type); + } else { + attrlist_find_or_create(alist, type, &a); + valuearray_init_bervalarray(vals, &values); + attr_replace(*a, values); + } +} + |