Dcow
Dcow

Reputation: 1463

QString parse system variables

How I should parse QString, which contains system variables ?What I want:

QString path = "%WINDIR%\\System32\\";
QString output  = parse(path);
QDebug()<<output; \\ output is "C:\\Windows\\System32\\"

Upvotes: 0

Views: 1341

Answers (2)

Silex
Silex

Reputation: 1837

I think you want something like this:

// Untested
QString parse(QString str)
{
    int pos = 0;
    QRegExp rx("%([^%]+)%"); // Match env var between two '%'
    rx.setMinimal(true);
    while((pos = rx.indexIn(str, pos)) != -1)
    {
        // Replace env var
        QString capture = rx.cap(1);
        QString replacement = getenv(capture.toAscii());
        str.replace("%" + capture + "%", replacement);

        // Skip env var + two '%'
        pos += rx.matchedLength() + 2;
    }
    return str;
}

QString path = parse("%WINDIR%\\System32");

Upvotes: 2

Ashif
Ashif

Reputation: 1684

I think, this is what you looking for. Please try this

QString windir = getenv ("WINDIR"); // Expanded
if (windir.isEmpty()) {
    fprintf(stderr, "Generator requires WINDIRto be set\n");

}    
windir += "\\System32";
qDebug()<<windir; 

Upvotes: 0

Related Questions