diff options
author | Dmitri Pal <dpal@redhat.com> | 2009-04-10 11:30:59 -0400 |
---|---|---|
committer | Simo Sorce <ssorce@redhat.com> | 2009-04-10 16:59:24 -0400 |
commit | 24967bd826ad5437a11b1431c8289b1881b52a17 (patch) | |
tree | 2bd20ac84fcba597abc0e492e23ccebb3940b340 /common/collection | |
parent | baf568c235c4f8e6e733c47bc0c76bf84b55b9b0 (diff) | |
download | sssd-24967bd826ad5437a11b1431c8289b1881b52a17.tar.gz sssd-24967bd826ad5437a11b1431c8289b1881b52a17.tar.xz sssd-24967bd826ad5437a11b1431c8289b1881b52a17.zip |
Added functions to create list of sections and attributes.
Diffstat (limited to 'common/collection')
-rw-r--r-- | common/collection/collection_tools.c | 100 | ||||
-rw-r--r-- | common/collection/collection_tools.h | 6 |
2 files changed, 106 insertions, 0 deletions
diff --git a/common/collection/collection_tools.c b/common/collection/collection_tools.c index cefe7ae4f..2fec6c558 100644 --- a/common/collection/collection_tools.c +++ b/common/collection/collection_tools.c @@ -638,4 +638,104 @@ int print_item(struct collection_item *handle, char *name) return error; } +/* Function to free the list of properties. */ +void free_property_list(char **str_list) +{ + int current = 0; + + TRACE_FLOW_STRING("free_property_list","Entry"); + + if (str_list != NULL) { + while(str_list[current]) { + free(str_list[current]); + current++; + } + free(str_list); + } + + TRACE_FLOW_STRING("free_property_list","Exit"); +} + + +/* Convert collection to list of properties */ +char **collection_to_list(struct collection_item *handle, int *size, int *error) +{ + struct collection_iterator *iterator; + struct collection_item *item = NULL; + char **list; + unsigned count; + int err; + int current = 0; + + TRACE_FLOW_STRING("collection_to_list","Entry"); + + /* Get number of the subsections */ + err = get_collection_count(handle,&count); + if (err) { + TRACE_ERROR_NUMBER("Failed to get count of items from collection.", err); + if (error) *error = err; + return NULL; + } + + /* Allocate memory for the sections */ + errno = 0; + list = (char **)calloc(count, sizeof(char *)); + if (list == NULL) { + err = errno; + TRACE_ERROR_NUMBER("Failed to get allocate memory.", err); + if (error) *error = ENOMEM; + return NULL; + } + + /* Now iterate to fill in the sections */ + /* Bind iterator */ + err = bind_iterator(&iterator, handle, COL_TRAVERSE_ONELEVEL); + if (err) { + TRACE_ERROR_NUMBER("Failed to bind.", err); + if (error) *error = err; + free(list); + return NULL; + } + + while(1) { + /* Loop through a collection */ + err = iterate_collection(iterator, &item); + if (err) { + TRACE_ERROR_NUMBER("Failed to iterate collection", err); + if (error) *error = err; + free_property_list(list); + unbind_iterator(iterator); + return NULL; + } + + /* Are we done ? */ + if (item == NULL) break; + + TRACE_INFO_STRING("Property:", get_item_property(item, NULL)); + + /* Skip head */ + if(get_item_type(item) == COL_TYPE_COLLECTION) continue; + + /* Allocate memory for the new string */ + errno = 0; + list[current] = strdup(get_item_property(item, NULL)); + if (list[current] == NULL) { + err = errno; + TRACE_ERROR_NUMBER("Failed to dup string.", err); + if (error) *error = ENOMEM; + free_property_list(list); + return NULL; + } + current++; + } + + /* Do not forget to unbind iterator - otherwise there will be a leak */ + unbind_iterator(iterator); + + if (size) *size = (int)(count - 1); + if (error) *error = EOK; + + TRACE_FLOW_STRING("collection_to_list returning", list == NULL ? "NULL" : list[0]); + return list; +} diff --git a/common/collection/collection_tools.h b/common/collection/collection_tools.h index defc4aa04..a8d13f749 100644 --- a/common/collection/collection_tools.h +++ b/common/collection/collection_tools.h @@ -98,4 +98,10 @@ int print_collection2(struct collection_item *handle); /* Find and print one item using default serialization */ int print_item(struct collection_item *handle, char *name); +/* Convert collection to list of properties */ +char **collection_to_list(struct collection_item *handle, int *size, int *error); + +/* Function to free the list of properties. */ +void free_property_list(char **str_list); + #endif |