diff options
author | Arttu Tarkiainen <arttu.tarkiainen@qt.io> | 2019-06-14 16:15:16 +0300 |
---|---|---|
committer | Arttu Tarkiainen <arttu.tarkiainen@qt.io> | 2019-08-16 08:33:43 +0000 |
commit | 3b310a3ec13a743ae2258bc43d92a5df42c6ad37 (patch) | |
tree | 83583b810174313f0f093711e8fb70f40a37afc7 /src/libs/installer | |
parent | 28f92d37842ef087ab4f35ae7018b6366d0044e9 (diff) |
Add regular expression support to replace operation
Introduce search with regular expressions support to replace
operation. Add unit tests and make associated documentation
changes.
Task-number: QTIFW-889
Change-Id: I82e30056030ebc900be49046fda1903b27a2824d
Reviewed-by: Katja Marttila <katja.marttila@qt.io>
Diffstat (limited to 'src/libs/installer')
-rw-r--r-- | src/libs/installer/replaceoperation.cpp | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/src/libs/installer/replaceoperation.cpp b/src/libs/installer/replaceoperation.cpp index 8cc1e0315..692bba715 100644 --- a/src/libs/installer/replaceoperation.cpp +++ b/src/libs/installer/replaceoperation.cpp @@ -31,6 +31,7 @@ #include <QtCore/QDir> #include <QtCore/QFile> #include <QtCore/QTextStream> +#include <QtCore/QRegularExpression> using namespace QInstaller; @@ -46,17 +47,45 @@ void ReplaceOperation::backup() bool ReplaceOperation::performOperation() { + static const QLatin1String stringMode("string"); + static const QLatin1String regexMode("regex"); + // Arguments: // 1. filename // 2. Source-String // 3. Replace-String - if (!checkArgumentCount(3)) + // 4. mode=string|regex + + // Calling with three arguments defaults to string search, + // this provides backward compatibility with the old syntax. + if (!checkArgumentCount(3, 4)) return false; const QStringList args = arguments(); const QString fileName = args.at(0); const QString before = args.at(1); const QString after = args.at(2); + QString mode = args.value(3); + + if (mode.isEmpty()) + mode = stringMode; + + if (before.isEmpty()) { + setError(InvalidArguments); + setErrorString(tr("Current search argument calling \"%1\" with " + "empty search argument is not supported.").arg(name())); + + return false; + } + + if (!(mode == stringMode || mode == regexMode)) { + setError(InvalidArguments); + setErrorString(tr("Current mode argument calling \"%1\" with " + "arguments \"%2\" is not supported. Please use string or regex.") + .arg(name(), arguments().join(QLatin1String("; ")))); + + return false; + } QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) { @@ -78,7 +107,12 @@ bool ReplaceOperation::performOperation() } stream.setDevice(&file); - stream << replacedFileContent.replace(before, after); + if (mode == regexMode) { + QRegularExpression regex(before); + stream << replacedFileContent.replace(regex, after); + } else if (mode == stringMode) { + stream << replacedFileContent.replace(before, after); + } file.close(); return true; |