What\'s the best way to expand
${MyPath}/filename.txt to /home/user/filename.txt
or
%MyPath%/filename.txt to c:\\Document
Using Qt, this works for me:
#include
#include
QString expand_environment_variables( QString s )
{
QString r(s);
QRegExp env_var("\\$([A-Za-z0-9_]+)");
int i;
while((i = env_var.indexIn(r)) != -1) {
QByteArray value(qgetenv(env_var.cap(1).toLatin1().data()));
if(value.size() > 0) {
r.remove(i, env_var.matchedLength());
r.insert(i, value);
} else
break;
}
return r;
}
expand_environment_variables(QString("$HOME/.myconfigfile")); yields /home/martin/.myconfigfile (It also works with nested expansions)