forked from qt-creator/qt-creator
Utils: MacroExpander API cosmetics.
Make const-correct, add convenience function for commandline parameter expansion. Change-Id: I12c3651e4e7b8a0a9319d1dfbea676b622b1a41a Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com>
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include "macroexpander.h"
|
||||
#include "qtcprocess.h"
|
||||
|
||||
#include "algorithm.h"
|
||||
|
||||
@@ -45,11 +46,52 @@ const char kPathPostfix[] = ":Path";
|
||||
const char kFileNamePostfix[] = ":FileName";
|
||||
const char kFileBaseNamePostfix[] = ":FileBaseName";
|
||||
|
||||
class MacroExpanderPrivate
|
||||
class MacroExpanderPrivate : public AbstractMacroExpander
|
||||
{
|
||||
public:
|
||||
MacroExpanderPrivate() : m_accumulating(false) {}
|
||||
|
||||
bool resolveMacro(const QString &name, QString *ret)
|
||||
{
|
||||
bool found;
|
||||
*ret = value(name.toUtf8(), &found);
|
||||
if (found)
|
||||
return true;
|
||||
|
||||
found = Utils::anyOf(m_subProviders, [name, ret] (const MacroExpanderProvider &p) -> bool {
|
||||
MacroExpander *expander = p ? p() : 0;
|
||||
return expander && expander->resolveMacro(name, ret);
|
||||
});
|
||||
|
||||
if (found)
|
||||
return true;
|
||||
|
||||
return this == globalMacroExpander()->d ? false : globalMacroExpander()->d->resolveMacro(name, ret);
|
||||
}
|
||||
|
||||
QString value(const QByteArray &variable, bool *found)
|
||||
{
|
||||
MacroExpander::StringFunction sf = m_map.value(variable);
|
||||
if (sf) {
|
||||
if (found)
|
||||
*found = true;
|
||||
return sf();
|
||||
}
|
||||
|
||||
for (auto it = m_prefixMap.constBegin(); it != m_prefixMap.constEnd(); ++it) {
|
||||
if (variable.startsWith(it.key())) {
|
||||
MacroExpander::PrefixFunction pf = it.value();
|
||||
if (found)
|
||||
*found = true;
|
||||
return pf(QString::fromUtf8(variable.mid(it.key().count())));
|
||||
}
|
||||
}
|
||||
if (found)
|
||||
*found = false;
|
||||
|
||||
return QString();
|
||||
}
|
||||
|
||||
QHash<QByteArray, MacroExpander::StringFunction> m_map;
|
||||
QHash<QByteArray, MacroExpander::PrefixFunction> m_prefixMap;
|
||||
QMap<QByteArray, QString> m_descriptions;
|
||||
@@ -187,49 +229,18 @@ MacroExpander::~MacroExpander()
|
||||
/*!
|
||||
* \internal
|
||||
*/
|
||||
bool MacroExpander::resolveMacro(const QString &name, QString *ret)
|
||||
bool MacroExpander::resolveMacro(const QString &name, QString *ret) const
|
||||
{
|
||||
bool found;
|
||||
*ret = value(name.toUtf8(), &found);
|
||||
if (found)
|
||||
return true;
|
||||
|
||||
found = Utils::anyOf(d->m_subProviders, [name, ret] (const MacroExpanderProvider &p) -> bool {
|
||||
MacroExpander *expander = p ? p() : 0;
|
||||
return expander && expander->resolveMacro(name, ret);
|
||||
});
|
||||
|
||||
if (found)
|
||||
return true;
|
||||
|
||||
return this == globalMacroExpander() ? false : globalMacroExpander()->resolveMacro(name, ret);
|
||||
return d->resolveMacro(name, ret);
|
||||
}
|
||||
|
||||
/*!
|
||||
* Returns the value of the given \a variable. If \a found is given, it is
|
||||
* set to true if the variable has a value at all, false if not.
|
||||
*/
|
||||
QString MacroExpander::value(const QByteArray &variable, bool *found)
|
||||
QString MacroExpander::value(const QByteArray &variable, bool *found) const
|
||||
{
|
||||
StringFunction sf = d->m_map.value(variable);
|
||||
if (sf) {
|
||||
if (found)
|
||||
*found = true;
|
||||
return sf();
|
||||
}
|
||||
|
||||
for (auto it = d->m_prefixMap.constBegin(); it != d->m_prefixMap.constEnd(); ++it) {
|
||||
if (variable.startsWith(it.key())) {
|
||||
PrefixFunction pf = it.value();
|
||||
if (found)
|
||||
*found = true;
|
||||
return pf(QString::fromUtf8(variable.mid(it.key().count())));
|
||||
}
|
||||
}
|
||||
if (found)
|
||||
*found = false;
|
||||
|
||||
return QString();
|
||||
return d->value(variable, found);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -239,18 +250,23 @@ QString MacroExpander::value(const QByteArray &variable, bool *found)
|
||||
* \sa MacroExpander
|
||||
* \sa macroExpander()
|
||||
*/
|
||||
QString MacroExpander::expand(const QString &stringWithVariables)
|
||||
QString MacroExpander::expand(const QString &stringWithVariables) const
|
||||
{
|
||||
QString res = stringWithVariables;
|
||||
Utils::expandMacros(&res, this);
|
||||
Utils::expandMacros(&res, d);
|
||||
return res;
|
||||
}
|
||||
|
||||
QByteArray MacroExpander::expand(const QByteArray &stringWithVariables)
|
||||
QByteArray MacroExpander::expand(const QByteArray &stringWithVariables) const
|
||||
{
|
||||
return expand(QString::fromLatin1(stringWithVariables)).toLatin1();
|
||||
}
|
||||
|
||||
QString MacroExpander::expandProcessArgs(const QString &argsWithVariables) const
|
||||
{
|
||||
return QtcProcess::expandMacros(argsWithVariables, d);
|
||||
}
|
||||
|
||||
/*!
|
||||
* Makes the given string-valued \a prefix known to the variable manager,
|
||||
* together with a localized \a description.
|
||||
@@ -333,7 +349,7 @@ void MacroExpander::registerFileVariables(const QByteArray &prefix,
|
||||
* \sa registerVariable()
|
||||
* \sa registerFileVariables()
|
||||
*/
|
||||
QList<QByteArray> MacroExpander::variables()
|
||||
QList<QByteArray> MacroExpander::variables() const
|
||||
{
|
||||
return d->m_descriptions.keys();
|
||||
}
|
||||
@@ -341,7 +357,7 @@ QList<QByteArray> MacroExpander::variables()
|
||||
/*!
|
||||
* Returns the description that was registered for the \a variable.
|
||||
*/
|
||||
QString MacroExpander::variableDescription(const QByteArray &variable)
|
||||
QString MacroExpander::variableDescription(const QByteArray &variable) const
|
||||
{
|
||||
return d->m_descriptions.value(variable);
|
||||
}
|
||||
@@ -357,6 +373,11 @@ MacroExpanders MacroExpander::subExpanders() const
|
||||
return expanders;
|
||||
}
|
||||
|
||||
AbstractMacroExpander *MacroExpander::abstractExpander() const
|
||||
{
|
||||
return d;
|
||||
}
|
||||
|
||||
QString MacroExpander::displayName() const
|
||||
{
|
||||
return d->m_displayName;
|
||||
|
||||
Reference in New Issue
Block a user