1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

[QzTools] Added function to split arguments from command.

This commit is contained in:
nowrep 2013-02-15 10:47:34 +01:00
parent 94f1af7f2f
commit 5641d380d8
4 changed files with 140 additions and 0 deletions

View File

@ -462,6 +462,92 @@ bool QzTools::isUtf8(const char* string)
return true;
}
static inline bool isQuote(const QChar &c)
{
return (c == QLatin1Char('"') || c == QLatin1Char('\''));
}
// Function splits command line into arguments
// eg. /usr/bin/foo -o test -b "bar bar" -s="sed sed"
// => '/usr/bin/foo' '-o' 'test' '-b' 'bar bar' '-s=sed sed'
QStringList QzTools::splitCommandArguments(const QString &command)
{
QString line = command.trimmed();
if (line.isEmpty()) {
return QStringList();
}
QChar SPACE(' ');
QChar EQUAL('=');
QChar BSLASH('\\');
QChar QUOTE('"');
QStringList r;
int equalPos = -1; // Position of = in opt="value"
int startPos = isQuote(line.at(0)) ? 1 : 0;
bool inWord = !isQuote(line.at(0));
bool inQuote = !inWord;
if (inQuote) {
QUOTE = line.at(0);
}
const int strlen = line.length();
for (int i = 0; i < strlen; ++i) {
const QChar &c = line.at(i);
if (inQuote && c == QUOTE && i > 0 && line.at(i - 1) != BSLASH) {
QString str = line.mid(startPos, i - startPos);
if (equalPos > -1) {
str.remove(equalPos - startPos + 1, 1);
}
inQuote = false;
if (!str.isEmpty()) {
r.append(str);
}
continue;
}
else if (!inQuote && isQuote(c)) {
inQuote = true;
QUOTE = c;
if (!inWord) {
startPos = i + 1;
}
else if (i > 0 && line.at(i - 1) == EQUAL) {
equalPos = i - 1;
}
}
if (inQuote) {
continue;
}
if (inWord && (c == SPACE || i == strlen - 1)) {
int len = (i == strlen - 1) ? -1 : i - startPos;
const QString &str = line.mid(startPos, len);
inWord = false;
if (!str.isEmpty()) {
r.append(str);
}
}
else if (!inWord && c != SPACE) {
inWord = true;
startPos = i;
}
}
// Unmatched quote
if (inQuote) {
return QStringList();
}
return r;
}
// Qt5 migration help functions
bool QzTools::isCertificateValid(const QSslCertificate &cert)
{

View File

@ -57,6 +57,7 @@ QPixmap QT_QUPZILLA_EXPORT createPixmapForSite(const QIcon &icon, const QString
QString QT_QUPZILLA_EXPORT applyDirectionToPage(QString &pageContents);
QString QT_QUPZILLA_EXPORT resolveFromPath(const QString &name);
QStringList QT_QUPZILLA_EXPORT splitCommandArguments(const QString &command);
QIcon QT_QUPZILLA_EXPORT iconFromFileName(const QString &fileName);
bool QT_QUPZILLA_EXPORT isUtf8(const char* string);

View File

@ -67,3 +67,53 @@ void QzToolsTest::getFileNameFromUrl()
QCOMPARE(QzTools::getFileNameFromUrl(url), result);
}
void QzToolsTest::splitCommandArguments_data()
{
QTest::addColumn<QString>("command");
QTest::addColumn<QStringList>("result");
QTest::newRow("Basic") << "/usr/bin/foo -o foo.out"
<< (QStringList() << "/usr/bin/foo" << "-o" << "foo.out");
QTest::newRow("Empty") << QString()
<< QStringList();
QTest::newRow("OnlySpaces") << QString(" ")
<< QStringList();
QTest::newRow("OnlyQuotes") << QString("\"\" \"\"")
<< QStringList();
QTest::newRow("EmptyQuotesAndSpace") << QString("\"\" \"\" \" \"")
<< QStringList(" ");
QTest::newRow("MultipleSpaces") << " /usr/foo -o foo.out "
<< (QStringList() << "/usr/foo" << "-o" << "foo.out");
QTest::newRow("Quotes") << "\"/usr/foo\" \"-o\" \"foo.out\""
<< (QStringList() << "/usr/foo" << "-o" << "foo.out");
QTest::newRow("SingleQuotes") << "'/usr/foo' '-o' 'foo.out'"
<< (QStringList() << "/usr/foo" << "-o" << "foo.out");
QTest::newRow("SingleAndDoubleQuotes") << " '/usr/foo' \"-o\" 'foo.out' "
<< (QStringList() << "/usr/foo" << "-o" << "foo.out");
QTest::newRow("SingleInDoubleQuotes") << "/usr/foo \"-o 'ds' \" 'foo.out' "
<< (QStringList() << "/usr/foo" << "-o 'ds' " << "foo.out");
QTest::newRow("DoubleInSingleQuotes") << "/usr/foo -o 'foo\" d \".out' "
<< (QStringList() << "/usr/foo" << "-o" << "foo\" d \".out");
QTest::newRow("SpacesWithQuotes") << QString(" \" \" \" \" ")
<< (QStringList() << " " << " ");
QTest::newRow("QuotesAndSpaces") << "/usr/foo -o \"foo - out\""
<< (QStringList() << "/usr/foo" << "-o" << "foo - out");
QTest::newRow("EqualAndQuotes") << "/usr/foo -o=\"foo - out\""
<< (QStringList() << "/usr/foo" << "-o=foo - out");
QTest::newRow("EqualWithSpaces") << "/usr/foo -o = \"foo - out\""
<< (QStringList() << "/usr/foo" << "-o" << "=" << "foo - out");
QTest::newRow("MultipleSpacesAndQuotes") << " /usr/foo -o=\" foo.out \" "
<< (QStringList() << "/usr/foo" << "-o= foo.out ");
// Unmatched quotes should be treated as an error
QTest::newRow("UnmatchedQuote") << "/usr/bin/foo -o \"bar"
<< QStringList();
}
void QzToolsTest::splitCommandArguments()
{
QFETCH(QString, command);
QFETCH(QStringList, result);
QCOMPARE(QzTools::splitCommandArguments(command), result);
}

View File

@ -31,6 +31,9 @@ private slots:
void getFileNameFromUrl_data();
void getFileNameFromUrl();
void splitCommandArguments_data();
void splitCommandArguments();
};
#endif // QZTOOLSTEST_H