diff --git a/src/plugins/projectexplorer/environment.cpp b/src/plugins/projectexplorer/environment.cpp index 1a2d69cef8c41f9becac3102c66747058fe04554..d1363c519e98e183101187fd32cf46a95928ecd3 100644 --- a/src/plugins/projectexplorer/environment.cpp +++ b/src/plugins/projectexplorer/environment.cpp @@ -32,7 +32,6 @@ #include <QtCore/QProcess> #include <QtCore/QDir> #include <QtCore/QString> -#include <QtCore/QDebug> using namespace ProjectExplorer; @@ -362,5 +361,83 @@ QString Environment::joinArgumentList(const QStringList &arguments) return result; } - - +enum State { BASE, VARIABLE, OPTIONALVARIABLEBRACE, STRING, STRING_ESCAPE, ESCAPE }; + +/** Expand environment variables in a string. + * + * Environment variables are accepted in the following forms: + * $SOMEVAR, ${SOMEVAR} and %SOMEVAR%. + * + * The following escape sequences are supported: + * "\$", "\\" and "\"" which will be replaced by '$', '\' and '%' + * respectively. + * + * Strings enclosed in '"' characters do not get varaibles + * substituted. Escape codes are processed though. + * + */ +QString Environment::expandVariables(const QString &input) const +{ + QString result; + QString variable; + QChar endVariable; + State state = BASE; + + int length = input.count(); + for (int i = 0; i < length; ++i) { + QChar c = input.at(i); + if (state == BASE) { + if (c == '\\') { + state = ESCAPE; + } else if (c == '$') { + state = OPTIONALVARIABLEBRACE; + variable.clear(); + endVariable = QChar(0); + } else if (c == '%') { + state = VARIABLE; + variable.clear(); + endVariable = '%'; + } else if (c == '\"') { + state = STRING; + result += c; + } else { + result += c; + } + } else if (state == VARIABLE) { + if (c == endVariable) { + result += value(variable); + state = BASE; + } else if (c.isLetterOrNumber() || c == '_') { + variable += c; + } else { + result += value(variable); + result += c; + state = BASE; + } + } else if (state == OPTIONALVARIABLEBRACE) { + if (c == '{') + endVariable = '}'; + else + variable = c; + state = VARIABLE; + } else if (state == STRING) { + if (c == '\\') { + state = STRING_ESCAPE; + } else if (c == '\"') { + state = BASE; + result += c; + } else { + result += c; + } + } else if (state == STRING_ESCAPE) { + result += c; + state = STRING; + } else if (state == ESCAPE){ + result += c; + state = BASE; + } + } + if (state == VARIABLE) + result += value(variable); + return result; +} diff --git a/src/plugins/projectexplorer/environment.h b/src/plugins/projectexplorer/environment.h index 289320a00304772c65288a34c4fce11225517fd3..41b2ac27706234b873ba3a56b7c2e399c5949fe9 100644 --- a/src/plugins/projectexplorer/environment.h +++ b/src/plugins/projectexplorer/environment.h @@ -95,6 +95,8 @@ public: static QStringList parseCombinedArgString(const QString &program); static QString joinArgumentList(const QStringList &arguments); + QString expandVariables(const QString &) const; + bool operator!=(const Environment &other) const; bool operator==(const Environment &other) const; private: