diff --git a/utils/plist_utils.c b/utils/plist_utils.c index fe4ed422e0..da4545d759 100644 --- a/utils/plist_utils.c +++ b/utils/plist_utils.c @@ -62,19 +62,24 @@ xbps_add_obj_to_array(prop_array_t array, prop_object_t obj) return true; } -prop_object_iterator_t -xbps_get_array_iter_from_dict(prop_dictionary_t dict, const char *key) +void +xbps_callback_array_iter_in_dict(prop_dictionary_t dict, const char *key, + void (*func)(prop_object_t)) { - prop_array_t array; + prop_object_iterator_t iter; + prop_object_t obj; - if (dict == NULL || key == NULL) - return NULL; + if (func == NULL) + return; - array = prop_dictionary_get(dict, key); - if (array == NULL || prop_object_type(array) != PROP_TYPE_ARRAY) - return NULL; + iter = xbps_get_array_iter_from_dict(dict, key); + if (iter == NULL) + return; - return prop_array_iterator(array); + while ((obj = prop_object_iterator_next(iter))) + (*func)(obj); + + prop_object_iterator_release(iter); } prop_dictionary_t @@ -128,6 +133,21 @@ xbps_find_string_in_array(prop_array_t array, const char *val) return false; } +prop_object_iterator_t +xbps_get_array_iter_from_dict(prop_dictionary_t dict, const char *key) +{ + prop_array_t array; + + if (dict == NULL || key == NULL) + return NULL; + + array = prop_dictionary_get(dict, key); + if (array == NULL || prop_object_type(array) != PROP_TYPE_ARRAY) + return NULL; + + return prop_array_iterator(array); +} + bool xbps_register_repository(const char *uri) { @@ -205,42 +225,25 @@ fail: } void -xbps_list_pkgs_in_dict(prop_dictionary_t dict, const char *key) +xbps_list_pkgs_in_dict(prop_object_t obj) { - prop_object_iterator_t iter; - prop_object_t obj; const char *pkgname, *version, *short_desc; - iter = xbps_get_array_iter_from_dict(dict, key); - if (iter == NULL) + if (prop_object_type(obj) != PROP_TYPE_DICTIONARY) return; - while ((obj = prop_object_iterator_next(iter))) { - prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname); - prop_dictionary_get_cstring_nocopy(obj, "version", &version); - prop_dictionary_get_cstring_nocopy(obj, "short_desc", - &short_desc); - if (pkgname && version && short_desc) - printf("%s (%s)\t%s\n", pkgname, version, short_desc); - } - - prop_object_iterator_release(iter); + prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname); + prop_dictionary_get_cstring_nocopy(obj, "version", &version); + prop_dictionary_get_cstring_nocopy(obj, "short_desc", &short_desc); + if (pkgname && version && short_desc) + printf("%s (%s)\t%s\n", pkgname, version, short_desc); } void -xbps_list_strings_in_array(prop_dictionary_t dict, const char *key) +xbps_list_strings_in_array(prop_object_t obj) { - prop_object_iterator_t iter; - prop_object_t obj; - - iter = xbps_get_array_iter_from_dict(dict, key); - if (iter == NULL) + if (prop_object_type(obj) != PROP_TYPE_STRING) return; - while ((obj = prop_object_iterator_next(iter))) { - if (prop_object_type(obj) == PROP_TYPE_STRING) - printf("%s\n", prop_string_cstring_nocopy(obj)); - } - - prop_object_iterator_release(iter); + printf("%s\n", prop_string_cstring_nocopy(obj)); } diff --git a/utils/plist_utils.h b/utils/plist_utils.h index 12b8f8fdf3..fa7e4bafbd 100644 --- a/utils/plist_utils.h +++ b/utils/plist_utils.h @@ -51,6 +51,19 @@ xbps_add_array_to_dict(prop_dictionary_t, prop_array_t, const char *); bool xbps_add_obj_to_array(prop_array_t, prop_object_t); +/* + * Executes a function callback to process all objects that are + * found in array with specified key inside of a dictionary. + * + * Arguments: + * - prop_dictionary_t: dictionary to search on. + * - const char *: key of the array. + * - (*func)(prop_object_t): callback associated. + */ +void +xbps_callback_array_iter_in_dict(prop_dictionary_t, const char *, + void (*func)(prop_object_t)); + /* * Finds a package's dictionary into the main dictionary. * @@ -93,21 +106,19 @@ xbps_get_array_iter_from_dict(prop_dictionary_t, const char *); * using a triplet: pkgname, version and short_desc. * * Arguments: - * - prop_dictionary_t: dictionary where to search on. - * - const char *: the key associated with the dictionary. + * - prop_object_t: the object to be processed. */ void -xbps_list_pkgs_in_dict(prop_dictionary_t, const char *); +xbps_list_pkgs_in_dict(prop_object_t); /* * Lists all string values in an array object in a dictionary. * * Arguments: - * - prop_dictionary_t: dictionary that has the array. - * - const char *: key of the array. + * - prop_object_t: the object to be processed. */ void -xbps_list_strings_in_array(prop_dictionary_t, const char *); +xbps_list_strings_in_array(prop_object_t); /* * Registers a repository specified by an URI into the pool. diff --git a/utils/xbps-bin.c b/utils/xbps-bin.c index 4b1f70c3e7..897c19a47b 100644 --- a/utils/xbps-bin.c +++ b/utils/xbps-bin.c @@ -157,7 +157,8 @@ main(int argc, char **argv) exit(EINVAL); } - xbps_list_strings_in_array(dict, "repository-list"); + xbps_callback_array_iter_in_dict(dict, "repository-list", + xbps_list_strings_in_array); } else if (strcmp(argv[1], "show") == 0) { /* Shows info about a binary package. */ diff --git a/utils/xbps-pkgdb.c b/utils/xbps-pkgdb.c index 7ec6b8a08b..2edfa14629 100644 --- a/utils/xbps-pkgdb.c +++ b/utils/xbps-pkgdb.c @@ -292,7 +292,8 @@ main(int argc, char **argv) usage(); dbdict = prop_dictionary_internalize_from_file(dbfile); - xbps_list_pkgs_in_dict(dbdict, "packages_installed"); + xbps_callback_array_iter_in_dict(dbdict, + "packages_installed", xbps_list_pkgs_in_dict); } else if (strcmp(argv[1], "version") == 0) { /* Prints version of an installed package */