Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=6f1fd190fc1c529c671a571563d334d9d29ee24c
commit 6f1fd190fc1c529c671a571563d334d9d29ee24c Author: Michel Hermier <[email protected]> Date: Sun Aug 17 15:05:55 2014 +0200 libpacman: Reimplement FComparator/FMatcher in C++ templates. diff --git a/lib/libpacman/CMakeLists.txt b/lib/libpacman/CMakeLists.txt index 529229d..aaede5e 100644 --- a/lib/libpacman/CMakeLists.txt +++ b/lib/libpacman/CMakeLists.txt @@ -8,11 +8,12 @@ set(FLIB_SOURCES kernel/fobject.cpp kernel/fobject.h kernel/fsignal.h + kernel/fstr.cpp + kernel/fstr.h fstdlib/canonicalize.c fstring/basename.c fstring/case.c fstring/dirname.c - fstring/matcher.c hash/md5.c hash/md5driver.c hash/sha1.c diff --git a/lib/libpacman/db.h b/lib/libpacman/db.h index 6b9bc8e..7f4b704 100644 --- a/lib/libpacman/db.h +++ b/lib/libpacman/db.h @@ -69,13 +69,13 @@ public: // Cache operations pmlist_t *filter(const libpacman::PackageMatcher &packagematcher); pmlist_t *filter(const FStrMatcher *strmatcher, int packagestrmatcher_flags); - FPtrList *filter(const FStringList *needles, int packagestrmatcher_flags, int strmatcher_flags = F_STRMATCHER_EQUAL); - FPtrList *filter(const char *pattern, int packagestrmatcher_flags, int strmatcher_flags = F_STRMATCHER_EQUAL); + FPtrList *filter(const FStringList *needles, int packagestrmatcher_flags, int strmatcher_flags = FStrMatcher::EQUAL); + FPtrList *filter(const char *pattern, int packagestrmatcher_flags, int strmatcher_flags = FStrMatcher::EQUAL); libpacman::Package *find(const libpacman::PackageMatcher &packagematcher); libpacman::Package *find(const FStrMatcher *strmatcher, int packagestrmatcher_flags); libpacman::Package *find(const char *target, int packagestrmatcher_flags = PM_PACKAGE_FLAG_NAME, - int strmatcher_flags = F_STRMATCHER_EQUAL); + int strmatcher_flags = FStrMatcher::EQUAL); FPtrList *whatPackagesProvide(const char *target); virtual pmlist_t *getowners(const char *filename); /* Make pure virtual */ diff --git a/lib/libpacman/fstring.h b/lib/libpacman/fstring.h index d1f2789..76a0774 100644 --- a/lib/libpacman/fstring.h +++ b/lib/libpacman/fstring.h @@ -57,74 +57,6 @@ char *f_strtrim(char *str); int f_str_tolower(char *dst, const char *src); int f_str_toupper(char *dst, const char *src); -typedef int (*FStrComparatorFunc)(const char *str, const void *stringcomparator_data); -typedef int (*FStrMatcherFunc)(const char *str, const void *stringmatcher_data); -typedef void (*FStrVisitorFunc)(char *str, void *stringvisitor_data); - -typedef struct FStrComparator FStrComparator; -typedef struct FStrMatcher FStrMatcher; -typedef struct FStrVisitor FStrVisitor; - -struct FStrComparator { - FStrComparatorFunc fn; - void *data; -}; - -struct FStrMatcher { - FStrMatcherFunc fn; - void *data; -}; - -struct FStrVisitor { - FStrVisitorFunc fn; - void *data; -}; - -static inline -int f_str_compare(const char *str, const FStrComparator *strcomparator) { - return f_compare(str, (const FComparator *)strcomparator); -} - -static inline -int f_str_compare_all(const char *str, const FStrComparator **strcomparators) { - return f_compare_all(str, (const FComparator **)strcomparators); -} - -static inline -int f_str_match(const char *str, const FStrMatcher *strmatcher) { - return f_match(str, (const FMatcher *)strmatcher); -} - -static inline -int f_str_match_all(const char *str, const FStrMatcher **strmatchers) { - return f_match_all(str, (const FMatcher **)strmatchers); -} - -static inline -int f_str_match_any(const char *str, const FStrMatcher **strmatchers) { - return f_match_any(str, (const FMatcher **)strmatchers); -} - -static inline -int f_str_match_comparator(const char *str, const FStrComparator *strcomparator) { - return f_match_comparator(str, (const FComparator *)strcomparator); -} - -static inline -void f_str_visit(char *str, FStrVisitor *strvisitor) { - f_visit(str, (FVisitor *)strvisitor); -} - -#define F_STRMATCHER_IGNORE_CASE (1<<0) -#define F_STRMATCHER_EQUAL (1<<1) -#define F_STRMATCHER_REGEXP (1<<2) -#define F_STRMATCHER_SUBSTRING (1<<3) -#define F_STRMATCHER_ALL (F_STRMATCHER_EQUAL | F_STRMATCHER_REGEXP | F_STRMATCHER_SUBSTRING) -#define F_STRMATCHER_ALL_IGNORE_CASE (F_STRMATCHER_IGNORE_CASE | F_STRMATCHER_ALL) - -int f_strmatcher_init(FStrMatcher *strmatcher, const char *pattern, int flags); -int f_strmatcher_fini(FStrMatcher *strmatcher); - #ifdef __cplusplus } #endif diff --git a/lib/libpacman/fstring/matcher.c b/lib/libpacman/fstring/matcher.c deleted file mode 100644 index 5c1a875..0000000 --- a/lib/libpacman/fstring/matcher.c +++ /dev/null @@ -1,109 +0,0 @@ -/* - * matcher.c - * - * Copyright (c) 2013 by Michel Hermier <[email protected]> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, - * USA. - */ - -#include "config.h" - -#include "fstring.h" - -#include "fstdlib.h" - -#include <regex.h> - -typedef struct FStrMatcherData FStrMatcherData; - -struct FStrMatcherData { - int flags; - char *str; - regex_t regex; -}; - -static -int f_strmatcher_match(const char *str, const void *_data) -{ - const FStrMatcherData *data = _data; - - if(data->flags & F_STRMATCHER_IGNORE_CASE) { - char *_str = alloca(strlen(str) + sizeof(char)); - f_str_toupper(_str, str); - str = _str; - } - if(((data->flags & F_STRMATCHER_EQUAL) && strcmp(str, data->str) == 0) || - ((data->flags & F_STRMATCHER_SUBSTRING) && strstr(str, data->str) != NULL) || - ((data->flags & F_STRMATCHER_REGEXP) && regexec(&data->regex, str, 0, 0, 0) == 0)) { - return 1; - } - return 0; -} - -int f_strmatcher_init(FStrMatcher *strmatcher, const char *pattern, int flags) -{ - FStrMatcherData *data = NULL; - - ASSERT(strmatcher != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - ASSERT(!f_strempty(pattern), RET_ERR(PM_ERR_WRONG_ARGS, -1)); - ASSERT((data = f_zalloc(sizeof(*data))) != NULL, return -1); - - if(flags & (F_STRMATCHER_EQUAL | F_STRMATCHER_SUBSTRING)) { - if((data->str = strdup(pattern)) != NULL) { - if(flags & F_STRMATCHER_IGNORE_CASE) { - f_strtoupper(data->str); - } - } else { - flags = 0; /* strdup fails, the matcher is invalid */ - } - } - if(flags & F_STRMATCHER_REGEXP) { - int regex_flags = REG_EXTENDED | REG_NOSUB; - - regex_flags |= flags & F_STRMATCHER_IGNORE_CASE ? REG_ICASE : 0; - if(regcomp(&data->regex, pattern, regex_flags) != 0) { - flags |= ~F_STRMATCHER_REGEXP; - } - } - strmatcher->fn = f_strmatcher_match; - strmatcher->data = data; - data->flags = flags; - if((flags & ~F_STRMATCHER_IGNORE_CASE) == 0) { - f_strmatcher_fini(strmatcher); - RET_ERR(PM_ERR_WRONG_ARGS, -1); - } - return 0; -} - -int f_strmatcher_fini(FStrMatcher *strmatcher) -{ - FStrMatcherData *data = NULL; - - ASSERT(strmatcher != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - ASSERT(strmatcher->fn == f_strmatcher_match, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - ASSERT((data = strmatcher->data) != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - - free(data->str); - if(data->flags & F_STRMATCHER_REGEXP) { - regfree(&data->regex); - } - free(data); - strmatcher->fn = NULL; - strmatcher->data = NULL; - return 0; -} - -/* vim: set ts=2 sw=2 noet: */ diff --git a/lib/libpacman/kernel/fstr.cpp b/lib/libpacman/kernel/fstr.cpp new file mode 100644 index 0000000..8979fdb --- /dev/null +++ b/lib/libpacman/kernel/fstr.cpp @@ -0,0 +1,94 @@ +/* + * fstr.cpp + * + * Copyright (c) 2014 by Michel Hermier <[email protected]> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ + +#include "fstr.h" + +#include "fstring.h" + +FStrMatcher::FStrMatcher() + : m_flags(0), m_str(NULL) +{ } + +FStrMatcher::FStrMatcher(const char *pattern, int flags) +{ + if(flags & (EQUAL | SUBSTRING)) { + if((m_str = strdup(pattern)) != NULL) { + if(flags & IGNORE_CASE) { + f_strtoupper(m_str); + } + } else { + flags = 0; /* strdup fails, the matcher is invalid */ + } + } + if(flags & REGEXP) { + int regex_flags = REG_EXTENDED | REG_NOSUB; + + regex_flags |= flags & IGNORE_CASE ? REG_ICASE : 0; + if(regcomp(&m_regex, pattern, regex_flags) != 0) { + flags |= ~REGEXP; + } + } + m_flags = flags; +#if 0 + if((flags & ~IGNORE_CASE) == 0) { + f_strmatcher_fini(strmatcher); + RET_ERR(PM_ERR_WRONG_ARGS, -1); + } +#endif +} + +FStrMatcher::~FStrMatcher() +{ + free(m_str); + if(m_flags & REGEXP) { + regfree(&m_regex); + } +} + +bool FStrMatcher::match(const char *str) const +{ + if(m_flags & IGNORE_CASE) { + char *_str = alloca(strlen(str) + sizeof(char)); + f_str_toupper(_str, str); + str = _str; + } + if(((m_flags & EQUAL) && strcmp(str, m_str) == 0) || + ((m_flags & SUBSTRING) && strstr(str, m_str) != NULL) || + ((m_flags & REGEXP) && regexec(&m_regex, str, 0, 0, 0) == 0)) { + return 1; + } + return 0; +} + +int f_stringlist_any_match(const FStringList *list, const FStrMatcher *matcher) +{ + const FStringListItem *it; + + for(it = f_list_first_const(list); it != f_list_end_const(list); it = it->next) { + if(matcher->match(f_stringlistitem_to_str(it)) != 0) { + return 1; + } + } + return 0; +} + +/* vim: set ts=2 sw=2 noet: */ + diff --git a/lib/libpacman/kernel/fstr.h b/lib/libpacman/kernel/fstr.h new file mode 100644 index 0000000..d6ed69c --- /dev/null +++ b/lib/libpacman/kernel/fstr.h @@ -0,0 +1,62 @@ +/* + * fstr.h + * + * Copyright (c) 2014 by Michel Hermier <[email protected]> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ +#ifndef F_STR_H +#define F_STR_H + +#include "fstring.h" + +#include "util/fmatcher.h" +#include "util/stringlist.h" + +#include <regex.h> + +class FStrMatcher + : public FMatcher<const char *> +{ +public: + enum { + EQUAL = (1<<0), + IGNORE_CASE = (1<<1), + REGEXP = (1<<2), + SUBSTRING = (1<<3), + + ALL = (EQUAL | REGEXP | SUBSTRING), + ALL_IGNORE_CASE = (IGNORE_CASE | ALL), + }; + + FStrMatcher(); + FStrMatcher(const char *pattern, int flags = EQUAL/*, owner*/); + + ~FStrMatcher(); + + virtual bool match(const char *str) const override; + +protected: + int m_flags; + char *m_str; + regex_t m_regex; +}; + +int f_stringlist_any_match(const FStringList *list, const FStrMatcher *matcher); + +#endif /* F_STR_H */ + +/* vim: set ts=2 sw=2 noet: */ diff --git a/lib/libpacman/package.cpp b/lib/libpacman/package.cpp index f7c454a..cd75085 100644 --- a/lib/libpacman/package.cpp +++ b/lib/libpacman/package.cpp @@ -321,15 +321,15 @@ struct FPackageStrMatcher static int _pacman_strmatcher_match(const FStrMatcher *strmatcher, Package *pkg, int flags) { /* FIXME: Make const when const accessors are available */ - if(((flags & PM_PACKAGE_FLAG_NAME) && f_str_match(pkg->name(), strmatcher)) || - ((flags & PM_PACKAGE_FLAG_VERSION) && f_str_match(pkg->version(), strmatcher)) || - ((flags & PM_PACKAGE_FLAG_DESCRIPTION) && f_str_match(pkg->description(), strmatcher)) || - ((flags & PM_PACKAGE_FLAG_BUILDDATE) && f_str_match(pkg->builddate, strmatcher)) || - ((flags & PM_PACKAGE_FLAG_BUILDTYPE) && f_str_match(pkg->buildtype, strmatcher)) || - ((flags & PM_PACKAGE_FLAG_INSTALLDATE) && f_str_match(pkg->installdate, strmatcher)) || - ((flags & PM_PACKAGE_FLAG_PACKAGER) && f_str_match(pkg->packager, strmatcher)) || + if(((flags & PM_PACKAGE_FLAG_NAME) && strmatcher->match(pkg->name())) || + ((flags & PM_PACKAGE_FLAG_VERSION) && strmatcher->match(pkg->version())) || + ((flags & PM_PACKAGE_FLAG_DESCRIPTION) && strmatcher->match(pkg->description())) || + ((flags & PM_PACKAGE_FLAG_BUILDDATE) && strmatcher->match(pkg->builddate)) || + ((flags & PM_PACKAGE_FLAG_BUILDTYPE) && strmatcher->match(pkg->buildtype)) || + ((flags & PM_PACKAGE_FLAG_INSTALLDATE) && strmatcher->match(pkg->installdate)) || + ((flags & PM_PACKAGE_FLAG_PACKAGER) && strmatcher->match(pkg->packager)) || // ((flags & PM_PACKAGE_FLAG_HASH) && ) || - ((flags & PM_PACKAGE_FLAG_ARCH) && f_str_match(pkg->arch, strmatcher)) || + ((flags & PM_PACKAGE_FLAG_ARCH) && strmatcher->match(pkg->arch)) || ((flags & PM_PACKAGE_FLAG_LOCALISED_DESCRIPTION) && f_stringlist_any_match(pkg->desc_localized, strmatcher)) || ((flags & PM_PACKAGE_FLAG_LICENSE) && f_stringlist_any_match(pkg->license, strmatcher)) || ((flags & PM_PACKAGE_FLAG_REPLACES) && f_stringlist_any_match(pkg->replaces(), strmatcher)) || @@ -353,32 +353,21 @@ int PackageMatcher_match_cb(const void *ptr, const void *matcher_data) return ((const PackageMatcher *)matcher_data)->match((const Package *)ptr); } -PackageMatcher::PackageMatcher(const char *pattern, int flags = PM_PACKAGE_FLAG_NAME, int strmatcher_flags = F_STRMATCHER_EQUAL) - : m_strmatcher(&m_strmatcher_internal), m_flags(flags) -{ - f_strmatcher_init(m_strmatcher, pattern, strmatcher_flags); -} +PackageMatcher::PackageMatcher(const char *pattern, int flags, int strmatcher_flags) + : m_strmatcher(&m_strmatcher_internal), m_flags(flags), m_strmatcher_internal(pattern, strmatcher_flags) +{ } PackageMatcher::PackageMatcher(const FStrMatcher *strmatcher, int flags) : m_strmatcher(strmatcher), m_flags(flags) -{ - fn = PackageMatcher_match_cb; - data = this; -} +{ } PackageMatcher::~PackageMatcher() -{ - if(m_strmatcher == &m_strmatcher_internal) { - f_strmatcher_fini(m_strmatcher); - } -} +{ } -#if 0 bool PackageMatcher::match(const libpacman::Package *package) const { return match(package, ~0); } -#endif bool PackageMatcher::match(const Package *package, int mask = ~0) const { diff --git a/lib/libpacman/package.h b/lib/libpacman/package.h index a50921d..5e7e3c7 100644 --- a/lib/libpacman/package.h +++ b/lib/libpacman/package.h @@ -34,6 +34,7 @@ #include "trans.h" #include "kernel/fobject.h" +#include "kernel/fstr.h" namespace libpacman { @@ -167,17 +168,15 @@ public: }; class PackageMatcher - : FMatcher + : FMatcher<const libpacman::Package *> { public: - PackageMatcher(const char *str, int flags = PM_PACKAGE_FLAG_NAME, int strmatcher_flags = F_STRMATCHER_EQUAL); + PackageMatcher(const char *str, int flags = PM_PACKAGE_FLAG_NAME, int strmatcher_flags = FStrMatcher::EQUAL); PackageMatcher(const FStrMatcher *strmatcher, int flags); ~PackageMatcher(); -#if 0 virtual bool match(const libpacman::Package *package) const override; -#endif - bool match(const libpacman::Package *package, int mask = ~0) const; + bool match(const libpacman::Package *package, int mask) const; private: const FStrMatcher *m_strmatcher; diff --git a/lib/libpacman/pacman.cpp b/lib/libpacman/pacman.cpp index 737a67e..55b0503 100644 --- a/lib/libpacman/pacman.cpp +++ b/lib/libpacman/pacman.cpp @@ -1031,7 +1031,7 @@ pmlist_t *pacman_db_search(pmdb_t *_db) ret = db->filter(handle->needles, PM_PACKAGE_FLAG_NAME | PM_PACKAGE_FLAG_DESCRIPTION | PM_PACKAGE_FLAG_PROVIDES, - F_STRMATCHER_ALL_IGNORE_CASE); + FStrMatcher::ALL_IGNORE_CASE); FREELIST(handle->needles); return(ret); } diff --git a/lib/libpacman/util/fcallback.h b/lib/libpacman/util/fcallback.h index 5a07b64..ebe242e 100644 --- a/lib/libpacman/util/fcallback.h +++ b/lib/libpacman/util/fcallback.h @@ -24,191 +24,6 @@ #include "fstdlib.h" #include "util.h" -typedef int (*FComparatorFunc)(const void *ptr, const void *comparator_data); - -typedef struct FComparator FComparator; - -struct FComparator { -#ifdef __cplusplus - FComparator() - : fn(NULL), data(NULL) - { } - - FComparator(FComparatorFunc _fn, const void *_data) - : fn(_fn), data(_data) - { } -#endif - - FComparatorFunc fn; - const void *data; -}; - -static inline -int f_comparator_init(FComparator *self, FComparatorFunc fn, const void *data) -{ - ASSERT(self != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - - self->fn = fn; - self->data = data; - return 0; -} - -static inline -int f_comparator_fini(FComparator *self) -{ - ASSERT(self != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - - self->fn = NULL; - self->data = NULL; - return 0; -} - -static inline -FComparator *f_comparator_new(FComparatorFunc fn, const void *data) -{ - FComparator *self; - - ASSERT((self = (FComparator *)f_zalloc(sizeof(*self))) != NULL, return NULL); - f_comparator_init(self, fn, data); - return self; -} - -static inline -int f_comparator_delete(FComparator *self) -{ - ASSERT(self != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - - f_comparator_fini(self); - free(self); - return 0; -} - -static inline -int f_compare(const void *ptr, const FComparator *comparator) { - ASSERT(comparator != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - ASSERT(comparator->fn != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - - return comparator->fn(ptr, comparator->data); -} - -static inline -int f_compare_all(const void *ptr, const FComparator **comparators) { - ASSERT(comparators != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - - while(*comparators != NULL) { - int match; - - if((match = f_compare(ptr, *comparators)) != 0) { - return match; - } - comparators++; - } - return 0; -} - -typedef int (*FMatcherFunc)(const void *ptr, const void *matcher_data); - -typedef struct FMatcher FMatcher; - -struct FMatcher { -#ifdef __cplusplus - FMatcher() - : fn(NULL), data(NULL) - { } - - FMatcher(FMatcherFunc _fn, const void *_data) - : fn(_fn), data(_data) - { } -#endif - - FMatcherFunc fn; - const void *data; -}; - -static inline -int f_matcher_init(FMatcher *self, FMatcherFunc fn, const void *data) -{ - ASSERT(self != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - - self->fn = fn; - self->data = data; - return 0; -} - -static inline -int f_matcher_fini(FMatcher *self) -{ - ASSERT(self != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - - self->fn = NULL; - self->data = NULL; - return 0; -} - -static inline -FMatcher *f_matcher_new(FMatcherFunc fn, const void *data) -{ - FMatcher *self; - - ASSERT((self = (FMatcher *)f_zalloc(sizeof(*self))) != NULL, return NULL); - f_matcher_init(self, fn, data); - return self; -} - -static inline -int f_matcher_delete(FMatcher *self) -{ - ASSERT(self != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - - f_matcher_fini(self); - free(self); - return 0; -} - -static inline -int f_match(const void *ptr, const FMatcher *matcher) { - ASSERT(matcher != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - ASSERT(matcher->fn != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - - return matcher->fn(ptr, matcher->data); -} - -static inline -int f_match_all(const void *ptr, const FMatcher **matchers) { - ASSERT(matchers != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - - while(*matchers != NULL) { - int match; - - if((match = f_match(ptr, *matchers)) != 0) { - return match; - } - matchers++; - } - return 0; -} - -static inline -int f_match_any(const void *ptr, const FMatcher **matchers) { - ASSERT(matchers != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - - while(*matchers != NULL) { - if(f_match(ptr, *matchers) == 0) { - return 1; - } - matchers++; - } - return 0; -} - -static inline -int f_match_comparator(const void *ptr, const FComparator *comparator) { - ASSERT(comparator != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - ASSERT(comparator->fn != NULL, RET_ERR(PM_ERR_WRONG_ARGS, -1)); - - return comparator->fn(ptr, comparator->data) == 0; -} - typedef void (*FVisitorFunc)(void *ptr, void *visitor_data); typedef struct FVisitor FVisitor; diff --git a/lib/libpacman/util/fcomparator.h b/lib/libpacman/util/fcomparator.h new file mode 100644 index 0000000..8e82dfa --- /dev/null +++ b/lib/libpacman/util/fcomparator.h @@ -0,0 +1,58 @@ +/* + * fcomparator.h + * + * Copyright (c) 2014 by Michel Hermier <[email protected]> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ +#ifndef F_COMPARATOR_H +#define F_COMPARATOR_H + +#include "fmatcher.h" + +template <typename T> +class FComparator + : public FMatcher<T> +{ +public: + FComparator() + { } + + virtual ~FComparator() override + { } + + virtual int compare(const T &o) const = 0; + + bool match(const T &o) const override + { + return compare(o) == 0; + } + +protected: + FComparator(const FComparator<T> &o) + { } + + FComparator<T> &operator = (const FComparator<T> &o) + { + return *this; + } +}; + +// FIXME: Add FComparatorList with compare_all + +#endif /* F_COMPARATOR_H */ + +/* vim: set ts=2 sw=2 noet: */ diff --git a/lib/libpacman/util/flist.c b/lib/libpacman/util/flist.c index e22ff3b..c3417e6 100644 --- a/lib/libpacman/util/flist.c +++ b/lib/libpacman/util/flist.c @@ -109,34 +109,6 @@ int f_list_fini(FList *self, FVisitor *visitor) return f_list_clear(self, visitor); } -int f_list_all_match(const FList *list, const FMatcher *matcher) -{ - const FListItem *it; - - if(f_list_empty(list)) { - return 0; - } - - for(it = f_list_first_const(list); it != f_list_end_const(list); it = it->next) { - if(f_match(it, matcher) == 0) { - return 0; - } - } - return 1; -} - -int f_list_any_match(const FList *list, const FMatcher *matcher) -{ - const FListItem *it; - - for(it = f_list_first_const(list); it != f_list_end_const(list); it = it->next) { - if(f_match(it, matcher) != 0) { - return 1; - } - } - return 0; -} - int f_list_append(FList *self, FListItem *item) { return f_listitem_insert_after(item, f_list_last(self)); diff --git a/lib/libpacman/util/flist.h b/lib/libpacman/util/flist.h index 7d75a5b..59b9f3e 100644 --- a/lib/libpacman/util/flist.h +++ b/lib/libpacman/util/flist.h @@ -54,8 +54,6 @@ int f_list_init(FList *self); int f_list_fini(FList *self, FVisitor *visitor); #define f_list_add f_list_append -int f_list_all_match(const FList *list, const FMatcher *matcher); -int f_list_any_match(const FList *list, const FMatcher *matcher); int f_list_append(FList *self, FListItem *item); int f_list_append_unique(FList *self, FListItem *item, FListItemComparatorFunc comparator); int f_list_clear(FList *self, FVisitor *visitor); diff --git a/lib/libpacman/util/fmatcher.h b/lib/libpacman/util/fmatcher.h new file mode 100644 index 0000000..c87f501 --- /dev/null +++ b/lib/libpacman/util/fmatcher.h @@ -0,0 +1,53 @@ +/* + * fmatcher.h + * + * Copyright (c) 2014 by Michel Hermier <[email protected]> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ +#ifndef F_MATCHER_H +#define F_MATCHER_H + +#include "fstdlib.h" +#include "util.h" + +template <typename T> +class FMatcher +{ +public: + FMatcher() + { } + + virtual ~FMatcher() + { } + + virtual bool match(T data) const = 0; + +protected: + FMatcher(const FMatcher<T> &o) + { } + + FMatcher<T> &operator = (const FMatcher<T> &o) + { + return *this; + } +}; + +// FIXME: Add FMatcherList with match_all match_any + +#endif /* F_MATCHER_H */ + +/* vim: set ts=2 sw=2 noet: */ diff --git a/lib/libpacman/util/fptrlist.c b/lib/libpacman/util/fptrlist.c index 1ebc694..d633e62 100644 --- a/lib/libpacman/util/fptrlist.c +++ b/lib/libpacman/util/fptrlist.c @@ -50,10 +50,6 @@ void *f_ptrlistitem_data(const FPtrListItem *self) return self->data; } -int f_ptrlistitem_match(const FPtrListItem *item, const FMatcher *matcher) { - return f_match(item->data, matcher); -} - int f_ptrlistitem_ptrcmp(const FPtrListItem *item, const void *ptr) { return f_ptrcmp(item->data, ptr); } @@ -99,26 +95,6 @@ const FList *f_ptrlist_as_FList_const(const FPtrList *self) #endif } -int f_ptrlist_all_match(const FPtrList *list, const FMatcher *matcher) -{ - FMatcher itemmatcher = { - .fn = (FMatcherFunc)f_ptrlistitem_match, - .data = matcher, - }; - - return f_list_all_match(f_ptrlist_as_FList_const(list), &itemmatcher); -} - -int f_ptrlist_any_match(const FPtrList *list, const FMatcher *matcher) -{ - FMatcher itemmatcher = { - .fn = (FMatcherFunc)f_ptrlistitem_match, - .data = matcher, - }; - - return f_list_any_match(f_ptrlist_as_FList_const(list), &itemmatcher); -} - #ifndef F_NOCOMPAT FPtrList *f_ptrlist_append(FPtrList *list, void *data) { @@ -174,19 +150,4 @@ int f_ptrlist_contains_ptr(const FPtrList *list, const void *ptr) return f_ptrlist_contains(list, f_ptrlistitem_ptrcmp, ptr); } -FPtrList *f_ptrlist_filter(const FPtrList *list, const FMatcher *matcher) -{ - FPtrList *retlist = NULL; - const FPtrListItem *end = f_ptrlist_end_const(list), *it = f_ptrlist_first_const(list); - - for(; it != end; it = f_ptrlistitem_next(it)) { - void *data = f_ptrlistitem_data(it); - - if(f_match(data, matcher)) { - retlist = f_ptrlist_append(retlist, data); - } - } - return retlist; -} - /* vim: set ts=2 sw=2 noet: */ diff --git a/lib/libpacman/util/fptrlist.h b/lib/libpacman/util/fptrlist.h index 43afa33..03981f1 100644 --- a/lib/libpacman/util/fptrlist.h +++ b/lib/libpacman/util/fptrlist.h @@ -45,7 +45,6 @@ void *f_ptrlistitem_data(const FPtrListItem *self); #define f_ptrlistitem_next(self) ((FPtrListItem *)f_listitem_next((FListItem *)(self))) #define f_ptrlistitem_previous(self) ((FPtrListItem *)f_listitem_previous((FListItem *)(self))) -int f_ptrlistitem_match(const FPtrListItem *item, const FMatcher *matcher); int f_ptrlistitem_ptrcmp(const FPtrListItem *item, const void *ptr); FPtrList *f_ptrlist_new(void); @@ -58,8 +57,6 @@ FList *f_ptrlist_as_FList(FPtrList *self); const FList *f_ptrlist_as_FList_const(const FPtrList *self); #define f_ptrlist_add f_ptrlist_append -int f_ptrlist_all_match(const FPtrList *list, const FMatcher *matcher); -int f_ptrlist_any_match(const FPtrList *list, const FMatcher *matcher); #ifndef F_NOCOMPAT FPtrList *f_ptrlist_append(FPtrList *list, void *data); #else @@ -72,7 +69,6 @@ int f_ptrlist_contains_ptr(const FPtrList *list, const void *ptr); #define f_ptrlist_empty f_list_empty #define f_ptrlist_end(list) ((FPtrListItem *)f_ptrlist_end_const(list)) #define f_ptrlist_end_const(list) ((const FPtrListItem *)f_list_end_const(f_ptrlist_as_FList_const(list))) -FPtrList *f_ptrlist_filter(const FPtrList *list, const FMatcher *matcher); #define f_ptrlist_first(self) ((FPtrListItem *)f_ptrlist_first_const(self)) #define f_ptrlist_first_const(self) ((const FPtrListItem *)f_list_first_const(f_ptrlist_as_FList_const(self))) #define f_ptrlist_last(self) ((FPtrListItem *)f_ptrlist_last_const(self)) diff --git a/lib/libpacman/util/stringlist.c b/lib/libpacman/util/stringlist.c index 1fc1a30..cdaa409 100644 --- a/lib/libpacman/util/stringlist.c +++ b/lib/libpacman/util/stringlist.c @@ -116,16 +116,6 @@ int f_stringlist_delete(FStringList *self) return f_ptrlist_delete(self, &f_stringlistitem_destroyvisitor); } -int f_stringlist_all_match(const FStringList *list, const FStrMatcher *matcher) -{ - return f_ptrlist_all_match(list, (const FMatcher *)matcher); -} - -int f_stringlist_any_match(const FStringList *list, const FStrMatcher *matcher) -{ - return f_ptrlist_any_match(list, (const FMatcher *)matcher); -} - FStringList *f_stringlist_append(FStringList *list, const char *s) { return f_ptrlist_append(list, f_strdup(s)); diff --git a/lib/libpacman/util/stringlist.h b/lib/libpacman/util/stringlist.h index 8fcfe37..aeb2e3d 100644 --- a/lib/libpacman/util/stringlist.h +++ b/lib/libpacman/util/stringlist.h @@ -59,8 +59,6 @@ const char *f_stringlistitem_to_str(const FStringListItem *self); FStringList *f_stringlist_new(void); int f_stringlist_delete(FStringList *self); -int f_stringlist_all_match(const FStringList *list, const FStrMatcher *matcher); -int f_stringlist_any_match(const FStringList *list, const FStrMatcher *matcher); FStringList *f_stringlist_append(FStringList *list, const char *s); FStringList *f_stringlist_append_stringlist(FStringList *dest, const FStringList *src); int f_stringlist_clear(FStringList *self); _______________________________________________ Frugalware-git mailing list [email protected] http://frugalware.org/mailman/listinfo/frugalware-git
