Added three new helper functions to the API.

xbps_find_pkg_in_array_by_name
xbps_find_pkg_in_array_by_pattern
xbps_remove_pkgname_from_array
This commit is contained in:
Juan RP 2011-01-29 12:28:23 +01:00
parent 298305c1d5
commit 241751f1b7
2 changed files with 101 additions and 23 deletions

View File

@ -476,6 +476,28 @@ bool xbps_find_virtual_pkg_in_dict(prop_dictionary_t pkgd,
const char *str, const char *str,
bool bypattern); bool bypattern);
/**
* Finds a package dictionary in a proplib array by matching a package name.
*
* @param[in] array The proplib array where to look for.
* @param[in] name The package name to match.
*
* @return true on success, false otherwise and errno is set appropiately.
*/
prop_dictionary_t xbps_find_pkg_in_array_by_name(prop_array_t array,
const char *name);
/**
* Finds a package dictionary in a proplib array by matching a package pattern.
*
* @param[in] array The proplib array where to look for.
* @param[in] pattern The package pattern to match.
*
* @return true on success, false otherwise and errno is set appropiately.
*/
prop_dictionary_t xbps_find_pkg_in_array_by_pattern(prop_array_t array,
const char *pattern);
/** /**
* Finds a package name matching an string object in a proplib array. * Finds a package name matching an string object in a proplib array.
* *
@ -579,6 +601,16 @@ bool xbps_remove_pkg_from_dict_by_name(prop_dictionary_t dict,
*/ */
bool xbps_remove_string_from_array(prop_array_t array, const char *str); bool xbps_remove_string_from_array(prop_array_t array, const char *str);
/**
* Removes a string from a proplib's array matched by a package name.
*
* @param[in] array Proplib array where to look for.
* @param[in] name Package name to match.
*
* @return true on success, false otherwise and errno is set appropiately.
*/
bool xbps_remove_pkgname_from_array(prop_array_t array, const char *name);
/*@}*/ /*@}*/
/** @addtogroup purge */ /** @addtogroup purge */

View File

@ -251,20 +251,17 @@ xbps_find_virtual_pkg_in_dict(prop_dictionary_t d,
} }
static prop_dictionary_t static prop_dictionary_t
find_pkg_in_dict(prop_dictionary_t d, find_pkg_in_array(prop_array_t array, const char *str, bool bypattern)
const char *key,
const char *str,
bool bypattern)
{ {
prop_object_iterator_t iter; prop_object_iterator_t iter;
prop_object_t obj = NULL; prop_object_t obj;
const char *dpkgn, *pkgver; const char *pkgver, *dpkgn;
assert(d != NULL); assert(array != NULL);
assert(str != NULL); assert(name != NULL);
assert(key != NULL);
if ((iter = xbps_get_array_iter_from_dict(d, key)) == NULL) iter = prop_array_iterator(array);
if (iter == NULL)
return NULL; return NULL;
while ((obj = prop_object_iterator_next(iter))) { while ((obj = prop_object_iterator_next(iter))) {
@ -289,10 +286,40 @@ find_pkg_in_dict(prop_dictionary_t d,
errno = ENOENT; errno = ENOENT;
return NULL; return NULL;
} }
return obj; return obj;
} }
prop_dictionary_t
xbps_find_pkg_in_array_by_name(prop_array_t array, const char *name)
{
return find_pkg_in_array(array, name, false);
}
prop_dictionary_t
xbps_find_pkg_in_array_by_pattern(prop_array_t array, const char *pattern)
{
return find_pkg_in_array(array, pattern, true);
}
static prop_dictionary_t
find_pkg_in_dict(prop_dictionary_t d,
const char *key,
const char *str,
bool bypattern)
{
prop_array_t array;
assert(d != NULL);
assert(str != NULL);
assert(key != NULL);
array = prop_dictionary_get(d, key);
if (prop_object_type(array) != PROP_TYPE_ARRAY)
return NULL;
return find_pkg_in_array(array, str, bypattern);
}
prop_dictionary_t prop_dictionary_t
xbps_find_pkg_in_dict_by_name(prop_dictionary_t dict, xbps_find_pkg_in_dict_by_name(prop_dictionary_t dict,
const char *key, const char *key,
@ -420,26 +447,39 @@ xbps_get_pkg_dict_from_metadata_plist(const char *pkgn, const char *plist)
} }
static bool static bool
remove_string_from_array(prop_array_t array, const char *str, bool byname) remove_string_from_array(prop_array_t array, const char *str, int mode)
{ {
prop_object_t obj; prop_object_t obj;
const char *curname; const char *curname, *pkgdep;
char *curpkgname;
size_t i, idx = 0; size_t i, idx = 0;
bool found = false; bool found = false;
for (i = 0; i < prop_array_count(array); i++) { for (i = 0; i < prop_array_count(array); i++) {
obj = prop_array_get(array, i); obj = prop_array_get(array, i);
if (byname) { if (mode == 0) {
/* match by name */ /* exact match, obj is a string */
prop_dictionary_get_cstring_nocopy(obj, if (prop_string_equals_cstring(obj, str)) {
"pkgname", &curname);
if (strcmp(curname, str) == 0) {
found = true; found = true;
break; break;
} }
} else { } else if (mode == 1) {
/* exact match */ /* match by pkgname, obj is a string */
if (prop_string_equals_cstring(obj, str)) { pkgdep = prop_string_cstring_nocopy(obj);
curpkgname = xbps_get_pkg_name(pkgdep);
if (curpkgname == NULL)
break;
if (strcmp(curpkgname, str) == 0) {
free(curpkgname);
found = true;
break;
}
free(curpkgname);
} else if (mode == 2) {
/* match by pkgname, obj is a dictionary */
prop_dictionary_get_cstring_nocopy(obj,
"pkgname", &curname);
if (strcmp(curname, str) == 0) {
found = true; found = true;
break; break;
} }
@ -456,13 +496,19 @@ remove_string_from_array(prop_array_t array, const char *str, bool byname)
bool bool
xbps_remove_string_from_array(prop_array_t array, const char *str) xbps_remove_string_from_array(prop_array_t array, const char *str)
{ {
return remove_string_from_array(array, str, false); return remove_string_from_array(array, str, 0);
}
bool
xbps_remove_pkgname_from_array(prop_array_t array, const char *name)
{
return remove_string_from_array(array, name, 1);
} }
bool bool
xbps_remove_pkg_from_array_by_name(prop_array_t array, const char *name) xbps_remove_pkg_from_array_by_name(prop_array_t array, const char *name)
{ {
return remove_string_from_array(array, name, true); return remove_string_from_array(array, name, 2);
} }
bool bool