From e8b240eb3aab7e2584e245f03700ede30a3fc23a Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Mon, 17 Feb 2025 21:33:30 +0100 Subject: refactor: Deduplicate and Move SectionInSubTree to Configuration Deduplicate the copies in a central one, mark it unavailable to external users. --- apt-pkg/contrib/configuration.cc | 33 +++++++++++++++++++++++++++++++++ apt-pkg/contrib/configuration.h | 3 +++ 2 files changed, 36 insertions(+) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/configuration.cc b/apt-pkg/contrib/configuration.cc index 28cf3e480..cf72aa3d7 100644 --- a/apt-pkg/contrib/configuration.cc +++ b/apt-pkg/contrib/configuration.cc @@ -1226,3 +1226,36 @@ bool Configuration::MatchAgainstConfig::Match(char const * str) const return false; } /*}}}*/ +// helper for Install-Recommends-Sections and Never-MarkAuto-Sections /*{{{*/ +static bool ConfigValueInSubTree(Configuration *config, const char *SubTree, std::string_view const needle) +{ + if (needle.empty()) + return false; + Configuration::Item const *Opts = config->Tree(SubTree); + if (Opts != nullptr && Opts->Child != nullptr) + { + Opts = Opts->Child; + for (; Opts != nullptr; Opts = Opts->Next) + { + if (Opts->Value.empty()) + continue; + if (needle == Opts->Value) + return true; + } + } + return false; +} +bool Configuration::SectionInSubTree(char const *const SubTree, std::string_view Needle) +{ + if (ConfigValueInSubTree(this, SubTree, Needle)) + return true; + auto const sub = Needle.rfind('/'); + if (sub == std::string_view::npos) + { + std::string special{"/"}; + special.append(Needle); + return ConfigValueInSubTree(this, SubTree, special); + } + return ConfigValueInSubTree(this, SubTree, Needle.substr(sub + 1)); +} + /*}}}*/ diff --git a/apt-pkg/contrib/configuration.h b/apt-pkg/contrib/configuration.h index 4a21d977b..30a1cbf43 100644 --- a/apt-pkg/contrib/configuration.h +++ b/apt-pkg/contrib/configuration.h @@ -117,6 +117,9 @@ class APT_PUBLIC Configuration void Dump(std::ostream& str, char const * const root, char const * const format, bool const emptyValue); +#ifdef APT_COMPILING_APT + bool SectionInSubTree(char const *const SubTree, std::string_view Needle); +#endif explicit Configuration(const Item *Root); Configuration(); ~Configuration(); -- cgit v1.2.3-70-g09d2