Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=a4cc2a52ae4a629b111f18a13d68e0bb831f06fe
commit a4cc2a52ae4a629b111f18a13d68e0bb831f06fe Author: Michel Hermier <[email protected]> Date: Wed Sep 24 10:38:45 2014 +0200 libpacman: Add FStringList add_nocopy, remove and remove_if. Add flib::find_if. diff --git a/lib/libpacman/deps.cpp b/lib/libpacman/deps.cpp index 1087ea1..466bc91 100644 --- a/lib/libpacman/deps.cpp +++ b/lib/libpacman/deps.cpp @@ -583,11 +583,6 @@ int _pacman_depcmp(Package *pkg, pmdepend_t *dep) /* Helper function for comparing strings */ -static int str_cmp(const void *s1, const void *s2) -{ - return(strcmp(s1, s2)); -} - int inList(FStringList *lst, const char *lItem) { auto ll = lst->begin(), end = lst->end(); while(ll != end) { @@ -603,7 +598,6 @@ extern "C" { int pacman_output_generate(FStringList *targets, FPtrList *dblist) { FStringList found; Package *pkg = NULL; - char *match = NULL; int foundMatch = 0; unsigned int inforeq = INFRQ_DEPENDS; for(auto j = dblist->begin(), end = dblist->end(); j != end; ++j) { @@ -613,7 +607,7 @@ int pacman_output_generate(FStringList *targets, FPtrList *dblist) { pkg = db->readpkg(inforeq); while(pkg != NULL) { const char *pname = pkg->name(); - if(targets != NULL && targets->remove((void*) pname, str_cmp, (const char **)&match)) { + if(targets != NULL && targets->remove(pname)) { foundMatch = 1; auto &depends = pkg->depends(); for(auto k = depends.begin(), k_end = depends.end(); k != k_end; ++k) { @@ -631,7 +625,6 @@ int pacman_output_generate(FStringList *targets, FPtrList *dblist) { printf("%s ", pname); found.add(pname); } - FREE(match); } pkg = db->readpkg(inforeq); } diff --git a/lib/libpacman/trans.cpp b/lib/libpacman/trans.cpp index 3bd2400..956b0b2 100644 --- a/lib/libpacman/trans.cpp +++ b/lib/libpacman/trans.cpp @@ -1704,9 +1704,7 @@ int __pmtrans_t::commit(FPtrList **data) } } /* splice out this entry from requiredby */ - if(depinfo->requiredby().remove((void *)pkg_local->name(), str_cmp, (const char **)&data)) { - FREE(data); - } + depinfo->requiredby().remove(pkg_local->name()); _pacman_log(PM_LOG_DEBUG, _("updating 'requiredby' field for package '%s'"), depinfo->name()); if(db_local->write(depinfo, INFRQ_DEPENDS)) { _pacman_log(PM_LOG_ERROR, _("could not update 'requiredby' database entry %s-%s"), diff --git a/lib/libpacman/util/falgorithm.h b/lib/libpacman/util/falgorithm.h index 865e235..ac339a1 100644 --- a/lib/libpacman/util/falgorithm.h +++ b/lib/libpacman/util/falgorithm.h @@ -63,7 +63,21 @@ namespace flib template <class InputIterator, class T> InputIterator find(InputIterator first, InputIterator last, const T &val) { - for(; first != last && !(*first == val); ++first) { + return find(first, last, + [&] (const typename InputIterator::reference &o) -> bool + { return o == val; }); + } + + template <class InputIterable, class UnaryPredicate> + auto find_if(InputIterable iterable, UnaryPredicate pred) + { + return find(iterable.begin(), iterable.end(), pred); + } + + template <class InputIterator, class UnaryPredicate> + InputIterator find_if(InputIterator first, InputIterator last, UnaryPredicate pred) + { + for(; first != last && !pred(*first); ++first) { /* Nothing to do but iterate */ } return first; diff --git a/lib/libpacman/util/fstringlist.cpp b/lib/libpacman/util/fstringlist.cpp index 029fda5..89cf210 100644 --- a/lib/libpacman/util/fstringlist.cpp +++ b/lib/libpacman/util/fstringlist.cpp @@ -160,12 +160,17 @@ FStringList &FStringList::operator = (FStringList &&o) return *this; } -FStringList &FStringList::add(const char *s) +FStringList &FStringList::add_nocopy(char *s) { - FList::add(f_strdup(s)); + FList::add(s); return *this; } +FStringList &FStringList::add(const char *s) +{ + return add_nocopy(f_strdup(s)); +} + FStringList &FStringList::add(const FStringList &o) { for(auto lp = o.begin(), end = o.end(); lp != end; ++lp) { @@ -189,8 +194,14 @@ FStringList &FStringList::vaddf(const char *fmt, va_list ap) char *dest; vasprintf(&dest, fmt, ap); - FList::add(dest); - return *this; + return add_nocopy(dest); +} + +FStringList::size_type FStringList::remove(const char *s) +{ + return remove_if( + [&] (const char *v) -> bool + { return strcmp(v, s) == 0; }); } /* vim: set ts=2 sw=2 noet: */ diff --git a/lib/libpacman/util/fstringlist.h b/lib/libpacman/util/fstringlist.h index fbf1050..0bbd2ab 100644 --- a/lib/libpacman/util/fstringlist.h +++ b/lib/libpacman/util/fstringlist.h @@ -62,10 +62,31 @@ public: FStringList &operator = (const FStringList &o); FStringList &operator = (FStringList &&o); + FStringList &add_nocopy(char *s); FStringList &add(const char *s); FStringList &add(const FStringList &o); FStringList &addf(const char *fmt, ...); FStringList &vaddf(const char *fmt, va_list ap); + + size_type remove(const char *s); + + template <class UnaryPredicate> + size_type remove_if(UnaryPredicate pred) + { + size_type remove_count = 0; + auto it = begin(), end = this->end(); + while(it != end) { + char *s = (char *)*it; + if(pred(s)) { + free(s); + it = erase(it); + ++remove_count; + } else { + ++it; + } + } + return remove_count; + } }; #endif _______________________________________________ Frugalware-git mailing list [email protected] http://frugalware.org/mailman/listinfo/frugalware-git
