Refactored custom parameter validation code into a separate class.

This commit is contained in:
LoRd_MuldeR 2017-01-07 03:18:03 +01:00
parent d0b0a6fcad
commit 35d83b4df3
8 changed files with 290 additions and 184 deletions

193
src/string_validator.cpp Normal file
View File

@ -0,0 +1,193 @@
///////////////////////////////////////////////////////////////////////////////
// Simple x264 Launcher
// Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
//
// 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<QWidget*>(w->parent())->mapToGlobal(w->pos()), tr("<b>Warning:</b> You entered a parameter that is forbidden. Please note that the GUI will automatically set <i>this</i> 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;
}

60
src/string_validator.h Normal file
View File

@ -0,0 +1,60 @@
///////////////////////////////////////////////////////////////////////////////
// Simple x264 Launcher
// Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
//
// 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 <MUtils/OSSupport.h>
//Qt
#include <QValidator>
#include <QLabel>
#include <QToolTip>
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;
};

View File

@ -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)

View File

@ -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<QWidget*>(w->parent())->mapToGlobal(w->pos()), tr("<b>Warning:</b> You entered a parameter that is forbidden. Please note that the GUI will automatically set <i>this</i> 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

View File

@ -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
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp;%(Outputs)</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(SolutionDir)tmp\$(ProjectName)\MOC_%(Filename).cpp;%(Outputs)</Outputs>
</CustomBuild>
<ClInclude Include="src\encoder_nvenc.h" />
<ClInclude Include="src\model_clipInfo.h" />
<ClInclude Include="src\source_factory.h" />
<ClInclude Include="src\string_validator.h" />
<ClInclude Include="tmp\x264_launcher\UIC_win_about.h" />
<ClInclude Include="tmp\x264_launcher\UIC_win_addJob.h" />
<ClInclude Include="tmp\x264_launcher\UIC_win_editor.h" />
@ -428,11 +433,13 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im
<ItemGroup>
<ClCompile Include="src\encoder_abstract.cpp" />
<ClCompile Include="src\encoder_factory.cpp" />
<ClCompile Include="src\encoder_nvenc.cpp" />
<ClCompile Include="src\encoder_x264.cpp" />
<ClCompile Include="src\encoder_x265.cpp" />
<ClCompile Include="src\job_object.cpp" />
<ClCompile Include="src\input_filter.cpp" />
<ClCompile Include="src\mediainfo.cpp" />
<ClCompile Include="src\model_clipInfo.cpp" />
<ClCompile Include="src\model_jobList.cpp" />
<ClCompile Include="src\model_logFile.cpp" />
<ClCompile Include="src\model_options.cpp" />
@ -442,6 +449,7 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im
<ClCompile Include="src\source_avisynth.cpp" />
<ClCompile Include="src\source_factory.cpp" />
<ClCompile Include="src\source_vapoursynth.cpp" />
<ClCompile Include="src\string_validator.cpp" />
<ClCompile Include="src\thread_avisynth.cpp" />
<ClCompile Include="src\thread_binaries.cpp" />
<ClCompile Include="src\thread_encode.cpp" />

View File

@ -111,6 +111,15 @@
<ClInclude Include="src\source_factory.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\encoder_nvenc.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\model_clipInfo.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\string_validator.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\main.cpp">
@ -266,6 +275,15 @@
<ClCompile Include="src\source_factory.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\encoder_nvenc.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\model_clipInfo.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\string_validator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="src\win_main.h">

View File

@ -290,6 +290,7 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im
<ClInclude Include="src\encoder_nvenc.h" />
<ClInclude Include="src\model_clipInfo.h" />
<ClInclude Include="src\source_factory.h" />
<ClInclude Include="src\string_validator.h" />
<ClInclude Include="tmp\x264_launcher\UIC_win_about.h" />
<ClInclude Include="tmp\x264_launcher\UIC_win_addJob.h" />
<ClInclude Include="tmp\x264_launcher\UIC_win_editor.h" />
@ -448,6 +449,7 @@ copy /Y "$(ProjectDir)\..\Prerequisites\Qt4\$(PlatformToolset)\Shared\plugins\im
<ClCompile Include="src\source_avisynth.cpp" />
<ClCompile Include="src\source_factory.cpp" />
<ClCompile Include="src\source_vapoursynth.cpp" />
<ClCompile Include="src\string_validator.cpp" />
<ClCompile Include="src\thread_avisynth.cpp" />
<ClCompile Include="src\thread_binaries.cpp" />
<ClCompile Include="src\thread_encode.cpp" />

View File

@ -117,6 +117,9 @@
<ClInclude Include="src\model_clipInfo.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\string_validator.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\main.cpp">
@ -278,6 +281,9 @@
<ClCompile Include="src\model_clipInfo.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\string_validator.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="src\win_main.h">