From 84614cabfaaab5eb4be04688a84cef8d79493f75 Mon Sep 17 00:00:00 2001 From: Oswald Buddenhagen Date: Fri, 27 Apr 2012 14:01:08 +0200 Subject: add write_file() function this dumps the contents of a variable into a file. each element of the variable is considered a line; line terminators are added. all missing directories are automatically created. Change-Id: Idafeb873cea64e6705c894b3ab0ef21df69e7170 Reviewed-by: Marius Storm-Olsen --- qmake/project.cpp | 39 +++++++++++++++++++++- .../tools/qmake/testdata/functions/functions.pro | 16 ++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/qmake/project.cpp b/qmake/project.cpp index 2a03580260..643e5fda9e 100644 --- a/qmake/project.cpp +++ b/qmake/project.cpp @@ -119,7 +119,7 @@ enum TestFunc { T_REQUIRES=1, T_GREATERTHAN, T_LESSTHAN, T_EQUALS, 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_ERROR, - T_MESSAGE, T_WARNING, T_IF, T_OPTION, T_CACHE }; + T_MESSAGE, T_WARNING, T_IF, T_OPTION, T_CACHE, T_WRITE_FILE }; QHash qmake_testFunctions() { static QHash *qmake_test_functions = 0; @@ -155,6 +155,7 @@ QHash qmake_testFunctions() qmake_test_functions->insert("warning", T_WARNING); qmake_test_functions->insert("option", T_OPTION); qmake_test_functions->insert("cache", T_CACHE); + qmake_test_functions->insert("write_file", T_WRITE_FILE); } return *qmake_test_functions; } @@ -1889,6 +1890,11 @@ writeFile(const QString &name, QIODevice::OpenMode mode, const QString &contents { 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; @@ -3039,6 +3045,37 @@ QMakeProject::doProjectTest(QString func, QList args_list, QHash 3) { + fprintf(stderr, "%s:%d: write_file(name, [content var, [append]]) requires one to three arguments.\n", + parser.file.toLatin1().constData(), parser.line_no); + return false; + } + QIODevice::OpenMode mode = QIODevice::Truncate; + QString contents; + if (args.count() >= 2) { + QStringList vals = values(args.at(1), place); + if (!vals.isEmpty()) + contents = vals.join(QLatin1String("\n")) + QLatin1Char('\n'); + if (args.count() >= 3) + if (!args.at(2).compare(QLatin1String("append"), Qt::CaseInsensitive)) + mode = QIODevice::Append; + } + QFileInfo qfi(args.at(0)); + if (!QDir::current().mkpath(qfi.path())) { + fprintf(stderr, "%s:%d: ERROR creating directory %s\n", + parser.file.toLatin1().constData(), parser.line_no, + qfi.path().toLatin1().constData()); + return false; + } + QString errStr; + if (!writeFile(args.at(0), mode, contents, &errStr)) { + fprintf(stderr, "%s:%d ERROR writing %s: %s\n", + parser.file.toLatin1().constData(), parser.line_no, + args.at(0).toLatin1().constData(), errStr.toLatin1().constData()); + return false; + } + return true; } default: fprintf(stderr, "%s:%d: Unknown test function: %s\n", parser.file.toLatin1().constData(), parser.line_no, func.toLatin1().constData()); diff --git a/tests/auto/tools/qmake/testdata/functions/functions.pro b/tests/auto/tools/qmake/testdata/functions/functions.pro index ad66ee863d..1da7fd923b 100644 --- a/tests/auto/tools/qmake/testdata/functions/functions.pro +++ b/tests/auto/tools/qmake/testdata/functions/functions.pro @@ -84,4 +84,18 @@ myTestFunction("oink baa moo") message("FAILED: myTestFunction: $$RESULT") } - +moo = "this is a test" "for real" +fn = $$OUT_PWD/testdir/afile +write_file($$fn, moo)|message("FAILED: write_file() failed") +exists($$fn)|message("FAILED: write_file() didn't write anything") +mooout = $$cat($$fn, line) +equals(moo, $$mooout)|message("FAILED: write_file() wrote something wrong") +moo += "another line" +write_file($$fn, moo)|message("FAILED: write_file() failed (take 2)") +mooout = $$cat($$fn, line) +equals(moo, $$mooout)|message("FAILED: write_file() wrote something wrong (take 2)") +mooadd = "yet another line" +write_file($$fn, mooadd, append)|message("FAILED: write_file() failed (append)") +moo += $$mooadd +mooout = $$cat($$fn, line) +equals(moo, $$mooout)|message("FAILED: write_file() wrote something wrong when appending") -- cgit v1.2.3