Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Marco Bubke
flatpak-qt-creator
Commits
27ded207
Commit
27ded207
authored
Sep 22, 2010
by
Tobias Hunger
Browse files
Environment: Add method to expand environment variables
Add a method to expand environment variables in a string. Reviewed-by: dt
parent
6622947c
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/plugins/projectexplorer/environment.cpp
View file @
27ded207
...
...
@@ -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
;
}
src/plugins/projectexplorer/environment.h
View file @
27ded207
...
...
@@ -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:
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment