Make xbps_callback_array_in_dict() return a bool.

The func cb accepted also returns a bool now.

--HG--
extra : convert_revision : 1adf9a54e111f5d7f3dd78a829c3cf680d15f769
This commit is contained in:
Juan RP 2008-12-20 04:53:56 +01:00
parent 835d4e7b7c
commit 6698f3a1ff
3 changed files with 43 additions and 24 deletions

View file

@ -33,7 +33,7 @@
#include "xbps_api.h" #include "xbps_api.h"
static void xbps_list_strings_in_array2(prop_object_t); static bool xbps_list_strings_in_array2(prop_object_t);
bool bool
xbps_add_obj_to_dict(prop_dictionary_t dict, prop_object_t obj, xbps_add_obj_to_dict(prop_dictionary_t dict, prop_object_t obj,
@ -64,24 +64,29 @@ xbps_add_obj_to_array(prop_array_t array, prop_object_t obj)
return true; return true;
} }
void bool
xbps_callback_array_iter_in_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)) bool (*func)(prop_object_t))
{ {
prop_object_iterator_t iter; prop_object_iterator_t iter;
prop_object_t obj; prop_object_t obj;
if (func == NULL) if (func == NULL)
return; return false;
iter = xbps_get_array_iter_from_dict(dict, key); iter = xbps_get_array_iter_from_dict(dict, key);
if (iter == NULL) if (iter == NULL)
return; return false;
while ((obj = prop_object_iterator_next(iter))) while ((obj = prop_object_iterator_next(iter))) {
(*func)(obj); if (!(*func)(obj)) {
prop_object_iterator_release(iter);
return false;
}
}
prop_object_iterator_release(iter); prop_object_iterator_release(iter);
return true;
} }
prop_dictionary_t prop_dictionary_t
@ -250,9 +255,10 @@ xbps_show_pkg_info(prop_dictionary_t dict)
} else if (prop_object_type(obj2) == PROP_TYPE_ARRAY) { } else if (prop_object_type(obj2) == PROP_TYPE_ARRAY) {
printf("\n\t"); printf("\n\t");
xbps_callback_array_iter_in_dict(dict, if (!xbps_callback_array_iter_in_dict(dict,
prop_dictionary_keysym_cstring_nocopy(obj), prop_dictionary_keysym_cstring_nocopy(obj),
xbps_list_strings_in_array2); xbps_list_strings_in_array2))
return;
printf("\n"); printf("\n");
} }
} }
@ -260,28 +266,32 @@ xbps_show_pkg_info(prop_dictionary_t dict)
prop_object_iterator_release(iter); prop_object_iterator_release(iter);
} }
void bool
xbps_list_pkgs_in_dict(prop_object_t obj) xbps_list_pkgs_in_dict(prop_object_t obj)
{ {
const char *pkgname, *version, *short_desc; const char *pkgname, *version, *short_desc;
if (prop_object_type(obj) != PROP_TYPE_DICTIONARY) if (prop_object_type(obj) != PROP_TYPE_DICTIONARY)
return; return false;
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", &short_desc); prop_dictionary_get_cstring_nocopy(obj, "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);
return true;
}
return false;
} }
static void static bool
xbps_list_strings_in_array2(prop_object_t obj) xbps_list_strings_in_array2(prop_object_t obj)
{ {
static uint16_t count; static uint16_t count;
if (prop_object_type(obj) != PROP_TYPE_STRING) if (prop_object_type(obj) != PROP_TYPE_STRING)
return; return false;
if (count == 4) { if (count == 4) {
printf("\n\t"); printf("\n\t");
@ -290,13 +300,15 @@ xbps_list_strings_in_array2(prop_object_t obj)
printf("%s ", prop_string_cstring_nocopy(obj)); printf("%s ", prop_string_cstring_nocopy(obj));
count++; count++;
return true;
} }
void bool
xbps_list_strings_in_array(prop_object_t obj) xbps_list_strings_in_array(prop_object_t obj)
{ {
if (prop_object_type(obj) != PROP_TYPE_STRING) if (prop_object_type(obj) != PROP_TYPE_STRING)
return; return false;
printf("%s\n", prop_string_cstring_nocopy(obj)); printf("%s\n", prop_string_cstring_nocopy(obj));
return true;
} }

View file

@ -59,10 +59,12 @@ xbps_add_obj_to_array(prop_array_t, prop_object_t);
* - prop_dictionary_t: dictionary to search on. * - prop_dictionary_t: dictionary to search on.
* - const char *: key of the array. * - const char *: key of the array.
* - (*func)(prop_object_t): callback associated. * - (*func)(prop_object_t): callback associated.
*
* Returns true on success, false otherwise and the loop is terminated.
*/ */
void bool
xbps_callback_array_iter_in_dict(prop_dictionary_t, const char *, xbps_callback_array_iter_in_dict(prop_dictionary_t, const char *,
void (*func)(prop_object_t)); bool (*func)(prop_object_t));
/* /*
* Finds a package's dictionary into the main dictionary. * Finds a package's dictionary into the main dictionary.
@ -83,7 +85,7 @@ xbps_find_pkg_in_dict(prop_dictionary_t, const char *);
* - prop_array_t: array to search for the string. * - prop_array_t: array to search for the string.
* - const char *: string value of the object to be found. * - const char *: string value of the object to be found.
* *
* Returns true on success, false on failure. * Returns true on success, false otherwise.
*/ */
bool bool
xbps_find_string_in_array(prop_array_t, const char *); xbps_find_string_in_array(prop_array_t, const char *);
@ -106,8 +108,10 @@ xbps_get_array_iter_from_dict(prop_dictionary_t, const char *);
* *
* Arguments: * Arguments:
* - prop_object_t: the object to be processed. * - prop_object_t: the object to be processed.
*
* Returns true on success, false otherwise.
*/ */
void bool
xbps_list_pkgs_in_dict(prop_object_t); xbps_list_pkgs_in_dict(prop_object_t);
/* /*
@ -115,8 +119,10 @@ xbps_list_pkgs_in_dict(prop_object_t);
* *
* Arguments: * Arguments:
* - prop_object_t: the object to be processed. * - prop_object_t: the object to be processed.
*
* Returns true on success, false otherwise.
*/ */
void bool
xbps_list_strings_in_array(prop_object_t); xbps_list_strings_in_array(prop_object_t);
/* /*
@ -125,7 +131,7 @@ xbps_list_strings_in_array(prop_object_t);
* Arguments: * Arguments:
* - const char *: URI to register. * - const char *: URI to register.
* *
* Returns true on success, false on failure. * Returns true on success, false otherwise.
*/ */
bool bool
xbps_register_repository(const char *); xbps_register_repository(const char *);

View file

@ -291,8 +291,9 @@ main(int argc, char **argv)
usage(); usage();
dbdict = prop_dictionary_internalize_from_file(dbfile); dbdict = prop_dictionary_internalize_from_file(dbfile);
xbps_callback_array_iter_in_dict(dbdict, if (!xbps_callback_array_iter_in_dict(dbdict,
"packages", xbps_list_pkgs_in_dict); "packages", xbps_list_pkgs_in_dict))
exit(EINVAL);
} 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 */