From b3e422a1fa36e272dd2376fbd96c0c4d37a84d41 Mon Sep 17 00:00:00 2001 From: Bernd Wachter Date: Sat, 6 Jul 2013 14:44:23 +0300 Subject: [PATCH] Add proper recursive search and default sections for single variable lookup Recursive search used to return the first match found, now return the last match found. Additionally, fall back to an autodetected default section if required. --- libssu/ssudeviceinfo.cpp | 5 ++++ libssu/ssuvariables.cpp | 62 +++++++++++++++++++++++++++++++--------- libssu/ssuvariables.h | 11 +++++-- 3 files changed, 61 insertions(+), 17 deletions(-) diff --git a/libssu/ssudeviceinfo.cpp b/libssu/ssudeviceinfo.cpp index 3af33b3..bbd7354 100644 --- a/libssu/ssudeviceinfo.cpp +++ b/libssu/ssudeviceinfo.cpp @@ -371,6 +371,11 @@ QStringList SsuDeviceInfo::repos(bool rnd, int filter){ } QVariant SsuDeviceInfo::variable(QString section, const QString &key){ + /// @todo compat-setting as ssudeviceinfo guaranteed to prepend sections with var-; + /// SsuVariables does not have this guarantee. Remove from here as well. + if (!section.startsWith("var-")) + section = "var-" + section; + return SsuVariables::variable(boardMappings, section, key); } diff --git a/libssu/ssuvariables.cpp b/libssu/ssuvariables.cpp index 8e94c09..eed5286 100644 --- a/libssu/ssuvariables.cpp +++ b/libssu/ssuvariables.cpp @@ -136,6 +136,7 @@ SsuSettings *SsuVariables::settings(){ return m_settings; } +/// @todo add override capability with an override-section in ssu.ini QVariant SsuVariables::variable(QString section, const QString &key){ if (m_settings != NULL) return variable(m_settings, section, key); @@ -144,23 +145,19 @@ QVariant SsuVariables::variable(QString section, const QString &key){ } QVariant SsuVariables::variable(SsuSettings *settings, QString section, const QString &key){ - if (!section.startsWith("var-")) - section = "var-" + section; + QVariant value; - if (settings->contains(section + "/" + key)){ - return settings->value(section + "/" + key); - } + value = readVariable(settings, section, key, 0); - if (settings->contains(section + "/variables")){ - QStringList sections = settings->value(section + "/variables").toStringList(); - foreach(const QString §ion, sections){ - QVariant value = variable(settings, section, key); - if (value.type() != QMetaType::UnknownType) - return value; - } + // first check if the value is defined in the main section, and fall back + // to default sections + if (value.type() == QMetaType::UnknownType){ + QString dSection = defaultSection(settings, section); + if (!dSection.isEmpty()) + value = readVariable(settings, dSection, key, 0, false); } - return QVariant(); + return value; } void SsuVariables::variableSection(QString section, QHash *storageHash){ @@ -179,7 +176,6 @@ void SsuVariables::variableSection(SsuSettings *settings, QString section, QHash } } - // resolve a configuration section, recursively following all 'variables' sections. // variables which exist in more than one section will get overwritten when discovered // again @@ -239,3 +235,41 @@ void SsuVariables::readSection(SsuSettings *settings, QString section, } settings->endGroup(); } + +QVariant SsuVariables::readVariable(SsuSettings *settings, QString section, const QString &key, + int recursionDepth, bool logOverride){ + QVariant value; + + if (recursionDepth >= SSU_MAX_RECURSION){ + SsuLog::instance()->print(LOG_WARNING, + QString("Maximum recursion depth for resolving %1 from %2::%3") + .arg(key) + .arg(settings->fileName()) + .arg(section)); + return value; + } + + // variables directly in the section take precedence + if (settings->contains(section + "/" + key)){ + return settings->value(section + "/" + key); + } + + /// @todo add logging for overrides + if (settings->contains(section + "/variables")){ + // child should log unless the parent is a default section + bool childLogOverride = true; + if (section.startsWith("default-") || section.startsWith("var-default-")) + childLogOverride = false; + + QStringList sections = settings->value(section + "/variables").toStringList(); + foreach(const QString §ion, sections){ + if (section.startsWith("var-")) + value = readVariable(settings, section, key, recursionDepth + 1, childLogOverride); + else + value = readVariable(settings, "var-" + section, key, + recursionDepth + 1, childLogOverride); + } + } + + return value; +} diff --git a/libssu/ssuvariables.h b/libssu/ssuvariables.h index cfc3c95..0b6ef4e 100644 --- a/libssu/ssuvariables.h +++ b/libssu/ssuvariables.h @@ -46,7 +46,7 @@ class SsuVariables: public QObject { * Return a variable from the given variable section. 'var'- is automatically * prepended to the section name if not specified already. Recursive search * through several variable sections (specified in the section) is supported, - * returned will be the first occurence of the variable. + * returned will be the last occurence of the variable. */ QVariant variable(QString section, const QString &key); static QVariant variable(SsuSettings *settings, QString section, const QString &key); @@ -64,10 +64,15 @@ class SsuVariables: public QObject { * the default section with the requested section. */ void variableSection(QString section, QHash *storageHash); - static void variableSection(SsuSettings *settings, QString section, QHash *storageHash); + static void variableSection(SsuSettings *settings, QString section, + QHash *storageHash); private: - static void readSection(SsuSettings *settings, QString section, QHash *storageHash, int recursionDepth, bool logOverride=true); + static void readSection(SsuSettings *settings, QString section, + QHash *storageHash, int recursionDepth, + bool logOverride=true); + static QVariant readVariable(SsuSettings *settings, QString section, const QString &key, + int recursionDepth, bool logOverride=true); SsuSettings *m_settings; };