diff options
author | Oswald Buddenhagen <oswald.buddenhagen@nokia.com> | 2012-07-31 17:27:48 +0200 |
---|---|---|
committer | Oswald Buddenhagen <oswald.buddenhagen@nokia.com> | 2012-08-01 16:49:02 +0200 |
commit | fa91da2d413c94c17a92ad78b47b0cd6a962da95 (patch) | |
tree | 145663aad8a18bbcf259fc806852e8c86a90707d /src/shared/proparser | |
parent | b7c3b390f54bd6b2e02487cee45ac620e7dae15e (diff) | |
download | qt-creator-fa91da2d413c94c17a92ad78b47b0cd6a962da95.tar.gz |
add write_file() function
follow suit with qmake ...
Change-Id: I4e57e160b811b6496adf208b49f1dd0df0b57563
Reviewed-by: Daniel Teske <daniel.teske@nokia.com>
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@nokia.com>
Diffstat (limited to 'src/shared/proparser')
-rw-r--r-- | src/shared/proparser/qmakebuiltins.cpp | 62 | ||||
-rw-r--r-- | src/shared/proparser/qmakeevaluator.h | 2 |
2 files changed, 63 insertions, 1 deletions
diff --git a/src/shared/proparser/qmakebuiltins.cpp b/src/shared/proparser/qmakebuiltins.cpp index 679e57d606..0653f6391e 100644 --- a/src/shared/proparser/qmakebuiltins.cpp +++ b/src/shared/proparser/qmakebuiltins.cpp @@ -90,7 +90,7 @@ enum TestFunc { T_EXISTS, T_EXPORT, T_CLEAR, T_UNSET, T_EVAL, T_CONFIG, T_SYSTEM, T_RETURN, T_BREAK, T_NEXT, T_DEFINED, T_CONTAINS, T_INFILE, T_COUNT, T_ISEMPTY, T_INCLUDE, T_LOAD, T_DEBUG, T_LOG, T_MESSAGE, T_WARNING, T_ERROR, T_IF, - T_MKPATH, T_TOUCH + T_MKPATH, T_WRITE_FILE, T_TOUCH }; void QMakeEvaluator::initFunctionStatics() @@ -176,6 +176,7 @@ void QMakeEvaluator::initFunctionStatics() { "warning", T_WARNING }, { "error", T_ERROR }, { "mkpath", T_MKPATH }, + { "write_file", T_WRITE_FILE }, { "touch", T_TOUCH }, }; for (unsigned i = 0; i < sizeof(testInits)/sizeof(testInits[0]); ++i) @@ -274,6 +275,48 @@ quoteValue(const ProString &val) return ret; } +static bool +doWriteFile(const QString &name, QIODevice::OpenMode mode, const QString &contents, QString *errStr) +{ + QByteArray bytes = contents.toLocal8Bit(); + QFile cfile(name); + if (!(mode & QIODevice::Append) && cfile.open(QIODevice::ReadOnly | QIODevice::Text)) { + if (cfile.readAll() == bytes) + return true; + cfile.close(); + } + if (!cfile.open(mode | QIODevice::WriteOnly | QIODevice::Text)) { + *errStr = cfile.errorString(); + return false; + } + cfile.write(bytes); + cfile.close(); + if (cfile.error() != QFile::NoError) { + *errStr = cfile.errorString(); + return false; + } + return true; +} + +QMakeEvaluator::VisitReturn +QMakeEvaluator::writeFile(const QString &ctx, const QString &fn, QIODevice::OpenMode mode, + const QString &contents) +{ + QFileInfo qfi(fn); + if (!QDir::current().mkpath(qfi.path())) { + evalError(fL1S("Cannot create %1directory %2.") + .arg(ctx, QDir::toNativeSeparators(qfi.path()))); + return ReturnFalse; + } + QString errStr; + if (!doWriteFile(qfi.filePath(), mode, contents, &errStr)) { + evalError(fL1S("Cannot write %1file %2: %3.") + .arg(ctx, QDir::toNativeSeparators(qfi.filePath()), errStr)); + return ReturnFalse; + } + return ReturnTrue; +} + #ifndef QT_BOOTSTRAPPED void QMakeEvaluator::runProcess(QProcess *proc, const QString &command, QProcess::ProcessChannel chan) const @@ -1381,6 +1424,23 @@ QMakeEvaluator::VisitReturn QMakeEvaluator::evaluateConditionalFunction( } return ReturnTrue; } + case T_WRITE_FILE: { + if (args.count() > 3) { + evalError(fL1S("write_file(name, [content var, [append]]) requires one to three arguments.")); + return ReturnFalse; + } + QIODevice::OpenMode mode = QIODevice::Truncate; + QString contents; + if (args.count() >= 2) { + const ProStringList &vals = values(args.at(1)); + if (!vals.isEmpty()) + contents = vals.join(fL1S("\n")) + QLatin1Char('\n'); + if (args.count() >= 3) + if (!args.at(2).toQString(m_tmp1).compare(fL1S("append"), Qt::CaseInsensitive)) + mode = QIODevice::Append; + } + return writeFile(QString(), resolvePath(args.at(0).toQString(m_tmp1)), mode, contents); + } case T_TOUCH: { if (args.count() != 2) { evalError(fL1S("touch(file, reffile) requires two arguments.")); diff --git a/src/shared/proparser/qmakeevaluator.h b/src/shared/proparser/qmakeevaluator.h index 759ffc9002..9fceae9952 100644 --- a/src/shared/proparser/qmakeevaluator.h +++ b/src/shared/proparser/qmakeevaluator.h @@ -181,6 +181,8 @@ public: QHash<ProString, QSet<ProString> > &dependencies, ProValueMap &dependees, ProStringList &rootSet) const; + VisitReturn writeFile(const QString &ctx, const QString &fn, QIODevice::OpenMode mode, + const QString &contents); #ifndef QT_BOOTSTRAPPED void runProcess(QProcess *proc, const QString &command, QProcess::ProcessChannel chan) const; #endif |