Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=02625a0b9e54941d9ec88e3af62f5f7b5a270a57
commit 02625a0b9e54941d9ec88e3af62f5f7b5a270a57 Author: Michel Hermier <[email protected]> Date: Wed Dec 11 13:46:41 2013 +0100 libpacman: Add compare and match callbacks (also provide str callbacks). diff --git a/lib/libpacman/fstring.h b/lib/libpacman/fstring.h index c1f6778..2cebd32 100644 --- a/lib/libpacman/fstring.h +++ b/lib/libpacman/fstring.h @@ -18,11 +18,13 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. */ -#ifndef _F_STRING_H -#define _F_STRING_H +#ifndef F_STRING_H +#define F_STRING_H #include <string.h> +#include "util/fcallback.h" + static inline int f_strempty(const char *s) { @@ -38,6 +40,64 @@ size_t f_strlen(const char *s) char *f_strtoupper(char *str); char *f_strtrim(char *str); -#endif /* _F_STRING_H */ +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 *str_comparator) { + return f_compare(str, (const FComparator *)str_comparator); +} + +static inline +int f_str_compare_all(const char *str, const FStrComparator **str_comparators) { + return f_compare_all(str, (const FComparator **)str_comparators); +} + +static inline +int f_str_match(const char *str, const FStrMatcher *str_matcher) { + return f_match(str, (const FMatcher *)str_matcher); +} + +static inline +int f_str_match_all(const char *str, const FStrMatcher **str_matchers) { + return f_match_all(str, (const FMatcher **)str_matchers); +} + +static inline +int f_str_match_any(const char *str, const FStrMatcher **str_matchers) { + return f_match_any(str, (const FMatcher **)str_matchers); +} + +static inline +int f_str_match_comparator(const char *str, const FStrComparator *str_comparator) { + return f_match_comparator(str, (const FComparator *)str_comparator); +} + +static inline +void f_str_visit(char *str, FStrVisitor *str_visitor) { + f_visit(str, (FVisitor *)str_visitor); +} + +#endif /* F_STRING_H */ /* vim: set ts=2 sw=2 noet: */ diff --git a/lib/libpacman/util/fcallback.h b/lib/libpacman/util/fcallback.h index bd850a4..450e9d8 100644 --- a/lib/libpacman/util/fcallback.h +++ b/lib/libpacman/util/fcallback.h @@ -23,24 +23,31 @@ #include "util.h" -typedef int (*FComparatorFunc)(const void *ptr, const void *visitor_data); +typedef int (*FComparatorFunc)(const void *ptr, const void *comparator_data); +typedef int (*FMatcherFunc)(const void *ptr, const void *matcher_data); typedef void (*FVisitorFunc)(void *ptr, void *visitor_data); -typedef struct __FComparator FComparator; -typedef struct __FVisitor FVisitor; +typedef struct FComparator FComparator; +typedef struct FMatcher FMatcher; +typedef struct FVisitor FVisitor; -struct __FComparator { +struct FComparator { FComparatorFunc fn; void *data; }; -struct __FVisitor { +struct FMatcher { + FMatcherFunc fn; + void *data; +}; + +struct FVisitor { FVisitorFunc fn; void *data; }; static inline -int f_compare(void *ptr, const FComparator *comparator) { +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)); @@ -48,6 +55,65 @@ int f_compare(void *ptr, const FComparator *comparator) { } 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; +} + +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; +} + +static inline void f_visit(void *ptr, const FVisitor *visitor) { ASSERT(visitor != NULL, return); ASSERT(visitor->fn != NULL, return); _______________________________________________ Frugalware-git mailing list [email protected] http://frugalware.org/mailman/listinfo/frugalware-git
