Skip to content

Commit

Permalink
Add proper recursive search and default sections for single variable …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
Bernd Wachter committed Jul 6, 2013
1 parent 4f7e4a7 commit b3e422a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 17 deletions.
5 changes: 5 additions & 0 deletions libssu/ssudeviceinfo.cpp
Expand Up @@ -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);
}

Expand Down
62 changes: 48 additions & 14 deletions libssu/ssuvariables.cpp
Expand Up @@ -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);
Expand All @@ -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 &section, 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<QString, QString> *storageHash){
Expand All @@ -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
Expand Down Expand Up @@ -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 &section, 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;
}
11 changes: 8 additions & 3 deletions libssu/ssuvariables.h
Expand Up @@ -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);
Expand All @@ -64,10 +64,15 @@ class SsuVariables: public QObject {
* the default section with the requested section.
*/
void variableSection(QString section, QHash<QString, QString> *storageHash);
static void variableSection(SsuSettings *settings, QString section, QHash<QString, QString> *storageHash);
static void variableSection(SsuSettings *settings, QString section,
QHash<QString, QString> *storageHash);

private:
static void readSection(SsuSettings *settings, QString section, QHash<QString, QString> *storageHash, int recursionDepth, bool logOverride=true);
static void readSection(SsuSettings *settings, QString section,
QHash<QString, QString> *storageHash, int recursionDepth,
bool logOverride=true);
static QVariant readVariable(SsuSettings *settings, QString section, const QString &key,
int recursionDepth, bool logOverride=true);
SsuSettings *m_settings;
};

Expand Down

0 comments on commit b3e422a

Please sign in to comment.