libxbps: move match funcs from plist_find.c into its own file.

This commit is contained in:
Juan RP 2011-07-19 21:07:04 +02:00
parent d0372b5555
commit 468366b1b2
3 changed files with 126 additions and 85 deletions

View File

@ -43,7 +43,7 @@ OBJS += package_unpack.o package_requiredby.o package_register.o
OBJS += package_purge.o package_replaces.o
OBJS += transaction_dictionary.o transaction_sortdeps.o
OBJS += download.o fexec.o humanize_number.o plist.o
OBJS += plist_archive_entry.o plist_find.o plist_remove.o
OBJS += plist_archive_entry.o plist_find.o plist_match.o plist_remove.o
OBJS += plist_fetch.o util.o mkpath.o initend.o
OBJS += regpkgdb_dictionary.o match.o dewey.o
OBJS += repository_findpkg.o repository_finddeps.o

View File

@ -39,22 +39,6 @@
* These functions manipulate plist files and objects shared by almost
* all library functions.
*/
bool
xbps_match_virtual_pkg_in_dict(prop_dictionary_t d,
const char *str,
bool bypattern)
{
prop_array_t provides;
bool found = false;
if ((provides = prop_dictionary_get(d, "provides"))) {
if (bypattern)
found = xbps_match_pkgpattern_in_array(provides, str);
else
found = xbps_match_pkgname_in_array(provides, str);
}
return found;
}
static prop_dictionary_t
find_pkg_in_array(prop_array_t array,
@ -384,71 +368,3 @@ xbps_find_virtualpkg_dict_installed(const char *str, bool bypattern)
{
return find_pkgd_installed(str, bypattern, true);
}
static bool
match_string_in_array(prop_array_t array, const char *str, int mode)
{
prop_object_iterator_t iter;
prop_object_t obj;
const char *pkgdep;
char *curpkgname;
bool found = false;
assert(array != NULL);
assert(str != NULL);
iter = prop_array_iterator(array);
if (iter == NULL)
return false;
while ((obj = prop_object_iterator_next(iter))) {
assert(prop_object_type(obj) == PROP_TYPE_STRING);
if (mode == 0) {
/* match by string */
if (prop_string_equals_cstring(obj, str)) {
found = true;
break;
}
} else if (mode == 1) {
/* match by pkgname */
pkgdep = prop_string_cstring_nocopy(obj);
curpkgname = xbps_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 pkgpattern */
pkgdep = prop_string_cstring_nocopy(obj);
if (xbps_pkgpattern_match(pkgdep, str)) {
found = true;
break;
}
}
}
prop_object_iterator_release(iter);
return found;
}
bool
xbps_match_string_in_array(prop_array_t array, const char *str)
{
return match_string_in_array(array, str, 0);
}
bool
xbps_match_pkgname_in_array(prop_array_t array, const char *pkgname)
{
return match_string_in_array(array, pkgname, 1);
}
bool
xbps_match_pkgpattern_in_array(prop_array_t array, const char *pattern)
{
return match_string_in_array(array, pattern, 2);
}

125
lib/plist_match.c Normal file
View File

@ -0,0 +1,125 @@
/*-
* Copyright (c) 2008-2011 Juan Romero Pardines.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "xbps_api_impl.h"
/**
* @file lib/plist_find.c
* @brief PropertyList generic routines
* @defgroup plist PropertyList generic functions
*
* These functions manipulate plist files and objects shared by almost
* all library functions.
*/
bool
xbps_match_virtual_pkg_in_dict(prop_dictionary_t d,
const char *str,
bool bypattern)
{
prop_array_t provides;
bool found = false;
if ((provides = prop_dictionary_get(d, "provides"))) {
if (bypattern)
found = xbps_match_pkgpattern_in_array(provides, str);
else
found = xbps_match_pkgname_in_array(provides, str);
}
return found;
}
static bool
match_string_in_array(prop_array_t array, const char *str, int mode)
{
prop_object_iterator_t iter;
prop_object_t obj;
const char *pkgdep;
char *curpkgname;
bool found = false;
assert(array != NULL);
assert(str != NULL);
iter = prop_array_iterator(array);
if (iter == NULL)
return false;
while ((obj = prop_object_iterator_next(iter))) {
assert(prop_object_type(obj) == PROP_TYPE_STRING);
if (mode == 0) {
/* match by string */
if (prop_string_equals_cstring(obj, str)) {
found = true;
break;
}
} else if (mode == 1) {
/* match by pkgname */
pkgdep = prop_string_cstring_nocopy(obj);
curpkgname = xbps_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 pkgpattern */
pkgdep = prop_string_cstring_nocopy(obj);
if (xbps_pkgpattern_match(pkgdep, str)) {
found = true;
break;
}
}
}
prop_object_iterator_release(iter);
return found;
}
bool
xbps_match_string_in_array(prop_array_t array, const char *str)
{
return match_string_in_array(array, str, 0);
}
bool
xbps_match_pkgname_in_array(prop_array_t array, const char *pkgname)
{
return match_string_in_array(array, pkgname, 1);
}
bool
xbps_match_pkgpattern_in_array(prop_array_t array, const char *pattern)
{
return match_string_in_array(array, pattern, 2);
}