From 35d83b4df3cf669bd6150cf77a3f3bddba7937dd Mon Sep 17 00:00:00 2001 From: lordmulder Date: Sat, 7 Jan 2017 03:18:03 +0100 Subject: [PATCH] Refactored custom parameter validation code into a separate class. --- src/string_validator.cpp | 193 +++++++++++++++++++++++++ src/string_validator.h | 60 ++++++++ src/version.h | 2 +- src/win_addJob.cpp | 185 +----------------------- x264_launcher_MSVC2013.vcxproj | 8 + x264_launcher_MSVC2013.vcxproj.filters | 18 +++ x264_launcher_MSVC2015.vcxproj | 2 + x264_launcher_MSVC2015.vcxproj.filters | 6 + 8 files changed, 290 insertions(+), 184 deletions(-) create mode 100644 src/string_validator.cpp create mode 100644 src/string_validator.h diff --git a/src/string_validator.cpp b/src/string_validator.cpp new file mode 100644 index 0000000..1509572 --- /dev/null +++ b/src/string_validator.cpp @@ -0,0 +1,193 @@ +/////////////////////////////////////////////////////////////////////////////// +// Simple x264 Launcher +// Copyright (C) 2004-2016 LoRd_MuldeR +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// http://www.gnu.org/licenses/gpl-2.0.txt +/////////////////////////////////////////////////////////////////////////////// + +#include "string_validator.h" + +/////////////////////////////////////////////////////////////////////////////// +// StringValidator +/////////////////////////////////////////////////////////////////////////////// + +StringValidator::StringValidator(QLabel *notifier, QLabel *icon) +: + m_notifier(notifier), m_icon(icon) +{ + m_notifier->hide(); + m_icon->hide(); +} + +void StringValidator::fixup(QString &input) const +{ + input = input.simplified(); +} + + +bool StringValidator::checkParam(const QStringList &input, const char *const params[], const bool &doubleMinus) const +{ + for(QStringList::ConstIterator iter = input.constBegin(); iter != input.constEnd(); iter++) + { + for(size_t k = 0; params[k]; k++) + { + const QString param = QLatin1String(params[k]); + const QString prefix = ((param.length() > 1) && doubleMinus) ? QLatin1String("--") : QLatin1String("-"); + if(iter->compare(QString("%1%2").arg(prefix, param), Qt::CaseInsensitive) == 0) + { + if(m_notifier) + { + m_notifier->setText(tr("Forbidden parameter: %1").arg(*iter)); + } + return true; + } + } + if(iter->startsWith(QLatin1String("--"), Qt::CaseInsensitive)) + { + for(int i = 2; i < iter->length(); i++) + { + const QChar c = iter->at(i); + if((c == QLatin1Char('=')) && (i > 2) && (i + 1 < iter->length())) + { + break; /*to allow "--param=value" format*/ + } + if((!c.isLetter()) && ((i < 3) || ((!c.isNumber()) && ((i + 1 >= iter->length()) || (c != QLatin1Char('-')))))) + { + if(m_notifier) + { + m_notifier->setText(tr("Invalid string: %1").arg(*iter)); + } + return true; + } + } + } + } + + return false; +} + +bool StringValidator::checkPrefix(const QStringList &input, const bool &doubleMinus) const +{ + for(QStringList::ConstIterator iter = input.constBegin(); iter != input.constEnd(); iter++) + { + static const char *const c[3] = { "--", "-", NULL }; + for(size_t i = 0; c[i]; i++) + { + const QString prefix = QString::fromLatin1(c[i]); + if(iter->compare(prefix, Qt::CaseInsensitive) == 0) + { + if(m_notifier) + { + m_notifier->setText(tr("Invalid parameter: %1").arg(prefix)); + } + return true; + } + } + if + ( + ((!doubleMinus) && iter->startsWith("--", Qt::CaseInsensitive)) || + (doubleMinus && iter->startsWith("-", Qt::CaseInsensitive) && (!iter->startsWith("--", Qt::CaseInsensitive)) && (iter->length() > 2) && (!iter->at(1).isDigit())) || + (doubleMinus && iter->startsWith("--", Qt::CaseInsensitive) && (iter->length() < 4)) + ) + { + if(m_notifier) + { + m_notifier->setText(tr("Invalid syntax: %1").arg(*iter)); + } + return true; + } + } + return false; +} + +bool StringValidator::checkCharacters(const QStringList &input) const +{ + static const char c[] = {'*', '?', '<', '>', '|', NULL}; + + for(QStringList::ConstIterator iter = input.constBegin(); iter != input.constEnd(); iter++) + { + for(size_t i = 0; c[i]; i++) + { + if(iter->indexOf(QLatin1Char(c[i])) >= 0) + { + if(m_notifier) + { + m_notifier->setText(tr("Invalid character: '%1'").arg(QLatin1Char(c[i]))); + } + return true; + } + } + } + return false; +} + +const bool &StringValidator::setStatus(const bool &flag, const QString &toolName) const +{ + if(flag) + { + if(m_notifier) + { + if(m_notifier->isHidden()) m_notifier->show(); + if(m_icon) { if(m_icon->isHidden()) m_icon->show(); } + if(QWidget *w = m_notifier->topLevelWidget()->focusWidget()) + { + QToolTip::showText(static_cast(w->parent())->mapToGlobal(w->pos()), tr("Warning: You entered a parameter that is forbidden. Please note that the GUI will automatically set this parameter for you (if required)."), m_notifier, QRect()); + } + } + } + else + { + if(m_notifier) + { + if(m_notifier->isVisible()) m_notifier->hide(); + if(m_icon) { if(m_icon->isVisible()) m_icon->hide(); } + QToolTip::hideText(); + } + } + return flag; +} + +/////////////////////////////////////////////////////////////////////////////// +// StringValidatorEncoder +/////////////////////////////////////////////////////////////////////////////// + +QValidator::State StringValidatorEncoder::validate(QString &input, int &pos) const +{ + static const char *const params[] = {"B", "o", "h", "p", "q", /*"fps", "frames",*/ "preset", "tune", "profile", + "stdin", "crf", "bitrate", "qp", "pass", "stats", "output", "help", "quiet", "codec", "y4m", NULL}; + + const QString commandLine = input.trimmed(); + const QStringList tokens = commandLine.isEmpty() ? QStringList() : MUtils::OS::crack_command_line(commandLine); + + const bool invalid = checkCharacters(tokens) || checkPrefix(tokens, true) || checkParam(tokens, params, true); + return setStatus(invalid, "encoder") ? QValidator::Intermediate : QValidator::Acceptable; +} + +/////////////////////////////////////////////////////////////////////////////// +// StringValidatorSource +/////////////////////////////////////////////////////////////////////////////// + +QValidator::State StringValidatorSource::validate(QString &input, int &pos) const +{ + static const char *const params[] = {"o", "frames", "seek", "raw", "hfyu", "slave", NULL}; + + const QString commandLine = input.trimmed(); + const QStringList tokens = commandLine.isEmpty() ? QStringList() : MUtils::OS::crack_command_line(commandLine); + + const bool invalid = checkCharacters(tokens) || checkPrefix(tokens, false) || checkParam(tokens, params, false); + return setStatus(invalid, "Avs2YUV") ? QValidator::Intermediate : QValidator::Acceptable; +} diff --git a/src/string_validator.h b/src/string_validator.h new file mode 100644 index 0000000..01e45c5 --- /dev/null +++ b/src/string_validator.h @@ -0,0 +1,60 @@ +/////////////////////////////////////////////////////////////////////////////// +// Simple x264 Launcher +// Copyright (C) 2004-2016 LoRd_MuldeR +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// http://www.gnu.org/licenses/gpl-2.0.txt +/////////////////////////////////////////////////////////////////////////////// + +#pragma once + +//MUtils +#include + +//Qt +#include +#include +#include + +class StringValidator : public QValidator +{ +public: + StringValidator(QLabel *notifier, QLabel *icon); + virtual State validate(QString &input, int &pos) const = 0; + virtual void fixup(QString &input) const; + +protected: + QLabel *const m_notifier, *const m_icon; + + bool checkParam(const QStringList &input, const char *const params[], const bool &doubleMinus) const; + bool checkPrefix(const QStringList &input, const bool &doubleMinus) const; + bool checkCharacters(const QStringList &input) const; + const bool &setStatus(const bool &flag, const QString &toolName) const; +}; + +class StringValidatorEncoder : public StringValidator +{ +public: + StringValidatorEncoder(QLabel *notifier, QLabel *icon) : StringValidator(notifier, icon) {} + virtual State validate(QString &input, int &pos) const; +}; + +class StringValidatorSource : public StringValidator +{ +public: + StringValidatorSource(QLabel *notifier, QLabel *icon) : StringValidator(notifier, icon) {} + virtual State validate(QString &input, int &pos) const; +}; \ No newline at end of file diff --git a/src/version.h b/src/version.h index e37d266..0ccc25e 100644 --- a/src/version.h +++ b/src/version.h @@ -26,7 +26,7 @@ #define VER_X264_MAJOR 2 #define VER_X264_MINOR 7 #define VER_X264_PATCH 7 -#define VER_X264_BUILD 1077 +#define VER_X264_BUILD 1079 #define VER_X264_PORTABLE_EDITION (0) diff --git a/src/win_addJob.cpp b/src/win_addJob.cpp index d3eac58..9353027 100644 --- a/src/win_addJob.cpp +++ b/src/win_addJob.cpp @@ -30,6 +30,7 @@ #include "model_recently.h" #include "encoder_factory.h" #include "mediainfo.h" +#include "string_validator.h" #include "win_help.h" #include "win_editor.h" @@ -113,188 +114,6 @@ private: bool *const m_flag; }; -/////////////////////////////////////////////////////////////////////////////// -// Validator -/////////////////////////////////////////////////////////////////////////////// - -class StringValidator : public QValidator -{ -public: - StringValidator(QLabel *notifier, QLabel *icon) - : - m_notifier(notifier), m_icon(icon) - { - m_notifier->hide(); - m_icon->hide(); - } - - virtual State validate(QString &input, int &pos) const = 0; - - virtual void fixup(QString &input) const - { - input = input.simplified(); - } - -protected: - QLabel *const m_notifier, *const m_icon; - - bool checkParam(const QStringList &input, const char *const params[], const bool &doubleMinus) const - { - for(QStringList::ConstIterator iter = input.constBegin(); iter != input.constEnd(); iter++) - { - for(size_t k = 0; params[k]; k++) - { - const QString param = QLatin1String(params[k]); - const QString prefix = ((param.length() > 1) && doubleMinus) ? QLatin1String("--") : QLatin1String("-"); - if(iter->compare(QString("%1%2").arg(prefix, param), Qt::CaseInsensitive) == 0) - { - if(m_notifier) - { - m_notifier->setText(tr("Forbidden parameter: %1").arg(*iter)); - } - return true; - } - } - if(iter->startsWith("--", Qt::CaseInsensitive)) - { - for(int i = 2; i < iter->length(); i++) - { - if((iter->at(i) == QLatin1Char('=')) && (i > 2) && (i + 1 < iter->length())) - { - break; /*to allow "--param=value" format*/ - } - if((!iter->at(i).isLetter()) && ((iter->at(i) != QLatin1Char('-')) || (i < 3))) - { - if(m_notifier) - { - m_notifier->setText(tr("Invalid string: %1").arg(*iter)); - } - return true; - } - } - } - } - - return false; - } - - bool checkPrefix(const QStringList &input, const bool &doubleMinus) const - { - for(QStringList::ConstIterator iter = input.constBegin(); iter != input.constEnd(); iter++) - { - static const char *const c[3] = { "--", "-", NULL }; - for(size_t i = 0; c[i]; i++) - { - const QString prefix = QString::fromLatin1(c[i]); - if(iter->compare(prefix, Qt::CaseInsensitive) == 0) - { - if(m_notifier) - { - m_notifier->setText(tr("Invalid parameter: %1").arg(prefix)); - } - return true; - } - } - if - ( - ((!doubleMinus) && iter->startsWith("--", Qt::CaseInsensitive)) || - (doubleMinus && iter->startsWith("-", Qt::CaseInsensitive) && (!iter->startsWith("--", Qt::CaseInsensitive)) && (iter->length() > 2) && (!iter->at(1).isDigit())) || - (doubleMinus && iter->startsWith("--", Qt::CaseInsensitive) && (iter->length() < 4)) - ) - { - if(m_notifier) - { - m_notifier->setText(tr("Invalid syntax: %1").arg(*iter)); - } - return true; - } - } - return false; - } - - bool checkCharacters(const QStringList &input) const - { - static const char c[] = {'*', '?', '<', '>', '|', NULL}; - - for(QStringList::ConstIterator iter = input.constBegin(); iter != input.constEnd(); iter++) - { - for(size_t i = 0; c[i]; i++) - { - if(iter->indexOf(QLatin1Char(c[i])) >= 0) - { - if(m_notifier) - { - m_notifier->setText(tr("Invalid character: '%1'").arg(QLatin1Char(c[i]))); - } - return true; - } - } - } - return false; - } - - const bool &setStatus(const bool &flag, const QString &toolName) const - { - if(flag) - { - if(m_notifier) - { - if(m_notifier->isHidden()) m_notifier->show(); - if(m_icon) { if(m_icon->isHidden()) m_icon->show(); } - if(QWidget *w = m_notifier->topLevelWidget()->focusWidget()) - { - QToolTip::showText(static_cast(w->parent())->mapToGlobal(w->pos()), tr("Warning: You entered a parameter that is forbidden. Please note that the GUI will automatically set this parameter for you (if required)."), m_notifier, QRect()); - } - } - } - else - { - if(m_notifier) - { - if(m_notifier->isVisible()) m_notifier->hide(); - if(m_icon) { if(m_icon->isVisible()) m_icon->hide(); } - QToolTip::hideText(); - } - } - return flag; - } -}; - -class StringValidatorEncoder : public StringValidator -{ -public: - StringValidatorEncoder(QLabel *notifier, QLabel *icon) : StringValidator(notifier, icon) {} - - virtual State validate(QString &input, int &pos) const - { - static const char *const params[] = {"B", "o", "h", "p", "q", /*"fps", "frames",*/ "preset", "tune", "profile", - "stdin", "crf", "bitrate", "qp", "pass", "stats", "output", "help", "quiet", "codec", "y4m", NULL}; - - const QString commandLine = input.trimmed(); - const QStringList tokens = commandLine.isEmpty() ? QStringList() : MUtils::OS::crack_command_line(commandLine); - - const bool invalid = checkCharacters(tokens) || checkPrefix(tokens, true) || checkParam(tokens, params, true); - return setStatus(invalid, "encoder") ? QValidator::Intermediate : QValidator::Acceptable; - } -}; - -class StringValidatorAvs2YUV : public StringValidator -{ -public: - StringValidatorAvs2YUV(QLabel *notifier, QLabel *icon) : StringValidator(notifier, icon) {} - - virtual State validate(QString &input, int &pos) const - { - static const char *const params[] = {"o", "frames", "seek", "raw", "hfyu", "slave", NULL}; - - const QString commandLine = input.trimmed(); - const QStringList tokens = commandLine.isEmpty() ? QStringList() : MUtils::OS::crack_command_line(commandLine); - - const bool invalid = checkCharacters(tokens) || checkPrefix(tokens, false) || checkParam(tokens, params, false); - return setStatus(invalid, "Avs2YUV") ? QValidator::Intermediate : QValidator::Acceptable; - } -}; - /////////////////////////////////////////////////////////////////////////////// // Constructor & Destructor /////////////////////////////////////////////////////////////////////////////// @@ -349,7 +168,7 @@ AddJobDialog::AddJobDialog(QWidget *parent, OptionsModel *const options, Recentl ui->editCustomX264Params->setValidator(new StringValidatorEncoder(ui->labelNotificationX264, ui->iconNotificationX264)); ui->editCustomX264Params->clear(); ui->editCustomAvs2YUVParams->installEventFilter(this); - ui->editCustomAvs2YUVParams->setValidator(new StringValidatorAvs2YUV(ui->labelNotificationAvs2YUV, ui->iconNotificationAvs2YUV)); + ui->editCustomAvs2YUVParams->setValidator(new StringValidatorSource(ui->labelNotificationAvs2YUV, ui->iconNotificationAvs2YUV)); ui->editCustomAvs2YUVParams->clear(); //Install event filter diff --git a/x264_launcher_MSVC2013.vcxproj b/x264_launcher_MSVC2013.vcxproj index 0b05928..0d1b073 100644 --- a/x264_launcher_MSVC2013.vcxproj +++ b/x264_launcher_MSVC2013.vcxproj @@ -167,6 +167,8 @@ mkdir "$(TargetDir)\imageformats" copy /Y "$(SolutionDir)res\toolset\x86\*.exe" "$(TargetDir)\toolset\x86\" copy /Y "$(SolutionDir)res\toolset\x64\*.exe" "$(TargetDir)\toolset\x64\" +copy /Y "$(SolutionDir)res\toolset\x86\*.dll" "$(TargetDir)\toolset\x86\" +copy /Y "$(SolutionDir)res\toolset\x64\*.dll" "$(TargetDir)\toolset\x64\" copy /Y "$(SolutionDir)res\toolset\common\*.exe" "$(TargetDir)\toolset\common\" copy /Y "$(SolutionDir)res\toolset\common\*.gpg" "$(TargetDir)\toolset\common\" @@ -285,7 +287,10 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im $(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp;%(Outputs) $(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp;%(Outputs) + + + @@ -428,11 +433,13 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im + + @@ -442,6 +449,7 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im + diff --git a/x264_launcher_MSVC2013.vcxproj.filters b/x264_launcher_MSVC2013.vcxproj.filters index 2aa795a..7184bfd 100644 --- a/x264_launcher_MSVC2013.vcxproj.filters +++ b/x264_launcher_MSVC2013.vcxproj.filters @@ -111,6 +111,15 @@ Header Files + + Header Files + + + Header Files + + + Header Files + @@ -266,6 +275,15 @@ Source Files + + Source Files + + + Source Files + + + Source Files + diff --git a/x264_launcher_MSVC2015.vcxproj b/x264_launcher_MSVC2015.vcxproj index 423cbd7..df2271f 100644 --- a/x264_launcher_MSVC2015.vcxproj +++ b/x264_launcher_MSVC2015.vcxproj @@ -290,6 +290,7 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im + @@ -448,6 +449,7 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im + diff --git a/x264_launcher_MSVC2015.vcxproj.filters b/x264_launcher_MSVC2015.vcxproj.filters index 4f4d701..7184bfd 100644 --- a/x264_launcher_MSVC2015.vcxproj.filters +++ b/x264_launcher_MSVC2015.vcxproj.filters @@ -117,6 +117,9 @@ Header Files + + Header Files + @@ -278,6 +281,9 @@ Source Files + + Source Files +