Introduce xbps_callback_array_iter_in_dict().

--HG--
extra : convert_revision : e0696de04df785d99ae1748a2d972a74e04c774f
This commit is contained in:
Juan RP 2008-12-20 00:55:48 +01:00
parent 742ae607c4
commit e992ee5c83
4 changed files with 60 additions and 44 deletions

View file

@ -62,19 +62,24 @@ xbps_add_obj_to_array(prop_array_t array, prop_object_t obj)
return true; return true;
} }
prop_object_iterator_t void
xbps_get_array_iter_from_dict(prop_dictionary_t dict, const char *key) 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) if (func == NULL)
return NULL; return;
array = prop_dictionary_get(dict, key); iter = xbps_get_array_iter_from_dict(dict, key);
if (array == NULL || prop_object_type(array) != PROP_TYPE_ARRAY) if (iter == NULL)
return NULL; return;
return prop_array_iterator(array); while ((obj = prop_object_iterator_next(iter)))
(*func)(obj);
prop_object_iterator_release(iter);
} }
prop_dictionary_t prop_dictionary_t
@ -128,6 +133,21 @@ xbps_find_string_in_array(prop_array_t array, const char *val)
return false; 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 bool
xbps_register_repository(const char *uri) xbps_register_repository(const char *uri)
{ {
@ -205,42 +225,25 @@ fail:
} }
void 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; const char *pkgname, *version, *short_desc;
iter = xbps_get_array_iter_from_dict(dict, key); if (prop_object_type(obj) != PROP_TYPE_DICTIONARY)
if (iter == NULL)
return; return;
while ((obj = prop_object_iterator_next(iter))) {
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname); prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
prop_dictionary_get_cstring_nocopy(obj, "version", &version); prop_dictionary_get_cstring_nocopy(obj, "version", &version);
prop_dictionary_get_cstring_nocopy(obj, "short_desc", prop_dictionary_get_cstring_nocopy(obj, "short_desc", &short_desc);
&short_desc);
if (pkgname && version && short_desc) if (pkgname && version && short_desc)
printf("%s (%s)\t%s\n", pkgname, version, short_desc); printf("%s (%s)\t%s\n", pkgname, version, short_desc);
}
prop_object_iterator_release(iter);
} }
void 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; if (prop_object_type(obj) != PROP_TYPE_STRING)
prop_object_t obj;
iter = xbps_get_array_iter_from_dict(dict, key);
if (iter == NULL)
return; return;
while ((obj = prop_object_iterator_next(iter))) {
if (prop_object_type(obj) == PROP_TYPE_STRING)
printf("%s\n", prop_string_cstring_nocopy(obj)); printf("%s\n", prop_string_cstring_nocopy(obj));
}
prop_object_iterator_release(iter);
} }

View file

@ -51,6 +51,19 @@ xbps_add_array_to_dict(prop_dictionary_t, prop_array_t, const char *);
bool bool
xbps_add_obj_to_array(prop_array_t, prop_object_t); 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. * 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. * using a triplet: pkgname, version and short_desc.
* *
* Arguments: * Arguments:
* - prop_dictionary_t: dictionary where to search on. * - prop_object_t: the object to be processed.
* - const char *: the key associated with the dictionary.
*/ */
void 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. * Lists all string values in an array object in a dictionary.
* *
* Arguments: * Arguments:
* - prop_dictionary_t: dictionary that has the array. * - prop_object_t: the object to be processed.
* - const char *: key of the array.
*/ */
void 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. * Registers a repository specified by an URI into the pool.

View file

@ -157,7 +157,8 @@ main(int argc, char **argv)
exit(EINVAL); 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) { } else if (strcmp(argv[1], "show") == 0) {
/* Shows info about a binary package. */ /* Shows info about a binary package. */

View file

@ -292,7 +292,8 @@ main(int argc, char **argv)
usage(); usage();
dbdict = prop_dictionary_internalize_from_file(dbfile); 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) { } else if (strcmp(argv[1], "version") == 0) {
/* Prints version of an installed package */ /* Prints version of an installed package */